@@ -20,7 +20,6 @@ import (
20
20
"context"
21
21
"github.com/pkg/errors"
22
22
corev1 "k8s.io/api/core/v1"
23
- "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
24
23
"sigs.k8s.io/cluster-api/test/framework/internal/log"
25
24
"sigs.k8s.io/cluster-api/util/patch"
26
25
@@ -92,7 +91,7 @@ type DiscoveryAndWaitForMachinePoolsInput struct {
92
91
Cluster * clusterv1.Cluster
93
92
}
94
93
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).
96
95
func DiscoveryAndWaitForMachinePools (ctx context.Context , input DiscoveryAndWaitForMachinePoolsInput , intervals ... interface {}) []* clusterv1exp.MachinePool {
97
96
Expect (ctx ).NotTo (BeNil (), "ctx is required for DiscoveryAndWaitForMachinePools" )
98
97
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
131
130
mgmtClient := input .ClusterProxy .GetClient ()
132
131
for i := range input .MachinePools {
133
132
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 )
135
134
patchHelper , err := patch .NewHelper (mp , mgmtClient )
136
135
Expect (err ).ToNot (HaveOccurred ())
137
136
137
+ oldVersion := mp .Spec .Template .Spec .Version
138
138
mp .Spec .Template .Spec .Version = & input .UpgradeVersion
139
139
Expect (patchHelper .Patch (ctx , mp )).To (Succeed ())
140
- }
141
140
142
- for i := range input .MachinePools {
143
- mp := input .MachinePools [i ]
144
- oldVersion := mp .Spec .Template .Spec .Version
145
141
log .Logf ("Waiting for Kubernetes versions of machines in MachinePool %s/%s to be upgraded from %s to %s" ,
146
142
mp .Namespace , mp .Name , * oldVersion , input .UpgradeVersion )
147
143
WaitForMachinePoolInstancesToBeUpgraded (ctx , WaitForMachinePoolInstancesToBeUpgradedInput {
148
144
Getter : mgmtClient ,
145
+ WorkloadClusterGetter : input .ClusterProxy .GetWorkloadCluster (ctx , input .Cluster .Namespace , input .Cluster .Name ).GetClient (),
149
146
Cluster : input .Cluster ,
150
147
MachineCount : int (* mp .Spec .Replicas ),
151
148
KubernetesUpgradeVersion : input .UpgradeVersion ,
152
- MachinePool : * mp ,
149
+ MachinePool : mp ,
153
150
}, input .WaitForMachinePoolToBeUpgraded ... )
154
151
}
155
152
}
@@ -190,10 +187,11 @@ func ScaleMachinePoolAndWait(ctx context.Context, input ScaleMachinePoolAndWaitI
190
187
// WaitForMachinePoolInstancesToBeUpgradedInput is the input for WaitForMachinePoolInstancesToBeUpgraded.
191
188
type WaitForMachinePoolInstancesToBeUpgradedInput struct {
192
189
Getter Getter
190
+ WorkloadClusterGetter Getter
193
191
Cluster * clusterv1.Cluster
194
192
KubernetesUpgradeVersion string
195
193
MachineCount int
196
- MachinePool clusterv1exp.MachinePool
194
+ MachinePool * clusterv1exp.MachinePool
197
195
}
198
196
199
197
// 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
207
205
208
206
log .Logf ("Ensuring all MachinePool Instances have upgraded kubernetes version %s" , input .KubernetesUpgradeVersion )
209
207
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 ,
214
219
})
215
220
216
221
matches := 0
@@ -230,41 +235,30 @@ func WaitForMachinePoolInstancesToBeUpgraded(ctx context.Context, input WaitForM
230
235
231
236
// GetMachinesPoolInstancesInput is the input for GetMachinesPoolInstances.
232
237
type GetMachinesPoolInstancesInput struct {
233
- Getter Getter
234
- Namespace string
235
- MachinePool clusterv1exp.MachinePool
238
+ WorkloadClusterGetter Getter
239
+ Namespace string
240
+ MachinePool * clusterv1exp.MachinePool
236
241
}
237
242
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.WorkloadClusterGetter 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.MachinePool can't be nil when calling getMachinePoolInstanceVersions" )
250
249
250
+ instances := input .MachinePool .Status .NodeRefs
251
251
versions := make ([]string , len (instances ))
252
252
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 ])
257
261
}
258
262
259
263
return versions
260
264
}
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