Skip to content

Commit 3b55934

Browse files
authored
Merge pull request #9269 from killianmuldoon/pr-change-api-version
🌱 Add test for ownerReference apiVersion update
2 parents 810a86f + 5e4d8c5 commit 3b55934

File tree

3 files changed

+62
-59
lines changed

3 files changed

+62
-59
lines changed

test/e2e/clusterctl_upgrade_test.go

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ package e2e
2222
import (
2323
. "github.com/onsi/ginkgo/v2"
2424
"k8s.io/utils/pointer"
25-
26-
"sigs.k8s.io/cluster-api/test/framework"
2725
)
2826

2927
var _ = Describe("When testing clusterctl upgrades (v1.0=>current)", func() {
@@ -51,17 +49,6 @@ var _ = Describe("When testing clusterctl upgrades (v1.0=>current)", func() {
5149
WorkloadKubernetesVersion: "v1.23.17",
5250
MgmtFlavor: "topology",
5351
WorkloadFlavor: "",
54-
// This check ensures that ownerReference apiVersions are updated for all types after the upgrade.
55-
PostUpgrade: func(proxy framework.ClusterProxy, namespace, clusterName string) {
56-
framework.ValidateOwnerReferencesOnUpdate(proxy, namespace,
57-
framework.CoreOwnerReferenceAssertion,
58-
framework.ExpOwnerReferenceAssertions,
59-
framework.DockerInfraOwnerReferenceAssertions,
60-
framework.KubeadmBootstrapOwnerReferenceAssertions,
61-
framework.KubeadmControlPlaneOwnerReferenceAssertions,
62-
framework.KubernetesReferenceAssertions,
63-
)
64-
},
6552
}
6653
})
6754
})
@@ -88,17 +75,6 @@ var _ = Describe("When testing clusterctl upgrades (v1.4=>current)", func() {
8875
WorkloadKubernetesVersion: "v1.27.3",
8976
MgmtFlavor: "topology",
9077
WorkloadFlavor: "",
91-
// This check ensures that ownerReference apiVersions are updated for all types after the upgrade.
92-
PostUpgrade: func(proxy framework.ClusterProxy, namespace, clusterName string) {
93-
framework.ValidateOwnerReferencesOnUpdate(proxy, namespace,
94-
framework.CoreOwnerReferenceAssertion,
95-
framework.ExpOwnerReferenceAssertions,
96-
framework.DockerInfraOwnerReferenceAssertions,
97-
framework.KubeadmBootstrapOwnerReferenceAssertions,
98-
framework.KubeadmControlPlaneOwnerReferenceAssertions,
99-
framework.KubernetesReferenceAssertions,
100-
)
101-
},
10278
}
10379
})
10480
})
@@ -125,17 +101,6 @@ var _ = Describe("When testing clusterctl upgrades using ClusterClass (v1.4=>cur
125101
WorkloadKubernetesVersion: "v1.27.3",
126102
MgmtFlavor: "topology",
127103
WorkloadFlavor: "topology",
128-
// This check ensures that ownerReference apiVersions are updated for all types after the upgrade.
129-
PostUpgrade: func(proxy framework.ClusterProxy, namespace, clusterName string) {
130-
framework.ValidateOwnerReferencesOnUpdate(proxy, namespace,
131-
framework.CoreOwnerReferenceAssertion,
132-
framework.ExpOwnerReferenceAssertions,
133-
framework.DockerInfraOwnerReferenceAssertions,
134-
framework.KubeadmBootstrapOwnerReferenceAssertions,
135-
framework.KubeadmControlPlaneOwnerReferenceAssertions,
136-
framework.KubernetesReferenceAssertions,
137-
)
138-
},
139104
}
140105
})
141106
})
@@ -156,17 +121,6 @@ var _ = Describe("When testing clusterctl upgrades (v1.5=>current)", func() {
156121
WorkloadKubernetesVersion: "v1.28.0",
157122
MgmtFlavor: "topology",
158123
WorkloadFlavor: "",
159-
// This check ensures that ownerReference apiVersions are updated for all types after the upgrade.
160-
PostUpgrade: func(proxy framework.ClusterProxy, namespace, clusterName string) {
161-
framework.ValidateOwnerReferencesOnUpdate(proxy, namespace,
162-
framework.CoreOwnerReferenceAssertion,
163-
framework.ExpOwnerReferenceAssertions,
164-
framework.DockerInfraOwnerReferenceAssertions,
165-
framework.KubeadmBootstrapOwnerReferenceAssertions,
166-
framework.KubeadmControlPlaneOwnerReferenceAssertions,
167-
framework.KubernetesReferenceAssertions,
168-
)
169-
},
170124
}
171125
})
172126
})
@@ -187,17 +141,6 @@ var _ = Describe("When testing clusterctl upgrades using ClusterClass (v1.5=>cur
187141
WorkloadKubernetesVersion: "v1.28.0",
188142
MgmtFlavor: "topology",
189143
WorkloadFlavor: "topology",
190-
// This check ensures that ownerReference apiVersions are updated for all types after the upgrade.
191-
PostUpgrade: func(proxy framework.ClusterProxy, namespace, clusterName string) {
192-
framework.ValidateOwnerReferencesOnUpdate(proxy, namespace,
193-
framework.CoreOwnerReferenceAssertion,
194-
framework.ExpOwnerReferenceAssertions,
195-
framework.DockerInfraOwnerReferenceAssertions,
196-
framework.KubeadmBootstrapOwnerReferenceAssertions,
197-
framework.KubeadmControlPlaneOwnerReferenceAssertions,
198-
framework.KubernetesReferenceAssertions,
199-
)
200-
},
201144
}
202145
})
203146
})

