-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
create_tasks.go
184 lines (164 loc) · 6.68 KB
/
create_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
package manager
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/kris-nova/logger"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"
"github.com/weaveworks/eksctl/pkg/actions/accessentry"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/cfn/builder"
iamoidc "github.com/weaveworks/eksctl/pkg/iam/oidc"
"github.com/weaveworks/eksctl/pkg/kubernetes"
"github.com/weaveworks/eksctl/pkg/nodebootstrap"
"github.com/weaveworks/eksctl/pkg/utils/tasks"
"github.com/weaveworks/eksctl/pkg/vpc"
)
const (
managedByKubernetesLabelKey = "app.kubernetes.io/managed-by"
managedByKubernetesLabelValue = "eksctl"
)
// NewTasksToCreateCluster defines all tasks required to create a cluster along
// with some nodegroups; see CreateAllNodeGroups for how onlyNodeGroupSubset works.
func (c *StackCollection) NewTasksToCreateCluster(ctx context.Context, nodeGroups []*api.NodeGroup,
managedNodeGroups []*api.ManagedNodeGroup, accessConfig *api.AccessConfig, accessEntryCreator accessentry.CreatorInterface, postClusterCreationTasks ...tasks.Task) *tasks.TaskTree {
taskTree := tasks.TaskTree{Parallel: false}
taskTree.Append(&createClusterTask{
info: fmt.Sprintf("create cluster control plane %q", c.spec.Metadata.Name),
stackCollection: c,
supportsManagedNodes: true,
ctx: ctx,
})
if len(accessConfig.AccessEntries) > 0 {
taskTree.Append(accessEntryCreator.CreateTasks(ctx, accessConfig.AccessEntries))
}
appendNodeGroupTasksTo := func(taskTree *tasks.TaskTree) {
vpcImporter := vpc.NewStackConfigImporter(c.MakeClusterStackName())
nodeGroupTasks := &tasks.TaskTree{
Parallel: true,
IsSubTask: true,
}
disableAccessEntryCreation := accessConfig.AuthenticationMode == ekstypes.AuthenticationModeConfigMap
if unmanagedNodeGroupTasks := c.NewUnmanagedNodeGroupTask(ctx, nodeGroups, false, false, disableAccessEntryCreation, vpcImporter); unmanagedNodeGroupTasks.Len() > 0 {
unmanagedNodeGroupTasks.IsSubTask = true
nodeGroupTasks.Append(unmanagedNodeGroupTasks)
}
if managedNodeGroupTasks := c.NewManagedNodeGroupTask(ctx, managedNodeGroups, false, vpcImporter); managedNodeGroupTasks.Len() > 0 {
managedNodeGroupTasks.IsSubTask = true
nodeGroupTasks.Append(managedNodeGroupTasks)
}
if nodeGroupTasks.Len() > 0 {
taskTree.Append(nodeGroupTasks)
}
}
if len(postClusterCreationTasks) > 0 {
postClusterCreationTaskTree := &tasks.TaskTree{
Parallel: false,
IsSubTask: true,
}
postClusterCreationTaskTree.Append(postClusterCreationTasks...)
appendNodeGroupTasksTo(postClusterCreationTaskTree)
taskTree.Append(postClusterCreationTaskTree)
} else {
appendNodeGroupTasksTo(&taskTree)
}
return &taskTree
}
// NewUnmanagedNodeGroupTask returns tasks for creating self-managed nodegroups.
func (c *StackCollection) NewUnmanagedNodeGroupTask(ctx context.Context, nodeGroups []*api.NodeGroup, forceAddCNIPolicy, skipEgressRules, disableAccessEntryCreation bool, vpcImporter vpc.Importer) *tasks.TaskTree {
task := &UnmanagedNodeGroupTask{
ClusterConfig: c.spec,
NodeGroups: nodeGroups,
CreateNodeGroupResourceSet: func(options builder.NodeGroupOptions) NodeGroupResourceSet {
return builder.NewNodeGroupResourceSet(c.ec2API, c.iamAPI, options)
},
NewBootstrapper: func(clusterConfig *api.ClusterConfig, ng *api.NodeGroup) (nodebootstrap.Bootstrapper, error) {
return nodebootstrap.NewBootstrapper(clusterConfig, ng)
},
EKSAPI: c.eksAPI,
StackManager: c,
}
return task.Create(ctx, CreateNodeGroupOptions{
ForceAddCNIPolicy: forceAddCNIPolicy,
SkipEgressRules: skipEgressRules,
DisableAccessEntryCreation: disableAccessEntryCreation,
VPCImporter: vpcImporter,
})
}
// NewManagedNodeGroupTask defines tasks required to create managed nodegroups
func (c *StackCollection) NewManagedNodeGroupTask(ctx context.Context, nodeGroups []*api.ManagedNodeGroup, forceAddCNIPolicy bool, vpcImporter vpc.Importer) *tasks.TaskTree {
taskTree := &tasks.TaskTree{Parallel: true}
for _, ng := range nodeGroups {
// Disable parallelisation if any tags propagation is done
// since nodegroup must be created to propagate tags to its ASGs.
subTask := &tasks.TaskTree{
Parallel: false,
IsSubTask: true,
}
subTask.Append(&managedNodeGroupTask{
stackCollection: c,
nodeGroup: ng,
forceAddCNIPolicy: forceAddCNIPolicy,
vpcImporter: vpcImporter,
info: fmt.Sprintf("create managed nodegroup %q", ng.Name),
ctx: ctx,
})
if api.IsEnabled(ng.PropagateASGTags) {
subTask.Append(&managedNodeGroupTagsToASGPropagationTask{
stackCollection: c,
nodeGroup: ng,
info: fmt.Sprintf("propagate tags to ASG for managed nodegroup %q", ng.Name),
ctx: ctx,
})
}
taskTree.Append(subTask)
}
return taskTree
}
// NewTasksToCreateIAMServiceAccounts defines tasks required to create all of the IAM ServiceAccounts
func (c *StackCollection) NewTasksToCreateIAMServiceAccounts(serviceAccounts []*api.ClusterIAMServiceAccount, oidc *iamoidc.OpenIDConnectManager, clientSetGetter kubernetes.ClientSetGetter) *tasks.TaskTree {
taskTree := &tasks.TaskTree{Parallel: true}
for i := range serviceAccounts {
sa := serviceAccounts[i]
saTasks := &tasks.TaskTree{
Parallel: false,
IsSubTask: true,
}
if sa.AttachRoleARN == "" {
saTasks.Append(&taskWithClusterIAMServiceAccountSpec{
info: fmt.Sprintf("create IAM role for serviceaccount %q", sa.NameString()),
stackCollection: c,
serviceAccount: sa,
oidc: oidc,
})
} else {
logger.Debug("attachRoleARN was provided, skipping role creation")
sa.Status = &api.ClusterIAMServiceAccountStatus{
RoleARN: &sa.AttachRoleARN,
}
}
if sa.Labels == nil {
sa.Labels = make(map[string]string)
}
sa.Labels[managedByKubernetesLabelKey] = managedByKubernetesLabelValue
if !api.IsEnabled(sa.RoleOnly) {
saTasks.Append(&kubernetesTask{
info: fmt.Sprintf("create serviceaccount %q", sa.NameString()),
kubernetes: clientSetGetter,
objectMeta: sa.ClusterIAMMeta.AsObjectMeta(),
call: func(clientSet kubernetes.Interface, objectMeta v1.ObjectMeta) error {
sa.SetAnnotations()
objectMeta.SetAnnotations(sa.AsObjectMeta().Annotations)
objectMeta.SetLabels(sa.AsObjectMeta().Labels)
if err := kubernetes.MaybeCreateServiceAccountOrUpdateMetadata(clientSet, objectMeta); err != nil {
return errors.Wrapf(err, "failed to create service account %s/%s", objectMeta.GetNamespace(), objectMeta.GetName())
}
return nil
},
})
}
taskTree.Append(saTasks)
}
return taskTree
}