Skip to content

Commit f519172

Browse files
authored
Merge pull request #7037 from ykakarap/manual_cherry-pick_6926
[release-1.2] ✨ self hosted tests should check for rollouts
2 parents b98907f + ee289c9 commit f519172

File tree

2 files changed

+53
-20
lines changed

2 files changed

+53
-20
lines changed

test/e2e/clusterctl_upgrade.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ import (
3131
corev1 "k8s.io/api/core/v1"
3232
apierrors "k8s.io/apimachinery/pkg/api/errors"
3333
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
35+
"k8s.io/apimachinery/pkg/types"
36+
"k8s.io/apimachinery/pkg/util/sets"
3437
"k8s.io/client-go/discovery"
3538
"k8s.io/utils/pointer"
3639
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -296,7 +299,8 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
296299

297300
// Get the workloadCluster before the management cluster is upgraded to make sure that the upgrade did not trigger
298301
// any unexpected rollouts.
299-
preUpgradeMachineList := &clusterv1alpha3.MachineList{}
302+
preUpgradeMachineList := &unstructured.UnstructuredList{}
303+
preUpgradeMachineList.SetGroupVersionKind(clusterv1alpha3.GroupVersion.WithKind("MachineList"))
300304
err = managementClusterProxy.GetClient().List(
301305
ctx,
302306
preUpgradeMachineList,
@@ -322,15 +326,16 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
322326

323327
// After the upgrade check that there were no unexpected rollouts.
324328
Consistently(func() bool {
325-
postUpgradeMachineList := &clusterv1.MachineList{}
329+
postUpgradeMachineList := &unstructured.UnstructuredList{}
330+
postUpgradeMachineList.SetGroupVersionKind(clusterv1.GroupVersion.WithKind("MachineList"))
326331
err = managementClusterProxy.GetClient().List(
327332
ctx,
328333
postUpgradeMachineList,
329334
client.InNamespace(testNamespace.Name),
330335
client.MatchingLabels{clusterv1.ClusterLabelName: workLoadClusterName},
331336
)
332337
Expect(err).NotTo(HaveOccurred())
333-
return machinesMatch(preUpgradeMachineList, postUpgradeMachineList)
338+
return matchUnstructuredLists(preUpgradeMachineList, postUpgradeMachineList)
334339
}, "3m", "30s").Should(BeTrue(), "Machines should remain the same after the upgrade")
335340

336341
// After upgrading we are sure the version is the latest version of the API,
@@ -577,23 +582,23 @@ func waitForClusterDeletedV1alpha4(ctx context.Context, input waitForClusterDele
577582
}, intervals...).Should(BeTrue())
578583
}
579584

