Skip to content

Commit c5e69d1

Browse files
Merge pull request #18778 from jim-minter/issue18772
Automatic merge from submit-queue (batch tested with PRs 18778, 18709, 18876, 18897, 18652). apply build defaults to init containers fixes #18772
2 parents c75f466 + 1965a58 commit c5e69d1

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

pkg/build/controller/build/defaults/defaults.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -80,26 +80,33 @@ func (b BuildDefaults) applyPodDefaults(pod *v1.Pod) {
8080

8181
// Apply default resources
8282
defaultResources := b.config.Resources
83+
allContainers := make([]*v1.Container, 0, len(pod.Spec.Containers)+len(pod.Spec.InitContainers))
8384
for i := range pod.Spec.Containers {
84-
podEnv := &pod.Spec.Containers[i].Env
85-
util.MergeTrustedEnvWithoutDuplicates(util.CopyApiEnvVarToV1EnvVar(b.config.Env), podEnv, false)
85+
allContainers = append(allContainers, &pod.Spec.Containers[i])
86+
}
87+
for i := range pod.Spec.InitContainers {
88+
allContainers = append(allContainers, &pod.Spec.InitContainers[i])
89+
}
90+
91+
for _, c := range allContainers {
92+
util.MergeTrustedEnvWithoutDuplicates(util.CopyApiEnvVarToV1EnvVar(b.config.Env), &c.Env, false)
8693

87-
if pod.Spec.Containers[i].Resources.Limits == nil {
88-
pod.Spec.Containers[i].Resources.Limits = v1.ResourceList{}
94+
if c.Resources.Limits == nil {
95+
c.Resources.Limits = v1.ResourceList{}
8996
}
9097
for name, value := range defaultResources.Limits {
91-
if _, ok := pod.Spec.Containers[i].Resources.Limits[v1.ResourceName(name)]; !ok {
98+
if _, ok := c.Resources.Limits[v1.ResourceName(name)]; !ok {
9299
glog.V(5).Infof("Setting default resource limit %s for pod %s/%s to %v", name, pod.Namespace, pod.Name, value)
93-
pod.Spec.Containers[i].Resources.Limits[v1.ResourceName(name)] = value
100+
c.Resources.Limits[v1.ResourceName(name)] = value
94101
}
95102
}
96-
if pod.Spec.Containers[i].Resources.Requests == nil {
97-
pod.Spec.Containers[i].Resources.Requests = v1.ResourceList{}
103+
if c.Resources.Requests == nil {
104+
c.Resources.Requests = v1.ResourceList{}
98105
}
99106
for name, value := range defaultResources.Requests {
100-
if _, ok := pod.Spec.Containers[i].Resources.Requests[v1.ResourceName(name)]; !ok {
107+
if _, ok := c.Resources.Requests[v1.ResourceName(name)]; !ok {
101108
glog.V(5).Infof("Setting default resource request %s for pod %s/%s to %v", name, pod.Namespace, pod.Name, value)
102-
pod.Spec.Containers[i].Resources.Requests[v1.ResourceName(name)] = value
109+
c.Resources.Requests[v1.ResourceName(name)] = value
103110
}
104111
}
105112
}

pkg/build/controller/build/defaults/defaults_test.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,9 @@ func TestResourceDefaults(t *testing.T) {
571571
// normally the buildconfig resources would be applied to the pod
572572
// when it was created, but this pod didn't get created by the normal
573573
// pod creation flow, so fake this out.
574+
for i := range pod.Spec.InitContainers {
575+
pod.Spec.InitContainers[i].Resources = buildutil.CopyApiResourcesToV1Resources(&test.BuildResource)
576+
}
574577
for i := range pod.Spec.Containers {
575578
pod.Spec.Containers[i].Resources = buildutil.CopyApiResourcesToV1Resources(&test.BuildResource)
576579
}
@@ -585,9 +588,12 @@ func TestResourceDefaults(t *testing.T) {
585588
if !kapihelper.Semantic.DeepEqual(test.ExpectedResource, build.Spec.Resources) {
586589
t.Fatalf("%v:Build resource expected expected=actual, %#v != %#v", name, test.ExpectedResource, build.Spec.Resources)
587590
}
588-
for i := range pod.Spec.Containers {
589-
if !kapihelper.Semantic.DeepEqual(buildutil.CopyApiResourcesToV1Resources(&test.ExpectedResource), pod.Spec.Containers[i].Resources) {
590-
t.Fatalf("%v:Pod container %d resource expected expected=actual, got expected:\n%#v\nactual:\n%#v", name, i, test.ExpectedResource, pod.Spec.Containers[i].Resources)
591+
592+
allContainers := append([]v1.Container{}, pod.Spec.Containers...)
593+
allContainers = append(allContainers, pod.Spec.InitContainers...)
594+
for i, c := range allContainers {
595+
if !kapihelper.Semantic.DeepEqual(buildutil.CopyApiResourcesToV1Resources(&test.ExpectedResource), c.Resources) {
596+
t.Fatalf("%v: Pod container %d resource expected expected=actual, got expected:\n%#v\nactual:\n%#v", name, i, test.ExpectedResource, c.Resources)
591597
}
592598
}
593599
}

0 commit comments

Comments
 (0)