|
| 1 | +package docker |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/docker/docker/api" |
| 12 | + "github.com/docker/docker/client" |
| 13 | + "github.com/docker/go-connections/tlsconfig" |
| 14 | + "k8s.io/client-go/tools/clientcmd" |
| 15 | +) |
| 16 | + |
| 17 | +func newDockerClientFromEnvironment() (client.CommonAPIClient, error) { |
| 18 | + cli, err := client.NewClientWithOpts(client.FromEnv) |
| 19 | + if err != nil { |
| 20 | + return nil, fmt.Errorf("Error getting docker client: %s", err) |
| 21 | + } |
| 22 | + |
| 23 | + cli.NegotiateAPIVersion(context.Background()) |
| 24 | + return cli, nil |
| 25 | +} |
| 26 | + |
| 27 | +func newDockerClientFromMinikube() (client.CommonAPIClient, error) { |
| 28 | + env, err := getMinikubeEnvironment() |
| 29 | + if err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + |
| 33 | + var httpclient *http.Client |
| 34 | + if dockerCertPath := env["DOCKER_CERT_PATH"]; dockerCertPath != "" { |
| 35 | + options := tlsconfig.Options{ |
| 36 | + CAFile: filepath.Join(dockerCertPath, "ca.pem"), |
| 37 | + CertFile: filepath.Join(dockerCertPath, "cert.pem"), |
| 38 | + KeyFile: filepath.Join(dockerCertPath, "key.pem"), |
| 39 | + InsecureSkipVerify: env["DOCKER_TLS_VERIFY"] == "", |
| 40 | + } |
| 41 | + tlsc, err := tlsconfig.Client(options) |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + |
| 46 | + httpclient = &http.Client{ |
| 47 | + Transport: &http.Transport{ |
| 48 | + TLSClientConfig: tlsc, |
| 49 | + }, |
| 50 | + CheckRedirect: client.CheckRedirect, |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + host := env["DOCKER_HOST"] |
| 55 | + if host == "" { |
| 56 | + host = client.DefaultDockerHost |
| 57 | + } |
| 58 | + version := env["DOCKER_API_VERSION"] |
| 59 | + if version == "" { |
| 60 | + version = api.DefaultVersion |
| 61 | + } |
| 62 | + |
| 63 | + return client.NewClient(host, version, httpclient, nil) |
| 64 | +} |
| 65 | + |
| 66 | +func isMinikube() bool { |
| 67 | + if isMinikubeVar == nil { |
| 68 | + loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() |
| 69 | + kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{}) |
| 70 | + cfg, err := kubeConfig.RawConfig() |
| 71 | + |
| 72 | + if err != nil { |
| 73 | + return false |
| 74 | + } |
| 75 | + |
| 76 | + isMinikube := cfg.CurrentContext == "minikube" |
| 77 | + isMinikubeVar = &isMinikube |
| 78 | + } |
| 79 | + |
| 80 | + return *isMinikubeVar |
| 81 | +} |
| 82 | + |
| 83 | +func getMinikubeEnvironment() (map[string]string, error) { |
| 84 | + cmd := exec.Command("minikube", "docker-env", "--shell", "none") |
| 85 | + out, err := cmd.Output() |
| 86 | + |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + |
| 91 | + env := map[string]string{} |
| 92 | + |
| 93 | + for _, line := range strings.Split(string(out), "\n") { |
| 94 | + envKeyValue := strings.Split(line, "=") |
| 95 | + |
| 96 | + if len(envKeyValue) != 2 { |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + env[envKeyValue[0]] = envKeyValue[1] |
| 101 | + } |
| 102 | + |
| 103 | + return env, nil |
| 104 | +} |
0 commit comments