-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathaws_client.go
82 lines (70 loc) · 2.48 KB
/
aws_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package util
import (
"encoding/base64"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elb"
g "github.com/onsi/ginkgo/v2"
o "github.com/onsi/gomega"
"github.com/tidwall/gjson"
e2e "k8s.io/kubernetes/test/e2e/framework"
)
// GetAwsCredentialFromCluster get aws credential from cluster
func GetAwsCredentialFromCluster(oc *CLI) {
credential, err := oc.AsAdmin().WithoutNamespace().Run("get").Args("secret/aws-creds", "-n", "kube-system", "-o", "json").Output()
// Skip for sts and c2s clusters.
if err != nil {
g.Skip("Did not get credential to access aws, skip the testing.")
}
o.Expect(err).NotTo(o.HaveOccurred())
accessKeyIDBase64, secureKeyBase64 := gjson.Get(credential, `data.aws_access_key_id`).String(), gjson.Get(credential, `data.aws_secret_access_key`).String()
accessKeyID, err1 := base64.StdEncoding.DecodeString(accessKeyIDBase64)
o.Expect(err1).NotTo(o.HaveOccurred())
secureKey, err2 := base64.StdEncoding.DecodeString(secureKeyBase64)
o.Expect(err2).NotTo(o.HaveOccurred())
clusterRegion, err3 := oc.AsAdmin().WithoutNamespace().Run("get").Args("infrastructure", "cluster", "-o=jsonpath={.status.platformStatus.aws.region}").Output()
o.Expect(err3).NotTo(o.HaveOccurred())
os.Setenv("AWS_ACCESS_KEY_ID", string(accessKeyID))
os.Setenv("AWS_SECRET_ACCESS_KEY", string(secureKey))
os.Setenv("AWS_REGION", clusterRegion)
}
// InitAwsSession init session
func InitAwsSession(region string) *session.Session {
sess := session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String(region),
},
}))
return sess
}
type ELBClient struct {
svc *elb.ELB
}
// NewELBClient creates an ECRClient
func NewELBClient(sess *session.Session) *ELBClient {
return &ELBClient{
svc: elb.New(sess),
}
}
// GetLBHealthCheckPortPath get load balance health check port and path
func (elbClient *ELBClient) GetLBHealthCheckPortPath(lbName string) (string, error) {
input := &elb.DescribeLoadBalancersInput{
LoadBalancerNames: []*string{
aws.String(lbName),
},
}
result, err := elbClient.svc.DescribeLoadBalancers(input)
if err != nil {
e2e.Logf("Failed to describe load balancer: %v", err)
return "", err
}
if len(result.LoadBalancerDescriptions) == 0 {
e2e.Logf("Failed to get load balancers: %v", err)
}
healthCheck := result.LoadBalancerDescriptions[0].HealthCheck
if healthCheck == nil {
e2e.Logf("Failed to get health check: %v", err)
}
return *healthCheck.Target, nil
}