@@ -17,19 +17,13 @@ limitations under the License.
17
17
package machinedeployment
18
18
19
19
import (
20
- "strconv"
21
20
"testing"
22
21
23
22
. "github.com/onsi/gomega"
24
- "github.com/pkg/errors"
25
23
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
- "k8s.io/client-go/tools/record"
27
24
"k8s.io/utils/ptr"
28
- "sigs.k8s.io/controller-runtime/pkg/client"
29
- "sigs.k8s.io/controller-runtime/pkg/client/fake"
30
25
31
26
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
32
- "sigs.k8s.io/cluster-api/internal/controllers/machinedeployment/mdutil"
33
27
)
34
28
35
29
func TestReconcileNewMachineSet (t * testing.T ) {
@@ -41,36 +35,6 @@ func TestReconcileNewMachineSet(t *testing.T) {
41
35
expectedNewMachineSetReplicas int
42
36
error error
43
37
}{
44
- {
45
- name : "It fails when machineDeployment has no replicas" ,
46
- machineDeployment : & clusterv1.MachineDeployment {
47
- ObjectMeta : metav1.ObjectMeta {
48
- Namespace : "foo" ,
49
- Name : "bar" ,
50
- },
51
- },
52
- newMachineSet : & clusterv1.MachineSet {
53
- Spec : clusterv1.MachineSetSpec {
54
- Replicas : ptr.To [int32 ](2 ),
55
- },
56
- },
57
- error : errors .Errorf ("spec.replicas for MachineDeployment foo/bar is nil, this is unexpected" ),
58
- },
59
- {
60
- name : "It fails when new machineSet has no replicas" ,
61
- machineDeployment : & clusterv1.MachineDeployment {
62
- Spec : clusterv1.MachineDeploymentSpec {
63
- Replicas : ptr.To [int32 ](2 ),
64
- },
65
- },
66
- newMachineSet : & clusterv1.MachineSet {
67
- ObjectMeta : metav1.ObjectMeta {
68
- Namespace : "foo" ,
69
- Name : "bar" ,
70
- },
71
- },
72
- error : errors .Errorf ("spec.replicas for MachineSet foo/bar is nil, this is unexpected" ),
73
- },
74
38
{
75
39
name : "RollingUpdate strategy: Scale up: 0 -> 2" ,
76
40
machineDeployment : & clusterv1.MachineDeployment {
@@ -268,21 +232,12 @@ func TestReconcileNewMachineSet(t *testing.T) {
268
232
t .Run (tc .name , func (t * testing.T ) {
269
233
g := NewWithT (t )
270
234
271
- resources := []client.Object {
272
- tc .machineDeployment ,
273
- }
274
-
275
- allMachineSets := append (tc .oldMachineSets , tc .newMachineSet )
276
- for key := range allMachineSets {
277
- resources = append (resources , allMachineSets [key ])
278
- }
279
-
280
- r := & Reconciler {
281
- Client : fake .NewClientBuilder ().WithObjects (resources ... ).Build (),
282
- recorder : record .NewFakeRecorder (32 ),
283
- }
235
+ planner := newRolloutPlanner ()
236
+ planner .md = tc .machineDeployment
237
+ planner .newMS = tc .newMachineSet
238
+ planner .oldMSs = tc .oldMachineSets
284
239
285
- err := r .reconcileNewMachineSet (ctx , allMachineSets , tc . newMachineSet , tc . machineDeployment )
240
+ err := planner .reconcileNewMachineSet (ctx )
286
241
if tc .error != nil {
287
242
g .Expect (err ).To (HaveOccurred ())
288
243
g .Expect (err .Error ()).To (BeEquivalentTo (tc .error .Error ()))
@@ -291,22 +246,23 @@ func TestReconcileNewMachineSet(t *testing.T) {
291
246
292
247
g .Expect (err ).ToNot (HaveOccurred ())
293
248
294
- freshNewMachineSet := & clusterv1.MachineSet {}
295
- err = r .Client .Get (ctx , client .ObjectKeyFromObject (tc .newMachineSet ), freshNewMachineSet )
296
- g .Expect (err ).ToNot (HaveOccurred ())
297
-
298
- g .Expect (* freshNewMachineSet .Spec .Replicas ).To (BeEquivalentTo (tc .expectedNewMachineSetReplicas ))
299
-
300
- _ , ok := freshNewMachineSet .GetAnnotations ()[clusterv1 .DisableMachineCreateAnnotation ]
301
- g .Expect (ok ).To (BeFalse ())
302
-
303
- desiredReplicasAnnotation , ok := freshNewMachineSet .GetAnnotations ()[clusterv1 .DesiredReplicasAnnotation ]
304
- g .Expect (ok ).To (BeTrue ())
305
- g .Expect (strconv .Atoi (desiredReplicasAnnotation )).To (BeEquivalentTo (* tc .machineDeployment .Spec .Replicas ))
249
+ scaleIntent := ptr .Deref (tc .newMachineSet .Spec .Replicas , 0 )
250
+ if v , ok := planner .scaleIntents [tc .newMachineSet .Name ]; ok {
251
+ scaleIntent = v
252
+ }
253
+ g .Expect (scaleIntent ).To (BeEquivalentTo (tc .expectedNewMachineSetReplicas ))
306
254
307
- maxReplicasAnnotation , ok := freshNewMachineSet .GetAnnotations ()[clusterv1 .MaxReplicasAnnotation ]
308
- g .Expect (ok ).To (BeTrue ())
309
- g .Expect (strconv .Atoi (maxReplicasAnnotation )).To (BeEquivalentTo (* tc .machineDeployment .Spec .Replicas + mdutil .MaxSurge (* tc .machineDeployment )))
255
+ // TODO(in-place): Restore tests on DisableMachineCreateAnnotation and MaxReplicasAnnotation as soon as handling those annotation is moved into the rollout planner
256
+ // _, ok := freshNewMachineSet.GetAnnotations()[clusterv1.DisableMachineCreateAnnotation]
257
+ // g.Expect(ok).To(BeFalse())
258
+ //
259
+ // desiredReplicasAnnotation, ok := freshNewMachineSet.GetAnnotations()[clusterv1.DesiredReplicasAnnotation]
260
+ // g.Expect(ok).To(BeTrue())
261
+ // g.Expect(strconv.Atoi(desiredReplicasAnnotation)).To(BeEquivalentTo(*tc.machineDeployment.Spec.Replicas))
262
+ //
263
+ // maxReplicasAnnotation, ok := freshNewMachineSet.GetAnnotations()[clusterv1.MaxReplicasAnnotation]
264
+ // g.Expect(ok).To(BeTrue())
265
+ // g.Expect(strconv.Atoi(maxReplicasAnnotation)).To(BeEquivalentTo(*tc.machineDeployment.Spec.Replicas + mdutil.MaxSurge(*tc.machineDeployment)))
310
266
})
311
267
}
312
268
}
@@ -320,36 +276,6 @@ func TestReconcileOldMachineSets(t *testing.T) {
320
276
expectedOldMachineSetsReplicas int
321
277
error error
322
278
}{
323
- {
324
- name : "It fails when machineDeployment has no replicas" ,
325
- machineDeployment : & clusterv1.MachineDeployment {
326
- ObjectMeta : metav1.ObjectMeta {
327
- Namespace : "foo" ,
328
- Name : "bar" ,
329
- },
330
- },
331
- newMachineSet : & clusterv1.MachineSet {
332
- Spec : clusterv1.MachineSetSpec {
333
- Replicas : ptr.To [int32 ](2 ),
334
- },
335
- },
336
- error : errors .Errorf ("spec.replicas for MachineDeployment foo/bar is nil, this is unexpected" ),
337
- },
338
- {
339
- name : "It fails when new machineSet has no replicas" ,
340
- machineDeployment : & clusterv1.MachineDeployment {
341
- Spec : clusterv1.MachineDeploymentSpec {
342
- Replicas : ptr.To [int32 ](2 ),
343
- },
344
- },
345
- newMachineSet : & clusterv1.MachineSet {
346
- ObjectMeta : metav1.ObjectMeta {
347
- Namespace : "foo" ,
348
- Name : "bar" ,
349
- },
350
- },
351
- error : errors .Errorf ("spec.replicas for MachineSet foo/bar is nil, this is unexpected" ),
352
- },
353
279
{
354
280
name : "RollingUpdate strategy: Scale down old MachineSets when all new replicas are available" ,
355
281
machineDeployment : & clusterv1.MachineDeployment {
@@ -465,33 +391,25 @@ func TestReconcileOldMachineSets(t *testing.T) {
465
391
t .Run (tc .name , func (t * testing.T ) {
466
392
g := NewWithT (t )
467
393
468
- resources := []client.Object {
469
- tc .machineDeployment ,
470
- }
471
-
472
- allMachineSets := append (tc .oldMachineSets , tc .newMachineSet )
473
- for key := range allMachineSets {
474
- resources = append (resources , allMachineSets [key ])
475
- }
476
-
477
- r := & Reconciler {
478
- Client : fake .NewClientBuilder ().WithObjects (resources ... ).Build (),
479
- recorder : record .NewFakeRecorder (32 ),
480
- }
394
+ planner := newRolloutPlanner ()
395
+ planner .md = tc .machineDeployment
396
+ planner .newMS = tc .newMachineSet
397
+ planner .oldMSs = tc .oldMachineSets
481
398
482
- err := r .reconcileOldMachineSets (ctx , allMachineSets , tc . oldMachineSets , tc . newMachineSet , tc . machineDeployment )
399
+ err := planner .reconcileOldMachineSets (ctx )
483
400
if tc .error != nil {
484
401
g .Expect (err ).To (HaveOccurred ())
485
402
g .Expect (err .Error ()).To (BeEquivalentTo (tc .error .Error ()))
486
403
return
487
404
}
488
405
489
406
g .Expect (err ).ToNot (HaveOccurred ())
490
- for key := range tc .oldMachineSets {
491
- freshOldMachineSet := & clusterv1.MachineSet {}
492
- err = r .Client .Get (ctx , client .ObjectKeyFromObject (tc .oldMachineSets [key ]), freshOldMachineSet )
493
- g .Expect (err ).ToNot (HaveOccurred ())
494
- g .Expect (* freshOldMachineSet .Spec .Replicas ).To (BeEquivalentTo (tc .expectedOldMachineSetsReplicas ))
407
+ for i := range tc .oldMachineSets {
408
+ scaleIntent := ptr .Deref (tc .oldMachineSets [i ].Spec .Replicas , 0 )
409
+ if v , ok := planner .scaleIntents [tc .oldMachineSets [i ].Name ]; ok {
410
+ scaleIntent = v
411
+ }
412
+ g .Expect (scaleIntent ).To (BeEquivalentTo (tc .expectedOldMachineSetsReplicas ))
495
413
}
496
414
})
497
415
}
0 commit comments