Skip to content

Commit

Permalink
Merge pull request #9269 from killianmuldoon/pr-change-api-version
Browse files Browse the repository at this point in the history
🌱 Add test for ownerReference apiVersion update
  • Loading branch information
k8s-ci-robot authored Aug 24, 2023
2 parents 810a86f + 5e4d8c5 commit 3b55934
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 59 deletions.
57 changes: 0 additions & 57 deletions test/e2e/clusterctl_upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ package e2e
import (
. "github.com/onsi/ginkgo/v2"
"k8s.io/utils/pointer"

"sigs.k8s.io/cluster-api/test/framework"
)

var _ = Describe("When testing clusterctl upgrades (v1.0=>current)", func() {
Expand Down Expand Up @@ -51,17 +49,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 All @@ -88,17 +75,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 @@ -125,17 +101,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,
)
},
}
})
})
Expand All @@ -156,17 +121,6 @@ var _ = Describe("When testing clusterctl upgrades (v1.5=>current)", func() {
WorkloadKubernetesVersion: "v1.28.0",
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 @@ -187,17 +141,6 @@ var _ = Describe("When testing clusterctl upgrades using ClusterClass (v1.5=>cur
WorkloadKubernetesVersion: "v1.28.0",
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 @@ -353,6 +367,34 @@ func forceClusterClassReconcile(ctx context.Context, cli client.Client, clusterK
}
}

func changeOwnerReferencesAPIVersion(ctx context.Context, proxy ClusterProxy, namespace string) {
graph, err := clusterctlcluster.GetOwnerGraph(ctx, 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(ctx, namespace, proxy.GetKubeconfigPath())
Expect(err).ToNot(HaveOccurred())
Expand Down

0 comments on commit 3b55934

Please sign in to comment.