diff --git a/pkg/api/validation/objectmeta.go b/pkg/api/validation/objectmeta.go index 7c1c69054..593d7ba8c 100644 --- a/pkg/api/validation/objectmeta.go +++ b/pkg/api/validation/objectmeta.go @@ -91,15 +91,16 @@ func validateOwnerReference(ownerReference metav1.OwnerReference, fldPath *field // ValidateOwnerReferences validates that a set of owner references are correctly defined. func ValidateOwnerReferences(ownerReferences []metav1.OwnerReference, fldPath *field.Path) field.ErrorList { allErrs := field.ErrorList{} - controllerName := "" + firstControllerName := "" for _, ref := range ownerReferences { allErrs = append(allErrs, validateOwnerReference(ref, fldPath)...) if ref.Controller != nil && *ref.Controller { - if controllerName != "" { + curControllerName := ref.Kind + "/" + ref.Name + if firstControllerName != "" { allErrs = append(allErrs, field.Invalid(fldPath, ownerReferences, - fmt.Sprintf("Only one reference can have Controller set to true. Found \"true\" in references for %v and %v", controllerName, ref.Name))) + fmt.Sprintf("Only one reference can have Controller set to true. Found \"true\" in references for %v and %v", firstControllerName, curControllerName))) } else { - controllerName = ref.Name + firstControllerName = curControllerName } } } diff --git a/pkg/api/validation/objectmeta_test.go b/pkg/api/validation/objectmeta_test.go index 349da67f8..0331cf9f3 100644 --- a/pkg/api/validation/objectmeta_test.go +++ b/pkg/api/validation/objectmeta_test.go @@ -163,34 +163,34 @@ func TestValidateObjectMetaOwnerReferences(t *testing.T) { ownerReferences: []metav1.OwnerReference{ { APIVersion: "customresourceVersion", - Kind: "customresourceKind", + Kind: "customresourceKind1", Name: "name", UID: "1", Controller: &falseVar, }, { APIVersion: "customresourceVersion", - Kind: "customresourceKind", + Kind: "customresourceKind2", Name: "name", UID: "2", Controller: &trueVar, }, { APIVersion: "customresourceVersion", - Kind: "customresourceKind", + Kind: "customresourceKind3", Name: "name", UID: "3", Controller: &trueVar, }, { APIVersion: "customresourceVersion", - Kind: "customresourceKind", + Kind: "customresourceKind4", Name: "name", UID: "4", }, }, expectError: true, - expectedErrorMessage: "Only one reference can have Controller set to true", + expectedErrorMessage: "Only one reference can have Controller set to true. Found \"true\" in references for customresourceKind2/name and customresourceKind3/name", }, }