11package framework
22
33import (
4+ "context"
45 "fmt"
6+ "time"
57
8+ . "github.com/onsi/gomega"
9+ apierrors "k8s.io/apimachinery/pkg/api/errors"
610 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+ "k8s.io/apimachinery/pkg/util/wait"
12+ "k8s.io/klog/v2"
13+ awsv1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
714 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
815 "sigs.k8s.io/controller-runtime/pkg/client"
16+ runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
917)
1018
1119// GetMachines gets a list of machines from the default cluster API namespace.
@@ -41,6 +49,30 @@ func GetMachines(cl client.Client, selectors ...*metav1.LabelSelector) ([]*clust
4149 return machines , nil
4250}
4351
52+ // GetMachine get a machine by its name from the cluster API namespace.
53+ func GetMachine (cl client.Client , name string ) (* clusterv1.Machine , error ) {
54+ machine := & clusterv1.Machine {}
55+ key := client.ObjectKey {Namespace : CAPINamespace , Name : name }
56+
57+ if err := cl .Get (context .Background (), key , machine ); err != nil {
58+ return nil , fmt .Errorf ("error querying api for machine object: %w" , err )
59+ }
60+
61+ return machine , nil
62+ }
63+
64+ // GetAWSMachine get a awsmachine by its name from the cluster API namespace.
65+ func GetAWSMachine (cl client.Client , name string ) (* awsv1.AWSMachine , error ) {
66+ machine := & awsv1.AWSMachine {}
67+ key := client.ObjectKey {Namespace : CAPINamespace , Name : name }
68+
69+ if err := cl .Get (context .Background (), key , machine ); err != nil {
70+ return nil , fmt .Errorf ("error querying api for awsmachine object: %w" , err )
71+ }
72+
73+ return machine , nil
74+ }
75+
4476// FilterRunningMachines returns a slice of only those Machines in the input
4577// that are in the "Running" phase.
4678func FilterRunningMachines (machines []* clusterv1.Machine ) []* clusterv1.Machine {
@@ -54,3 +86,33 @@ func FilterRunningMachines(machines []*clusterv1.Machine) []*clusterv1.Machine {
5486
5587 return result
5688}
89+
90+ // DeleteMachines deletes the specified machines and returns an error on failure.
91+ func DeleteMachines (cl client.Client , machines ... * clusterv1.Machine ) error {
92+ return wait .PollUntilContextTimeout (ctx , RetryShort , time .Minute , true , func (ctx context.Context ) (bool , error ) {
93+ for _ , machine := range machines {
94+ if err := cl .Delete (ctx , machine ); err != nil {
95+ klog .Errorf ("Error querying api for machine object %q: %v, retrying..." , machine .Name , err )
96+ return false , err
97+ }
98+ }
99+
100+ return true , nil
101+ })
102+ }
103+
104+ // WaitForMachinesDeleted polls until the given Machines are not found.
105+ func WaitForMachinesDeleted (cl client.Client , machines ... * clusterv1.Machine ) {
106+ Eventually (func () bool {
107+ for _ , m := range machines {
108+ if err := cl .Get (context .Background (), runtimeclient.ObjectKey {
109+ Name : m .GetName (),
110+ Namespace : m .GetNamespace (),
111+ }, & clusterv1.Machine {}); ! apierrors .IsNotFound (err ) {
112+ return false // Not deleted, or other error.
113+ }
114+ }
115+
116+ return true // Everything was deleted.
117+ }, WaitLong , RetryMedium ).Should (BeTrue (), "error encountered while waiting for Machines to be deleted." )
118+ }
0 commit comments