Skip to content

Commit

Permalink
Merge pull request #112223 from astraw99/fix-ownerRef-validate
Browse files Browse the repository at this point in the history
Fix ownerRef controller validate err msg

Kubernetes-commit: 9349688d40f14e5b6814ae6daecd386a3f6fe7b4
  • Loading branch information
k8s-publishing-bot committed Nov 8, 2022
2 parents 9e85d3a + b0dd9ec commit 0ceff90
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
9 changes: 5 additions & 4 deletions pkg/api/validation/objectmeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/api/validation/objectmeta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}

Expand Down

0 comments on commit 0ceff90

Please sign in to comment.