-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
hostinfo.go
180 lines (146 loc) · 5.9 KB
/
hostinfo.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// 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"
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/awsutil"
)
// Info contains information about a host
type Info struct {
cancel context.CancelFunc
logger *zap.Logger
awsSession *session.Session
refreshInterval time.Duration
containerOrchestrator string
instanceIDReadyC chan bool // close of this channel indicates instance ID is ready
instanceIPReadyC chan bool // close of this channel indicates instance Ip is ready
ebsVolumeReadyC chan bool // close of this channel indicates ebsVolume is initialized. It is used only in test
ec2TagsReadyC chan bool // close of this channel indicates ec2Tags is initialized. It is used only in test
nodeCapacity nodeCapacityProvider
ec2Metadata ec2MetadataProvider
ebsVolume ebsVolumeProvider
ec2Tags ec2TagsProvider
awsSessionCreator func(*zap.Logger, awsutil.ConnAttr, *awsutil.AWSSessionSettings) (*aws.Config, *session.Session, error)
nodeCapacityCreator func(*zap.Logger, ...nodeCapacityOption) (nodeCapacityProvider, error)
ec2MetadataCreator func(context.Context, *session.Session, time.Duration, chan bool, chan bool, *zap.Logger, ...ec2MetadataOption) ec2MetadataProvider
ebsVolumeCreator func(context.Context, *session.Session, string, string, time.Duration, *zap.Logger, ...ebsVolumeOption) ebsVolumeProvider
ec2TagsCreator func(context.Context, *session.Session, string, string, string, time.Duration, *zap.Logger, ...ec2TagsOption) ec2TagsProvider
}
type machineInfoOption func(*Info)
// NewInfo creates a new Info struct
func NewInfo(containerOrchestrator string, refreshInterval time.Duration, logger *zap.Logger, options ...machineInfoOption) (*Info, error) {
ctx, cancel := context.WithCancel(context.Background())
mInfo := &Info{
cancel: cancel,
refreshInterval: refreshInterval,
instanceIDReadyC: make(chan bool),
instanceIPReadyC: make(chan bool),
logger: logger,
containerOrchestrator: containerOrchestrator,
awsSessionCreator: awsutil.GetAWSConfigSession,
nodeCapacityCreator: newNodeCapacity,
ec2MetadataCreator: newEC2Metadata,
ebsVolumeCreator: newEBSVolume,
ec2TagsCreator: newEC2Tags,
// used in test only
ebsVolumeReadyC: make(chan bool),
ec2TagsReadyC: make(chan bool),
}
for _, opt := range options {
opt(mInfo)
}
nodeCapacity, err := mInfo.nodeCapacityCreator(logger)
if err != nil {
return nil, fmt.Errorf("failed to initialize NodeCapacity: %w", err)
}
mInfo.nodeCapacity = nodeCapacity
defaultSessionConfig := awsutil.CreateDefaultSessionConfig()
_, session, err := mInfo.awsSessionCreator(logger, &awsutil.Conn{}, &defaultSessionConfig)
if err != nil {
return nil, fmt.Errorf("failed to create aws session: %w", err)
}
mInfo.awsSession = session
mInfo.ec2Metadata = mInfo.ec2MetadataCreator(ctx, session, refreshInterval, mInfo.instanceIDReadyC, mInfo.instanceIPReadyC, logger)
go mInfo.lazyInitEBSVolume(ctx)
go mInfo.lazyInitEC2Tags(ctx)
return mInfo, nil
}
func (m *Info) lazyInitEBSVolume(ctx context.Context) {
// wait until the instance id is ready
<-m.instanceIDReadyC
// Because ebs volumes only change occasionally, we refresh every 5 collection intervals to reduce ec2 api calls
m.ebsVolume = m.ebsVolumeCreator(ctx, m.awsSession, m.GetInstanceID(), m.GetRegion(),
5*m.refreshInterval, m.logger)
close(m.ebsVolumeReadyC)
}
func (m *Info) lazyInitEC2Tags(ctx context.Context) {
// wait until the instance id is ready
<-m.instanceIDReadyC
m.ec2Tags = m.ec2TagsCreator(ctx, m.awsSession, m.GetInstanceID(), m.GetRegion(), m.containerOrchestrator, m.refreshInterval, m.logger)
close(m.ec2TagsReadyC)
}
// GetInstanceID returns the ec2 instance id for the host
func (m *Info) GetInstanceID() string {
return m.ec2Metadata.getInstanceID()
}
// GetInstanceType returns the ec2 instance type for the host
func (m *Info) GetInstanceType() string {
return m.ec2Metadata.getInstanceType()
}
// GetRegion returns the region for the host
func (m *Info) GetRegion() string {
return m.ec2Metadata.getRegion()
}
// GetInstanceIP returns the IP address of the host
func (m *Info) GetInstanceIP() string {
return m.ec2Metadata.getInstanceIP()
}
// GetNumCores returns the number of cpu cores on the host
func (m *Info) GetNumCores() int64 {
return m.nodeCapacity.getNumCores()
}
// GetMemoryCapacity returns the total memory (in bytes) on the host
func (m *Info) GetMemoryCapacity() int64 {
return m.nodeCapacity.getMemoryCapacity()
}
// GetEBSVolumeID returns the ebs volume id corresponding to the given device name
func (m *Info) GetEBSVolumeID(devName string) string {
if m.ebsVolume != nil {
return m.ebsVolume.getEBSVolumeID(devName)
}
return ""
}
// GetClusterName returns the cluster name associated with the host
func (m *Info) GetClusterName() string {
if m.ec2Tags != nil {
return m.ec2Tags.getClusterName()
}
return ""
}
// GetInstanceIPReadyC returns the channel to show the status of host IP
func (m *Info) GetInstanceIPReadyC() chan bool {
return m.instanceIPReadyC
}
// GetAutoScalingGroupName returns the auto scaling group associated with the host
func (m *Info) GetAutoScalingGroupName() string {
if m.ec2Tags != nil {
return m.ec2Tags.getAutoScalingGroupName()
}
return ""
}
// ExtractEbsIDsUsedByKubernetes extracts the ebs volume id used by kubernetes cluster from host mount file
func (m *Info) ExtractEbsIDsUsedByKubernetes() map[string]string {
if m.ebsVolume != nil {
return m.ebsVolume.extractEbsIDsUsedByKubernetes()
}
return map[string]string{}
}
// Shutdown stops the host Info
func (m *Info) Shutdown() {
m.cancel()
}