1+ // Copyright 2024 Red Hat, Inc.
2+ //
3+ // Licensed under the Apache License, Version 2.0 (the "License");
4+ // you may not use this file except in compliance with the License.
5+ // You may obtain a copy of the License at
6+ //
7+ // http://www.apache.org/licenses/LICENSE-2.0
8+ //
9+ // Unless required by applicable law or agreed to in writing, software
10+ // distributed under the License is distributed on an "AS IS" BASIS,
11+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ // See the License for the specific language governing permissions and
13+ // limitations under the License.
114package e2e
215
316import (
@@ -20,28 +33,49 @@ import (
2033 awsv1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
2134 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2235 "sigs.k8s.io/controller-runtime/pkg/client"
36+ "sigs.k8s.io/controller-runtime/pkg/client/config"
2337 yaml "sigs.k8s.io/yaml"
2438)
2539
2640const (
2741 awsMachineTemplateName = "aws-machine-template"
42+ infrastructureName = "cluster"
43+ infraAPIVersion = "infrastructure.cluster.x-k8s.io/v1beta1"
2844)
2945
3046var _ = Describe ("Cluster API AWS MachineSet" , Ordered , func () {
3147 var (
48+ cl client.Client
49+ ctx = context .Background ()
3250 awsMachineTemplate * awsv1.AWSMachineTemplate
3351 machineSet * clusterv1.MachineSet
3452 mapiDefaultMS * mapiv1.MachineSet
3553 mapiDefaultProviderSpec * mapiv1.AWSMachineProviderConfig
3654 awsClient * ec2.EC2
55+ platform configv1.PlatformType
56+ clusterName string
3757 )
3858
3959 BeforeAll (func () {
60+ cfg , err := config .GetConfig ()
61+ Expect (err ).ToNot (HaveOccurred (), "Failed to GetConfig" )
62+
63+ cl , err = client .New (cfg , client.Options {})
64+ Expect (err ).ToNot (HaveOccurred (), "Failed to create Kubernetes client for test" )
65+
66+ infra := & configv1.Infrastructure {}
67+ infraName := client.ObjectKey {
68+ Name : infrastructureName ,
69+ }
70+ Expect (cl .Get (ctx , infraName , infra )).To (Succeed (), "Failed to get cluster infrastructure object" )
71+ Expect (infra .Status .PlatformStatus ).ToNot (BeNil (), "expected the infrastructure Status.PlatformStatus to not be nil" )
72+ clusterName = infra .Status .InfrastructureName
73+ platform = infra .Status .PlatformStatus .Type
4074 if platform != configv1 .AWSPlatformType {
4175 Skip ("Skipping AWS E2E tests" )
4276 }
4377 mapiDefaultMS , mapiDefaultProviderSpec = getDefaultAWSMAPIProviderSpec (cl )
44- awsClient = createAWSClient (mapiDefaultProviderSpec .Placement .Region )
78+ awsClient = createAWSClient (cl , mapiDefaultProviderSpec .Placement .Region )
4579 })
4680
4781 AfterEach (func () {
@@ -75,13 +109,13 @@ var _ = Describe("Cluster API AWS MachineSet", Ordered, func() {
75109
76110 framework .WaitForMachineSet (cl , machineSet .Name )
77111
78- compareInstances (awsClient , mapiDefaultMS .Name , "aws-machineset" )
112+ compareInstances (cl , awsClient , mapiDefaultMS .Name , "aws-machineset" )
79113 })
80114})
81115
82116func getDefaultAWSMAPIProviderSpec (cl client.Client ) (* mapiv1.MachineSet , * mapiv1.AWSMachineProviderConfig ) {
83117 machineSetList := & mapiv1.MachineSetList {}
84- Expect (cl .List (ctx , machineSetList , client .InNamespace (framework .MAPINamespace ))).To (Succeed ())
118+ Expect (cl .List (framework . GetContext () , machineSetList , client .InNamespace (framework .MAPINamespace ))).To (Succeed ())
85119
86120 Expect (machineSetList .Items ).ToNot (HaveLen (0 ))
87121 machineSet := & machineSetList .Items [0 ]
@@ -156,15 +190,17 @@ func newAWSMachineTemplate(mapiProviderSpec *mapiv1.AWSMachineProviderConfig) *a
156190 return awsMachineTemplate
157191}
158192
159- func createAWSClient (region string ) * ec2.EC2 {
193+ func createAWSClient (cl client. Client , region string ) * ec2.EC2 {
160194 var secret corev1.Secret
161- Expect (cl .Get (context .Background (), client.ObjectKey {
195+
196+ Expect (cl .Get (framework .GetContext (), client.ObjectKey {
162197 Namespace : framework .CAPINamespace ,
163198 Name : "capa-manager-bootstrap-credentials" ,
164199 }, & secret )).To (Succeed ())
165200
166201 accessKey := secret .Data ["aws_access_key_id" ]
167202 Expect (accessKey ).ToNot (BeNil ())
203+
168204 secretAccessKey := secret .Data ["aws_secret_access_key" ]
169205 Expect (secretAccessKey ).ToNot (BeNil ())
170206
@@ -183,11 +219,12 @@ func createAWSClient(region string) *ec2.EC2 {
183219 return ec2 .New (sess )
184220}
185221
186- func getMAPICreatedInstance (awsClient * ec2.EC2 , msName string ) ec2.Instance {
222+ func getMAPICreatedInstance (cl client. Client , awsClient * ec2.EC2 , msName string ) ec2.Instance {
187223 Expect (awsClient ).ToNot (BeNil ())
188224 Expect (msName ).ToNot (BeEmpty ())
225+
189226 mapiMachineList := & mapiv1.MachineList {}
190- Expect (cl .List (ctx , mapiMachineList , client .InNamespace (framework .MAPINamespace ), client.MatchingLabels {
227+ Expect (cl .List (framework . GetContext () , mapiMachineList , client .InNamespace (framework .MAPINamespace ), client.MatchingLabels {
191228 "machine.openshift.io/cluster-api-machineset" : msName ,
192229 })).To (Succeed ())
193230 Expect (len (mapiMachineList .Items )).To (BeNumerically (">" , 0 ))
@@ -214,12 +251,13 @@ func getMAPICreatedInstance(awsClient *ec2.EC2, msName string) ec2.Instance {
214251 return * result .Reservations [0 ].Instances [0 ]
215252}
216253
217- func getCAPICreatedInstance (awsClient * ec2.EC2 , msName string ) ec2.Instance {
254+ func getCAPICreatedInstance (cl client. Client , awsClient * ec2.EC2 , msName string ) ec2.Instance {
218255 Expect (awsClient ).ToNot (BeNil ())
219256 Expect (msName ).ToNot (BeEmpty ())
257+
220258 capiMachineList := & awsv1.AWSMachineList {}
221259
222- Expect (cl .List (ctx , capiMachineList , client .InNamespace (framework .CAPINamespace ), client.MatchingLabels {
260+ Expect (cl .List (framework . GetContext () , capiMachineList , client .InNamespace (framework .CAPINamespace ), client.MatchingLabels {
223261 "machine.openshift.io/cluster-api-machineset" : msName ,
224262 })).To (Succeed ())
225263 Expect (capiMachineList .Items ).To (HaveLen (1 ))
@@ -240,10 +278,11 @@ func getCAPICreatedInstance(awsClient *ec2.EC2, msName string) ec2.Instance {
240278 return * result .Reservations [0 ].Instances [0 ]
241279}
242280
243- func compareInstances (awsClient * ec2.EC2 , mapiMsName , capiMsName string ) {
281+ func compareInstances (cl client. Client , awsClient * ec2.EC2 , mapiMsName , capiMsName string ) {
244282 By ("Comparing instances created by MAPI and CAPI" )
245- mapiEC2Instance := getMAPICreatedInstance (awsClient , mapiMsName )
246- capiEC2Instance := getCAPICreatedInstance (awsClient , capiMsName )
283+
284+ mapiEC2Instance := getMAPICreatedInstance (cl , awsClient , mapiMsName )
285+ capiEC2Instance := getCAPICreatedInstance (cl , awsClient , capiMsName )
247286
248287 // Ignore fields that are unique for each instance
249288 ignoreInstanceFields := cmpopts .IgnoreFields (ec2.Instance {},
@@ -270,7 +309,7 @@ func compareInstances(awsClient *ec2.EC2, mapiMsName, capiMsName string) {
270309 "PrivateIpAddress" ,
271310 )
272311
273- ignorePrivateIpFields := cmpopts .IgnoreFields (ec2.InstancePrivateIpAddress {},
312+ ignorePrivateIPFields := cmpopts .IgnoreFields (ec2.InstancePrivateIpAddress {},
274313 "PrivateDnsName" ,
275314 "PrivateIpAddress" ,
276315 )
@@ -282,7 +321,7 @@ func compareInstances(awsClient *ec2.EC2, mapiMsName, capiMsName string) {
282321 ignoreInstanceFields ,
283322 ignoreBlockDeviceFields ,
284323 ignoreNicFields ,
285- ignorePrivateIpFields ,
324+ ignorePrivateIPFields ,
286325 ignoreTags ,
287326 }
288327
0 commit comments