This repository has been archived by the owner on Jun 13, 2021. It is now read-only.
/
driver.go
133 lines (114 loc) · 3.65 KB
/
driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package cnab
import (
"bytes"
"io"
"os"
"strings"
"github.com/docker/app/internal/cliopts"
store2 "github.com/docker/app/internal/store"
"github.com/deislabs/cnab-go/claim"
"github.com/deislabs/cnab-go/driver"
dockerDriver "github.com/deislabs/cnab-go/driver/docker"
"github.com/docker/app/internal"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/context/docker"
"github.com/docker/cli/cli/context/store"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
)
// BindMount
type BindMount struct {
required bool
endpoint string
}
const defaultSocketPath string = "/var/run/docker.sock"
func RequiredClaimBindMount(c claim.Claim, dockerCli command.Cli) (BindMount, error) {
var specifiedOrchestrator string
if rawOrchestrator, ok := c.Parameters[internal.ParameterOrchestratorName]; ok {
specifiedOrchestrator = rawOrchestrator.(string)
}
return RequiredBindMount(dockerCli.CurrentContext(), specifiedOrchestrator, dockerCli.ContextStore())
}
// RequiredBindMount Returns the path required to bind mount when running
// the invocation image.
func RequiredBindMount(targetContextName string, targetOrchestrator string, s store.Store) (BindMount, error) {
if targetOrchestrator == "kubernetes" {
return BindMount{}, nil
}
if targetContextName == "" {
targetContextName = "default"
}
// in case of docker desktop, we want to rewrite the context in cases where it targets the local swarm or Kubernetes
s = &internal.DockerDesktopAwareStore{Store: s}
ctxMeta, err := s.GetMetadata(targetContextName)
if err != nil {
return BindMount{}, err
}
dockerCtx, err := command.GetDockerContext(ctxMeta)
if err != nil {
return BindMount{}, err
}
if dockerCtx.StackOrchestrator == command.OrchestratorKubernetes {
return BindMount{}, nil
}
dockerEndpoint, err := docker.EndpointFromContext(ctxMeta)
if err != nil {
return BindMount{}, err
}
host := dockerEndpoint.Host
return BindMount{isDockerHostLocal(host), socketPath(host)}, nil
}
func socketPath(host string) string {
if strings.HasPrefix(host, "unix://") {
return strings.TrimPrefix(host, "unix://")
}
return defaultSocketPath
}
func isDockerHostLocal(host string) bool {
return host == "" || strings.HasPrefix(host, "unix://") || strings.HasPrefix(host, "npipe://")
}
// prepareDriver prepares a driver per the user's request.
func prepareDriver(dockerCli command.Cli, bindMount BindMount, stdout io.Writer) (driver.Driver, *bytes.Buffer) {
d := &dockerDriver.Driver{}
errBuf := bytes.NewBuffer(nil)
d.SetDockerCli(dockerCli)
if stdout != nil {
d.SetContainerOut(stdout)
}
d.SetContainerErr(errBuf)
if bindMount.required {
d.AddConfigurationOptions(func(config *container.Config, hostConfig *container.HostConfig) error {
config.User = "0:0"
mounts := []mount.Mount{
{
Type: mount.TypeBind,
Source: bindMount.endpoint,
Target: bindMount.endpoint,
},
}
hostConfig.Mounts = mounts
return nil
})
}
// Load any driver-specific config out of the environment.
driverCfg := map[string]string{}
for env := range d.Config() {
if value, ok := os.LookupEnv(env); ok {
driverCfg[env] = value
}
}
d.SetConfig(driverCfg)
return d, errBuf
}
func SetupDriver(installation *store2.Installation, dockerCli command.Cli, opts cliopts.InstallerContextOptions, stdout io.Writer) (driver.Driver, *bytes.Buffer, error) {
dockerCli, err := opts.SetInstallerContext(dockerCli)
if err != nil {
return nil, nil, err
}
bind, err := RequiredClaimBindMount(installation.Claim, dockerCli)
if err != nil {
return nil, nil, err
}
driverImpl, errBuf := prepareDriver(dockerCli, bind, stdout)
return driverImpl, errBuf, nil
}