@@ -25,6 +25,7 @@ import (
25
25
. "github.com/onsi/ginkgo/v2"
26
26
. "github.com/onsi/gomega"
27
27
"github.com/pkg/errors"
28
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
28
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
30
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
30
31
"k8s.io/apimachinery/pkg/util/sets"
@@ -448,6 +449,41 @@ func machineDeploymentOptions(deployment clusterv1.MachineDeployment) []client.L
448
449
}
449
450
}
450
451
452
+ // DeleteAndWaitMachineDeploymentInput is the input for DeleteAndWaitMachineDeployment.
453
+ type DeleteAndWaitMachineDeploymentInput struct {
454
+ ClusterProxy ClusterProxy
455
+ Cluster * clusterv1.Cluster
456
+ MachineDeployment * clusterv1.MachineDeployment
457
+ WaitForMachineDeployments []interface {}
458
+ DeletePropagationPolicy * metav1.DeletionPropagation
459
+ }
460
+
461
+ // DeleteAndWaitMachineDeployment deletes MachineDeployment and waits until it is gone.
462
+ func DeleteAndWaitMachineDeployment (ctx context.Context , input DeleteAndWaitMachineDeploymentInput ) {
463
+ Expect (ctx ).NotTo (BeNil (), "ctx is required for DeleteAndWaitMachineDeployment" )
464
+ Expect (input .ClusterProxy ).ToNot (BeNil (), "Invalid argument. input.ClusterProxy can't be nil when calling DeleteAndWaitMachineDeployment" )
465
+ Expect (input .Cluster ).ToNot (BeNil (), "Invalid argument. input.Cluster can't be nil when calling DeleteAndWaitMachineDeployment" )
466
+ Expect (input .MachineDeployment ).ToNot (BeNil (), "Invalid argument. input.MachineDeployment can't be nil when calling DeleteAndWaitMachineDeployment" )
467
+
468
+ log .Logf ("Deleting MachineDeployment %s" , klog .KObj (input .MachineDeployment ))
469
+ Expect (input .ClusterProxy .GetClient ().Delete (ctx , input .MachineDeployment , & client.DeleteOptions {PropagationPolicy : input .DeletePropagationPolicy })).To (Succeed ())
470
+
471
+ log .Logf ("Waiting for MD to be deleted" )
472
+ Eventually (func (g Gomega ) {
473
+ selectorMap , err := metav1 .LabelSelectorAsMap (& input .MachineDeployment .Spec .Selector )
474
+ g .Expect (err ).ToNot (HaveOccurred ())
475
+
476
+ ms := & clusterv1.MachineSetList {}
477
+ err = input .ClusterProxy .GetClient ().List (ctx , ms , client .InNamespace (input .Cluster .Namespace ), client .MatchingLabels (selectorMap ))
478
+ g .Expect (err ).ToNot (HaveOccurred ())
479
+ g .Expect (ms .Items ).To (BeEmpty ())
480
+
481
+ md := & clusterv1.MachineDeployment {}
482
+ err = input .ClusterProxy .GetClient ().Get (ctx , client .ObjectKeyFromObject (input .MachineDeployment ), md )
483
+ g .Expect (apierrors .IsNotFound (err )).To (BeTrue ())
484
+ }, input .WaitForMachineDeployments ... ).Should (Succeed (), "Timed out waiting for Machine Deployment deletion" )
485
+ }
486
+
451
487
// ScaleAndWaitMachineDeploymentInput is the input for ScaleAndWaitMachineDeployment.
452
488
type ScaleAndWaitMachineDeploymentInput struct {
453
489
ClusterProxy ClusterProxy
0 commit comments