forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
admission.go
114 lines (100 loc) · 4.07 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
package overrides
import (
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/api/v1"
buildadmission "github.com/openshift/origin/pkg/build/admission"
overridesapi "github.com/openshift/origin/pkg/build/admission/overrides/api"
"github.com/openshift/origin/pkg/build/admission/overrides/api/validation"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
configapi "github.com/openshift/origin/pkg/cmd/server/api"
)
type BuildOverrides struct {
config *overridesapi.BuildOverridesConfig
}
// NewBuildOverrides creates a new BuildOverrides that will apply the overrides specified in the plugin config
func NewBuildOverrides(pluginConfig map[string]configapi.AdmissionPluginConfig) (BuildOverrides, error) {
config := &overridesapi.BuildOverridesConfig{}
err := buildadmission.ReadPluginConfig(pluginConfig, overridesapi.BuildOverridesPlugin, config)
if err != nil {
return BuildOverrides{}, err
}
errs := validation.ValidateBuildOverridesConfig(config)
if len(errs) > 0 {
return BuildOverrides{}, errs.ToAggregate()
}
glog.V(4).Infof("Initialized build overrides plugin with config: %#v", *config)
return BuildOverrides{config: config}, nil
}
// ApplyOverrides applies configured overrides to a build in a build pod
func (b BuildOverrides) ApplyOverrides(pod *v1.Pod) error {
if b.config == nil {
return nil
}
build, version, err := buildadmission.GetBuildFromPod(pod)
if err != nil {
return err
}
glog.V(4).Infof("Applying overrides to build %s/%s", build.Namespace, build.Name)
if b.config.ForcePull {
if build.Spec.Strategy.DockerStrategy != nil {
glog.V(5).Infof("Setting docker strategy ForcePull to true in build %s/%s", build.Namespace, build.Name)
build.Spec.Strategy.DockerStrategy.ForcePull = true
}
if build.Spec.Strategy.SourceStrategy != nil {
glog.V(5).Infof("Setting source strategy ForcePull to true in build %s/%s", build.Namespace, build.Name)
build.Spec.Strategy.SourceStrategy.ForcePull = true
}
if build.Spec.Strategy.CustomStrategy != nil {
err := applyForcePullToPod(pod)
if err != nil {
return err
}
glog.V(5).Infof("Setting custom strategy ForcePull to true in build %s/%s", build.Namespace, build.Name)
build.Spec.Strategy.CustomStrategy.ForcePull = true
}
}
// Apply label overrides
for _, lbl := range b.config.ImageLabels {
glog.V(5).Infof("Overriding image label %s=%s in build %s/%s", lbl.Name, lbl.Value, build.Namespace, build.Name)
overrideLabel(lbl, &build.Spec.Output.ImageLabels)
}
if len(b.config.NodeSelector) != 0 && pod.Spec.NodeSelector == nil {
pod.Spec.NodeSelector = map[string]string{}
}
for k, v := range b.config.NodeSelector {
glog.V(5).Infof("Adding override nodeselector %s=%s to build pod %s/%s", k, v, pod.Namespace, pod.Name)
pod.Spec.NodeSelector[k] = v
}
if len(b.config.Annotations) != 0 && pod.Annotations == nil {
pod.Annotations = map[string]string{}
}
for k, v := range b.config.Annotations {
glog.V(5).Infof("Adding override annotation %s=%s to build pod %s/%s", k, v, pod.Namespace, pod.Name)
pod.Annotations[k] = v
}
return buildadmission.SetBuildInPod(pod, build, version)
}
func applyForcePullToPod(pod *v1.Pod) error {
for i := range pod.Spec.InitContainers {
glog.V(5).Infof("Setting ImagePullPolicy to PullAlways on init container %s of pod %s/%s", pod.Spec.InitContainers[i].Name, pod.Namespace, pod.Name)
pod.Spec.InitContainers[i].ImagePullPolicy = v1.PullAlways
}
for i := range pod.Spec.Containers {
glog.V(5).Infof("Setting ImagePullPolicy to PullAlways on container %s of pod %s/%s", pod.Spec.Containers[i].Name, pod.Namespace, pod.Name)
pod.Spec.Containers[i].ImagePullPolicy = v1.PullAlways
}
return nil
}
func overrideLabel(overridingLabel buildapi.ImageLabel, buildLabels *[]buildapi.ImageLabel) {
found := false
for i, lbl := range *buildLabels {
if lbl.Name == overridingLabel.Name {
glog.V(5).Infof("Replacing label %s (original value %q) with new value %q", lbl.Name, lbl.Value, overridingLabel.Value)
(*buildLabels)[i] = overridingLabel
found = true
}
}
if !found {
*buildLabels = append(*buildLabels, overridingLabel)
}
}