Make SessionSetup faster and support for command execution

This commit is contained in:
Jonathan Leibiusky @xetorthio
2017-09-11 18:09:27 -03:00
parent 86044788b9
commit fe299fed90
9 changed files with 162 additions and 124 deletions

View File

@@ -30,6 +30,7 @@ const (
)
type DockerApi interface {
GetClient() *client.Client
CreateNetwork(id string, opts types.NetworkCreate) error
ConnectNetwork(container, network, ip string) (string, error)
NetworkInspect(id string) (types.NetworkResource, error)
@@ -49,7 +50,7 @@ type DockerApi interface {
DisconnectNetwork(containerId, networkId string) error
DeleteNetwork(id string) error
Exec(instanceName string, command []string) (int, error)
SwarmInit() (*SwarmTokens, error)
SwarmInit(advertiseAddr string) (*SwarmTokens, error)
SwarmJoin(addr, token string) error
ConfigCreate(name string, labels map[string]string, data []byte) error
ConfigDelete(name string) error
@@ -64,6 +65,10 @@ type docker struct {
c *client.Client
}
func (d *docker) GetClient() *client.Client {
return d.c
}
func (d *docker) ConfigCreate(name string, labels map[string]string, data []byte) error {
config := swarm.ConfigSpec{}
config.Name = name
@@ -467,53 +472,8 @@ func (d *docker) DeleteNetwork(id string) error {
return nil
}
/*
func (d *docker) New(ip string, cert, key []byte) (DockerApi, error) {
// We check if the client needs to use TLS
var tlsConfig *tls.Config
if len(cert) > 0 && len(key) > 0 {
tlsConfig = tlsconfig.ClientDefault()
tlsConfig.InsecureSkipVerify = true
tlsCert, err := tls.X509KeyPair(cert, key)
if err != nil {
return nil, err
}
tlsConfig.Certificates = []tls.Certificate{tlsCert}
}
transport := &http.Transport{
DialContext: (&net.Dialer{
Timeout: 1 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext}
if tlsConfig != nil {
transport.TLSClientConfig = tlsConfig
}
cli := &http.Client{
Transport: transport,
}
c, err := client.NewClient(fmt.Sprintf("http://%s:2375", ip), api.DefaultVersion, cli, nil)
if err != nil {
return nil, fmt.Errorf("Could not connect to DinD docker daemon. %s", err)
}
// try to connect up to 5 times and then give up
for i := 0; i < 5; i++ {
_, err := c.Ping(context.Background())
if err != nil {
if client.IsErrConnectionFailed(err) {
// connection has failed, maybe instance is not ready yet, sleep and retry
log.Printf("Connection to [%s] has failed, maybe instance is not ready yet, sleeping and retrying in 1 second. Try #%d\n", fmt.Sprintf("http://%s:2375", ip), i+1)
time.Sleep(time.Second)
continue
}
return nil, err
}
}
return NewDocker(c), nil
}
*/
func (d *docker) SwarmInit() (*SwarmTokens, error) {
req := swarm.InitRequest{AdvertiseAddr: "eth0", ListenAddr: "0.0.0.0:2377"}
func (d *docker) SwarmInit(advertiseAddr string) (*SwarmTokens, error) {
req := swarm.InitRequest{AdvertiseAddr: advertiseAddr, ListenAddr: "0.0.0.0:2377"}
_, err := d.c.SwarmInit(context.Background(), req)
if err != nil {

View File

@@ -37,7 +37,11 @@ func (f *localCachedFactory) GetForSession(sessionId string) (DockerApi, error)
defer f.rw.Unlock()
if f.sessionClient != nil {
return f.sessionClient, nil
if err := f.check(f.sessionClient.GetClient()); err == nil {
return f.sessionClient, nil
} else {
f.sessionClient.GetClient().Close()
}
}
c, err := client.NewEnvClient()
@@ -69,7 +73,11 @@ func (f *localCachedFactory) GetForInstance(instance *types.Instance) (DockerApi
defer c.rw.Unlock()
if c.client != nil {
return c.client, nil
if err := f.check(c.client.GetClient()); err == nil {
return c.client, nil
} else {
c.client.GetClient().Close()
}
}
// Need to create client to the DinD docker daemon

View File

@@ -6,6 +6,7 @@ import (
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/stretchr/testify/mock"
)
@@ -13,6 +14,11 @@ type Mock struct {
mock.Mock
}
func (m *Mock) GetClient() *client.Client {
args := m.Called()
return args.Get(0).(*client.Client)
}
func (m *Mock) CreateNetwork(id string, opts types.NetworkCreate) error {
args := m.Called(id, opts)
return args.Error(0)
@@ -96,8 +102,8 @@ func (m *Mock) Exec(instanceName string, command []string) (int, error) {
args := m.Called(instanceName, command)
return args.Int(0), args.Error(1)
}
func (m *Mock) SwarmInit() (*SwarmTokens, error) {
args := m.Called()
func (m *Mock) SwarmInit(advertiseAddr string) (*SwarmTokens, error) {
args := m.Called(advertiseAddr)
return args.Get(0).(*SwarmTokens), args.Error(1)
}
func (m *Mock) SwarmJoin(addr, token string) error {