Skip to content

Commit

Permalink
skip testing create CapacityReservation case if CreateCapacityReserva…
Browse files Browse the repository at this point in the history
…tion failed
  • Loading branch information
huali9 committed Nov 21, 2024
1 parent 202e9ef commit 26311ca
Showing 1 changed file with 4 additions and 43 deletions.
47 changes: 4 additions & 43 deletions pkg/providers/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ package providers

import (
"context"
"encoding/base64"
"encoding/json"
"fmt"

"k8s.io/client-go/kubernetes"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

Expand All @@ -22,48 +17,12 @@ import (

"github.com/openshift/cluster-api-actuator-pkg/pkg/framework"
"github.com/openshift/cluster-api-actuator-pkg/pkg/framework/gatherer"
"github.com/tidwall/gjson"
)

const (
amiIDMetadataEndpoint = "http://169.254.169.254/latest/meta-data/ami-id"
)

// createAWSClient create AWS client.
func createAWSClient(oc *gatherer.CLI) *framework.AwsClient {
awscreds, err := oc.WithoutNamespace().Run("get").Args("secret/aws-creds", "-n", "kube-system", "-o", "json").Output()
if err != nil {
Skip("Unable to get AWS credentials secret, skipping the testing.")
}

accessKeyIDBase64, secureKeyBase64 := gjson.Get(awscreds, `data.aws_access_key_id`).String(), gjson.Get(awscreds, `data.aws_secret_access_key`).String()

accessKeyID, err := base64.StdEncoding.DecodeString(accessKeyIDBase64)
Expect(err).NotTo(HaveOccurred())
secureKey, err := base64.StdEncoding.DecodeString(secureKeyBase64)
Expect(err).NotTo(HaveOccurred())
clusterRegion, err := oc.WithoutNamespace().Run("get").Args("infrastructure", "cluster", "-o=jsonpath={.status.platformStatus.aws.region}").Output()
Expect(err).NotTo(HaveOccurred())

awsConfig := &aws.Config{
Region: aws.String(clusterRegion),
Credentials: credentials.NewStaticCredentials(
string(accessKeyID),
string(secureKey),
"",
),
}

sess, err := session.NewSession(awsConfig)
Expect(err).ToNot(HaveOccurred())

aClient := &framework.AwsClient{
Svc: ec2.New(sess),
}

return aClient
}

var _ = Describe("MetadataServiceOptions", framework.LabelCloudProviderSpecific, framework.LabelProviderAWS, func() {
var client runtimeclient.Client
var clientset *kubernetes.Clientset
Expand Down Expand Up @@ -278,9 +237,11 @@ var _ = Describe("CapacityReservationID", framework.LabelCloudProviderSpecific,

By("Access AWS to create CapacityReservation")
oc, _ := framework.NewCLI()
awsClient := createAWSClient(oc)
awsClient := framework.CreateAWSClient(oc)
capacityReservationID, err := awsClient.CreateCapacityReservation(awsProviderConfig.InstanceType, "Linux/UNIX", awsProviderConfig.Placement.AvailabilityZone, 1)
Expect(err).ToNot(HaveOccurred())
if err != nil {
Skip(fmt.Sprintf("Skip because CreateCapacityReservation failed %v", err))
}
Expect(capacityReservationID).ToNot(Equal(""))

defer func() {
Expand Down

0 comments on commit 26311ca

Please sign in to comment.