Skip to content

Commit 8e1fb74

Browse files
author
OpenShift Bot
committed
Merge pull request #3935 from csrwng/docker_tls
Merged by openshift-bot
2 parents 5189463 + 5b9e4a6 commit 8e1fb74

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

pkg/cmd/util/docker/docker.go

+42-11
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package docker
22

33
import (
44
"os"
5+
"path"
56

6-
"github.com/fsouza/go-dockerclient"
7+
docker "github.com/fsouza/go-dockerclient"
78
"github.com/golang/glog"
89
"github.com/spf13/pflag"
910
)
@@ -26,10 +27,16 @@ func (_ *Helper) InstallFlags(flags *pflag.FlagSet) {
2627

2728
// GetClient returns a valid Docker client, the address of the client, or an error
2829
// if the client couldn't be created.
29-
func (_ *Helper) GetClient() (*docker.Client, string, error) {
30-
addr := getDockerEndpoint("")
31-
client, err := docker.NewClient(addr)
32-
return client, addr, err
30+
func (_ *Helper) GetClient() (client *docker.Client, endpoint string, err error) {
31+
cfg := getDockerConfig("")
32+
endpoint = cfg.Endpoint
33+
34+
if cfg.IsTLS() {
35+
client, err = docker.NewTLSClient(cfg.Endpoint, cfg.Cert(), cfg.Key(), cfg.CA())
36+
return
37+
}
38+
client, err = docker.NewClient(cfg.Endpoint)
39+
return
3340
}
3441

3542
// GetClientOrExit returns a valid Docker client and the address of the client,
@@ -42,15 +49,39 @@ func (h *Helper) GetClientOrExit() (*docker.Client, string) {
4249
return client, addr
4350
}
4451

45-
func getDockerEndpoint(dockerEndpoint string) string {
46-
var endpoint string
52+
type dockerConfig struct {
53+
Endpoint string
54+
CertPath string
55+
}
56+
57+
func (c *dockerConfig) IsTLS() bool {
58+
return len(c.CertPath) > 0
59+
}
60+
61+
func (c *dockerConfig) Cert() string {
62+
return path.Join(c.CertPath, "cert.pem")
63+
}
64+
65+
func (c *dockerConfig) Key() string {
66+
return path.Join(c.CertPath, "key.pem")
67+
}
68+
69+
func (c *dockerConfig) CA() string {
70+
return path.Join(c.CertPath, "ca.pem")
71+
}
72+
73+
func getDockerConfig(dockerEndpoint string) *dockerConfig {
74+
cfg := &dockerConfig{}
4775
if len(dockerEndpoint) > 0 {
48-
endpoint = dockerEndpoint
76+
cfg.Endpoint = dockerEndpoint
4977
} else if len(os.Getenv("DOCKER_HOST")) > 0 {
50-
endpoint = os.Getenv("DOCKER_HOST")
78+
cfg.Endpoint = os.Getenv("DOCKER_HOST")
5179
} else {
52-
endpoint = "unix:///var/run/docker.sock"
80+
cfg.Endpoint = "unix:///var/run/docker.sock"
5381
}
5482

55-
return endpoint
83+
if os.Getenv("DOCKER_TLS_VERIFY") == "1" {
84+
cfg.CertPath = os.Getenv("DOCKER_CERT_PATH")
85+
}
86+
return cfg
5687
}

0 commit comments

Comments
 (0)