@@ -18,10 +18,9 @@ package e2e
18
18
19
19
import (
20
20
"context"
21
- "encoding/base64 "
21
+ "encoding/json "
22
22
"errors"
23
23
"fmt"
24
- "os"
25
24
"path/filepath"
26
25
"strings"
27
26
"time"
@@ -31,11 +30,11 @@ import (
31
30
"github.com/apache/cloudstack-go/v2/cloudstack"
32
31
"github.com/blang/semver"
33
32
. "github.com/onsi/ginkgo"
34
- "gopkg.in/ini.v1"
35
33
corev1 "k8s.io/api/core/v1"
36
34
37
35
. "github.com/onsi/gomega"
38
36
"github.com/onsi/gomega/types"
37
+ "k8s.io/apimachinery/pkg/runtime"
39
38
k8stypes "k8s.io/apimachinery/pkg/types"
40
39
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
41
40
"sigs.k8s.io/cluster-api/test/framework"
@@ -166,6 +165,22 @@ func KubectlExec(ctx context.Context, command string, kubeconfigPath string, arg
166
165
return string (stdout ), nil
167
166
}
168
167
168
+ func GetK8sObject (ctx context.Context , resourceType , name , namespace , kubeconfigPath string , obj runtime.Object ) error {
169
+ getArgs := []string {"--ignore-not-found" , "--namespace" , namespace , resourceType , name , "-o" , "json" }
170
+ stdOut , err := KubectlExec (ctx , "get" , kubeconfigPath , getArgs ... )
171
+ if err != nil {
172
+ return fmt .Errorf ("getting %s/%s/%s with kubectl: %v" , resourceType , namespace , name , err )
173
+ }
174
+ if len (stdOut ) == 0 {
175
+ return fmt .Errorf ("not found %s/%s/%s" , resourceType , namespace , name )
176
+ }
177
+ if err = json .Unmarshal ([]byte (stdOut ), obj ); err != nil {
178
+ return fmt .Errorf ("parsing %s/%s/%s response: %v" , resourceType , namespace , name , err )
179
+ }
180
+
181
+ return nil
182
+ }
183
+
169
184
func DeployAppToWorkloadClusterAndWaitForDeploymentReady (ctx context.Context , workloadKubeconfigPath string , appName string , appConfigLink string , timeout int ) error {
170
185
applyArgs := []string {
171
186
"-f" , appConfigLink ,
@@ -230,15 +245,7 @@ func DownloadMetricsFromCAPCManager(ctx context.Context, bootstrapKubeconfigPath
230
245
return result , nil
231
246
}
232
247
233
- type cloudConfig struct {
234
- APIURL string `ini:"api-url"`
235
- APIKey string `ini:"api-key"`
236
- SecretKey string `ini:"secret-key"`
237
- VerifySSL bool `ini:"verify-ssl"`
238
- }
239
-
240
- func DestroyOneMachine (clusterName string , machineType string ) {
241
- client := createCloudStackClient ()
248
+ func DestroyOneMachine (client * cloudstack.CloudStackClient , clusterName string , machineType string ) {
242
249
matcher := clusterName + "-" + machineType
243
250
244
251
Byf ("Listing machines with %q" , matcher )
@@ -266,13 +273,11 @@ func DestroyOneMachine(clusterName string, machineType string) {
266
273
}
267
274
}
268
275
269
- func CheckAffinityGroupsDeleted (affinityIds []string ) error {
276
+ func CheckAffinityGroupsDeleted (client * cloudstack. CloudStackClient , affinityIds []string ) error {
270
277
if len (affinityIds ) == 0 {
271
278
return errors .New ("affinityIds are empty" )
272
279
}
273
280
274
- client := createCloudStackClient ()
275
-
276
281
for _ , affinityId := range affinityIds {
277
282
affinity , count , _ := client .AffinityGroup .GetAffinityGroupByID (affinityId )
278
283
if count > 0 {
@@ -282,9 +287,7 @@ func CheckAffinityGroupsDeleted(affinityIds []string) error {
282
287
return nil
283
288
}
284
289
285
- func CheckAffinityGroup (clusterName string , affinityType string ) []string {
286
- client := createCloudStackClient ()
287
-
290
+ func CheckAffinityGroup (client * cloudstack.CloudStackClient , clusterName string , affinityType string ) []string {
288
291
By ("Listing all machines" )
289
292
p := client .VirtualMachine .NewListVirtualMachinesParams ()
290
293
p .SetListall (true )
@@ -326,39 +329,39 @@ func CheckAffinityGroup(clusterName string, affinityType string) []string {
326
329
return affinityIds
327
330
}
328
331
329
- func CheckNetworkExists (networkName string ) (bool , error ) {
330
- client := createCloudStackClient ()
331
-
332
+ func CheckNetworkExists (client * cloudstack.CloudStackClient , networkName string ) (bool , error ) {
332
333
_ , count , err := client .Network .GetNetworkByName (networkName )
333
334
if err != nil {
334
335
if strings .Contains (err .Error (), "No match found for" ) {
335
336
return false , nil
336
337
}
337
338
return false , err
338
339
} else if count > 1 {
339
- return false , errors . New ( fmt .Sprintf ("Expected 0-1 Network with name %s, but got %d." , networkName , count ) )
340
+ return false , fmt .Errorf ("Expected 0-1 Network with name %s, but got %d." , networkName , count )
340
341
}
341
342
return count == 1 , nil
342
343
}
343
344
344
- func createCloudStackClient () * cloudstack.CloudStackClient {
345
- encodedSecret := os .Getenv ("CLOUDSTACK_B64ENCODED_SECRET" )
346
- secret , err := base64 .StdEncoding .DecodeString (encodedSecret )
347
- if err != nil {
348
- Fail ("Failed to decode: " + err .Error ())
349
- }
350
- cfg := & cloudConfig {VerifySSL : true }
351
- if rawCfg , err := ini .Load (secret ); err != nil {
352
- Fail ("Failed to load INI file: " + err .Error ())
353
- } else if g := rawCfg .Section ("Global" ); len (g .Keys ()) == 0 {
354
- Fail ("Global section not found" )
355
- } else if err = rawCfg .Section ("Global" ).StrictMapTo (cfg ); err != nil {
356
- Fail ("Error encountered while parsing Global section" )
345
+ func CreateCloudStackClient (ctx context.Context , kubeConfigPath string ) * cloudstack.CloudStackClient {
346
+ By ("Getting a CloudStack client secret" )
347
+ secret := & corev1.Secret {}
348
+ name := "secret1"
349
+ namepace := "default"
350
+ if err := GetK8sObject (ctx , "secret" , name , namepace , kubeConfigPath , secret ); err != nil {
351
+ Fail ("Failed to get secret: " + err .Error ())
357
352
}
358
353
359
354
By ("Creating a CloudStack client" )
360
- client := cloudstack .NewAsyncClient (cfg .APIURL , cfg .APIKey , cfg .SecretKey , cfg .VerifySSL )
361
- return client
355
+ apiURL := string (secret .Data ["api-url" ])
356
+ apiKey := string (secret .Data ["api-key" ])
357
+ secretKey := string (secret .Data ["secret-key" ])
358
+ verifySSL := string (secret .Data ["verify-ssl" ])
359
+ if apiURL == "" || apiKey == "" || secretKey == "" {
360
+ Fail (fmt .Sprintf ("Invalid secret: %+v, %s, %s, %s" , secret .Data , apiURL , apiKey , secretKey ))
361
+ }
362
+ fmt .Sprintf ("from secret: %s, %s, %s" , apiURL , apiKey , secretKey )
363
+
364
+ return cloudstack .NewClient (apiURL , apiKey , secretKey , strings .ToLower (verifySSL ) == "true" )
362
365
}
363
366
364
367
func checkVMHostAssignments (vm * cloudstack.VirtualMachine , cpHostIdSet map [string ]bool , mdHostIdSet map [string ]bool , affinityType string ) error {
@@ -397,7 +400,8 @@ func WaitForMachineRemediationAfterDestroy(ctx context.Context, proxy framework.
397
400
Byf ("Current number of healthy %s is %d" , machineMatcher , healthyMachineCount )
398
401
399
402
Byf ("Destroying one %s" , machineMatcher )
400
- DestroyOneMachine (cluster .Name , machineMatcher )
403
+ csClient := CreateCloudStackClient (ctx , proxy .GetKubeconfigPath ())
404
+ DestroyOneMachine (csClient , cluster .Name , machineMatcher )
401
405
402
406
Byf ("Waiting for the destroyed %s to be unhealthy" , machineMatcher )
403
407
WaitForHealthyMachineCount (ctx , mgmtClusterClient , workloadClusterClient , cluster , machineMatcher , healthyMachineCount - 1 , intervals )
@@ -480,9 +484,7 @@ func IsClusterReady(ctx context.Context, mgmtClient client.Client, cluster *clus
480
484
return c .Status .ControlPlaneReady && c .Status .InfrastructureReady
481
485
}
482
486
483
- func CheckDiskOfferingOfVmInstances (clusterName string , diskOfferingName string ) {
484
- client := createCloudStackClient ()
485
-
487
+ func CheckDiskOfferingOfVmInstances (client * cloudstack.CloudStackClient , clusterName string , diskOfferingName string ) {
486
488
Byf ("Listing machines with %q" , clusterName )
487
489
listResp , err := client .VirtualMachine .ListVirtualMachines (client .VirtualMachine .NewListVirtualMachinesParams ())
488
490
if err != nil {
@@ -494,9 +496,7 @@ func CheckDiskOfferingOfVmInstances(clusterName string, diskOfferingName string)
494
496
}
495
497
}
496
498
}
497
- func CheckVolumeSizeofVmInstances (clusterName string , volumeSize int64 ) {
498
- client := createCloudStackClient ()
499
-
499
+ func CheckVolumeSizeofVmInstances (client * cloudstack.CloudStackClient , clusterName string , volumeSize int64 ) {
500
500
Byf ("Listing machines with %q" , clusterName )
501
501
listResp , err := client .VirtualMachine .ListVirtualMachines (client .VirtualMachine .NewListVirtualMachinesParams ())
502
502
if err != nil {
0 commit comments