/
ec2tags.go
146 lines (122 loc) · 4.13 KB
/
ec2tags.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
138
139
140
141
142
143
144
145
146
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package host // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awscontainerinsightreceiver/internal/host"
import (
"context"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"go.uber.org/zap"
ci "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/containerinsight"
)
const (
clusterNameKey = "container-insight-eks-cluster-name"
clusterNameTagKeyPrefix = "kubernetes.io/cluster/"
autoScalingGroupNameTag = "aws:autoscaling:groupName"
)
type ec2TagsClient interface {
DescribeTagsWithContext(ctx context.Context, input *ec2.DescribeTagsInput,
opts ...request.Option) (*ec2.DescribeTagsOutput, error)
}
type ec2TagsProvider interface {
getClusterName() string
getAutoScalingGroupName() string
}
type ec2Tags struct {
refreshInterval time.Duration
maxJitterTime time.Duration
containerOrchestrator string
instanceID string
client ec2TagsClient
clusterName string
autoScalingGroupName string
isSucess chan bool // only used in testing
logger *zap.Logger
}
type ec2TagsOption func(*ec2Tags)
func newEC2Tags(ctx context.Context, session *session.Session, instanceID string, region string, containerOrchestrator string,
refreshInterval time.Duration, logger *zap.Logger, options ...ec2TagsOption) ec2TagsProvider {
et := &ec2Tags{
instanceID: instanceID,
client: ec2.New(session, aws.NewConfig().WithRegion(region)),
refreshInterval: refreshInterval,
maxJitterTime: 3 * time.Second,
logger: logger,
containerOrchestrator: containerOrchestrator,
}
for _, opt := range options {
opt(et)
}
shouldRefresh := func() bool {
if containerOrchestrator == ci.EKS {
// stop once we get the cluster name
return et.clusterName == ""
}
return et.autoScalingGroupName == ""
}
go RefreshUntil(ctx, et.refresh, et.refreshInterval, shouldRefresh, et.maxJitterTime)
return et
}
func (et *ec2Tags) fetchEC2Tags(ctx context.Context) map[string]string {
et.logger.Info("Fetch ec2 tags to detect cluster name and auto scaling group name", zap.String("instanceId", et.instanceID))
tags := make(map[string]string)
tagFilters := []*ec2.Filter{
{
Name: aws.String("resource-type"),
Values: aws.StringSlice([]string{"instance"}),
},
{
Name: aws.String("resource-id"),
Values: aws.StringSlice([]string{et.instanceID}),
},
}
input := &ec2.DescribeTagsInput{
Filters: tagFilters,
}
for {
result, err := et.client.DescribeTagsWithContext(ctx, input)
if err != nil {
et.logger.Warn("Fail to call ec2 DescribeTags", zap.Error(err), zap.String("instanceId", et.instanceID))
break
}
for _, tag := range result.Tags {
key := *tag.Key
tags[key] = *tag.Value
if strings.HasPrefix(key, clusterNameTagKeyPrefix) && *tag.Value == "owned" {
tags[clusterNameKey] = key[len(clusterNameTagKeyPrefix):]
}
}
if result.NextToken == nil {
break
}
input.SetNextToken(*result.NextToken)
}
return tags
}
func (et *ec2Tags) getClusterName() string {
return et.clusterName
}
func (et *ec2Tags) getAutoScalingGroupName() string {
return et.autoScalingGroupName
}
func (et *ec2Tags) refresh(ctx context.Context) {
tags := et.fetchEC2Tags(ctx)
et.logger.Info("Fetch ec2 tags successfully")
et.clusterName = tags[clusterNameKey]
et.autoScalingGroupName = tags[autoScalingGroupNameTag]
et.logger.Info("Fetch ec2 tags to detect cluster name and auto scaling group name", zap.String("instanceId", et.autoScalingGroupName))
et.logger.Info("Fetch ec2 tags to detect cluster name and auto scaling group name", zap.String("instanceId", et.clusterName))
if et.containerOrchestrator == ci.ECS {
if et.isSucess != nil && et.autoScalingGroupName != "" {
close(et.isSucess)
}
} else {
if et.isSucess != nil && et.autoScalingGroupName != "" && et.clusterName != "" {
// this will be executed only in testing
close(et.isSucess)
}
}
}