test/e2e/quick_start_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ var _ = Describe("When following the Cluster API quick-start", func() {
4747
framework.KubeadmControlPlaneOwnerReferenceAssertions,
4848
framework.KubernetesReferenceAssertions,
4949
)
50+
// This check ensures that owner references are correctly updated to the correct apiVersion.
51+
framework.ValidateOwnerReferencesOnUpdate(ctx, proxy, namespace, clusterName,
52+
framework.CoreOwnerReferenceAssertion,
53+
framework.ExpOwnerReferenceAssertions,
54+
framework.DockerInfraOwnerReferenceAssertions,
55+
framework.KubeadmBootstrapOwnerReferenceAssertions,
56+
framework.KubeadmControlPlaneOwnerReferenceAssertions,
57+
framework.KubernetesReferenceAssertions,
58+
)
5059
},
5160
}
5261
})
@@ -72,6 +81,15 @@ var _ = Describe("When following the Cluster API quick-start with ClusterClass [
7281
framework.KubeadmControlPlaneOwnerReferenceAssertions,
7382
framework.KubernetesReferenceAssertions,
7483
)
84+
// This check ensures that owner references are correctly updated to the correct apiVersion.
85+
framework.ValidateOwnerReferencesOnUpdate(ctx, proxy, namespace, clusterName,
86+
framework.CoreOwnerReferenceAssertion,
87+
framework.ExpOwnerReferenceAssertions,
88+
framework.DockerInfraOwnerReferenceAssertions,
89+
framework.KubeadmBootstrapOwnerReferenceAssertions,
90+
framework.KubeadmControlPlaneOwnerReferenceAssertions,
91+
framework.KubernetesReferenceAssertions,
92+
)
7593
},
7694
}
7795
})

test/framework/ownerreference_helpers.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,22 @@ import (
4242
)
4343

4444
// ValidateOwnerReferencesOnUpdate checks that expected owner references are updated to the correct apiVersion.
45-
func ValidateOwnerReferencesOnUpdate(proxy ClusterProxy, namespace string, assertFuncs ...map[string]func(reference []metav1.OwnerReference) error) {
46-
// Check that the ownerReferences are as expected on the first iteration.
45+
func ValidateOwnerReferencesOnUpdate(ctx context.Context, proxy ClusterProxy, namespace, clusterName string, assertFuncs ...map[string]func(reference []metav1.OwnerReference) error) {
46+
clusterKey := client.ObjectKey{Namespace: namespace, Name: clusterName}
47+
48+
// Changes the version of all the owner references to v1alpha1. Expect the apiVersion to be updated after reconciliation.
49+
setClusterPause(ctx, proxy.GetClient(), clusterKey, true)
50+
51+
// Change the version of the OwnerReferences on each object in the Graph to "v1alpha1"
52+
changeOwnerReferencesAPIVersion(ctx, proxy, namespace)
53+
54+
setClusterPause(ctx, proxy.GetClient(), clusterKey, false)
55+
56+
// Annotate the clusterClass, if one is in use, to speed up reconciliation. This ensures ClusterClass ownerReferences
57+
// are re-reconciled before asserting the owner reference graph.
58+
forceClusterClassReconcile(ctx, proxy.GetClient(), clusterKey)
59+
60+
// Check that the ownerReferences have updated their apiVersions to current versions after reconciliation.
4761
AssertOwnerReferences(namespace, proxy.GetKubeconfigPath(), assertFuncs...)
4862
}
4963

@@ -353,6 +367,34 @@ func forceClusterClassReconcile(ctx context.Context, cli client.Client, clusterK
353367
}
354368
}
355369

370+
func changeOwnerReferencesAPIVersion(ctx context.Context, proxy ClusterProxy, namespace string) {
371+
graph, err := clusterctlcluster.GetOwnerGraph(ctx, namespace, proxy.GetKubeconfigPath())
372+
Expect(err).ToNot(HaveOccurred())
373+
for _, object := range graph {
374+
ref := object.Object
375+
obj := new(unstructured.Unstructured)
376+
obj.SetAPIVersion(ref.APIVersion)
377+
obj.SetKind(ref.Kind)
378+
obj.SetName(ref.Name)
379+
380+
Expect(proxy.GetClient().Get(ctx, client.ObjectKey{Namespace: namespace, Name: object.Object.Name}, obj)).To(Succeed())
381+
helper, err := patch.NewHelper(obj, proxy.GetClient())
382+
Expect(err).ToNot(HaveOccurred())
383+
384+
newOwners := []metav1.OwnerReference{}
385+
for _, owner := range obj.GetOwnerReferences() {
386+
gv, err := schema.ParseGroupVersion(owner.APIVersion)
387+
Expect(err).To(Succeed())
388+
gv.Version = "v1alpha1"
389+
owner.APIVersion = gv.String()
390+
newOwners = append(newOwners, owner)
391+
}
392+
393+
obj.SetOwnerReferences(newOwners)
394+
Expect(helper.Patch(ctx, obj)).To(Succeed())
395+
}
396+
}
397+
356398
func removeOwnerReferences(ctx context.Context, proxy ClusterProxy, namespace string) {
357399
graph, err := clusterctlcluster.GetOwnerGraph(ctx, namespace, proxy.GetKubeconfigPath())
358400
Expect(err).ToNot(HaveOccurred())

0 commit comments

Comments
 (0)