Skip to content

Commit

Permalink
test: Ensure ownerRef assertions for all Kinds are evaluated
Browse files Browse the repository at this point in the history
The assertFuncs are passed as a list. Previously, if two assertFuncs
were defined for the same Kind, the assertFunc that appeared later
in the list would overwrite the one that appeared earlier.

If someone introduced a second assertFunc for a given Kind, they would
receive no signal that the first assertFunc was overwritten. That would
result in missed assertions.

With this change, if two or more assertFuncs are defined for the same
Kind, both are evaluated.
  • Loading branch information
dlipovetsky authored and k8s-infra-cherrypick-robot committed May 13, 2024
1 parent e226702 commit aac1100
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions test/framework/ownerreference_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ func ValidateOwnerReferencesResilience(ctx context.Context, proxy ClusterProxy,
}

func AssertOwnerReferences(namespace, kubeconfigPath string, ownerGraphFilterFunction clusterctlcluster.GetOwnerGraphFilterFunction, assertFuncs ...map[string]func(reference []metav1.OwnerReference) error) {
allAssertFuncs := map[string]func(reference []metav1.OwnerReference) error{}
allAssertFuncs := map[string][]func(reference []metav1.OwnerReference) error{}
for _, m := range assertFuncs {
for k, v := range m {
allAssertFuncs[k] = v
allAssertFuncs[k] = append(allAssertFuncs[k], v)
}
}
Eventually(func() error {
Expand All @@ -125,8 +125,10 @@ func AssertOwnerReferences(namespace, kubeconfigPath string, ownerGraphFilterFun
allErrs = append(allErrs, fmt.Errorf("kind %s does not have an associated ownerRef assertion function", v.Object.Kind))
continue
}
if err := allAssertFuncs[v.Object.Kind](v.Owners); err != nil {
allErrs = append(allErrs, errors.Wrapf(err, "Unexpected ownerReferences for %s/%s", v.Object.Kind, v.Object.Name))
for _, f := range allAssertFuncs[v.Object.Kind] {
if err := f(v.Owners); err != nil {
allErrs = append(allErrs, errors.Wrapf(err, "Unexpected ownerReferences for %s/%s", v.Object.Kind, v.Object.Name))
}
}
}
return kerrors.NewAggregate(allErrs)
Expand Down Expand Up @@ -308,7 +310,6 @@ var DockerInfraOwnerReferenceAssertions = map[string]func([]metav1.OwnerReferenc
dockerMachineKind: func(owners []metav1.OwnerReference) error {
// The DockerMachine must be owned and controlled by a Machine or a DockerMachinePool.
return HasOneOfExactOwners(owners, []metav1.OwnerReference{machineController}, []metav1.OwnerReference{machineController, dockerMachinePoolController})

},
dockerMachineTemplateKind: func(owners []metav1.OwnerReference) error {
// Base DockerMachineTemplates referenced in a ClusterClass must be owned by the ClusterClass.
Expand Down

0 comments on commit aac1100

Please sign in to comment.