@@ -27,16 +27,18 @@ import (
27
27
"github.com/apache/cloudstack-go/v2/cloudstack"
28
28
"github.com/blang/semver"
29
29
. "github.com/onsi/ginkgo"
30
- "github.com/onsi/gomega/types"
31
30
"gopkg.in/ini.v1"
32
31
corev1 "k8s.io/api/core/v1"
33
32
34
33
. "github.com/onsi/gomega"
34
+ "github.com/onsi/gomega/types"
35
+ k8stypes "k8s.io/apimachinery/pkg/types"
35
36
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
36
37
"sigs.k8s.io/cluster-api/test/framework"
37
38
"sigs.k8s.io/cluster-api/test/framework/clusterctl"
38
39
"sigs.k8s.io/cluster-api/test/framework/exec"
39
40
"sigs.k8s.io/cluster-api/util"
41
+ "sigs.k8s.io/controller-runtime/pkg/client"
40
42
)
41
43
42
44
// Test suite constants for e2e config variables.
@@ -188,7 +190,7 @@ type cloudConfig struct {
188
190
VerifySSL bool `ini:"verify-ssl"`
189
191
}
190
192
191
- func DestroyOneMachineAndWaitForReplacement (clusterName string , machineType string , intervals [] interface {} ) {
193
+ func DestroyOneMachine (clusterName string , machineType string ) {
192
194
encodedSecret := os .Getenv ("CLOUDSTACK_B64ENCODED_SECRET" )
193
195
secret , err := base64 .StdEncoding .DecodeString (encodedSecret )
194
196
if err != nil {
@@ -223,7 +225,6 @@ func DestroyOneMachineAndWaitForReplacement(clusterName string, machineType stri
223
225
}
224
226
}
225
227
}
226
- Byf ("Original number of machines: %d " , originalCount )
227
228
228
229
Byf ("Destroying machine %s" , vmToDestroy .Name )
229
230
stopParams := client .VirtualMachine .NewStopVirtualMachineParams (vmToDestroy .Id )
@@ -238,22 +239,72 @@ func DestroyOneMachineAndWaitForReplacement(clusterName string, machineType stri
238
239
if err != nil {
239
240
Fail ("Failed to destroy machine" )
240
241
}
241
- Byf ("Destroyed machine %s" , vmToDestroy .Name )
242
-
243
- By ("Waiting for a replacement machine" )
244
- Eventually (func () (int , error ) {
245
- By ("Counting machines" )
246
- listResp , err = client .VirtualMachine .ListVirtualMachines (client .VirtualMachine .NewListVirtualMachinesParams ())
247
- if err != nil {
248
- return - 1 , err
242
+ }
243
+
244
+ func WaitForMachineRemediationAfterDestroy (ctx context.Context , proxy framework.ClusterProxy , cluster * clusterv1.Cluster , machineMatcher string , healthyMachineCount int , intervals []interface {}) {
245
+ mgmtClusterClient := proxy .GetClient ()
246
+ workloadClusterClient := proxy .GetWorkloadCluster (ctx , cluster .Namespace , cluster .Name ).GetClient ()
247
+
248
+ WaitForHealthyMachineCount (ctx , mgmtClusterClient , workloadClusterClient , cluster , machineMatcher , healthyMachineCount , intervals )
249
+ Byf ("Current number of healthy %s is %d" , machineMatcher , healthyMachineCount )
250
+
251
+ Byf ("Destroying one %s" , machineMatcher )
252
+ DestroyOneMachine (cluster .Name , machineMatcher )
253
+
254
+ Byf ("Waiting for the destroyed %s to be unhealthy" , machineMatcher )
255
+ WaitForHealthyMachineCount (ctx , mgmtClusterClient , workloadClusterClient , cluster , machineMatcher , healthyMachineCount - 1 , intervals )
256
+
257
+ Byf ("Waiting for remediation of %s" , machineMatcher )
258
+ WaitForHealthyMachineCount (ctx , mgmtClusterClient , workloadClusterClient , cluster , machineMatcher , healthyMachineCount , intervals )
259
+ Byf ("%s machine remediated successfully" , machineMatcher )
260
+ }
261
+
262
+ func WaitForHealthyMachineCount (ctx context.Context , mgmtClient client.Client , workloadClient client.Client , cluster * clusterv1.Cluster , mhcMatcher string , healthyMachineCount int , intervals []interface {}) {
263
+ machineHealthChecks := framework .GetMachineHealthChecksForCluster (ctx , framework.GetMachineHealthChecksForClusterInput {
264
+ Lister : mgmtClient ,
265
+ ClusterName : cluster .Name ,
266
+ Namespace : cluster .Namespace ,
267
+ })
268
+
269
+ for _ , mhc := range machineHealthChecks {
270
+ Expect (mhc .Spec .UnhealthyConditions ).NotTo (BeEmpty ())
271
+ if ! strings .Contains (mhc .Name , mhcMatcher ) {
272
+ continue
249
273
}
250
- count := 0
251
- for _ , vm := range listResp .VirtualMachines {
252
- if strings .Contains (vm .Name , matcher ) {
253
- count ++
274
+
275
+ Eventually (func () (bool , error ) {
276
+ machines := framework .GetMachinesByMachineHealthCheck (ctx , framework.GetMachinesByMachineHealthCheckInput {
277
+ Lister : mgmtClient ,
278
+ ClusterName : cluster .Name ,
279
+ MachineHealthCheck : mhc ,
280
+ })
281
+
282
+ count := 0
283
+ for _ , machine := range machines {
284
+ if machine .Status .NodeRef == nil {
285
+ continue
286
+ }
287
+ node := & corev1.Node {}
288
+ err := workloadClient .Get (ctx , k8stypes.NamespacedName {Name : machine .Status .NodeRef .Name , Namespace : machine .Status .NodeRef .Namespace }, node )
289
+ if err != nil {
290
+ continue
291
+ }
292
+ if ! HasMatchingUnhealthyConditions (mhc , node .Status .Conditions ) {
293
+ count ++
294
+ }
295
+ }
296
+ return count == healthyMachineCount , nil
297
+ }, intervals ... ).Should (BeTrue ())
298
+ }
299
+ }
300
+
301
+ func HasMatchingUnhealthyConditions (machineHealthCheck * clusterv1.MachineHealthCheck , nodeConditions []corev1.NodeCondition ) bool {
302
+ for _ , unhealthyCondition := range machineHealthCheck .Spec .UnhealthyConditions {
303
+ for _ , nodeCondition := range nodeConditions {
304
+ if nodeCondition .Type == unhealthyCondition .Type && nodeCondition .Status == unhealthyCondition .Status {
305
+ return true
254
306
}
255
307
}
256
- Byf ("Current number of machines: %d" , count )
257
- return count , nil
258
- }, intervals ... ).Should (Equal (originalCount ))
308
+ }
309
+ return false
259
310
}
0 commit comments