return destroy func to clean up internal resources of storage

This commit is contained in:
Hongchao Deng
2016-08-24 16:35:21 -07:00
parent fff95275df
commit 9fc0e1e98d
48 changed files with 194 additions and 113 deletions

View File

@@ -30,16 +30,17 @@ import (
utilnet "k8s.io/kubernetes/pkg/util/net"
)
func newETCD2Storage(c storagebackend.Config) (storage.Interface, error) {
func newETCD2Storage(c storagebackend.Config) (storage.Interface, func(), error) {
tr, err := newTransportForETCD2(c.CertFile, c.KeyFile, c.CAFile)
if err != nil {
return nil, err
return nil, nil, err
}
client, err := newETCD2Client(tr, c.ServerList)
if err != nil {
return nil, err
return nil, nil, err
}
return etcd.NewEtcdStorage(client, c.Codec, c.Prefix, c.Quorum, c.DeserializationCacheSize), nil
s := etcd.NewEtcdStorage(client, c.Codec, c.Prefix, c.Quorum, c.DeserializationCacheSize)
return s, tr.CloseIdleConnections, nil
}
func newETCD2Client(tr *http.Transport, serverList []string) (etcd2client.Client, error) {

View File

@@ -26,7 +26,7 @@ import (
"golang.org/x/net/context"
)
func newETCD3Storage(c storagebackend.Config) (storage.Interface, error) {
func newETCD3Storage(c storagebackend.Config) (storage.Interface, func(), error) {
tlsInfo := transport.TLSInfo{
CertFile: c.CertFile,
KeyFile: c.KeyFile,
@@ -34,7 +34,7 @@ func newETCD3Storage(c storagebackend.Config) (storage.Interface, error) {
}
tlsConfig, err := tlsInfo.ClientConfig()
if err != nil {
return nil, err
return nil, nil, err
}
cfg := clientv3.Config{
@@ -43,8 +43,13 @@ func newETCD3Storage(c storagebackend.Config) (storage.Interface, error) {
}
client, err := clientv3.New(cfg)
if err != nil {
return nil, err
return nil, nil, err
}
etcd3.StartCompactor(context.Background(), client)
return etcd3.New(client, c.Codec, c.Prefix), nil
ctx, cancel := context.WithCancel(context.Background())
etcd3.StartCompactor(ctx, client)
destroyFunc := func() {
cancel()
client.Close()
}
return etcd3.New(client, c.Codec, c.Prefix), destroyFunc, nil
}

View File

@@ -24,7 +24,7 @@ import (
)
// Create creates a storage backend based on given config.
func Create(c storagebackend.Config) (storage.Interface, error) {
func Create(c storagebackend.Config) (storage.Interface, func(), error) {
switch c.Type {
case storagebackend.StorageTypeUnset, storagebackend.StorageTypeETCD2:
return newETCD2Storage(c)
@@ -35,6 +35,6 @@ func Create(c storagebackend.Config) (storage.Interface, error) {
// - Support non-quorum read.
return newETCD3Storage(c)
default:
return nil, fmt.Errorf("unknown storage type: %s", c.Type)
return nil, nil, fmt.Errorf("unknown storage type: %s", c.Type)
}
}

View File

@@ -56,7 +56,8 @@ func TestTLSConnection(t *testing.T) {
CAFile: caFile,
Codec: testapi.Default.Codec(),
}
storage, err := newETCD3Storage(cfg)
storage, destroyFunc, err := newETCD3Storage(cfg)
defer destroyFunc()
if err != nil {
t.Fatal(err)
}