Skip to content

Commit

Permalink
Merge pull request #9297 from killianmuldoon/pr-1.5-ownerRef-apichange
Browse files Browse the repository at this point in the history
[release-1.5] 🌱 Add test for ownerReference apiVersion update
  • Loading branch information
k8s-ci-robot committed Aug 24, 2023
2 parents 3f639f4 + f60dc3c commit 551ea99
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 58 deletions.
57 changes: 1 addition & 56 deletions test/e2e/clusterctl_upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var _ = Describe("When testing clusterctl upgrades (v0.4=>current)", func() {
WorkloadFlavor: "",
// This check ensures that ownerReference apiVersions are updated for all types after the upgrade.
PostUpgrade: func(proxy framework.ClusterProxy, namespace, clusterName string) {
framework.ValidateOwnerReferencesOnUpdate(proxy, namespace,
framework.AssertOwnerReferences(namespace, proxy.GetKubeconfigPath(),
framework.CoreOwnerReferenceAssertion,
framework.ExpOwnerReferenceAssertions,
framework.DockerInfraOwnerReferenceAssertions,
Expand Down Expand Up @@ -82,17 +82,6 @@ var _ = Describe("When testing clusterctl upgrades (v1.0=>current)", func() {
WorkloadKubernetesVersion: "v1.23.17",
MgmtFlavor: "topology",
WorkloadFlavor: "",
// This check ensures that ownerReference apiVersions are updated for all types after the upgrade.
PostUpgrade: func(proxy framework.ClusterProxy, namespace, clusterName string) {
framework.ValidateOwnerReferencesOnUpdate(proxy, namespace,
framework.CoreOwnerReferenceAssertion,
framework.ExpOwnerReferenceAssertions,
framework.DockerInfraOwnerReferenceAssertions,
framework.KubeadmBootstrapOwnerReferenceAssertions,
framework.KubeadmControlPlaneOwnerReferenceAssertions,
framework.KubernetesReferenceAssertions,
)
},
}
})
})
Expand Down Expand Up @@ -123,17 +112,6 @@ var _ = Describe("When testing clusterctl upgrades (v1.3=>current)", func() {
WorkloadKubernetesVersion: "v1.26.4",
MgmtFlavor: "topology",
WorkloadFlavor: "",
// This check ensures that ownerReference apiVersions are updated for all types after the upgrade.
PostUpgrade: func(proxy framework.ClusterProxy, namespace, clusterName string) {
framework.ValidateOwnerReferencesOnUpdate(proxy, namespace,
framework.CoreOwnerReferenceAssertion,
framework.ExpOwnerReferenceAssertions,
framework.DockerInfraOwnerReferenceAssertions,
framework.KubeadmBootstrapOwnerReferenceAssertions,
framework.KubeadmControlPlaneOwnerReferenceAssertions,
framework.KubernetesReferenceAssertions,
)
},
}
})
})
Expand Down Expand Up @@ -164,17 +142,6 @@ var _ = Describe("When testing clusterctl upgrades using ClusterClass (v1.3=>cur
WorkloadKubernetesVersion: "v1.26.4",
MgmtFlavor: "topology",
WorkloadFlavor: "topology",
// This check ensures that ownerReference apiVersions are updated for all types after the upgrade.
PostUpgrade: func(proxy framework.ClusterProxy, namespace, clusterName string) {
framework.ValidateOwnerReferencesOnUpdate(proxy, namespace,
framework.CoreOwnerReferenceAssertion,
framework.ExpOwnerReferenceAssertions,
framework.DockerInfraOwnerReferenceAssertions,
framework.KubeadmBootstrapOwnerReferenceAssertions,
framework.KubeadmControlPlaneOwnerReferenceAssertions,
framework.KubernetesReferenceAssertions,
)
},
}
})
})
Expand All @@ -195,17 +162,6 @@ var _ = Describe("When testing clusterctl upgrades (v1.4=>current)", func() {
WorkloadKubernetesVersion: "v1.27.3",
MgmtFlavor: "topology",
WorkloadFlavor: "",
// This check ensures that ownerReference apiVersions are updated for all types after the upgrade.
PostUpgrade: func(proxy framework.ClusterProxy, namespace, clusterName string) {
framework.ValidateOwnerReferencesOnUpdate(proxy, namespace,
framework.CoreOwnerReferenceAssertion,
framework.ExpOwnerReferenceAssertions,
framework.DockerInfraOwnerReferenceAssertions,
framework.KubeadmBootstrapOwnerReferenceAssertions,
framework.KubeadmControlPlaneOwnerReferenceAssertions,
framework.KubernetesReferenceAssertions,
)
},
}
})
})
Expand All @@ -226,17 +182,6 @@ var _ = Describe("When testing clusterctl upgrades using ClusterClass (v1.4=>cur
WorkloadKubernetesVersion: "v1.27.3",
MgmtFlavor: "topology",
WorkloadFlavor: "topology",
// This check ensures that ownerReference apiVersions are updated for all types after the upgrade.
PostUpgrade: func(proxy framework.ClusterProxy, namespace, clusterName string) {
framework.ValidateOwnerReferencesOnUpdate(proxy, namespace,
framework.CoreOwnerReferenceAssertion,
framework.ExpOwnerReferenceAssertions,
framework.DockerInfraOwnerReferenceAssertions,
framework.KubeadmBootstrapOwnerReferenceAssertions,
framework.KubeadmControlPlaneOwnerReferenceAssertions,
framework.KubernetesReferenceAssertions,
)
},
}
})
})
18 changes: 18 additions & 0 deletions test/e2e/quick_start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ var _ = Describe("When following the Cluster API quick-start", func() {
framework.KubeadmControlPlaneOwnerReferenceAssertions,
framework.KubernetesReferenceAssertions,
)
// This check ensures that owner references are correctly updated to the correct apiVersion.
framework.ValidateOwnerReferencesOnUpdate(ctx, proxy, namespace, clusterName,
framework.CoreOwnerReferenceAssertion,
framework.ExpOwnerReferenceAssertions,
framework.DockerInfraOwnerReferenceAssertions,
framework.KubeadmBootstrapOwnerReferenceAssertions,
framework.KubeadmControlPlaneOwnerReferenceAssertions,
framework.KubernetesReferenceAssertions,
)
},
}
})
Expand All @@ -72,6 +81,15 @@ var _ = Describe("When following the Cluster API quick-start with ClusterClass [
framework.KubeadmControlPlaneOwnerReferenceAssertions,
framework.KubernetesReferenceAssertions,
)
// This check ensures that owner references are correctly updated to the correct apiVersion.
framework.ValidateOwnerReferencesOnUpdate(ctx, proxy, namespace, clusterName,
framework.CoreOwnerReferenceAssertion,
framework.ExpOwnerReferenceAssertions,
framework.DockerInfraOwnerReferenceAssertions,
framework.KubeadmBootstrapOwnerReferenceAssertions,
framework.KubeadmControlPlaneOwnerReferenceAssertions,
framework.KubernetesReferenceAssertions,
)
},
}
})
Expand Down
46 changes: 44 additions & 2 deletions test/framework/ownerreference_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,22 @@ import (
)

