@@ -28,11 +28,14 @@ import (
2828	ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types" 
2929	. "github.com/onsi/ginkgo/v2" 
3030	. "github.com/onsi/gomega" 
31+ 	corev1 "k8s.io/api/core/v1" 
3132	"k8s.io/apimachinery/pkg/util/version" 
3233	crclient "sigs.k8s.io/controller-runtime/pkg/client" 
3334
3435	ekscontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/v2/controlplane/eks/api/v1beta2" 
36+ 	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 
3537	"sigs.k8s.io/cluster-api/test/framework" 
38+ 	clusterctl "sigs.k8s.io/cluster-api/test/framework/clusterctl" 
3639)
3740
3841type  waitForControlPlaneToBeUpgradedInput  struct  {
@@ -93,3 +96,73 @@ func GetControlPlaneByName(ctx context.Context, input GetControlPlaneByNameInput
9396	Expect (input .Getter .Get (ctx , key , cp )).To (Succeed (), "Failed to get AWSManagedControlPlane object %s/%s" , input .Namespace , input .Name )
9497	return  cp 
9598}
99+ 
100+ func  WaitForEKSControlPlaneInitialized (ctx  context.Context , input  clusterctl.ApplyCustomClusterTemplateAndWaitInput , result  * clusterctl.ApplyCustomClusterTemplateAndWaitResult ) {
101+ 	Expect (ctx ).NotTo (BeNil (), "ctx is required for WaitForEKSControlPlaneInitialized" )
102+ 	Expect (input .ClusterProxy ).ToNot (BeNil (), "Invalid argument. input.ClusterProxy can't be nil" )
103+ 
104+ 	var  awsCP  ekscontrolplanev1.AWSManagedControlPlane 
105+ 	Eventually (func (g  Gomega ) {
106+ 		list , err  :=  listAWSManagedControlPlanes (ctx , input .ClusterProxy .GetClient (), result .Cluster .Namespace , result .Cluster .Name )
107+ 		g .Expect (err ).To (Succeed (), "failed to list AWSManagedControlPlane resource" )
108+ 
109+ 		g .Expect (len (list .Items )).To (Equal (1 ),
110+ 			"expected exactly one AWSManagedControlPlane for %s/%s" ,
111+ 			result .Cluster .Namespace , result .Cluster .Name ,
112+ 		)
113+ 		awsCP  =  list .Items [0 ]
114+ 	}, 10 * time .Second , 1 * time .Second ).Should (Succeed ())
115+ 
116+ 	key  :=  crclient.ObjectKey {Namespace : awsCP .Namespace , Name : awsCP .Name }
117+ 	waitForControlPlaneReady (ctx , input .ClusterProxy .GetClient (), key , input .WaitForControlPlaneIntervals ... )
118+ }
119+ 
120+ func  WaitForEKSControlPlaneMachinesReady (ctx  context.Context , input  clusterctl.ApplyCustomClusterTemplateAndWaitInput , result  * clusterctl.ApplyCustomClusterTemplateAndWaitResult ) {
121+ 	Expect (ctx ).NotTo (BeNil (), "ctx is required for WaitForEKSControlPlaneMachinesReady" )
122+ 	Expect (input .ClusterProxy ).ToNot (BeNil (), "input.ClusterProxy can't be nil" )
123+ 
124+ 	var  awsCP  ekscontrolplanev1.AWSManagedControlPlane 
125+ 	Eventually (func (g  Gomega ) {
126+ 		list , err  :=  listAWSManagedControlPlanes (ctx , input .ClusterProxy .GetClient (), result .Cluster .Namespace , result .Cluster .Name )
127+ 		g .Expect (err ).To (Succeed ())
128+ 		awsCP  =  list .Items [0 ]
129+ 
130+ 		g .Expect (awsCP .Status .Ready ).To (BeTrue (),
131+ 			"waiting for AWSManagedControlPlane %s/%s to become Ready" ,
132+ 			awsCP .Namespace , awsCP .Name ,
133+ 		)
134+ 	}, input .WaitForControlPlaneIntervals ... ).Should (Succeed ())
135+ 
136+ 	workloadClusterProxy  :=  input .ClusterProxy .GetWorkloadCluster (ctx , result .Cluster .Namespace , input .ClusterName )
137+ 	waitForWorkloadClusterReachable (ctx , workloadClusterProxy .GetClient (), input .WaitForControlPlaneIntervals ... )
138+ }
139+ 
140+ // listAWSManagedControlPlanes returns a list of AWSManagedControlPlanes for the given cluster. 
141+ func  listAWSManagedControlPlanes (ctx  context.Context , client  crclient.Client , namespace , clusterName  string ) (* ekscontrolplanev1.AWSManagedControlPlaneList , error ) {
142+ 	list  :=  & ekscontrolplanev1.AWSManagedControlPlaneList {}
143+ 	err  :=  client .List (ctx , list ,
144+ 		crclient .InNamespace (namespace ),
145+ 		crclient.MatchingLabels {clusterv1 .ClusterNameLabel : clusterName },
146+ 	)
147+ 	return  list , err 
148+ }
149+ 
150+ // waitForControlPlaneReady polls until the given AWSManagedControlPlane is marked Ready. 
151+ func  waitForControlPlaneReady (ctx  context.Context , client  crclient.Client , key  crclient.ObjectKey , intervals  ... interface {}) {
152+ 	Eventually (func (g  Gomega ) {
153+ 		var  latest  ekscontrolplanev1.AWSManagedControlPlane 
154+ 		g .Expect (client .Get (ctx , key , & latest )).To (Succeed ())
155+ 		g .Expect (latest .Status .Ready ).To (BeTrue (),
156+ 			"AWSManagedControlPlane %s/%s is not Ready" , key .Namespace , key .Name ,
157+ 		)
158+ 	}, intervals ... ).Should (Succeed ())
159+ }
160+ 
161+ // waitForWorkloadClusterReachable checks when the kube-system namespace is reachable in the workload cluster. 
162+ func  waitForWorkloadClusterReachable (ctx  context.Context , client  crclient.Client , intervals  ... interface {}) {
163+ 	Eventually (func (g  Gomega ) {
164+ 		ns  :=  & corev1.Namespace {}
165+ 		g .Expect (client .Get (ctx , crclient.ObjectKey {Name : "kube-system" }, ns )).
166+ 			To (Succeed (), "workload API server not yet reachable" )
167+ 	}, intervals ... ).Should (Succeed ())
168+ }
0 commit comments