Skip to content

Commit 02c8d18

Browse files
author
Yuvaraj Kakaraparthi
committed
self hosted tests should check for rollouts
1 parent a0f3da1 commit 02c8d18

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/klog/v2"
3639
"k8s.io/utils/pointer"
@@ -297,7 +300,8 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
297300

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

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

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

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

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/klog/v2"
3031
"k8s.io/utils/pointer"
3132
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -82,6 +83,7 @@ func SelfHostedSpec(ctx context.Context, inputGetter func() SelfHostedSpecInput)
8283
It("Should pivot the bootstrap cluster to a self-hosted cluster", func() {
8384
By("Creating a workload cluster")
8485

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

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

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

0 commit comments

Comments
 (0)