From 0cd2588d4f7a7776f9a75e6ac39cd6ea99c16a3e Mon Sep 17 00:00:00 2001 From: Hiranmoy Das Chowdhury Date: Tue, 9 Apr 2024 11:14:33 +0600 Subject: [PATCH] deep copy issue in getting controller is solved --- staging/publishing/import-restrictions.yaml | 1 + .../apimachinery/pkg/apis/meta/v1/controller_ref.go | 13 ++++++++----- .../pkg/apis/meta/v1/controller_ref_test.go | 7 +++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/staging/publishing/import-restrictions.yaml b/staging/publishing/import-restrictions.yaml index b6a18b67c68..29df7c61c2a 100644 --- a/staging/publishing/import-restrictions.yaml +++ b/staging/publishing/import-restrictions.yaml @@ -41,6 +41,7 @@ - k8s.io/utils/net - k8s.io/utils/strings - k8s.io/klog + - k8s.io/utils/ptr - baseImportPath: "./staging/src/k8s.io/api" allowedImports: diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/controller_ref.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/controller_ref.go index 15b45ffa84b..5005beb12db 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/controller_ref.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/controller_ref.go @@ -18,6 +18,7 @@ package v1 import ( "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/utils/ptr" ) // IsControlledBy checks if the object has a controllerRef set to the given owner @@ -36,10 +37,14 @@ func GetControllerOf(controllee Object) *OwnerReference { return nil } cp := *ref + cp.Controller = ptr.To(*ref.Controller) + if ref.BlockOwnerDeletion != nil { + cp.BlockOwnerDeletion = ptr.To(*ref.BlockOwnerDeletion) + } return &cp } -// GetControllerOf returns a pointer to the controllerRef if controllee has a controller +// GetControllerOfNoCopy returns a pointer to the controllerRef if controllee has a controller func GetControllerOfNoCopy(controllee Object) *OwnerReference { refs := controllee.GetOwnerReferences() for i := range refs { @@ -52,14 +57,12 @@ func GetControllerOfNoCopy(controllee Object) *OwnerReference { // NewControllerRef creates an OwnerReference pointing to the given owner. func NewControllerRef(owner Object, gvk schema.GroupVersionKind) *OwnerReference { - blockOwnerDeletion := true - isController := true return &OwnerReference{ APIVersion: gvk.GroupVersion().String(), Kind: gvk.Kind, Name: owner.GetName(), UID: owner.GetUID(), - BlockOwnerDeletion: &blockOwnerDeletion, - Controller: &isController, + BlockOwnerDeletion: ptr.To(true), + Controller: ptr.To(true), } } diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/controller_ref_test.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/controller_ref_test.go index 44cfdf15894..3abfb9aa919 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/controller_ref_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/controller_ref_test.go @@ -69,6 +69,7 @@ func TestGetControllerOf(t *testing.T) { }, } controllerRef := NewControllerRef(obj1, gvk) + controllerRef.BlockOwnerDeletion = nil var falseRef = false obj2 := &metaObj{ ObjectMeta: ObjectMeta{ @@ -95,6 +96,12 @@ func TestGetControllerOf(t *testing.T) { if c.Name != controllerRef.Name || c.UID != controllerRef.UID { t.Errorf("Incorrect result of GetControllerOf: %v", c) } + + // test that all pointers are also deep copied + if (c.Controller == controllerRef.Controller) || + (c.BlockOwnerDeletion != nil && c.BlockOwnerDeletion == controllerRef.BlockOwnerDeletion) { + t.Errorf("GetControllerOf did not return deep copy: %v", c) + } } func BenchmarkGetControllerOf(b *testing.B) {