forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker.go
172 lines (158 loc) · 5.79 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
package strategy
import (
"fmt"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
buildutil "github.com/openshift/origin/pkg/build/util"
)
// DockerBuildStrategy creates a Docker build using a Docker builder image.
type DockerBuildStrategy struct {
Image string
// Codec is the codec to use for encoding the output pod.
// IMPORTANT: This may break backwards compatibility when
// it changes.
Codec runtime.Codec
}
// CreateBuildPod creates the pod to be used for the Docker build
// TODO: Make the Pod definition configurable
func (bs *DockerBuildStrategy) CreateBuildPod(build *buildapi.Build) (*v1.Pod, error) {
data, err := runtime.Encode(bs.Codec, 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(buildutil.CopyApiEnvVarToV1EnvVar(strategy.Env), &containerEnv, true)
}
serviceAccount := build.Spec.ServiceAccount
if len(serviceAccount) == 0 {
serviceAccount = buildutil.BuilderServiceAccountName
}
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: buildapi.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: buildutil.CopyApiResourcesToV1Resources(&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: buildutil.CopyApiResourcesToV1Resources(&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: buildutil.CopyApiResourcesToV1Resources(&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: buildutil.CopyApiResourcesToV1Resources(&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)
return pod, nil
}