Skip to content

Commit

Permalink
Fix pkg/inventory/deployment.go:ForDeployments(): don't modify the de…
Browse files Browse the repository at this point in the history
…ployment.spec.selector immutable field on update (#2380)

Signed-off-by: Serge Catudal <serge.catudal@gmail.com>
  • Loading branch information
secat authored Dec 7, 2023
1 parent 3667261 commit cb6ec64
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/inventory/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func ForDeployments(existing []appsv1.Deployment, desired []appsv1.Deployment) D
// we can't blindly DeepCopyInto, so, we select what we bring from the new to the old object
tp.Spec = v.Spec
tp.Spec = inject.PropagateOAuthCookieSecret(t.Spec, v.Spec)

// Deployment.Spec.Selector is an immutable field: we can't update
// this field with a new value, we MUST keep the original field value
tp.Spec.Selector = t.Spec.Selector

tp.ObjectMeta.OwnerReferences = v.ObjectMeta.OwnerReferences

for k, v := range v.ObjectMeta.Annotations {
Expand Down
40 changes: 40 additions & 0 deletions pkg/inventory/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,43 @@ func TestDeploymentSetReplicasWhenDesiredIsNotNil(t *testing.T) {
assert.Len(t, inv.Update, 1)
assert.Equal(t, desiredReplicas, *inv.Update[0].Spec.Replicas)
}

func TestDeploymentKeepSelectorOnUpdate(t *testing.T) {
desired := []appsv1.Deployment{{
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"foo": "bar"},
},
},
}}

desiredSelector := &metav1.LabelSelector{
MatchLabels: map[string]string{"keep": "me"},
}
existing := []appsv1.Deployment{{
Spec: appsv1.DeploymentSpec{
Selector: desiredSelector,
},
}}

inv := ForDeployments(existing, desired)
assert.Len(t, inv.Update, 1)
assert.Equal(t, desiredSelector, inv.Update[0].Spec.Selector)
}

func TestDeploymentSetSelectorOnCreate(t *testing.T) {
desiredSelector := &metav1.LabelSelector{
MatchLabels: map[string]string{"foo": "bar"},
}
desired := []appsv1.Deployment{{
Spec: appsv1.DeploymentSpec{
Selector: desiredSelector,
},
}}

existing := make([]appsv1.Deployment, 0)

inv := ForDeployments(existing, desired)
assert.Len(t, inv.Create, 1)
assert.Equal(t, desiredSelector, inv.Create[0].Spec.Selector)
}

0 comments on commit cb6ec64

Please sign in to comment.