580-
func machinesMatch(oldMachineList *clusterv1alpha3.MachineList, newMachineList *clusterv1.MachineList) bool {
581-
if len(oldMachineList.Items) != len(newMachineList.Items) {
585+
func matchUnstructuredLists(l1 *unstructured.UnstructuredList, l2 *unstructured.UnstructuredList) bool {
586+
if l1 == nil && l2 == nil {
587+
return true
588+
}
589+
if l1 == nil || l2 == nil {
582590
return false
583591
}
584-
585-
// Every machine from the old list should be present in the new list
586-
for _, oldMachine := range oldMachineList.Items {
587-
found := false
588-
for _, newMachine := range newMachineList.Items {
589-
if oldMachine.Name == newMachine.Name && oldMachine.Namespace == newMachine.Namespace {
590-
found = true
591-
break
592-
}
593-
}
594-
if !found {
595-
return false
596-
}
592+
if len(l1.Items) != len(l2.Items) {
593+
return false
594+
}
595+
s1 := sets.NewString()
596+
for _, i := range l1.Items {
597+
s1.Insert(types.NamespacedName{Namespace: i.GetNamespace(), Name: i.GetName()}.String())
598+
}
599+
s2 := sets.NewString()
600+
for _, i := range l2.Items {
601+
s2.Insert(types.NamespacedName{Namespace: i.GetNamespace(), Name: i.GetName()}.String())
597602
}
598-
return true
603+
return s1.Equal(s2)
599604
}

test/e2e/self_hosted.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
. "github.com/onsi/ginkgo"
2727
. "github.com/onsi/gomega"
2828
corev1 "k8s.io/api/core/v1"
29+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2930
"k8s.io/utils/pointer"
3031
"sigs.k8s.io/controller-runtime/pkg/client"
3132

@@ -81,6 +82,7 @@ func SelfHostedSpec(ctx context.Context, inputGetter func() SelfHostedSpecInput)
8182
It("Should pivot the bootstrap cluster to a self-hosted cluster", func() {
8283
By("Creating a workload cluster")
8384

85+
workloadClusterName := fmt.Sprintf("%s-%s", specName, util.RandomString(6))
8486
clusterctl.ApplyClusterTemplateAndWait(ctx, clusterctl.ApplyClusterTemplateAndWaitInput{
8587
ClusterProxy: input.BootstrapClusterProxy,
8688
ConfigCluster: clusterctl.ConfigClusterInput{
@@ -90,7 +92,7 @@ func SelfHostedSpec(ctx context.Context, inputGetter func() SelfHostedSpecInput)
9092
InfrastructureProvider: clusterctl.DefaultInfrastructureProvider,
9193
Flavor: input.Flavor,
9294
Namespace: namespace.Name,
93-
ClusterName: fmt.Sprintf("%s-%s", specName, util.RandomString(6)),
95+
ClusterName: workloadClusterName,
9496
KubernetesVersion: input.E2EConfig.GetVariable(KubernetesVersion),
9597
ControlPlaneMachineCount: pointer.Int64Ptr(1),
9698
WorkerMachineCount: pointer.Int64Ptr(1),
@@ -163,6 +165,18 @@ func SelfHostedSpec(ctx context.Context, inputGetter func() SelfHostedSpecInput)
163165
return selfHostedClusterProxy.GetClient().Get(ctx, client.ObjectKey{Name: "kube-system"}, kubeSystem)
164166
}, "5s", "100ms").Should(BeNil(), "Failed to assert self-hosted API server stability")
165167

168+
// Get the machines of the workloadCluster before it is moved to become self-hosted to make sure that the move did not trigger
169+
// any unexpected rollouts.
170+
preMoveMachineList := &unstructured.UnstructuredList{}
171+
preMoveMachineList.SetGroupVersionKind(clusterv1.GroupVersion.WithKind("MachineList"))
172+
err := input.BootstrapClusterProxy.GetClient().List(
173+
ctx,
174+
preMoveMachineList,
175+
client.InNamespace(namespace.Name),
176+
client.MatchingLabels{clusterv1.ClusterLabelName: workloadClusterName},
177+
)
178+
Expect(err).NotTo(HaveOccurred(), "Failed to list machines before move")
179+
166180
By("Moving the cluster to self hosted")
167181
clusterctl.Move(ctx, clusterctl.MoveInput{
168182
LogFolder: filepath.Join(input.ArtifactFolder, "clusters", "bootstrap"),
@@ -186,6 +200,20 @@ func SelfHostedSpec(ctx context.Context, inputGetter func() SelfHostedSpecInput)
186200
})
187201
Expect(controlPlane).ToNot(BeNil())
188202

203+
// After the move check that there were no unexpected rollouts.
204+
Consistently(func() bool {
205+
postMoveMachineList := &unstructured.UnstructuredList{}
206+
postMoveMachineList.SetGroupVersionKind(clusterv1.GroupVersion.WithKind("MachineList"))
207+
err = selfHostedClusterProxy.GetClient().List(
208+
ctx,
209+
postMoveMachineList,
210+
client.InNamespace(namespace.Name),
211+
client.MatchingLabels{clusterv1.ClusterLabelName: workloadClusterName},
212+
)
213+
Expect(err).NotTo(HaveOccurred(), "Failed to list machines after move")
214+
return matchUnstructuredLists(preMoveMachineList, postMoveMachineList)
215+
}, "3m", "30s").Should(BeTrue(), "Machines should not roll out after move to self-hosted cluster")
216+
189217
By("PASSED!")
190218
})
191219

0 commit comments

Comments
 (0)