-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
tasks.go
252 lines (229 loc) · 6.44 KB
/
tasks.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package addon
import (
"context"
"fmt"
"slices"
"strings"
"time"
"k8s.io/client-go/kubernetes"
cfntypes "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/eks"
iamoidc "github.com/weaveworks/eksctl/pkg/iam/oidc"
"github.com/weaveworks/eksctl/pkg/utils/tasks"
)
var knownAddons = map[string]struct {
IsDefault bool
CreateBeforeNodeGroup bool
}{
api.VPCCNIAddon: {
IsDefault: true,
CreateBeforeNodeGroup: true,
},
api.KubeProxyAddon: {
IsDefault: true,
CreateBeforeNodeGroup: true,
},
api.CoreDNSAddon: {
IsDefault: true,
CreateBeforeNodeGroup: true,
},
api.PodIdentityAgentAddon: {
CreateBeforeNodeGroup: true,
},
api.AWSEBSCSIDriverAddon: {},
api.AWSEFSCSIDriverAddon: {},
}
func CreateAddonTasks(ctx context.Context, cfg *api.ClusterConfig, clusterProvider *eks.ClusterProvider, iamRoleCreator IAMRoleCreator, forceAll bool, timeout time.Duration) (*tasks.TaskTree, *tasks.TaskTree, *tasks.GenericTask, []string) {
var addons []*api.Addon
var autoDefaultAddonNames []string
if !cfg.AddonsConfig.DisableDefaultAddons {
addons = make([]*api.Addon, len(cfg.Addons))
copy(addons, cfg.Addons)
for addonName, addonInfo := range knownAddons {
if addonInfo.IsDefault && !slices.ContainsFunc(cfg.Addons, func(a *api.Addon) bool {
return strings.EqualFold(a.Name, addonName)
}) {
addons = append(addons, &api.Addon{Name: addonName})
autoDefaultAddonNames = append(autoDefaultAddonNames, addonName)
}
}
} else {
addons = cfg.Addons
}
var (
preAddons []*api.Addon
postAddons []*api.Addon
)
var vpcCNIAddon *api.Addon
for _, addon := range addons {
addonInfo, ok := knownAddons[addon.Name]
if ok && addonInfo.CreateBeforeNodeGroup {
preAddons = append(preAddons, addon)
} else {
postAddons = append(postAddons, addon)
}
if addon.Name == api.VPCCNIAddon {
vpcCNIAddon = addon
}
}
preTasks := &tasks.TaskTree{Parallel: false}
postTasks := &tasks.TaskTree{Parallel: false}
makeAddonTask := func(addons []*api.Addon, wait bool) *createAddonTask {
return &createAddonTask{
info: "create addons",
addons: addons,
ctx: ctx,
cfg: cfg,
clusterProvider: clusterProvider,
forceAll: forceAll,
timeout: timeout,
wait: wait,
iamRoleCreator: iamRoleCreator,
}
}
if len(preAddons) > 0 {
preTasks.Append(makeAddonTask(preAddons, false))
}
if len(postAddons) > 0 {
postTasks.Append(makeAddonTask(postAddons, cfg.HasNodes()))
}
var updateVPCCNI *tasks.GenericTask
if vpcCNIAddon != nil && api.IsEnabled(cfg.IAM.WithOIDC) {
updateVPCCNI = &tasks.GenericTask{
Description: "update VPC CNI to use IRSA if required",
Doer: func() error {
addonManager, err := createAddonManager(ctx, clusterProvider, cfg)
if err != nil {
return err
}
return addonManager.Update(ctx, vpcCNIAddon, nil, clusterProvider.AWSProvider.WaitTimeout())
},
}
}
return preTasks, postTasks, updateVPCCNI, autoDefaultAddonNames
}
type createAddonTask struct {
// Context should ideally be passed to methods and not be a struct field,
// but the current task code requires it to be passed this way.
ctx context.Context
info string
cfg *api.ClusterConfig
clusterProvider *eks.ClusterProvider
addons []*api.Addon
forceAll, wait bool
timeout time.Duration
iamRoleCreator IAMRoleCreator
}
func (t *createAddonTask) Describe() string { return t.info }
func (t *createAddonTask) Do(errorCh chan error) error {
addonManager, err := createAddonManager(t.ctx, t.clusterProvider, t.cfg)
if err != nil {
return err
}
addonManager.DisableAWSNodePatch = true
// always install EKS Pod Identity Agent Addon first, if present,
// as other addons might require IAM permissions
for _, a := range t.addons {
if a.CanonicalName() != api.PodIdentityAgentAddon {
continue
}
if t.forceAll {
a.Force = true
}
if !t.wait {
t.timeout = 0
}
err := addonManager.Create(t.ctx, a, t.iamRoleCreator, t.timeout)
if err != nil {
go func() {
errorCh <- err
}()
return err
}
}
for _, a := range t.addons {
if a.CanonicalName() == api.PodIdentityAgentAddon {
continue
}
if t.forceAll {
a.Force = true
}
if !t.wait {
t.timeout = 0
}
err := addonManager.Create(t.ctx, a, t.iamRoleCreator, t.timeout)
if err != nil {
go func() {
errorCh <- err
}()
return err
}
}
go func() {
errorCh <- nil
}()
return nil
}
func createAddonManager(ctx context.Context, clusterProvider *eks.ClusterProvider, cfg *api.ClusterConfig) (*Manager, error) {
var (
oidc *iamoidc.OpenIDConnectManager
oidcProviderExists bool
)
if api.IsEnabled(cfg.IAM.WithOIDC) {
var err error
oidc, err = clusterProvider.NewOpenIDConnectManager(ctx, cfg)
if err != nil {
return nil, err
}
oidcProviderExists, err = oidc.CheckProviderExists(ctx)
if err != nil {
return nil, err
}
}
stackManager := clusterProvider.NewStackManager(cfg)
return New(cfg, clusterProvider.AWSProvider.EKS(), stackManager, oidcProviderExists, oidc, func() (kubernetes.Interface, error) {
return clusterProvider.NewStdClientSet(cfg)
})
}
type deleteAddonIAMTask struct {
ctx context.Context
info string
stack *cfntypes.Stack
stackManager StackManager
wait bool
}
func (t *deleteAddonIAMTask) Describe() string { return t.info }
func (t *deleteAddonIAMTask) Do(errorCh chan error) error {
errMsg := fmt.Sprintf("deleting addon IAM %q", *t.stack.StackName)
if t.wait {
if err := t.stackManager.DeleteStackBySpecSync(t.ctx, t.stack, errorCh); err != nil {
return fmt.Errorf("%s: %w", errMsg, err)
}
return nil
}
defer close(errorCh)
if _, err := t.stackManager.DeleteStackBySpec(t.ctx, t.stack); err != nil {
return fmt.Errorf("%s: %w", errMsg, err)
}
return nil
}
func runAllTasks(taskTree *tasks.TaskTree) error {
logger.Debug(taskTree.Describe())
if errs := taskTree.DoAllSync(); len(errs) > 0 {
var allErrs []string
for _, err := range errs {
allErrs = append(allErrs, err.Error())
}
return fmt.Errorf(strings.Join(allErrs, "\n"))
}
completedAction := func() string {
if taskTree.PlanMode {
return "skipped"
}
return "completed successfully"
}
logger.Debug("all tasks were %s", completedAction())
return nil
}