-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
tasks.go
325 lines (290 loc) · 10.3 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package eks
import (
"context"
"fmt"
"time"
"github.com/weaveworks/eksctl/pkg/actions/irsa"
"github.com/kris-nova/logger"
"github.com/pkg/errors"
"github.com/weaveworks/eksctl/pkg/cfn/manager"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"github.com/weaveworks/eksctl/pkg/addons"
"github.com/weaveworks/eksctl/pkg/fargate"
iamoidc "github.com/weaveworks/eksctl/pkg/iam/oidc"
"github.com/weaveworks/eksctl/pkg/utils"
"github.com/weaveworks/eksctl/pkg/utils/tasks"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/kubernetes"
)
type clusterConfigTask struct {
info string
spec *api.ClusterConfig
call func(*api.ClusterConfig) error
}
func (t *clusterConfigTask) Describe() string { return t.info }
func (t *clusterConfigTask) Do(errs chan error) error {
err := t.call(t.spec)
close(errs)
return err
}
// VPCControllerTask represents a task to install the VPC controller
type VPCControllerTask struct {
Info string
ClusterProvider *ClusterProvider
ClusterConfig *api.ClusterConfig
PlanMode bool
}
// Describe implements Task
func (v *VPCControllerTask) Describe() string { return v.Info }
// Do implements Task
func (v *VPCControllerTask) Do(errCh chan error) error {
defer close(errCh)
rawClient, err := v.ClusterProvider.NewRawClient(v.ClusterConfig)
if err != nil {
return err
}
oidc, err := v.ClusterProvider.NewOpenIDConnectManager(v.ClusterConfig)
if err != nil {
return err
}
stackCollection := manager.NewStackCollection(v.ClusterProvider.Provider, v.ClusterConfig)
clientSet, err := v.ClusterProvider.NewStdClientSet(v.ClusterConfig)
if err != nil {
return err
}
irsaManager := irsa.New(v.ClusterConfig.Metadata.Name, stackCollection, oidc, clientSet)
irsa := addons.NewIRSAHelper(oidc, stackCollection, irsaManager, v.ClusterConfig.Metadata.Name)
// TODO PlanMode doesn't work as intended
vpcController := addons.NewVPCController(rawClient, irsa, v.ClusterConfig.Status, v.ClusterProvider.Provider.Region(), v.PlanMode)
if err := vpcController.Deploy(); err != nil {
return errors.Wrap(err, "error installing VPC controller")
}
return nil
}
type devicePluginTask struct {
info string
clusterProvider *ClusterProvider
spec *api.ClusterConfig
mkPlugin addons.MkDevicePlugin
}
func (n *devicePluginTask) Describe() string { return n.info }
func (n *devicePluginTask) Do(errCh chan error) error {
defer close(errCh)
rawClient, err := n.clusterProvider.NewRawClient(n.spec)
if err != nil {
return err
}
neuronDevicePlugin := n.mkPlugin(rawClient, n.clusterProvider.Provider.Region(), false)
if err := neuronDevicePlugin.Deploy(); err != nil {
return errors.Wrap(err, "error installing Neuron device plugin")
}
return nil
}
func newNvidiaDevicePluginTask(
clusterProvider *ClusterProvider,
spec *api.ClusterConfig,
) tasks.Task {
t := devicePluginTask{
info: "install Nvidia device plugin",
clusterProvider: clusterProvider,
spec: spec,
mkPlugin: addons.NewNvidiaDevicePlugin,
}
return &t
}
func newNeuronDevicePluginTask(
clusterProvider *ClusterProvider,
spec *api.ClusterConfig,
) tasks.Task {
t := devicePluginTask{
info: "install Neuron device plugin",
clusterProvider: clusterProvider,
spec: spec,
mkPlugin: addons.NewNeuronDevicePlugin,
}
return &t
}
type restartDaemonsetTask struct {
name string
namespace string
clusterProvider *ClusterProvider
spec *api.ClusterConfig
}
func (t *restartDaemonsetTask) Describe() string {
return fmt.Sprintf(`restart daemonset "%s/%s"`, t.namespace, t.name)
}
func (t *restartDaemonsetTask) Do(errCh chan error) error {
defer close(errCh)
clientSet, err := t.clusterProvider.NewStdClientSet(t.spec)
if err != nil {
return err
}
ds := clientSet.AppsV1().DaemonSets(t.namespace)
dep, err := ds.Get(context.TODO(), t.name, metav1.GetOptions{})
if err != nil {
return err
}
if dep.Spec.Template.Annotations == nil {
dep.Spec.Template.Annotations = make(map[string]string)
}
dep.Spec.Template.Annotations["eksctl.io/restartedAt"] = time.Now().Format(time.RFC3339)
bytes, err := runtime.Encode(unstructured.UnstructuredJSONScheme, dep)
if err != nil {
return errors.Wrapf(err, "failed to marshal %q deployment", t.name)
}
if _, err := ds.Patch(context.TODO(), t.name, types.MergePatchType, bytes, metav1.PatchOptions{}); err != nil {
return errors.Wrap(err, "failed to patch deployment")
}
logger.Info(`daemonset "%s/%s" restarted`, t.namespace, t.name)
return nil
}
// CreateExtraClusterConfigTasks returns all tasks for updating cluster configuration not depending on the control plane availability
func (c *ClusterProvider) CreateExtraClusterConfigTasks(cfg *api.ClusterConfig, installVPCController bool) *tasks.TaskTree {
newTasks := &tasks.TaskTree{
Parallel: false,
IsSubTask: true,
}
if len(cfg.Metadata.Tags) > 0 {
newTasks.Append(&clusterConfigTask{
info: "tag cluster",
spec: cfg,
call: c.UpdateClusterTags,
})
}
if !cfg.HasClusterCloudWatchLogging() {
logger.Info("CloudWatch logging will not be enabled for cluster %q in %q", cfg.Metadata.Name, cfg.Metadata.Region)
logger.Info("you can enable it with 'eksctl utils update-cluster-logging --enable-types={SPECIFY-YOUR-LOG-TYPES-HERE (e.g. all)} --region=%s --cluster=%s'", cfg.Metadata.Region, cfg.Metadata.Name)
} else {
newTasks.Append(&clusterConfigTask{
info: "update CloudWatch logging configuration",
spec: cfg,
call: c.UpdateClusterConfigForLogging,
})
}
c.maybeAppendTasksForEndpointAccessUpdates(cfg, newTasks)
if len(cfg.VPC.PublicAccessCIDRs) > 0 {
newTasks.Append(&clusterConfigTask{
info: "update public access CIDRs",
spec: cfg,
call: c.UpdatePublicAccessCIDRs,
})
}
if cfg.IsFargateEnabled() {
manager := fargate.NewFromProvider(cfg.Metadata.Name, c.Provider)
newTasks.Append(&fargateProfilesTask{
info: "create fargate profiles",
spec: cfg,
clusterProvider: c,
manager: &manager,
})
}
if api.IsEnabled(cfg.IAM.WithOIDC) {
c.appendCreateTasksForIAMServiceAccounts(cfg, newTasks)
}
if installVPCController {
newTasks.Append(&VPCControllerTask{
Info: "install Windows VPC controller",
ClusterConfig: cfg,
ClusterProvider: c,
})
}
return newTasks
}
// ClusterTasksForNodeGroups returns all tasks dependent on node groups
func (c *ClusterProvider) ClusterTasksForNodeGroups(cfg *api.ClusterConfig, installNeuronDevicePluginParam, installNvidiaDevicePluginParam bool) *tasks.TaskTree {
tasks := &tasks.TaskTree{
Parallel: false,
IsSubTask: false,
}
var haveNeuronInstanceType bool
for _, ng := range cfg.NodeGroups {
haveNeuronInstanceType = haveNeuronInstanceType || api.HasInstanceType(ng, utils.IsInferentiaInstanceType)
}
var haveNvidiaInstanceType bool
for _, ng := range cfg.NodeGroups {
haveNvidiaInstanceType = haveNvidiaInstanceType || api.HasInstanceType(ng, utils.IsGPUInstanceType)
}
if haveNeuronInstanceType && installNeuronDevicePluginParam {
tasks.Append(newNeuronDevicePluginTask(c, cfg))
}
if haveNvidiaInstanceType && installNvidiaDevicePluginParam {
tasks.Append(newNvidiaDevicePluginTask(c, cfg))
}
return tasks
}
func (c *ClusterProvider) appendCreateTasksForIAMServiceAccounts(cfg *api.ClusterConfig, tasks *tasks.TaskTree) {
// we don't have all the information to construct full iamoidc.OpenIDConnectManager now,
// instead we just create a reference that gets updated when first task runs, and gets
// used by this would be more elegant if it was all done via CloudFormation and we didn't
// have to put wires across all the things like this; this whole function is needed because
// we cannot manage certain EKS features with CloudFormation
oidcPlaceholder := &iamoidc.OpenIDConnectManager{}
tasks.Append(&clusterConfigTask{
info: "associate IAM OIDC provider",
spec: cfg,
call: func(cfg *api.ClusterConfig) error {
if err := c.RefreshClusterStatus(cfg); err != nil {
return err
}
oidc, err := c.NewOpenIDConnectManager(cfg)
if err != nil {
return err
}
if err := oidc.CreateProvider(); err != nil {
return err
}
*oidcPlaceholder = *oidc
// Make sure control plane is reachable
clientSet, err := c.NewStdClientSet(cfg)
if err != nil {
return errors.Wrap(err, "failed to get ClientSet")
}
if err := c.WaitForControlPlane(cfg.Metadata, clientSet); err != nil {
return errors.Wrap(err, "failed to wait for control plane")
}
return nil
},
})
clientSet := &kubernetes.CallbackClientSet{
Callback: func() (kubernetes.Interface, error) {
return c.NewStdClientSet(cfg)
},
}
// as this is non-CloudFormation context, we need to construct a new stackManager,
// given a clientSet getter and OpenIDConnectManager reference we can build out
// the list of tasks for each of the service accounts that need to be created
newTasks := c.NewStackManager(cfg).NewTasksToCreateIAMServiceAccounts(
api.IAMServiceAccountsWithImplicitServiceAccounts(cfg),
oidcPlaceholder,
clientSet,
)
newTasks.IsSubTask = true
tasks.Append(newTasks)
tasks.Append(&restartDaemonsetTask{
namespace: "kube-system",
name: "aws-node",
clusterProvider: c,
spec: cfg,
})
}
func (c *ClusterProvider) maybeAppendTasksForEndpointAccessUpdates(cfg *api.ClusterConfig, tasks *tasks.TaskTree) {
// if a cluster config doesn't have the default api endpoint access, append a new task
// so that we update the cluster with the new access configuration. This is a
// non-CloudFormation context, so we create a task to send it through the EKS API.
// A caveat is that sending the default endpoint parameters for a cluster as an update will
// return an error from the EKS API, so we must check for this before sending the request.
if cfg.HasClusterEndpointAccess() && api.EndpointsEqual(*cfg.VPC.ClusterEndpoints, *api.ClusterEndpointAccessDefaults()) {
// No tasks to append here as there's no updates to make.
logger.Info(cfg.DefaultEndpointsMsg())
} else {
logger.Info(cfg.CustomEndpointsMsg())
tasks.Append(&clusterConfigTask{
info: "update cluster VPC endpoint access configuration",
spec: cfg,
call: c.UpdateClusterConfigForEndpoints,
})
}
}