/
cloudformation.go
175 lines (144 loc) · 3.88 KB
/
cloudformation.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
package collector
import (
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sync/errgroup"
clientaws "github.com/giantswarm/aws-operator/client/aws"
"github.com/giantswarm/aws-operator/service/controller/key"
)
const (
labelStackType = "stack_type"
)
const (
// Second part of the metric name, right after namespace.
subsystemCloudFormation = "cloudformation"
)
var (
cloudFormationStackDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemCloudFormation, "info"),
"Metrics for Cloud Formation Stack statuses.",
[]string{
labelAccountID,
labelCluster,
labelID,
labelInstallation,
labelName,
labelOrganization,
labelStackType,
labelState,
},
nil,
)
)
// Configuration struct.
type CloudFormationConfig struct {
Helper *helper
Logger micrologger.Logger
InstallationName string
}
// Main struct for this collector.
type CloudFormation struct {
helper *helper
logger micrologger.Logger
installationName string
}
// Creates a new CloudFormation metrics collector.
func NewCloudFormation(config CloudFormationConfig) (*CloudFormation, 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)
}
cf := &CloudFormation{
helper: config.Helper,
logger: config.Logger,
installationName: config.InstallationName,
}
return cf, nil
}
// Collect is the main metrics collection function.
func (cf *CloudFormation) Collect(ch chan<- prometheus.Metric) error {
awsClientsList, err := cf.helper.GetAWSClients()
if err != nil {
return microerror.Mask(err)
}
var g errgroup.Group
for _, item := range awsClientsList {
awsClients := item
g.Go(func() error {
err := cf.collectForAccount(ch, awsClients)
if err != nil {
return microerror.Mask(err)
}
return nil
})
}
err = g.Wait()
if err != nil {
return microerror.Mask(err)
}
return nil
}
// Describe emits the description for the metrics collected here.
func (cf *CloudFormation) Describe(ch chan<- *prometheus.Desc) error {
ch <- cloudFormationStackDesc
return nil
}
// collectForAccount collects metrics for one AWS account.
func (cf *CloudFormation) collectForAccount(ch chan<- prometheus.Metric, awsClients clientaws.Clients) error {
o, err := awsClients.CloudFormation.DescribeStacks(&cloudformation.DescribeStacksInput{})
if err != nil {
return microerror.Mask(err)
}
accountID, err := cf.helper.AWSAccountID(awsClients)
if err != nil {
return microerror.Mask(err)
}
for _, stack := range o.Stacks {
var cluster, installation, name, organization, stackType string
for _, tag := range stack.Tags {
switch *tag.Key {
case tagCluster:
cluster = *tag.Value
case key.TagInstallation:
installation = *tag.Value
case tagName:
name = *tag.Value
case tagOrganization:
organization = *tag.Value
case tagStack:
stackType = *tag.Value
}
}
if installation != cf.installationName {
continue
}
if !isOwnStack(stackType) {
continue
}
ch <- prometheus.MustNewConstMetric(
cloudFormationStackDesc,
prometheus.GaugeValue,
GaugeValue,
accountID,
cluster,
*stack.StackId,
installation,
name,
organization,
stackType,
*stack.StackStatus,
)
}
return nil
}
// Check if the input stack is our own by checking the name of the stack type
func isOwnStack(StackType string) bool {
return StackType == key.StackTCCP || StackType == key.StackTCCPF || StackType == key.StackTCCPI || StackType == key.StackTCNP || StackType == key.StackTCNPF
}