-
Notifications
You must be signed in to change notification settings - Fork 22
/
helper.go
186 lines (152 loc) · 4.55 KB
/
helper.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
181
182
183
184
185
186
package collector
import (
"fmt"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset"
clientaws "github.com/giantswarm/aws-operator/client/aws"
"github.com/giantswarm/aws-operator/service/accountid"
"github.com/giantswarm/aws-operator/service/credential"
)
type helperConfig struct {
CMAClient clientset.Interface
K8sClient kubernetes.Interface
Logger micrologger.Logger
AWSConfig clientaws.Config
}
type helper struct {
cmaClient clientset.Interface
k8sClient kubernetes.Interface
logger micrologger.Logger
awsConfig clientaws.Config
}
func newHelper(config helperConfig) (*helper, error) {
if config.CMAClient == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.CMAClient must not be empty", config)
}
if config.K8sClient == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.K8sClient must not be empty", config)
}
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config)
}
var emptyAWSConfig clientaws.Config
if config.AWSConfig == emptyAWSConfig {
return nil, microerror.Maskf(invalidConfigError, "%T.AWSConfig must not be empty", config)
}
h := &helper{
cmaClient: config.CMAClient,
k8sClient: config.K8sClient,
logger: config.Logger,
awsConfig: config.AWSConfig,
}
return h, nil
}
// GetARNs list all unique aws IAM ARN from credential secret.
func (h *helper) GetARNs() ([]string, error) {
var arns []string
clusterCRList, err := h.cmaClient.ClusterV1alpha1().Clusters(corev1.NamespaceAll).List(v1.ListOptions{})
if err != nil {
return nil, microerror.Mask(err)
}
// Get unique ARNs.
arnsMap := make(map[string]bool)
for _, clusterCR := range clusterCRList.Items {
arn, err := credential.GetARN(h.k8sClient, clusterCR)
// Collect as many ARNs as possible in order to provide most metrics.
// Ignore old cluster which do not have credential.
if credential.IsCredentialNameEmptyError(err) {
continue
} else if credential.IsCredentialNamespaceEmptyError(err) {
continue
} else if err != nil {
return nil, microerror.Mask(err)
}
arnsMap[arn] = true
}
// Ensure we check the default guest account for old cluster not having credential.
arn, err := credential.GetDefaultARN(h.k8sClient)
if err != nil {
return nil, microerror.Mask(err)
}
arnsMap[arn] = true
for arn := range arnsMap {
arns = append(arns, arn)
}
return arns, nil
}
// GetAWSClients return a list of aws clients for every guest cluster account plus
// the host cluster account.
func (h *helper) GetAWSClients() ([]clientaws.Clients, error) {
var (
clients []clientaws.Clients
clientsMap = make(map[string]clientaws.Clients)
)
arns, err := h.GetARNs()
if err != nil {
return nil, microerror.Mask(err)
}
// addClientFunc add awsClients to clients using account id as key to guaranatee uniqueness.
addClientFunc := func(awsClients clientaws.Clients, clients map[string]clientaws.Clients) error {
accountID, err := h.AWSAccountID(awsClients)
if err != nil {
return microerror.Mask(err)
}
_, ok := clients[accountID]
if !ok {
clients[accountID] = awsClients
}
return nil
}
// Control plane account.
awsClients, err := clientaws.NewClients(h.awsConfig)
if err != nil {
return nil, microerror.Mask(err)
}
err = addClientFunc(awsClients, clientsMap)
if err != nil {
return nil, microerror.Mask(err)
}
// Tenant cluster accounts.
for _, arn := range arns {
awsConfig := h.awsConfig
awsConfig.RoleARN = arn
awsClients, err := clientaws.NewClients(awsConfig)
if err != nil {
return nil, microerror.Mask(err)
}
err = addClientFunc(awsClients, clientsMap)
if err != nil {
return nil, microerror.Mask(err)
}
}
// Convert map to slice.
for accountID, client := range clientsMap {
clients = append(clients, client)
h.logger.Log("level", "debug", "message", fmt.Sprintf("collecting metrics in account: %s", accountID))
}
return clients, nil
}
// AWSAccountID return the AWS account ID.
func (h *helper) AWSAccountID(awsClients clientaws.Clients) (string, error) {
var err error
var accountIDService *accountid.AccountID
{
c := accountid.Config{
Logger: h.logger,
STS: awsClients.STS,
}
accountIDService, err = accountid.New(c)
if err != nil {
return "", microerror.Mask(err)
}
}
accountID, err := accountIDService.Lookup()
if err != nil {
return "", microerror.Mask(err)
}
return accountID, nil
}