-
Notifications
You must be signed in to change notification settings - Fork 22
/
cloud.go
137 lines (113 loc) · 3.49 KB
/
cloud.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package aws
import (
"github.com/aws/aws-network-policy-agent/pkg/aws/services"
"github.com/aws/aws-network-policy-agent/pkg/utils"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/pkg/errors"
)
const (
resourceID = "resource-id"
resourceKey = "key"
)
var (
clusterNameTags = []string{
"aws:eks:cluster-name",
}
)
type Cloud interface {
//CloudWatch provides API access to AWS Cloudwatch Service
CloudWatchLogs() services.CloudWatchLogs
// AccountID provides AccountID for the kubernetes cluster
AccountID() string
// Region for the kubernetes cluster
Region() string
// Cluster Name
ClusterName() string
}
func NewCloud(cfg CloudConfig) (Cloud, error) {
sess := session.Must(session.NewSession(aws.NewConfig()))
//injectUserAgent(&sess.Handlers)
metadata := services.NewEC2Metadata(sess)
if len(cfg.Region) == 0 {
region, err := metadata.Region()
if err != nil {
return nil, errors.Wrap(err, "failed to introspect region from EC2Metadata, specify --aws-region instead if EC2Metadata is unavailable")
}
cfg.Region = region
}
awsCfg := aws.NewConfig().WithRegion(cfg.Region).WithSTSRegionalEndpoint(endpoints.RegionalSTSEndpoint)
sess = sess.Copy(awsCfg)
instanceIdentityDocument, err := metadata.GetInstanceIdentityDocument()
if err != nil {
return nil, errors.Wrap(err, "failed to get instanceIdentityDocument from EC2Metadata")
}
ec2ServiceClient := ec2.New(sess)
cfg.ClusterName = getClusterName(ec2ServiceClient, instanceIdentityDocument)
return &defaultCloud{
cfg: cfg,
cloudWatchlogs: services.NewCloudWatchLogs(sess),
}, nil
}
var _ Cloud = &defaultCloud{}
type defaultCloud struct {
cfg CloudConfig
cloudWatchlogs services.CloudWatchLogs
}
func (c *defaultCloud) CloudWatchLogs() services.CloudWatchLogs {
return c.cloudWatchlogs
}
func (c *defaultCloud) AccountID() string {
return c.cfg.AccountID
}
func (c *defaultCloud) Region() string {
return c.cfg.Region
}
func (c *defaultCloud) ClusterName() string {
return c.cfg.ClusterName
}
func getClusterName(ec2ServiceClient ec2iface.EC2API, instanceIdentityDocument ec2metadata.EC2InstanceIdentityDocument) string {
var clusterName string
var err error
for _, tag := range clusterNameTags {
clusterName, err = getClusterTag(tag, ec2ServiceClient, instanceIdentityDocument)
if err == nil && clusterName != "" {
break
}
}
if clusterName == "" {
clusterName = utils.DEFAULT_CLUSTER_NAME
}
return clusterName
}
// getClusterTag is used to retrieve a tag from the ec2 instance
func getClusterTag(tagKey string, ec2ServiceClient ec2iface.EC2API, instanceIdentityDocument ec2metadata.EC2InstanceIdentityDocument) (string, error) {
input := ec2.DescribeTagsInput{
Filters: []*ec2.Filter{
{
Name: aws.String(resourceID),
Values: []*string{
aws.String(instanceIdentityDocument.InstanceID),
},
}, {
Name: aws.String(resourceKey),
Values: []*string{
aws.String(tagKey),
},
},
},
}
//log.Infof("Calling DescribeTags with key %s", tagKey)
results, err := ec2ServiceClient.DescribeTags(&input)
if err != nil {
return "", errors.Wrap(err, "GetClusterTag: Unable to obtain EC2 instance tags")
}
if len(results.Tags) < 1 {
return "", errors.Errorf("GetClusterTag: No tag matching key: %s", tagKey)
}
return aws.StringValue(results.Tags[0].Value), nil
}