forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocker.go
184 lines (168 loc) · 6.07 KB
/
docker.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package strategy
import (
"fmt"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
buildv1 "github.com/openshift/api/build/v1"
buildinstall "github.com/openshift/origin/pkg/build/apis/build/install"
"github.com/openshift/origin/pkg/build/buildapihelpers"
buildutil "github.com/openshift/origin/pkg/build/util"
)
var (
buildEncodingScheme = runtime.NewScheme()
buildEncodingCodecFactory = serializer.NewCodecFactory(buildEncodingScheme)
buildJSONCodec runtime.Encoder
)
func init() {
// TODO only use external versions, so we only add external types
buildinstall.Install(buildEncodingScheme)
buildJSONCodec = buildEncodingCodecFactory.LegacyCodec(buildv1.GroupVersion)
}
// DockerBuildStrategy creates a Docker build using a Docker builder image.
type DockerBuildStrategy struct {
Image string
}
// CreateBuildPod creates the pod to be used for the Docker build
// TODO: Make the Pod definition configurable
func (bs *DockerBuildStrategy) CreateBuildPod(build *buildv1.Build) (*v1.Pod, error) {
data, err := runtime.Encode(buildJSONCodec, build)
if err != nil {
return nil, fmt.Errorf("failed to encode the build: %v", err)
}
privileged := true
strategy := build.Spec.Strategy.DockerStrategy
containerEnv := []v1.EnvVar{
{Name: "BUILD", Value: string(data)},
}
addSourceEnvVars(build.Spec.Source, &containerEnv)
if len(strategy.Env) > 0 {
buildutil.MergeTrustedEnvWithoutDuplicates(strategy.Env, &containerEnv, true)
}
serviceAccount := build.Spec.ServiceAccount
if len(serviceAccount) == 0 {
serviceAccount = buildutil.BuilderServiceAccountName
}
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: buildapihelpers.GetBuildPodName(build),
Namespace: build.Namespace,
Labels: getPodLabels(build),
},
Spec: v1.PodSpec{
ServiceAccountName: serviceAccount,
Containers: []v1.Container{
{
Name: DockerBuild,
Image: bs.Image,
Command: []string{"openshift-docker-build"},
Env: copyEnvVarSlice(containerEnv),
// TODO: run unprivileged https://github.com/openshift/origin/issues/662
SecurityContext: &v1.SecurityContext{
Privileged: &privileged,
},
TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
VolumeMounts: []v1.VolumeMount{
{
Name: "buildworkdir",
MountPath: buildutil.BuildWorkDirMount,
},
},
ImagePullPolicy: v1.PullIfNotPresent,
Resources: build.Spec.Resources,
},
},
Volumes: []v1.Volume{
{
Name: "buildworkdir",
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
},
},
RestartPolicy: v1.RestartPolicyNever,
NodeSelector: build.Spec.NodeSelector,
},
}
// can't conditionalize the manage-dockerfile init container because we don't
// know until we've cloned, whether or not we've got a dockerfile to manage
// (also if it's a docker type build, we should always have a dockerfile to manage)
if build.Spec.Source.Git != nil || build.Spec.Source.Binary != nil {
gitCloneContainer := v1.Container{
Name: GitCloneContainer,
Image: bs.Image,
Command: []string{"openshift-git-clone"},
Env: copyEnvVarSlice(containerEnv),
TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
VolumeMounts: []v1.VolumeMount{
{
Name: "buildworkdir",
MountPath: buildutil.BuildWorkDirMount,
},
},
ImagePullPolicy: v1.PullIfNotPresent,
Resources: build.Spec.Resources,
}
if build.Spec.Source.Binary != nil {
gitCloneContainer.Stdin = true
gitCloneContainer.StdinOnce = true
}
setupSourceSecrets(pod, &gitCloneContainer, build.Spec.Source.SourceSecret)
pod.Spec.InitContainers = append(pod.Spec.InitContainers, gitCloneContainer)
}
if len(build.Spec.Source.Images) > 0 {
extractImageContentContainer := v1.Container{
Name: ExtractImageContentContainer,
Image: bs.Image,
Command: []string{"openshift-extract-image-content"},
Env: copyEnvVarSlice(containerEnv),
// TODO: run unprivileged https://github.com/openshift/origin/issues/662
SecurityContext: &v1.SecurityContext{
Privileged: &privileged,
},
TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
VolumeMounts: []v1.VolumeMount{
{
Name: "buildworkdir",
MountPath: buildutil.BuildWorkDirMount,
},
},
ImagePullPolicy: v1.PullIfNotPresent,
Resources: build.Spec.Resources,
}
setupDockerSecrets(pod, &extractImageContentContainer, build.Spec.Output.PushSecret, strategy.PullSecret, build.Spec.Source.Images)
pod.Spec.InitContainers = append(pod.Spec.InitContainers, extractImageContentContainer)
}
pod.Spec.InitContainers = append(pod.Spec.InitContainers,
v1.Container{
Name: "manage-dockerfile",
Image: bs.Image,
Command: []string{"openshift-manage-dockerfile"},
Env: copyEnvVarSlice(containerEnv),
TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
VolumeMounts: []v1.VolumeMount{
{
Name: "buildworkdir",
MountPath: buildutil.BuildWorkDirMount,
},
},
ImagePullPolicy: v1.PullIfNotPresent,
Resources: build.Spec.Resources,
},
)
if build.Spec.CompletionDeadlineSeconds != nil {
pod.Spec.ActiveDeadlineSeconds = build.Spec.CompletionDeadlineSeconds
}
setOwnerReference(pod, build)
setupDockerSocket(pod)
setupCrioSocket(pod)
setupDockerSecrets(pod, &pod.Spec.Containers[0], build.Spec.Output.PushSecret, strategy.PullSecret, build.Spec.Source.Images)
// For any secrets the user wants to reference from their Assemble script or Dockerfile, mount those
// secrets into the main container. The main container includes logic to copy them from the mounted
// location into the working directory.
// TODO: consider moving this into the git-clone container and doing the secret copying there instead.
setupInputSecrets(pod, &pod.Spec.Containers[0], build.Spec.Source.Secrets)
setupInputConfigMaps(pod, &pod.Spec.Containers[0], build.Spec.Source.ConfigMaps)
return pod, nil
}