Skip to content

Commit ce34730

Browse files
author
Cecile Robert-Michon
committed
Fix MachinePool upgrade e2e test
1 parent 19c30e6 commit ce34730

File tree

1 file changed

+36
-42
lines changed

1 file changed

+36
-42
lines changed

test/framework/machinepool_helpers.go

+36-42
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"github.com/pkg/errors"
2222
corev1 "k8s.io/api/core/v1"
23-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2423
"sigs.k8s.io/cluster-api/test/framework/internal/log"
2524
"sigs.k8s.io/cluster-api/util/patch"
2625

@@ -92,7 +91,7 @@ type DiscoveryAndWaitForMachinePoolsInput struct {
9291
Cluster *clusterv1.Cluster
9392
}
9493

95-
// DiscoveryAndWaitForMachinePools discovers the MachinePools existing in a cluster and waits for them to be ready (all the machine provisioned).
94+
// DiscoveryAndWaitForMachinePools discovers the MachinePools existing in a cluster and waits for them to be ready (all the machines provisioned).
9695
func DiscoveryAndWaitForMachinePools(ctx context.Context, input DiscoveryAndWaitForMachinePoolsInput, intervals ...interface{}) []*clusterv1exp.MachinePool {
9796
Expect(ctx).NotTo(BeNil(), "ctx is required for DiscoveryAndWaitForMachinePools")
9897
Expect(input.Lister).ToNot(BeNil(), "Invalid argument. input.Lister can't be nil when calling DiscoveryAndWaitForMachinePools")
@@ -131,25 +130,23 @@ func UpgradeMachinePoolAndWait(ctx context.Context, input UpgradeMachinePoolAndW
131130
mgmtClient := input.ClusterProxy.GetClient()
132131
for i := range input.MachinePools {
133132
mp := input.MachinePools[i]
134-
log.Logf("Patching the new kubernetes version to Machine Pool %s/%s", mp.Namespace, mp.Name)
133+
log.Logf("Patching the new Kubernetes version to Machine Pool %s/%s", mp.Namespace, mp.Name)
135134
patchHelper, err := patch.NewHelper(mp, mgmtClient)
136135
Expect(err).ToNot(HaveOccurred())
137136

137+
oldVersion := mp.Spec.Template.Spec.Version
138138
mp.Spec.Template.Spec.Version = &input.UpgradeVersion
139139
Expect(patchHelper.Patch(ctx, mp)).To(Succeed())
140-
}
141140

142-
for i := range input.MachinePools {
143-
mp := input.MachinePools[i]
144-
oldVersion := mp.Spec.Template.Spec.Version
145141
log.Logf("Waiting for Kubernetes versions of machines in MachinePool %s/%s to be upgraded from %s to %s",
146142
mp.Namespace, mp.Name, *oldVersion, input.UpgradeVersion)
147143
WaitForMachinePoolInstancesToBeUpgraded(ctx, WaitForMachinePoolInstancesToBeUpgradedInput{
148144
Getter: mgmtClient,
145+
WorkloadClusterGetter: input.ClusterProxy.GetWorkloadCluster(ctx, input.Cluster.Namespace, input.Cluster.Name).GetClient(),
149146
Cluster: input.Cluster,
150147
MachineCount: int(*mp.Spec.Replicas),
151148
KubernetesUpgradeVersion: input.UpgradeVersion,
152-
MachinePool: *mp,
149+
MachinePool: mp,
153150
}, input.WaitForMachinePoolToBeUpgraded...)
154151
}
155152
}
@@ -190,10 +187,11 @@ func ScaleMachinePoolAndWait(ctx context.Context, input ScaleMachinePoolAndWaitI
190187
// WaitForMachinePoolInstancesToBeUpgradedInput is the input for WaitForMachinePoolInstancesToBeUpgraded.
191188
type WaitForMachinePoolInstancesToBeUpgradedInput struct {
192189
Getter Getter
190+
WorkloadClusterGetter Getter
193191
Cluster *clusterv1.Cluster
194192
KubernetesUpgradeVersion string
195193
MachineCount int
196-
MachinePool clusterv1exp.MachinePool
194+
MachinePool *clusterv1exp.MachinePool
197195
}
198196

199197
// WaitForMachinePoolInstancesToBeUpgraded waits until all instances belonging to a MachinePool are upgraded to the correct kubernetes version.
@@ -207,10 +205,17 @@ func WaitForMachinePoolInstancesToBeUpgraded(ctx context.Context, input WaitForM
207205

208206
log.Logf("Ensuring all MachinePool Instances have upgraded kubernetes version %s", input.KubernetesUpgradeVersion)
209207
Eventually(func() (int, error) {
210-
versions := GetMachinePoolInstanceVersions(ctx, GetMachinesPoolInstancesInput{
211-
Getter: input.Getter,
212-
Namespace: input.Cluster.Namespace,
213-
MachinePool: input.MachinePool,
208+
nn := client.ObjectKey{
209+
Namespace: input.MachinePool.Namespace,
210+
Name: input.MachinePool.Name,
211+
}
212+
if err := input.Getter.Get(ctx, nn, input.MachinePool); err != nil {
213+
return 0, err
214+
}
215+
versions := getMachinePoolInstanceVersions(ctx, GetMachinesPoolInstancesInput{
216+
WorkloadClusterGetter: input.WorkloadClusterGetter,
217+
Namespace: input.Cluster.Namespace,
218+
MachinePool: input.MachinePool,
214219
})
215220

216221
matches := 0
@@ -230,41 +235,30 @@ func WaitForMachinePoolInstancesToBeUpgraded(ctx context.Context, input WaitForM
230235

231236
// GetMachinesPoolInstancesInput is the input for GetMachinesPoolInstances.
232237
type GetMachinesPoolInstancesInput struct {
233-
Getter Getter
234-
Namespace string
235-
MachinePool clusterv1exp.MachinePool
238+
WorkloadClusterGetter Getter
239+
Namespace string
240+
MachinePool *clusterv1exp.MachinePool
236241
}
237242

238-
// GetMachinePoolInstanceVersions returns the.
239-
func GetMachinePoolInstanceVersions(ctx context.Context, input GetMachinesPoolInstancesInput) []string {
240-
Expect(ctx).NotTo(BeNil(), "ctx is required for GetMachinePoolInstanceVersions")
241-
Expect(input.Namespace).ToNot(BeEmpty(), "Invalid argument. input.Namespace can't be empty when calling GetMachinePoolInstanceVersions")
242-
Expect(input.MachinePool).ToNot(BeNil(), "Invalid argument. input.MachineDeployment can't be nil when calling GetMachinePoolInstanceVersions")
243-
244-
obj := getUnstructuredRef(ctx, input.Getter, &input.MachinePool.Spec.Template.Spec.InfrastructureRef, input.Namespace)
245-
instances, found, err := unstructured.NestedSlice(obj.Object, "status", "instances")
246-
Expect(err).ToNot(HaveOccurred(), "failed to extract machines from unstructured")
247-
if !found {
248-
return nil
249-
}
243+
// getMachinePoolInstanceVersions returns the Kubernetes versions of the machine pool instances.
244+
func getMachinePoolInstanceVersions(ctx context.Context, input GetMachinesPoolInstancesInput) []string {
245+
Expect(ctx).NotTo(BeNil(), "ctx is required for getMachinePoolInstanceVersions")
246+
Expect(input.WorkloadClusterGetter).ToNot(BeNil(), "Invalid argument. input.Getter can't be nil when calling getMachinePoolInstanceVersions")
247+
Expect(input.Namespace).ToNot(BeEmpty(), "Invalid argument. input.Namespace can't be empty when calling getMachinePoolInstanceVersions")
248+
Expect(input.MachinePool).ToNot(BeNil(), "Invalid argument. input.MachineDeployment can't be nil when calling getMachinePoolInstanceVersions")
250249

250+
instances := input.MachinePool.Status.NodeRefs
251251
versions := make([]string, len(instances))
252252
for i, instance := range instances {
253-
version, found, err := unstructured.NestedString(instance.(map[string]interface{}), "version")
254-
Expect(err).ToNot(HaveOccurred(), "failed to extract versions from unstructured instance")
255-
Expect(found).To(BeTrue(), "unable to find nested version string in unstructured instance")
256-
versions[i] = version
253+
node := &corev1.Node{}
254+
err := input.WorkloadClusterGetter.Get(ctx, client.ObjectKey{Name: instance.Name}, node)
255+
if err != nil {
256+
versions[i] = "unknown"
257+
} else {
258+
versions[i] = node.Status.NodeInfo.KubeletVersion
259+
}
260+
log.Logf("Node %s version is %s", instance.Name, versions[i])
257261
}
258262

259263
return versions
260264
}
261-
262-
func getUnstructuredRef(ctx context.Context, getter Getter, ref *corev1.ObjectReference, namespace string) *unstructured.Unstructured {
263-
obj := new(unstructured.Unstructured)
264-
obj.SetAPIVersion(ref.APIVersion)
265-
obj.SetKind(ref.Kind)
266-
obj.SetName(ref.Name)
267-
key := client.ObjectKey{Name: obj.GetName(), Namespace: namespace}
268-
Expect(getter.Get(ctx, key, obj)).ToNot(HaveOccurred(), "failed to retrieve %s object %q/%q", obj.GetKind(), key.Namespace, key.Name)
269-
return obj
270-
}

0 commit comments

Comments
 (0)