/
defaults.go
288 lines (253 loc) · 9.58 KB
/
defaults.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package defaults
import (
"k8s.io/klog"
"k8s.io/kubernetes/pkg/api/legacyscheme"
corev1 "k8s.io/api/core/v1"
buildv1 "github.com/openshift/api/build/v1"
configv1 "github.com/openshift/api/config/v1"
openshiftcontrolplanev1 "github.com/openshift/api/openshiftcontrolplane/v1"
"github.com/openshift/origin/pkg/build/controller/common"
"github.com/openshift/origin/pkg/build/util"
buildutil "github.com/openshift/origin/pkg/build/util"
)
type BuildDefaults struct {
Config *openshiftcontrolplanev1.BuildDefaultsConfig
DefaultProxy *configv1.ProxySpec
}
// ApplyDefaults applies configured build defaults to a build pod
func (b BuildDefaults) ApplyDefaults(pod *corev1.Pod) error {
build, err := common.GetBuildFromPod(pod)
if err != nil {
return nil
}
if b.DefaultProxy != nil {
b.applyPodProxyDefaults(pod, build.Spec.Strategy.CustomStrategy != nil)
}
if b.Config != nil {
klog.V(4).Infof("Applying defaults to build %s/%s", build.Namespace, build.Name)
b.applyBuildDefaults(build)
klog.V(4).Infof("Applying defaults to pod %s/%s", pod.Namespace, pod.Name)
b.applyPodDefaults(pod, build.Spec.Strategy.CustomStrategy != nil)
}
err = setPodLogLevelFromBuild(pod, build)
if err != nil {
return err
}
return common.SetBuildInPod(pod, build)
}
// setPodLogLevelFromBuild extracts BUILD_LOGLEVEL from the Build environment
// and feeds it as an argument to the Pod's entrypoint. The BUILD_LOGLEVEL
// environment variable may have been set in multiple ways: a default value,
// by a BuildConfig, or by the BuildDefaults admission plugin. In this method
// we finally act on the value by injecting it into the Pod.
func setPodLogLevelFromBuild(pod *corev1.Pod, build *buildv1.Build) error {
var envs []corev1.EnvVar
// Check whether the build strategy supports --loglevel parameter.
switch {
case build.Spec.Strategy.DockerStrategy != nil:
envs = build.Spec.Strategy.DockerStrategy.Env
case build.Spec.Strategy.SourceStrategy != nil:
envs = build.Spec.Strategy.SourceStrategy.Env
default:
// The build strategy does not support --loglevel
return nil
}
buildLogLevel := "0" // The ultimate default for the build pod's loglevel if no actor sets BUILD_LOGLEVEL in the Build
for i := range envs {
env := envs[i]
if env.Name == "BUILD_LOGLEVEL" {
buildLogLevel = env.Value
break
}
}
c := &pod.Spec.Containers[0]
c.Args = append(c.Args, "--loglevel="+buildLogLevel)
for i := range pod.Spec.InitContainers {
pod.Spec.InitContainers[i].Args = append(pod.Spec.InitContainers[i].Args, "--loglevel="+buildLogLevel)
}
return nil
}
func (b BuildDefaults) applyPodProxyDefaults(pod *corev1.Pod, isCustomBuild bool) {
allContainers := []*corev1.Container{}
for i := range pod.Spec.Containers {
allContainers = append(allContainers, &pod.Spec.Containers[i])
}
for i := range pod.Spec.InitContainers {
allContainers = append(allContainers, &pod.Spec.InitContainers[i])
}
for _, c := range allContainers {
// All env vars are allowed to be set in a custom build pod, the user already has
// total control over the env+logic in a custom build pod anyway.
externalEnv := make([]corev1.EnvVar, 3)
externalEnv = append(externalEnv, corev1.EnvVar{Name: "HTTP_PROXY", Value: b.DefaultProxy.HTTPProxy})
externalEnv = append(externalEnv, corev1.EnvVar{Name: "HTTPS_PROXY", Value: b.DefaultProxy.HTTPSProxy})
externalEnv = append(externalEnv, corev1.EnvVar{Name: "NO_PROXY", Value: b.DefaultProxy.NoProxy})
if isCustomBuild {
util.MergeEnvWithoutDuplicates(externalEnv, &c.Env, false, []string{})
} else {
util.MergeTrustedEnvWithoutDuplicates(externalEnv, &c.Env, false)
}
}
}
func (b BuildDefaults) applyPodDefaults(pod *corev1.Pod, isCustomBuild bool) {
if len(b.Config.NodeSelector) != 0 && pod.Spec.NodeSelector == nil {
// only apply nodeselector defaults if the pod has no nodeselector labels
// already.
pod.Spec.NodeSelector = map[string]string{}
for k, v := range b.Config.NodeSelector {
addDefaultNodeSelector(k, v, pod.Spec.NodeSelector)
}
}
if len(b.Config.Annotations) != 0 {
if pod.Annotations == nil {
pod.Annotations = map[string]string{}
}
for k, v := range b.Config.Annotations {
addDefaultAnnotation(k, v, pod.Annotations)
}
}
// Apply default resources
defaultResources := b.Config.Resources
allContainers := make([]*corev1.Container, 0, len(pod.Spec.Containers)+len(pod.Spec.InitContainers))
for i := range pod.Spec.Containers {
allContainers = append(allContainers, &pod.Spec.Containers[i])
}
for i := range pod.Spec.InitContainers {
allContainers = append(allContainers, &pod.Spec.InitContainers[i])
}
for _, c := range allContainers {
// All env vars are allowed to be set in a custom build pod, the user already has
// total control over the env+logic in a custom build pod anyway.
externalEnv := make([]corev1.EnvVar, len(b.Config.Env))
for i, v := range b.Config.Env {
externalEnv[i] = corev1.EnvVar{}
if err := legacyscheme.Scheme.Convert(&v, &externalEnv[i], nil); err != nil {
panic(err)
}
}
if isCustomBuild {
util.MergeEnvWithoutDuplicates(externalEnv, &c.Env, false, []string{})
} else {
util.MergeTrustedEnvWithoutDuplicates(externalEnv, &c.Env, false)
}
if c.Resources.Limits == nil {
c.Resources.Limits = corev1.ResourceList{}
}
for name, value := range defaultResources.Limits {
if _, ok := c.Resources.Limits[corev1.ResourceName(name)]; !ok {
klog.V(5).Infof("Setting default resource limit %s for pod %s/%s to %v", name, pod.Namespace, pod.Name, value)
c.Resources.Limits[corev1.ResourceName(name)] = value
}
}
if c.Resources.Requests == nil {
c.Resources.Requests = corev1.ResourceList{}
}
for name, value := range defaultResources.Requests {
if _, ok := c.Resources.Requests[corev1.ResourceName(name)]; !ok {
klog.V(5).Infof("Setting default resource request %s for pod %s/%s to %v", name, pod.Namespace, pod.Name, value)
c.Resources.Requests[corev1.ResourceName(name)] = value
}
}
}
}
func (b BuildDefaults) applyBuildDefaults(build *buildv1.Build) {
// Apply default env
for _, envVar := range b.Config.Env {
klog.V(5).Infof("Adding default environment variable %s=%s to build %s/%s", envVar.Name, envVar.Value, build.Namespace, build.Name)
externalEnv := corev1.EnvVar{}
if err := legacyscheme.Scheme.Convert(&envVar, &externalEnv, nil); err != nil {
panic(err)
}
addDefaultEnvVar(build, externalEnv)
}
// Apply default labels
for _, lbl := range b.Config.ImageLabels {
klog.V(5).Infof("Adding default image label %s=%s to build %s/%s", lbl.Name, lbl.Value, build.Namespace, build.Name)
label := buildv1.ImageLabel{
Name: lbl.Name,
Value: lbl.Value,
}
addDefaultLabel(label, &build.Spec.Output.ImageLabels)
}
sourceDefaults := b.Config.SourceStrategyDefaults
sourceStrategy := build.Spec.Strategy.SourceStrategy
if sourceDefaults != nil && sourceDefaults.Incremental != nil && *sourceDefaults.Incremental &&
sourceStrategy != nil && sourceStrategy.Incremental == nil {
klog.V(5).Infof("Setting source strategy Incremental to true in build %s/%s", build.Namespace, build.Name)
t := true
build.Spec.Strategy.SourceStrategy.Incremental = &t
}
// Apply git proxy defaults
if build.Spec.Source.Git == nil {
return
}
if len(b.Config.GitHTTPProxy) != 0 {
if build.Spec.Source.Git.HTTPProxy == nil {
t := b.Config.GitHTTPProxy
klog.V(5).Infof("Setting default Git HTTP proxy of build %s/%s to %s", build.Namespace, build.Name, t)
build.Spec.Source.Git.HTTPProxy = &t
}
}
if len(b.Config.GitHTTPSProxy) != 0 {
if build.Spec.Source.Git.HTTPSProxy == nil {
t := b.Config.GitHTTPSProxy
klog.V(5).Infof("Setting default Git HTTPS proxy of build %s/%s to %s", build.Namespace, build.Name, t)
build.Spec.Source.Git.HTTPSProxy = &t
}
}
if len(b.Config.GitNoProxy) != 0 {
if build.Spec.Source.Git.NoProxy == nil {
t := b.Config.GitNoProxy
klog.V(5).Infof("Setting default Git no proxy of build %s/%s to %s", build.Namespace, build.Name, t)
build.Spec.Source.Git.NoProxy = &t
}
}
//Apply default resources
defaultResources := b.Config.Resources
if build.Spec.Resources.Limits == nil {
build.Spec.Resources.Limits = corev1.ResourceList{}
}
for name, value := range defaultResources.Limits {
if _, ok := build.Spec.Resources.Limits[corev1.ResourceName(name)]; !ok {
klog.V(5).Infof("Setting default resource limit %s for build %s/%s to %v", name, build.Namespace, build.Name, value)
build.Spec.Resources.Limits[corev1.ResourceName(name)] = value
}
}
if build.Spec.Resources.Requests == nil {
build.Spec.Resources.Requests = corev1.ResourceList{}
}
for name, value := range defaultResources.Requests {
if _, ok := build.Spec.Resources.Requests[corev1.ResourceName(name)]; !ok {
klog.V(5).Infof("Setting default resource request %s for build %s/%s to %v", name, build.Namespace, build.Name, value)
build.Spec.Resources.Requests[corev1.ResourceName(name)] = value
}
}
}
func addDefaultEnvVar(build *buildv1.Build, v corev1.EnvVar) {
envVars := buildutil.GetBuildEnv(build)
for i := range envVars {
if envVars[i].Name == v.Name {
return
}
}
envVars = append(envVars, v)
buildutil.SetBuildEnv(build, envVars)
}
func addDefaultLabel(defaultLabel buildv1.ImageLabel, buildLabels *[]buildv1.ImageLabel) {
for _, lbl := range *buildLabels {
if lbl.Name == defaultLabel.Name {
return
}
}
*buildLabels = append(*buildLabels, defaultLabel)
}
func addDefaultNodeSelector(k, v string, selectors map[string]string) {
if _, ok := selectors[k]; !ok {
selectors[k] = v
}
}
func addDefaultAnnotation(k, v string, annotations map[string]string) {
if _, ok := annotations[k]; !ok {
annotations[k] = v
}
}