Skip to content

Commit

Permalink
Merge pull request #282 from wking/resource-merge-index-mutating-exis…
Browse files Browse the repository at this point in the history
…ting

Bug 1783221: lib/resourcemerge/core: Fix panic on container/port removal
  • Loading branch information
openshift-merge-robot committed Feb 4, 2020
2 parents 54faf6f + e8e80c2 commit abfb48d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/resourcemerge/core.go
Expand Up @@ -62,7 +62,8 @@ func ensurePodSpec(modified *bool, existing *corev1.PodSpec, required corev1.Pod
}

func ensureContainers(modified *bool, existing *[]corev1.Container, required []corev1.Container) {
for i, existingContainer := range *existing {
for i := len(*existing) - 1; i >= 0; i-- {
existingContainer := &(*existing)[i]
var existingCurr *corev1.Container
for _, requiredContainer := range required {
if existingContainer.Name == requiredContainer.Name {
Expand Down Expand Up @@ -198,7 +199,8 @@ func ensureContainerPort(modified *bool, existing *corev1.ContainerPort, require
}

func EnsureServicePorts(modified *bool, existing *[]corev1.ServicePort, required []corev1.ServicePort) {
for i, existingServicePort := range *existing {
for i := len(*existing) - 1; i >= 0; i-- {
existingServicePort := &(*existing)[i]
var existingCurr *corev1.ServicePort
for _, requiredServicePort := range required {
if existingServicePort.Name == requiredServicePort.Name {
Expand Down
21 changes: 21 additions & 0 deletions lib/resourcemerge/core_test.go
Expand Up @@ -247,6 +247,27 @@ func TestEnsurePodSpec(t *testing.T) {
},
},
},
{
name: "remove a container",
existing: corev1.PodSpec{
Containers: []corev1.Container{
corev1.Container{Name: "test-A"},
corev1.Container{Name: "test-B"},
},
},
input: corev1.PodSpec{
Containers: []corev1.Container{
corev1.Container{Name: "test-B"},
},
},

expectedModified: true,
expected: corev1.PodSpec{
Containers: []corev1.Container{
corev1.Container{Name: "test-B"},
},
},
},
}

for _, test := range tests {
Expand Down

0 comments on commit abfb48d

Please sign in to comment.