From ed18ab54ba9925c835a69dd155b1217af9ab630a Mon Sep 17 00:00:00 2001 From: Matthew Cary Date: Thu, 5 Jan 2023 16:59:04 -0800 Subject: [PATCH] Avoid mutation of PVC in stateful set controller shared cache Change-Id: Ieb8e443e460150d16524ca1c1fb3770f546b2c28 --- pkg/controller/statefulset/stateful_pod_control.go | 1 + pkg/controller/statefulset/stateful_pod_control_test.go | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/pkg/controller/statefulset/stateful_pod_control.go b/pkg/controller/statefulset/stateful_pod_control.go index 0d045763e33..2b0546b4632 100644 --- a/pkg/controller/statefulset/stateful_pod_control.go +++ b/pkg/controller/statefulset/stateful_pod_control.go @@ -241,6 +241,7 @@ func (spc *StatefulPodControl) UpdatePodClaimForRetentionPolicy(set *apps.Statef return fmt.Errorf("Could not retrieve claim %s not found for %s when checking PVC deletion policy: %w", claimName, pod.Name, err) default: if !claimOwnerMatchesSetAndPod(claim, set, pod) { + claim = claim.DeepCopy() // Make a copy so we don't mutate the shared cache. needsUpdate := updateClaimOwnerRefForSetAndPod(claim, set, pod) if needsUpdate { err := spc.objectMgr.UpdateClaim(claim) diff --git a/pkg/controller/statefulset/stateful_pod_control_test.go b/pkg/controller/statefulset/stateful_pod_control_test.go index 4dcdfe90e3d..2eaf58bcffa 100644 --- a/pkg/controller/statefulset/stateful_pod_control_test.go +++ b/pkg/controller/statefulset/stateful_pod_control_test.go @@ -536,6 +536,11 @@ func TestStatefulPodControlUpdatePodClaimForRetentionPolicy(t *testing.T) { fakeClient := &fake.Clientset{} indexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}) claimLister := corelisters.NewPersistentVolumeClaimLister(indexer) + fakeClient.AddReactor("update", "persistentvolumeclaims", func(action core.Action) (bool, runtime.Object, error) { + update := action.(core.UpdateAction) + indexer.Update(update.GetObject()) + return true, update.GetObject(), nil + }) set := newStatefulSet(3) set.GetObjectMeta().SetUID("set-123") pod := newStatefulSetPod(set, 0)