Minor fixes and refactoring

parent 8abea7f2
......@@ -888,7 +888,7 @@ func (s *executor) disconnectNetwork(id string) error {
for _, network := range netList {
for _, pluggedContainer := range network.Containers {
if id == pluggedContainer.Name {
err = s.client.NetworkDisconnect(context.TODO(), network.ID, id, false)
err = s.client.NetworkDisconnect(context.TODO(), network.ID, id, true)
if err != nil {
s.Warningln("Can't disconnect possibly zombie container", pluggedContainer.Name, "from network", network.Name, "->", err)
} else {
......
......@@ -4,11 +4,12 @@ import (
"errors"
"github.com/docker/docker/api/types"
"golang.org/x/net/context"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/executors"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers/ssh"
"golang.org/x/net/context"
)
type sshExecutor struct {
......
......@@ -154,14 +154,14 @@ func testServiceFromNamedImage(t *testing.T, description, imageName, serviceName
Once()
networkContainersMap := map[string]types.EndpointResource{
"1": types.EndpointResource{Name: containerName},
"1": {Name: containerName},
}
c.On("NetworkList", context.TODO(), types.NetworkListOptions{}).
Return([]types.NetworkResource{{ID: networkID, Name: "network-name", Containers: networkContainersMap}}, nil).
Once()
c.On("NetworkDisconnect", context.TODO(), networkID, containerName, false).
c.On("NetworkDisconnect", context.TODO(), networkID, containerName, true).
Return(nil).
Once()
......
......@@ -69,7 +69,10 @@ func ReadDockerAuthConfigsFromHomeDir(userName string) (map[string]types.AuthCon
}
p := path.Join(homeDir, ".docker", "config.json")
r, err := os.Open(p)
defer r.Close()
if err != nil {
p := path.Join(homeDir, ".dockercfg")
r, err = os.Open(p)
......@@ -82,7 +85,6 @@ func ReadDockerAuthConfigsFromHomeDir(userName string) (map[string]types.AuthCon
return make(map[string]types.AuthConfig), nil
}
defer r.Close()
return ReadAuthConfigsFromReader(r)
}
......
package docker_helpers
import (
"net"
"net/http"
"path/filepath"
"time"
"github.com/docker/docker/client"
"github.com/docker/go-connections/sockets"
"github.com/docker/go-connections/tlsconfig"
)
var dockerDialer = &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}
// New attempts to create a new Docker client of the specified version.
//
// If no host is given in the DockerCredentials, it will attempt to look up
// details from the environment. If that fails, it will use the default
// connection details for your platform.
func New(c DockerCredentials, apiVersion string) (Client, error) {
if c.Host == "" {
c = credentialsFromEnv()
}
// Use the default if nothing is specified by caller *or* environment
if c.Host == "" {
c.Host = client.DefaultDockerHost
}
return newOfficialDockerClient(c, apiVersion)
}
func newHTTPTransport(c DockerCredentials) (*http.Transport, error) {
proto, addr, _, err := client.ParseHost(c.Host)
if err != nil {
return nil, err
}
tr := &http.Transport{}
if err := sockets.ConfigureTransport(tr, proto, addr); err != nil {
return nil, err
}
// FIXME: is a TLS connection with InsecureSkipVerify == true ever wanted?
if c.TLSVerify {
options := tlsconfig.Options{}
if c.CertPath != "" {
options.CAFile = filepath.Join(c.CertPath, "ca.pem")
options.CertFile = filepath.Join(c.CertPath, "cert.pem")
options.KeyFile = filepath.Join(c.CertPath, "key.pem")
}
tlsConfig, err := tlsconfig.Client(options)
if err != nil {
tr.CloseIdleConnections()
return nil, err
}
tr.TLSHandshakeTimeout = 10 * time.Second
tr.TLSClientConfig = tlsConfig
}
return tr, nil
}
package docker_helpers
import container "github.com/docker/docker/api/types/container"
import context "golang.org/x/net/context"
import io "io"
import mock "github.com/stretchr/testify/mock"
import network "github.com/docker/docker/api/types/network"
import types "github.com/docker/docker/api/types"
import (
"io"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/stretchr/testify/mock"
"golang.org/x/net/context"
)
// MockClient is an autogenerated mock type for the Client type
type MockClient struct {
......
......@@ -5,10 +5,14 @@ import (
"io"
"io/ioutil"
"net/http"
"path/filepath"
"time"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/go-connections/sockets"
"github.com/docker/go-connections/tlsconfig"
"golang.org/x/net/context"
)
......@@ -82,3 +86,55 @@ func (c *officialDockerClient) Close() error {
c.Transport.CloseIdleConnections()
return nil
}
// New attempts to create a new Docker client of the specified version.
//
// If no host is given in the DockerCredentials, it will attempt to look up
// details from the environment. If that fails, it will use the default
// connection details for your platform.
func New(c DockerCredentials, apiVersion string) (Client, error) {
if c.Host == "" {
c = credentialsFromEnv()
}
// Use the default if nothing is specified by caller *or* environment
if c.Host == "" {
c.Host = client.DefaultDockerHost
}
return newOfficialDockerClient(c, apiVersion)
}
func newHTTPTransport(c DockerCredentials) (*http.Transport, error) {
proto, addr, _, err := client.ParseHost(c.Host)
if err != nil {
return nil, err
}
tr := &http.Transport{}
if err := sockets.ConfigureTransport(tr, proto, addr); err != nil {
return nil, err
}
// FIXME: is a TLS connection with InsecureSkipVerify == true ever wanted?
if c.TLSVerify {
options := tlsconfig.Options{}
if c.CertPath != "" {
options.CAFile = filepath.Join(c.CertPath, "ca.pem")
options.CertFile = filepath.Join(c.CertPath, "cert.pem")
options.KeyFile = filepath.Join(c.CertPath, "key.pem")
}
tlsConfig, err := tlsconfig.Client(options)
if err != nil {
tr.CloseIdleConnections()
return nil, err
}
tr.TLSHandshakeTimeout = 10 * time.Second
tr.TLSClientConfig = tlsConfig
}
return tr, nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment