diff --git a/pkg/util/constants.go b/pkg/util/constants.go index 9b9ecb52d..5c9e89811 100644 --- a/pkg/util/constants.go +++ b/pkg/util/constants.go @@ -94,6 +94,8 @@ const ( StatefulSetKind = "StatefulSet" // EndpointSliceKind indicates the target resource is a endpointslice EndpointSliceKind = "EndpointSlice" + // PersistentVolumeClaimKind indicated the target resource is a persistentvolumeclaim + PersistentVolumeClaimKind = "PersistentVolumeClaim" // ServiceExportKind indicates the target resource is a serviceexport crd ServiceExportKind = "ServiceExport" diff --git a/pkg/util/objectwatcher/retain.go b/pkg/util/objectwatcher/retain.go index dbb411c4e..e91bc46dd 100644 --- a/pkg/util/objectwatcher/retain.go +++ b/pkg/util/objectwatcher/retain.go @@ -34,15 +34,17 @@ func RetainClusterFields(desiredObj, clusterObj *unstructured.Unstructured) erro // and be set by user in karmada-controller-plane. util.MergeAnnotations(desiredObj, clusterObj) - if targetKind == util.PodKind { + switch targetKind { + case util.PodKind: return retainPodFields(desiredObj, clusterObj) - } - if targetKind == util.ServiceKind { + case util.ServiceKind: return retainServiceFields(desiredObj, clusterObj) - } - if targetKind == util.ServiceAccountKind { + case util.ServiceAccountKind: return retainServiceAccountFields(desiredObj, clusterObj) + case util.PersistentVolumeClaimKind: + return retainPersistentVolumeClaimFields(desiredObj, clusterObj) } + return nil } @@ -175,3 +177,18 @@ func retainServiceAccountFields(desiredObj, clusterObj *unstructured.Unstructure } return nil } + +func retainPersistentVolumeClaimFields(desiredObj, clusterObj *unstructured.Unstructured) error { + // volumeName is allocated by member cluster and unchangeable, so it should be retained while updating + volumeName, ok, err := unstructured.NestedString(clusterObj.Object, "spec", "volumeName") + if err != nil { + return fmt.Errorf("error retrieving volumeName from pvc: %w", err) + } + if ok && len(volumeName) > 0 { + if err = unstructured.SetNestedField(desiredObj.Object, volumeName, "spec", "volumeName"); err != nil { + return fmt.Errorf("error setting volumeName for pvc: %w", err) + } + } + + return nil +}