Skip to content

Commit

Permalink
models: format zkclient.go & etcdclient.go
Browse files Browse the repository at this point in the history
  • Loading branch information
spinlock committed Sep 17, 2017
1 parent 3adf407 commit 4169d1e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
2 changes: 1 addition & 1 deletion cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func AutoOnlineWithDashboard(p *proxy.Proxy, dashboard string) {
log.Panicf("online proxy failed")
}

func AutoOnlineWithCoordinator(p *proxy.Proxy, name, addr string, auth string) {
func AutoOnlineWithCoordinator(p *proxy.Proxy, name, addr, auth string) {
client, err := models.NewClient(name, addr, auth, time.Minute)
if err != nil {
log.PanicErrorf(err, "create '%s' client to '%s' failed", name, addr)
Expand Down
11 changes: 7 additions & 4 deletions pkg/models/etcd/etcdclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,18 @@ func New(addrlist string, auth string, timeout time.Duration) (*Client, error) {
timeout = time.Second * 5
}

config := client.Config{
config := client.Config{
Endpoints: endpoints, Transport: client.DefaultTransport,
HeaderTimeoutPerRequest: time.Second * 5,
}

if auth != "" {
a := strings.Split(auth, ":")
config.Username = a[0]
config.Password = a[1]
split := strings.Split(auth, ":")
if len(split) != 2 || split[0] == "" {
return nil, errors.Errorf("invalid auth")
}
config.Username = split[0]
config.Password = split[1]
}

c, err := client.New(config)
Expand Down
51 changes: 28 additions & 23 deletions pkg/models/zk/zkclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ type Client struct {
conn *zk.Conn

addrlist string
auth string
username string
password string
timeout time.Duration

logger *zkLogger
Expand All @@ -55,9 +56,17 @@ func NewWithLogfunc(addrlist string, auth string, timeout time.Duration, logfunc
timeout = time.Second * 5
}
c := &Client{
addrlist: addrlist, auth: auth, timeout: timeout,
addrlist: addrlist, timeout: timeout,
logger: &zkLogger{logfunc},
}
if auth != "" {
split := strings.Split(auth, ":")
if len(split) != 2 || split[0] == "" {
return nil, errors.Errorf("invalid auth")
}
c.username = split[0]
c.password = split[1]
}
if err := c.reset(); err != nil {
return nil, err
}
Expand All @@ -78,8 +87,9 @@ func (c *Client) reset() error {

c.logger.Printf("zkclient setup new connection to %s", c.addrlist)

if c.auth != "" {
if err := c.conn.AddAuth("digest", []byte(c.auth)); err != nil {
if c.username != "" {
var auth = fmt.Sprintf("%s:%s", c.username, c.password)
if err := c.conn.AddAuth("digest", []byte(auth)); err != nil {
return errors.Trace(err)
}
}
Expand Down Expand Up @@ -162,15 +172,13 @@ func (c *Client) mkdir(conn *zk.Conn, path string) error {
if err := c.mkdir(conn, filepath.Dir(path)); err != nil {
return err
}
var acl []zk.ACL

if c.auth != "" {
auth := strings.Split(c.auth, ":")
acl = zk.DigestACL(zk.PermAll, auth[0], auth[1])
} else {
acl = zk.WorldACL(zk.PermAll)
}
_, err := conn.Create(path, []byte{}, 0, acl)
_, err := conn.Create(path, []byte{}, 0, func() []zk.ACL {
const perm = zk.PermAll
if c.username != "" {
return zk.DigestACL(perm, c.username, c.password)
}
return zk.WorldACL(perm)
}())
if err != nil && errors.NotEqual(err, zk.ErrNodeExists) {
return errors.Trace(err)
}
Expand Down Expand Up @@ -228,16 +236,13 @@ func (c *Client) create(conn *zk.Conn, path string, data []byte, flag int32) (st
if err := c.mkdir(conn, filepath.Dir(path)); err != nil {
return "", err
}

var acl []zk.ACL

if c.auth != "" {
auth := strings.Split(c.auth, ":")
acl = zk.DigestACL(zk.PermAdmin|zk.PermRead|zk.PermWrite, auth[0], auth[1])
} else {
acl = zk.WorldACL(zk.PermAdmin|zk.PermRead|zk.PermWrite)
}
p, err := conn.Create(path, data, flag, acl)
p, err := conn.Create(path, data, flag, func() []zk.ACL {
const perm = zk.PermAdmin | zk.PermRead | zk.PermWrite
if c.username != "" {
return zk.DigestACL(perm, c.username, c.password)
}
return zk.WorldACL(perm)
}())
if err != nil {
return "", errors.Trace(err)
}
Expand Down

0 comments on commit 4169d1e

Please sign in to comment.