// ValidateOwnerReferencesOnUpdate checks that expected owner references are updated to the correct apiVersion.
func ValidateOwnerReferencesOnUpdate(proxy ClusterProxy, namespace string, assertFuncs ...map[string]func(reference []metav1.OwnerReference) error) {
// Check that the ownerReferences are as expected on the first iteration.
func ValidateOwnerReferencesOnUpdate(ctx context.Context, proxy ClusterProxy, namespace, clusterName string, assertFuncs ...map[string]func(reference []metav1.OwnerReference) error) {
clusterKey := client.ObjectKey{Namespace: namespace, Name: clusterName}

// Changes the version of all the owner references to v1alpha1. Expect the apiVersion to be updated after reconciliation.
setClusterPause(ctx, proxy.GetClient(), clusterKey, true)

// Change the version of the OwnerReferences on each object in the Graph to "v1alpha1"
changeOwnerReferencesAPIVersion(ctx, proxy, namespace)

setClusterPause(ctx, proxy.GetClient(), clusterKey, false)

// Annotate the clusterClass, if one is in use, to speed up reconciliation. This ensures ClusterClass ownerReferences
// are re-reconciled before asserting the owner reference graph.
forceClusterClassReconcile(ctx, proxy.GetClient(), clusterKey)

// Check that the ownerReferences have updated their apiVersions to current versions after reconciliation.
AssertOwnerReferences(namespace, proxy.GetKubeconfigPath(), assertFuncs...)
}

Expand Down Expand Up @@ -351,6 +365,34 @@ func forceClusterClassReconcile(ctx context.Context, cli client.Client, clusterK
}
}

func changeOwnerReferencesAPIVersion(ctx context.Context, proxy ClusterProxy, namespace string) {
graph, err := clusterctlcluster.GetOwnerGraph(namespace, proxy.GetKubeconfigPath())
Expect(err).ToNot(HaveOccurred())
for _, object := range graph {
ref := object.Object
obj := new(unstructured.Unstructured)
obj.SetAPIVersion(ref.APIVersion)
obj.SetKind(ref.Kind)
obj.SetName(ref.Name)

Expect(proxy.GetClient().Get(ctx, client.ObjectKey{Namespace: namespace, Name: object.Object.Name}, obj)).To(Succeed())
helper, err := patch.NewHelper(obj, proxy.GetClient())
Expect(err).ToNot(HaveOccurred())

newOwners := []metav1.OwnerReference{}
for _, owner := range obj.GetOwnerReferences() {
gv, err := schema.ParseGroupVersion(owner.APIVersion)
Expect(err).To(Succeed())
gv.Version = "v1alpha1"
owner.APIVersion = gv.String()
newOwners = append(newOwners, owner)
}

obj.SetOwnerReferences(newOwners)
Expect(helper.Patch(ctx, obj)).To(Succeed())
}
}

func removeOwnerReferences(ctx context.Context, proxy ClusterProxy, namespace string) {
graph, err := clusterctlcluster.GetOwnerGraph(namespace, proxy.GetKubeconfigPath())
Expect(err).ToNot(HaveOccurred())
Expand Down

0 comments on commit 551ea99

Please sign in to comment.