Skip to content

Commit 70c58f5

Browse files
committed
fix: clear absent provider fields on reconcile
Zero object before unmarshal so disabled fields (e.g. readAffinity) are cleared on the live object rather than silently retained via json merge accumulation. Preserve labels, annotations, ownerRefs, resourceVersion and creationTimestamp across the reset. Signed-off-by: Leela Venkaiah G <lgangava@ibm.com>
1 parent 6a49c1a commit 70c58f5

1 file changed

Lines changed: 27 additions & 5 deletions

File tree

‎internal/controller/storageclient_controller.go‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -841,16 +841,38 @@ func (r *storageClientReconcile) reconcileResourcesByGK(
841841
func (r *storageClientReconcile) reconcileResource(obj client.Object, desiredState desiredKubeObject) error {
842842

843843
mutateFunc := func() error {
844-
// Unmarshal follows merge semantics, that means that we don't need to worry about overriding the status,
845-
// or any metadata fields. There is an exception when it comes to creationTimestamp which gets serialized into
846-
// default value.
844+
// Preserve metadata that must survive provider updates.
845+
labels := obj.GetLabels()
846+
annotations := obj.GetAnnotations()
847+
ownerRefs := obj.GetOwnerReferences()
848+
finalizers := obj.GetFinalizers()
849+
uid := obj.GetUID()
847850
creationTimestamp := obj.GetCreationTimestamp()
851+
resourceVersion := obj.GetResourceVersion()
852+
853+
// Zero the object so absent fields in the response clear existing
854+
// values rather than being silently retained via merge accumulation.
855+
goType := reflect.TypeOf(obj).Elem() // concrete struct type, e.g. CephConnection not *CephConnection
856+
zeroValue := reflect.New(goType).Elem() // zeroed struct value of that type
857+
objValue := reflect.ValueOf(obj).Elem() // addressable struct value behind obj's pointer
858+
objValue.Set(zeroValue) // reset in-place, same pointer CreateOrUpdate holds
859+
848860
if err := json.Unmarshal(desiredState.bytes, obj); err != nil {
849-
return fmt.Errorf("failed to unmarshal %s configuration response: %v", obj.GetName(), err)
861+
return fmt.Errorf("failed to unmarshal %s configuration response: %v", desiredState.Name, err)
850862
}
863+
864+
obj.SetName(desiredState.Name)
865+
obj.SetNamespace(desiredState.Namespace)
866+
obj.SetLabels(labels)
867+
obj.SetAnnotations(annotations)
868+
obj.SetOwnerReferences(ownerRefs)
869+
obj.SetFinalizers(finalizers)
870+
obj.SetUID(uid)
851871
obj.SetCreationTimestamp(creationTimestamp)
872+
obj.SetResourceVersion(resourceVersion)
873+
852874
if err := r.own(obj); err != nil {
853-
return fmt.Errorf("failed to own %s resource: %v", obj.GetName(), err)
875+
return fmt.Errorf("failed to own %s resource: %v", desiredState.Name, err)
854876
}
855877
return nil
856878
}

0 commit comments

Comments
 (0)