@@ -84,12 +84,71 @@ func TestMachineSetReconciler(t *testing.T) {
84
84
duration5m := & metav1.Duration {Duration : 5 * time .Minute }
85
85
replicas := int32 (2 )
86
86
version := "v1.14.2"
87
+ machineTemplateSpec := clusterv1.MachineTemplateSpec {
88
+ ObjectMeta : clusterv1.ObjectMeta {
89
+ Labels : map [string ]string {
90
+ "label-1" : "true" ,
91
+ },
92
+ Annotations : map [string ]string {
93
+ "annotation-1" : "true" ,
94
+ "precedence" : "MachineSet" ,
95
+ },
96
+ },
97
+ Spec : clusterv1.MachineSpec {
98
+ ClusterName : testCluster .Name ,
99
+ Version : & version ,
100
+ Bootstrap : clusterv1.Bootstrap {
101
+ ConfigRef : & corev1.ObjectReference {
102
+ APIVersion : "bootstrap.cluster.x-k8s.io/v1beta1" ,
103
+ Kind : "GenericBootstrapConfigTemplate" ,
104
+ Name : "ms-template" ,
105
+ },
106
+ },
107
+ InfrastructureRef : corev1.ObjectReference {
108
+ APIVersion : "infrastructure.cluster.x-k8s.io/v1beta1" ,
109
+ Kind : "GenericInfrastructureMachineTemplate" ,
110
+ Name : "ms-template" ,
111
+ },
112
+ NodeDrainTimeout : duration10m ,
113
+ NodeDeletionTimeout : duration10m ,
114
+ NodeVolumeDetachTimeout : duration10m ,
115
+ },
116
+ }
117
+
118
+ machineDeployment := & clusterv1.MachineDeployment {
119
+ ObjectMeta : metav1.ObjectMeta {
120
+ GenerateName : "md-" ,
121
+ Namespace : namespace .Name ,
122
+ Annotations : map [string ]string {
123
+ clusterv1 .RevisionAnnotation : "10" ,
124
+ },
125
+ },
126
+ Spec : clusterv1.MachineDeploymentSpec {
127
+ ClusterName : testCluster .Name ,
128
+ Replicas : & replicas ,
129
+ Template : machineTemplateSpec ,
130
+ },
131
+ }
132
+ g .Expect (env .Create (ctx , machineDeployment )).To (Succeed ())
133
+
87
134
instance := & clusterv1.MachineSet {
88
135
ObjectMeta : metav1.ObjectMeta {
89
136
GenerateName : "ms-" ,
90
137
Namespace : namespace .Name ,
91
138
Labels : map [string ]string {
92
- "label-1" : "true" ,
139
+ "label-1" : "true" ,
140
+ clusterv1 .MachineDeploymentNameLabel : machineDeployment .Name ,
141
+ },
142
+ Annotations : map [string ]string {
143
+ clusterv1 .RevisionAnnotation : "10" ,
144
+ },
145
+ OwnerReferences : []metav1.OwnerReference {
146
+ {
147
+ APIVersion : clusterv1 .GroupVersion .String (),
148
+ Kind : "MachineDeployment" ,
149
+ Name : machineDeployment .Name ,
150
+ UID : machineDeployment .UID ,
151
+ },
93
152
},
94
153
},
95
154
Spec : clusterv1.MachineSetSpec {
@@ -100,36 +159,7 @@ func TestMachineSetReconciler(t *testing.T) {
100
159
"label-1" : "true" ,
101
160
},
102
161
},
103
- Template : clusterv1.MachineTemplateSpec {
104
- ObjectMeta : clusterv1.ObjectMeta {
105
- Labels : map [string ]string {
106
- "label-1" : "true" ,
107
- },
108
- Annotations : map [string ]string {
109
- "annotation-1" : "true" ,
110
- "precedence" : "MachineSet" ,
111
- },
112
- },
113
- Spec : clusterv1.MachineSpec {
114
- ClusterName : testCluster .Name ,
115
- Version : & version ,
116
- Bootstrap : clusterv1.Bootstrap {
117
- ConfigRef : & corev1.ObjectReference {
118
- APIVersion : "bootstrap.cluster.x-k8s.io/v1beta1" ,
119
- Kind : "GenericBootstrapConfigTemplate" ,
120
- Name : "ms-template" ,
121
- },
122
- },
123
- InfrastructureRef : corev1.ObjectReference {
124
- APIVersion : "infrastructure.cluster.x-k8s.io/v1beta1" ,
125
- Kind : "GenericInfrastructureMachineTemplate" ,
126
- Name : "ms-template" ,
127
- },
128
- NodeDrainTimeout : duration10m ,
129
- NodeDeletionTimeout : duration10m ,
130
- NodeVolumeDetachTimeout : duration10m ,
131
- },
132
- },
162
+ Template : machineTemplateSpec ,
133
163
},
134
164
}
135
165
@@ -405,6 +435,33 @@ func TestMachineSetReconciler(t *testing.T) {
405
435
406
436
// Validate that the controller set the cluster name label in selector.
407
437
g .Expect (instance .Status .Selector ).To (ContainSubstring (testCluster .Name ))
438
+
439
+ t .Log ("Verifying MachineSet can be scaled down when templates don't exist, and MachineSet is not current" )
440
+ g .Expect (env .CleanupAndWait (ctx , bootstrapTmpl )).To (Succeed ())
441
+ g .Expect (env .CleanupAndWait (ctx , infraTmpl )).To (Succeed ())
442
+
443
+ t .Log ("Updating Replicas on MachineSet" )
444
+ patchHelper , err = patch .NewHelper (instance , env )
445
+ g .Expect (err ).ToNot (HaveOccurred ())
446
+ instance .SetAnnotations (map [string ]string {
447
+ clusterv1 .RevisionAnnotation : "9" ,
448
+ })
449
+ instance .Spec .Replicas = ptr .To (int32 (1 ))
450
+ g .Expect (patchHelper .Patch (ctx , instance )).Should (Succeed ())
451
+
452
+ // Verify that we have 1 replicas.
453
+ g .Eventually (func () (ready int ) {
454
+ if err := env .List (ctx , machines , client .InNamespace (namespace .Name )); err != nil {
455
+ return - 1
456
+ }
457
+ for _ , m := range machines .Items {
458
+ if ! m .DeletionTimestamp .IsZero () {
459
+ continue
460
+ }
461
+ ready ++
462
+ }
463
+ return
464
+ }, timeout * 3 ).Should (BeEquivalentTo (1 ))
408
465
})
409
466
}
410
467
0 commit comments