-
Notifications
You must be signed in to change notification settings - Fork 22
/
vpc.go
169 lines (138 loc) · 3.07 KB
/
vpc.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
package collector
import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sync/errgroup"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
clientaws "github.com/giantswarm/aws-operator/client/aws"
)
const (
labelCIDR = "cidr"
labelID = "id"
labelStack = "stack_name"
labelState = "state"
)
const (
tagName = "Name"
tagStack = "aws:cloudformation:stack-name"
)
const (
subsystemVPC = "vpc"
)
var (
vpcsDesc *prometheus.Desc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemVPC, "info"),
"VPC information.",
[]string{
labelAccountID,
labelCIDR,
labelCluster,
labelID,
labelInstallation,
labelName,
labelOrganization,
labelStack,
labelState,
},
nil,
)
)
type VPCConfig struct {
Helper *helper
Logger micrologger.Logger
InstallationName string
}
type VPC struct {
helper *helper
logger micrologger.Logger
installationName string
}
func NewVPC(config VPCConfig) (*VPC, error) {
if config.Helper == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Helper must not be empty", config)
}
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config)
}
if config.InstallationName == "" {
return nil, microerror.Maskf(invalidConfigError, "%T.InstallationName must not be empty", config)
}
v := &VPC{
helper: config.Helper,
logger: config.Logger,
installationName: config.InstallationName,
}
return v, nil
}
func (v *VPC) Collect(ch chan<- prometheus.Metric) error {
awsClientsList, err := v.helper.GetAWSClients()
if err != nil {
return microerror.Mask(err)
}
var g errgroup.Group
for _, item := range awsClientsList {
awsClients := item
g.Go(func() error {
err := v.collectForAccount(ch, awsClients)
if err != nil {
return microerror.Mask(err)
}
return nil
})
}
err = g.Wait()
if err != nil {
return microerror.Mask(err)
}
return nil
}
func (v *VPC) Describe(ch chan<- *prometheus.Desc) error {
ch <- vpcsDesc
return nil
}
func (v *VPC) collectForAccount(ch chan<- prometheus.Metric, awsClients clientaws.Clients) error {
o, err := awsClients.EC2.DescribeVpcs(&ec2.DescribeVpcsInput{})
if err != nil {
return microerror.Mask(err)
}
accountID, err := v.helper.AWSAccountID(awsClients)
if err != nil {
return microerror.Mask(err)
}
for _, vpc := range o.Vpcs {
var cluster, installation, name, organization, stackName string
for _, tag := range vpc.Tags {
switch *tag.Key {
case tagCluster:
cluster = *tag.Value
case tagInstallation:
installation = *tag.Value
case tagName:
name = *tag.Value
case tagOrganization:
organization = *tag.Value
case tagStack:
stackName = *tag.Value
}
}
if installation != v.installationName {
continue
}
ch <- prometheus.MustNewConstMetric(
vpcsDesc,
prometheus.GaugeValue,
GaugeValue,
accountID,
*vpc.CidrBlock,
cluster,
*vpc.VpcId,
installation,
name,
organization,
stackName,
*vpc.State,
)
}
return nil
}