-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
iam.go
162 lines (142 loc) · 5.25 KB
/
iam.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
package manager
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
"github.com/kris-nova/logger"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/cfn/builder"
"github.com/weaveworks/eksctl/pkg/cfn/outputs"
iamoidc "github.com/weaveworks/eksctl/pkg/iam/oidc"
)
// makeIAMServiceAccountStackName generates the name of the iamserviceaccount stack identified by its name, isolated by the cluster this StackCollection operates on and 'addon' suffix
func (c *StackCollection) makeIAMServiceAccountStackName(namespace, name string) string {
return fmt.Sprintf("eksctl-%s-addon-iamserviceaccount-%s-%s", c.spec.Metadata.Name, namespace, name)
}
// createIAMServiceAccountTask creates the iamserviceaccount in CloudFormation
func (c *StackCollection) createIAMServiceAccountTask(ctx context.Context, errs chan error, spec *api.ClusterIAMServiceAccount, oidc *iamoidc.OpenIDConnectManager) error {
name := c.makeIAMServiceAccountStackName(spec.Namespace, spec.Name)
logger.Info("building iamserviceaccount stack %q", name)
stack := builder.NewIAMRoleResourceSetForServiceAccount(spec, oidc)
if err := stack.AddAllResources(); err != nil {
return err
}
if spec.Tags == nil {
spec.Tags = make(map[string]string)
}
spec.Tags[api.IAMServiceAccountNameTag] = spec.NameString()
if err := c.CreateStack(ctx, name, stack, spec.Tags, nil, errs); err != nil {
logger.Info("an error occurred creating the stack, to cleanup resources, run 'eksctl delete iamserviceaccount --region=%s --name=%s --namespace=%s'", c.spec.Metadata.Region, spec.Name, spec.Namespace)
return err
}
return nil
}
// DescribeIAMServiceAccountStacks calls ListStacks and filters out iamserviceaccounts
func (c *StackCollection) DescribeIAMServiceAccountStacks(ctx context.Context) ([]*Stack, error) {
stacks, err := c.ListStacks(ctx)
if err != nil {
return nil, err
}
iamServiceAccountStacks := []*Stack{}
for _, s := range stacks {
if s.StackStatus == types.StackStatusDeleteComplete {
continue
}
if GetIAMServiceAccountName(s) != "" {
iamServiceAccountStacks = append(iamServiceAccountStacks, s)
}
}
logger.Debug("iamserviceaccounts = %v", iamServiceAccountStacks)
return iamServiceAccountStacks, nil
}
// ListIAMServiceAccountStacks calls DescribeIAMServiceAccountStacks and returns only iamserviceaccount names
func (c *StackCollection) ListIAMServiceAccountStacks(ctx context.Context) ([]string, error) {
stacks, err := c.DescribeIAMServiceAccountStacks(ctx)
if err != nil {
return nil, err
}
names := []string{}
for _, s := range stacks {
names = append(names, GetIAMServiceAccountName(s))
}
return names, nil
}
// GetIAMServiceAccounts calls DescribeIAMServiceAccountStacks and return native iamserviceaccounts
func (c *StackCollection) GetIAMServiceAccounts(ctx context.Context) ([]*api.ClusterIAMServiceAccount, error) {
stacks, err := c.DescribeIAMServiceAccountStacks(ctx)
if err != nil {
return nil, err
}
results := []*api.ClusterIAMServiceAccount{}
for _, s := range stacks {
meta, err := api.ClusterIAMServiceAccountNameStringToClusterIAMMeta(GetIAMServiceAccountName(s))
if err != nil {
return nil, err
}
serviceAccount := &api.ClusterIAMServiceAccount{
ClusterIAMMeta: *meta,
Status: &api.ClusterIAMServiceAccountStatus{
StackName: s.StackName,
},
}
for _, t := range s.Tags {
serviceAccount.Status.Tags = make(map[string]string)
serviceAccount.Status.Tags[*t.Key] = *t.Value
}
for _, c := range s.Capabilities {
serviceAccount.Status.Capabilities = make([]string, 0)
serviceAccount.Status.Capabilities = append(serviceAccount.Status.Capabilities, string(c))
}
// TODO: we need to make it easier to fetch full definition of the object,
// namely: all label, full role definition; we can do that by caching
// the ClusterConfig time we make an update and a mechanism of validating
// whether it is up to date;
// otherwise we could extend this with tedious calls to each of the API,
// but it's not very feasible and it's best ot create a general solution
outputCollectors := outputs.NewCollectorSet(map[string]outputs.Collector{
outputs.IAMServiceAccountRoleName: func(v string) error {
serviceAccount.Status.RoleARN = &v
return nil
},
})
if err := outputCollectors.MustCollect(*s); err != nil {
return nil, err
}
results = append(results, serviceAccount)
}
return results, nil
}
// GetIAMServiceAccountName will return iamserviceaccount name based on tags
func GetIAMServiceAccountName(s *Stack) string {
for _, tag := range s.Tags {
if *tag.Key == api.IAMServiceAccountNameTag {
return *tag.Value
}
}
return ""
}
func (c *StackCollection) GetIAMAddonsStacks(ctx context.Context) ([]*Stack, error) {
stacks, err := c.ListStacks(ctx)
if err != nil {
return nil, err
}
iamAddonStacks := []*Stack{}
for _, s := range stacks {
if s.StackStatus == types.StackStatusDeleteComplete {
continue
}
if c.GetIAMAddonName(s) != "" {
iamAddonStacks = append(iamAddonStacks, s)
}
}
logger.Debug("iamserviceaccounts = %v", iamAddonStacks)
return iamAddonStacks, nil
}
func (*StackCollection) GetIAMAddonName(s *Stack) string {
for _, tag := range s.Tags {
if *tag.Key == api.AddonNameTag {
return *tag.Value
}
}
return ""
}