Skip to content

Commit

Permalink
delete docker clients from _cache when deleteNode
Browse files Browse the repository at this point in the history
  • Loading branch information
timfeirg committed Jan 17, 2017
1 parent 7fd3b5e commit 48bca6c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.7.12d
0.7.12e
64 changes: 41 additions & 23 deletions store/etcd/node.go
Expand Up @@ -24,6 +24,34 @@ import (

const GIGABYTE = 1073741824

// cache connections
// otherwise they'll leak
type cache struct {
sync.Mutex
clients map[string]*engineapi.Client
}

func (c cache) set(host string, client *engineapi.Client) {
c.Lock()
defer c.Unlock()

c.clients[host] = client
}

func (c cache) get(host string) *engineapi.Client {
c.Lock()
defer c.Unlock()
return c.clients[host]
}

func (c cache) delete(host string) {
c.Lock()
defer c.Unlock()
delete(c.clients, host)
}

var _cache = cache{clients: make(map[string]*engineapi.Client)}

// get a node from etcd
// and construct it's docker client
// a node must belong to a pod
Expand Down Expand Up @@ -88,7 +116,7 @@ func (k *krypton) AddNode(name, endpoint, podname, cafile, certfile, keyfile str
}

// 尝试加载docker的客户端
engine, err := k.makeDockerClient(podname, name, endpoint, false)
engine, err := k.makeDockerClient(podname, name, endpoint, true)
if err != nil {
k.deleteNode(podname, name, endpoint)
return nil, err
Expand Down Expand Up @@ -141,6 +169,18 @@ func (k *krypton) deleteNode(podname, nodename, endpoint string) {
key := fmt.Sprintf(nodePrefixKey, podname, nodename)
k.etcd.Delete(context.Background(), key, &etcdclient.DeleteOptions{Recursive: true})
k.deleteCertFiles(podname, nodename, endpoint)
u, err := url.Parse(endpoint)
if err != nil {
log.Errorf("Bad endpoint: %s", endpoint)
return
}

host, _, err := net.SplitHostPort(u.Host)
if err != nil {
log.Errorf("Bad addr: %s", u.Host)
return
}
_cache.delete(host)
log.Debugf("Node (%s, %s, %s) deleted", podname, nodename, endpoint)
}

Expand Down Expand Up @@ -308,28 +348,6 @@ func (k *krypton) UpdateNodeCPU(podname, nodename string, cpu types.CPUMap, acti
return nil
}

// cache connections
// otherwise they'll leak
type cache struct {
sync.Mutex
clients map[string]*engineapi.Client
}

func (c cache) set(host string, client *engineapi.Client) {
c.Lock()
defer c.Unlock()

c.clients[host] = client
}

func (c cache) get(host string) *engineapi.Client {
c.Lock()
defer c.Unlock()
return c.clients[host]
}

var _cache = cache{clients: make(map[string]*engineapi.Client)}

// use endpoint, cert files path, and api version to create docker client
// we don't check whether this is connectable
func makeRawClient(endpoint, certpath, apiversion string) (*engineapi.Client, error) {
Expand Down

0 comments on commit 48bca6c

Please sign in to comment.