Skip to content

Commit 2f055c4

Browse files
Remove containers if requested in Update
1 parent b0250fa commit 2f055c4

File tree

2 files changed

+136
-40
lines changed

2 files changed

+136
-40
lines changed

lib/resourcemerge/core.go

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,8 @@ func ensurePodTemplateSpec(modified *bool, existing *corev1.PodTemplateSpec, req
2424
}
2525

2626
func ensurePodSpec(modified *bool, existing *corev1.PodSpec, required corev1.PodSpec) {
27-
// any container we specify, we require.
28-
for _, required := range required.InitContainers {
29-
var existingCurr *corev1.Container
30-
for j, curr := range existing.InitContainers {
31-
if curr.Name == required.Name {
32-
existingCurr = &existing.InitContainers[j]
33-
break
34-
}
35-
}
36-
if existingCurr == nil {
37-
*modified = true
38-
existing.Containers = append(existing.InitContainers, corev1.Container{})
39-
existingCurr = &existing.InitContainers[len(existing.InitContainers)-1]
40-
}
41-
ensureContainer(modified, existingCurr, required)
42-
}
43-
44-
for _, required := range required.Containers {
45-
var existingCurr *corev1.Container
46-
for j, curr := range existing.Containers {
47-
if curr.Name == required.Name {
48-
existingCurr = &existing.Containers[j]
49-
break
50-
}
51-
}
52-
if existingCurr == nil {
53-
*modified = true
54-
existing.Containers = append(existing.Containers, corev1.Container{})
55-
existingCurr = &existing.Containers[len(existing.Containers)-1]
56-
}
57-
ensureContainer(modified, existingCurr, required)
58-
}
27+
ensureContainers(modified, &existing.InitContainers, required.InitContainers)
28+
ensureContainers(modified, &existing.Containers, required.Containers)
5929

6030
// any volume we specify, we require.
6131
for _, required := range required.Volumes {
@@ -91,6 +61,38 @@ func ensurePodSpec(modified *bool, existing *corev1.PodSpec, required corev1.Pod
9161
setInt32Ptr(modified, &existing.Priority, required.Priority)
9262
}
9363

64+
func ensureContainers(modified *bool, existing *[]corev1.Container, required []corev1.Container) {
65+
for i, existingContainer := range *existing {
66+
matched := false
67+
for _, requiredContainer := range required {
68+
if existingContainer.Name == requiredContainer.Name {
69+
matched = true
70+
ensureContainer(modified, &existingContainer, requiredContainer)
71+
break
72+
}
73+
}
74+
if !matched {
75+
*modified = true
76+
*existing = append((*existing)[:i], (*existing)[i+1:]...)
77+
}
78+
}
79+
80+
for _, requiredContainer := range required {
81+
var existingCurr *corev1.Container
82+
for j, existingContainer := range *existing {
83+
if existingContainer.Name == requiredContainer.Name {
84+
existingCurr = &(*existing)[j]
85+
ensureContainer(modified, existingCurr, requiredContainer)
86+
break
87+
}
88+
}
89+
if existingCurr == nil {
90+
*modified = true
91+
*existing = append(*existing, requiredContainer)
92+
}
93+
}
94+
}
95+
9496
func ensureContainer(modified *bool, existing *corev1.Container, required corev1.Container) {
9597
setStringIfSet(modified, &existing.Name, required.Name)
9698
setStringIfSet(modified, &existing.Image, required.Image)

lib/resourcemerge/core_test.go

Lines changed: 102 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package resourcemerge
33
import (
44
"testing"
55

6+
"github.com/ghodss/yaml"
67
corev1 "k8s.io/api/core/v1"
78
"k8s.io/apimachinery/pkg/api/equality"
9+
"k8s.io/apimachinery/pkg/util/diff"
810
"k8s.io/utils/pointer"
911
)
1012

@@ -16,14 +18,98 @@ func TestEnsurePodSpec(t *testing.T) {
1618

1719
expectedModified bool
1820
expected corev1.PodSpec
19-
}{{
20-
name: "empty inputs",
21-
existing: corev1.PodSpec{},
22-
input: corev1.PodSpec{},
21+
}{
22+
{
23+
name: "empty inputs",
24+
existing: corev1.PodSpec{},
25+
input: corev1.PodSpec{},
2326

24-
expectedModified: false,
25-
expected: corev1.PodSpec{},
26-
}}
27+
expectedModified: false,
28+
expected: corev1.PodSpec{},
29+
},
30+
{
31+
name: "remove regular containers from existing",
32+
existing: corev1.PodSpec{
33+
Containers: []corev1.Container{
34+
corev1.Container{Name: "test"}}},
35+
input: corev1.PodSpec{},
36+
37+
expectedModified: true,
38+
expected: corev1.PodSpec{},
39+
},
40+
{
41+
name: "remove regular and init containers from existing",
42+
existing: corev1.PodSpec{
43+
InitContainers: []corev1.Container{
44+
corev1.Container{Name: "test-init"}},
45+
Containers: []corev1.Container{
46+
corev1.Container{Name: "test"}}},
47+
input: corev1.PodSpec{},
48+
49+
expectedModified: true,
50+
expected: corev1.PodSpec{},
51+
},
52+
{
53+
name: "remove init containers from existing",
54+
existing: corev1.PodSpec{
55+
InitContainers: []corev1.Container{
56+
corev1.Container{Name: "test-init"}}},
57+
input: corev1.PodSpec{},
58+
59+
expectedModified: true,
60+
expected: corev1.PodSpec{},
61+
},
62+
{
63+
name: "append regular and init containers",
64+
existing: corev1.PodSpec{
65+
InitContainers: []corev1.Container{
66+
corev1.Container{Name: "test-init-a"}},
67+
Containers: []corev1.Container{
68+
corev1.Container{Name: "test-a"}}},
69+
input: corev1.PodSpec{
70+
InitContainers: []corev1.Container{
71+
corev1.Container{Name: "test-init-a"},
72+
corev1.Container{Name: "test-init-b"},
73+
},
74+
Containers: []corev1.Container{
75+
corev1.Container{Name: "test-a"},
76+
corev1.Container{Name: "test-b"},
77+
},
78+
},
79+
80+
expectedModified: true,
81+
expected: corev1.PodSpec{
82+
InitContainers: []corev1.Container{
83+
corev1.Container{Name: "test-init-a"},
84+
corev1.Container{Name: "test-init-b"},
85+
},
86+
Containers: []corev1.Container{
87+
corev1.Container{Name: "test-a"},
88+
corev1.Container{Name: "test-b"},
89+
},
90+
},
91+
},
92+
{
93+
name: "match regular and init containers",
94+
existing: corev1.PodSpec{
95+
InitContainers: []corev1.Container{
96+
corev1.Container{Name: "test-init"}},
97+
Containers: []corev1.Container{
98+
corev1.Container{Name: "test"}}},
99+
input: corev1.PodSpec{
100+
InitContainers: []corev1.Container{
101+
corev1.Container{Name: "test-init"}},
102+
Containers: []corev1.Container{
103+
corev1.Container{Name: "test"}}},
104+
105+
expectedModified: false,
106+
expected: corev1.PodSpec{
107+
InitContainers: []corev1.Container{
108+
corev1.Container{Name: "test-init"}},
109+
Containers: []corev1.Container{
110+
corev1.Container{Name: "test"}}},
111+
},
112+
}
27113

28114
for _, test := range tests {
29115
t.Run(test.name, func(t *testing.T) {
@@ -34,8 +120,16 @@ func TestEnsurePodSpec(t *testing.T) {
34120
}
35121

36122
if !equality.Semantic.DeepEqual(test.existing, test.expected) {
37-
t.Errorf("mismatch PodSpec got: %v want: %v", test.existing, test.expected)
123+
t.Errorf("unexpected: %s", diff.ObjectReflectDiff(test.expected, test.existing))
38124
}
39125
})
40126
}
41127
}
128+
129+
func yamlOrDie(data interface{}) string {
130+
bytes, err := yaml.Marshal(data)
131+
if err != nil {
132+
panic(err)
133+
}
134+
return string(bytes)
135+
}

0 commit comments

Comments
 (0)