forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
admission.go
216 lines (188 loc) · 6.97 KB
/
admission.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
package defaults
import (
"github.com/golang/glog"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/v1"
buildadmission "github.com/openshift/origin/pkg/build/admission"
defaultsapi "github.com/openshift/origin/pkg/build/admission/defaults/api"
"github.com/openshift/origin/pkg/build/admission/defaults/api/validation"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
"github.com/openshift/origin/pkg/build/util"
buildutil "github.com/openshift/origin/pkg/build/util"
configapi "github.com/openshift/origin/pkg/cmd/server/api"
)
type BuildDefaults struct {
config *defaultsapi.BuildDefaultsConfig
}
// NewBuildDefaults creates a new BuildDefaults that will apply the defaults specified in the plugin config
func NewBuildDefaults(pluginConfig map[string]configapi.AdmissionPluginConfig) (BuildDefaults, error) {
config := &defaultsapi.BuildDefaultsConfig{}
err := buildadmission.ReadPluginConfig(pluginConfig, defaultsapi.BuildDefaultsPlugin, config)
if err != nil {
return BuildDefaults{}, err
}
errs := validation.ValidateBuildDefaultsConfig(config)
if len(errs) > 0 {
return BuildDefaults{}, errs.ToAggregate()
}
glog.V(4).Infof("Initialized build defaults plugin with config: %#v", *config)
return BuildDefaults{config: config}, nil
}
// ApplyDefaults applies configured build defaults to a build pod
func (b BuildDefaults) ApplyDefaults(pod *v1.Pod) error {
if b.config == nil {
return nil
}
build, version, err := buildadmission.GetBuildFromPod(pod)
if err != nil {
return nil
}
glog.V(4).Infof("Applying defaults to build %s/%s", build.Namespace, build.Name)
b.applyBuildDefaults(build)
b.applyPodDefaults(pod)
err = buildadmission.SetPodLogLevelFromBuild(pod, build)
if err != nil {
return err
}
return buildadmission.SetBuildInPod(pod, build, version)
}
func (b BuildDefaults) applyPodDefaults(pod *v1.Pod) {
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 && pod.Annotations == nil {
pod.Annotations = map[string]string{}
}
for k, v := range b.config.Annotations {
addDefaultAnnotations(k, v, pod.Annotations)
}
// Apply default resources
defaultResources := b.config.Resources
for i := range pod.Spec.Containers {
podEnv := &pod.Spec.Containers[i].Env
util.MergeTrustedEnvWithoutDuplicates(util.CopyApiEnvVarToV1EnvVar(b.config.Env), podEnv, false)
if pod.Spec.Containers[i].Resources.Limits == nil {
pod.Spec.Containers[i].Resources.Limits = v1.ResourceList{}
}
for name, value := range defaultResources.Limits {
if _, ok := pod.Spec.Containers[i].Resources.Limits[v1.ResourceName(name)]; !ok {
glog.V(5).Infof("Setting default resource limit %s for pod %s/%s to %s", name, pod.Namespace, pod.Name, value)
pod.Spec.Containers[i].Resources.Limits[v1.ResourceName(name)] = value
}
}
if pod.Spec.Containers[i].Resources.Requests == nil {
pod.Spec.Containers[i].Resources.Requests = v1.ResourceList{}
}
for name, value := range defaultResources.Requests {
if _, ok := pod.Spec.Containers[i].Resources.Requests[v1.ResourceName(name)]; !ok {
glog.V(5).Infof("Setting default resource request %s for pod %s/%s to %s", name, pod.Namespace, pod.Name, value)
pod.Spec.Containers[i].Resources.Requests[v1.ResourceName(name)] = value
}
}
}
}
func (b BuildDefaults) applyBuildDefaults(build *buildapi.Build) {
// Apply default env
for _, envVar := range b.config.Env {
glog.V(5).Infof("Adding default environment variable %s=%s to build %s/%s", envVar.Name, envVar.Value, build.Namespace, build.Name)
addDefaultEnvVar(build, envVar)
}
// Apply default labels
for _, lbl := range b.config.ImageLabels {
glog.V(5).Infof("Adding default image label %s=%s to build %s/%s", lbl.Name, lbl.Value, build.Namespace, build.Name)
addDefaultLabel(lbl, &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 {
glog.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
glog.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
glog.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
glog.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 = kapi.ResourceList{}
}
for name, value := range defaultResources.Limits {
if _, ok := build.Spec.Resources.Limits[name]; !ok {
glog.V(5).Infof("Setting default resource limit %s for build %s/%s to %s", name, build.Namespace, build.Name, value)
build.Spec.Resources.Limits[name] = value
}
}
if build.Spec.Resources.Requests == nil {
build.Spec.Resources.Requests = kapi.ResourceList{}
}
for name, value := range defaultResources.Requests {
if _, ok := build.Spec.Resources.Requests[name]; !ok {
glog.V(5).Infof("Setting default resource request %s for build %s/%s to %s", name, build.Namespace, build.Name, value)
build.Spec.Resources.Requests[name] = value
}
}
}
func addDefaultEnvVar(build *buildapi.Build, v kapi.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 buildapi.ImageLabel, buildLabels *[]buildapi.ImageLabel) {
found := false
for _, lbl := range *buildLabels {
if lbl.Name == defaultLabel.Name {
found = true
}
}
if !found {
*buildLabels = append(*buildLabels, defaultLabel)
}
}
func addDefaultNodeSelector(k, v string, selectors map[string]string) bool {
if _, ok := selectors[k]; !ok {
selectors[k] = v
return true
}
return false
}
func addDefaultAnnotations(k, v string, annotations map[string]string) bool {
if _, ok := annotations[k]; !ok {
annotations[k] = v
return true
}
return false
}