-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathcluster.go
409 lines (339 loc) · 14.5 KB
/
cluster.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package create
import (
"fmt"
"strings"
"github.com/kris-nova/logger"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/weaveworks/eksctl/pkg/eks"
"github.com/weaveworks/eksctl/pkg/ssh"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/authconfigmap"
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils"
"github.com/weaveworks/eksctl/pkg/kops"
"github.com/weaveworks/eksctl/pkg/printers"
"github.com/weaveworks/eksctl/pkg/utils"
"github.com/weaveworks/eksctl/pkg/utils/kubeconfig"
"github.com/weaveworks/eksctl/pkg/utils/names"
"github.com/weaveworks/eksctl/pkg/vpc"
)
func createClusterCmd(cmd *cmdutils.Cmd) {
cfg := api.NewClusterConfig()
ng := api.NewNodeGroup()
cmd.ClusterConfig = cfg
params := &cmdutils.CreateClusterCmdParams{}
cmd.SetDescription("cluster", "Create a cluster", "")
cmd.CobraCommand.RunE = func(_ *cobra.Command, args []string) error {
cmd.NameArg = cmdutils.GetNameArg(args)
return doCreateCluster(cmd, ng, params)
}
exampleClusterName := names.ForCluster("", "")
exampleNodeGroupName := names.ForNodeGroup("", "")
cmd.FlagSetGroup.InFlagSet("General", func(fs *pflag.FlagSet) {
fs.StringVarP(&cfg.Metadata.Name, "name", "n", "", fmt.Sprintf("EKS cluster name (generated if unspecified, e.g. %q)", exampleClusterName))
fs.StringToStringVarP(&cfg.Metadata.Tags, "tags", "", map[string]string{}, `A list of KV pairs used to tag the AWS resources (e.g. "Owner=John Doe,Team=Some Team")`)
cmdutils.AddRegionFlag(fs, cmd.ProviderConfig)
fs.StringSliceVar(¶ms.AvailabilityZones, "zones", nil, "(auto-select if unspecified)")
cmdutils.AddVersionFlag(fs, cfg.Metadata, "")
cmdutils.AddConfigFileFlag(fs, &cmd.ClusterConfigFile)
cmdutils.AddTimeoutFlag(fs, &cmd.ProviderConfig.WaitTimeout)
fs.BoolVarP(¶ms.InstallWindowsVPCController, "install-vpc-controllers", "", false, "Install VPC controller that's required for Windows workloads")
fs.BoolVarP(¶ms.Managed, "managed", "", false, "Create EKS-managed nodegroup")
fs.BoolVarP(¶ms.Fargate, "fargate", "", false, "Create a Fargate profile scheduling pods in the default and kube-system namespaces onto Fargate")
})
cmd.FlagSetGroup.InFlagSet("Initial nodegroup", func(fs *pflag.FlagSet) {
fs.StringVar(&ng.Name, "nodegroup-name", "", fmt.Sprintf("name of the nodegroup (generated if unspecified, e.g. %q)", exampleNodeGroupName))
fs.BoolVar(¶ms.WithoutNodeGroup, "without-nodegroup", false, "if set, initial nodegroup will not be created")
cmdutils.AddCommonCreateNodeGroupFlags(fs, cmd, ng)
})
cmd.FlagSetGroup.InFlagSet("Cluster and nodegroup add-ons", func(fs *pflag.FlagSet) {
cmdutils.AddCommonCreateNodeGroupIAMAddonsFlags(fs, ng)
})
cmd.FlagSetGroup.InFlagSet("VPC networking", func(fs *pflag.FlagSet) {
fs.IPNetVar(&cfg.VPC.CIDR.IPNet, "vpc-cidr", cfg.VPC.CIDR.IPNet, "global CIDR to use for VPC")
params.Subnets = map[api.SubnetTopology]*[]string{
api.SubnetTopologyPrivate: fs.StringSlice("vpc-private-subnets", nil, "re-use private subnets of an existing VPC"),
api.SubnetTopologyPublic: fs.StringSlice("vpc-public-subnets", nil, "re-use public subnets of an existing VPC"),
}
fs.StringVar(¶ms.KopsClusterNameForVPC, "vpc-from-kops-cluster", "", "re-use VPC from a given kops cluster")
fs.StringVar(cfg.VPC.NAT.Gateway, "vpc-nat-mode", api.ClusterSingleNAT, "VPC NAT mode, valid options: HighlyAvailable, Single, Disable")
})
cmdutils.AddCommonFlagsForAWS(cmd.FlagSetGroup, cmd.ProviderConfig, true)
cmd.FlagSetGroup.InFlagSet("Output kubeconfig", func(fs *pflag.FlagSet) {
cmdutils.AddCommonFlagsForKubeconfig(fs, ¶ms.KubeconfigPath, ¶ms.AuthenticatorRoleARN, ¶ms.SetContext, ¶ms.AutoKubeconfigPath, exampleClusterName)
fs.BoolVar(¶ms.WriteKubeconfig, "write-kubeconfig", true, "toggle writing of kubeconfig")
})
}
func doCreateCluster(cmd *cmdutils.Cmd, ng *api.NodeGroup, params *cmdutils.CreateClusterCmdParams) error {
ngFilter := cmdutils.NewNodeGroupFilter()
if err := cmdutils.NewCreateClusterLoader(cmd, ngFilter, ng, params).Load(); err != nil {
return err
}
cfg := cmd.ClusterConfig
meta := cmd.ClusterConfig.Metadata
printer := printers.NewJSONPrinter()
ctl, err := cmd.NewCtl()
if err != nil {
return err
}
cmdutils.LogRegionAndVersionInfo(meta)
if cfg.Metadata.Version == "" {
cfg.Metadata.Version = api.DefaultVersion
}
if cfg.Metadata.Version != api.DefaultVersion {
if !isValidVersion(cfg.Metadata.Version) {
if isDeprecatedVersion(cfg.Metadata.Version) {
return fmt.Errorf("invalid version, %s is now deprecated, supported values: %s\nsee also: https://docs.aws.amazon.com/eks/latest/userguide/kubernetes-versions.html", cfg.Metadata.Version, strings.Join(api.SupportedVersions(), ", "))
}
return fmt.Errorf("invalid version, supported values: %s", strings.Join(api.SupportedVersions(), ", "))
}
}
if err := cfg.ValidateClusterEndpointConfig(); err != nil {
return err
}
if err := ctl.CheckAuth(); err != nil {
return err
}
if params.AutoKubeconfigPath {
if params.KubeconfigPath != kubeconfig.DefaultPath {
return fmt.Errorf("--kubeconfig and --auto-kubeconfig %s", cmdutils.IncompatibleFlags)
}
params.KubeconfigPath = kubeconfig.AutoPath(meta.Name)
}
if checkSubnetsGivenAsFlags(params) {
// undo defaulting and reset it, as it's not set via config file;
// default value here causes errors as vpc.ImportVPC doesn't
// treat remote state as authority over local state
cfg.VPC.CIDR = nil
// load subnets from local map created from flags, into the config
for topology := range params.Subnets {
if err := vpc.ImportSubnetsFromList(ctl.Provider, cfg, topology, *params.Subnets[topology]); err != nil {
return err
}
}
}
logFiltered := cmdutils.ApplyFilter(cfg, ngFilter)
kubeNodeGroups := cmdutils.ToKubeNodeGroups(cfg)
if err := eks.ValidateFeatureCompatibility(cfg, kubeNodeGroups); err != nil {
return err
}
if params.InstallWindowsVPCController {
if !eks.SupportsWindowsWorkloads(kubeNodeGroups) {
return errors.New("running Windows workloads requires having both Windows and Linux (AmazonLinux2) node groups")
}
} else {
eks.LogWindowsCompatibility(kubeNodeGroups, cfg.Metadata)
}
subnetsGiven := cfg.HasAnySubnets() // this will be false when neither flags nor config has any subnets
createOrImportVPC := func() error {
subnetInfo := func() string {
return fmt.Sprintf("VPC (%s) and subnets (private:%v public:%v)",
cfg.VPC.ID, cfg.PrivateSubnetIDs(), cfg.PublicSubnetIDs())
}
customNetworkingNotice := "custom VPC/subnets will be used; if resulting cluster doesn't function as expected, make sure to review the configuration of VPC/subnets"
canUseForPrivateNodeGroups := func(ng *api.NodeGroup) error {
if ng.PrivateNetworking && !cfg.HasSufficientPrivateSubnets() {
return errors.New("none or too few private subnets to use with --node-private-networking")
}
return nil
}
if !subnetsGiven && params.KopsClusterNameForVPC == "" {
// default: create dedicated VPC
if err := ctl.SetAvailabilityZones(cfg, params.AvailabilityZones); err != nil {
return err
}
if err := vpc.SetSubnets(cfg); err != nil {
return err
}
return nil
}
if params.KopsClusterNameForVPC != "" {
// import VPC from a given kops cluster
if len(params.AvailabilityZones) != 0 {
return fmt.Errorf("--vpc-from-kops-cluster and --zones %s", cmdutils.IncompatibleFlags)
}
if cmd.CobraCommand.Flag("vpc-cidr").Changed {
return fmt.Errorf("--vpc-from-kops-cluster and --vpc-cidr %s", cmdutils.IncompatibleFlags)
}
if subnetsGiven {
return fmt.Errorf("--vpc-from-kops-cluster and --vpc-private-subnets/--vpc-public-subnets %s", cmdutils.IncompatibleFlags)
}
kw, err := kops.NewWrapper(cmd.ProviderConfig.Region, params.KopsClusterNameForVPC)
if err != nil {
return err
}
if err := kw.UseVPC(ctl.Provider, cfg); err != nil {
return err
}
for _, ng := range cfg.NodeGroups {
if err := canUseForPrivateNodeGroups(ng); err != nil {
return err
}
}
logger.Success("using %s from kops cluster %q", subnetInfo(), params.KopsClusterNameForVPC)
logger.Warning(customNetworkingNotice)
return nil
}
// use subnets as specified by --vpc-{private,public}-subnets flags
if len(params.AvailabilityZones) != 0 {
return fmt.Errorf("--vpc-private-subnets/--vpc-public-subnets and --zones %s", cmdutils.IncompatibleFlags)
}
if cmd.CobraCommand.Flag("vpc-cidr").Changed {
return fmt.Errorf("--vpc-private-subnets/--vpc-public-subnets and --vpc-cidr %s", cmdutils.IncompatibleFlags)
}
if err := vpc.ImportAllSubnets(ctl.Provider, cfg); err != nil {
return err
}
if err := cfg.HasSufficientSubnets(); err != nil {
logger.Critical("unable to use given %s", subnetInfo())
return err
}
for _, ng := range cfg.NodeGroups {
if err := canUseForPrivateNodeGroups(ng); err != nil {
return err
}
}
logger.Success("using existing %s", subnetInfo())
logger.Warning(customNetworkingNotice)
return nil
}
if err := createOrImportVPC(); err != nil {
return err
}
for _, ng := range cfg.NodeGroups {
// resolve AMI
if err := ctl.EnsureAMI(meta.Version, ng); err != nil {
return err
}
logger.Info("nodegroup %q will use %q [%s/%s]", ng.Name, ng.AMI, ng.AMIFamily, cfg.Metadata.Version)
// load or use SSH key - name includes cluster name and the
// fingerprint, so if unique keys provided, each will get
// loaded and used as intended and there is no need to have
// nodegroup name in the key name
publicKeyName, err := ssh.LoadKey(ng.SSH, meta.Name, ng.Name, ctl.Provider.EC2())
if err != nil {
return err
}
if publicKeyName != "" {
ng.SSH.PublicKeyName = &publicKeyName
}
}
nodeGroupService := eks.NewNodeGroupService(cfg, ctl.Provider.EC2())
if err := nodeGroupService.NormalizeManaged(cfg.ManagedNodeGroups); err != nil {
return err
}
logger.Info("using Kubernetes version %s", meta.Version)
logger.Info("creating %s", cfg.LogString())
// TODO dry-run mode should provide a way to render config with all defaults set
// we should also make a call to resolve the AMI and write the result, similarly
// the body of the SSH key can be read
if err := printer.LogObj(logger.Debug, "cfg.json = \\\n%s\n", cfg); err != nil {
return err
}
{ // core action
stackManager := ctl.NewStackManager(cfg)
if cmd.ClusterConfigFile == "" {
logMsg := func(resource string) {
logger.Info("will create 2 separate CloudFormation stacks for cluster itself and the initial %s", resource)
}
if len(cfg.NodeGroups) == 1 {
logMsg("nodegroup")
} else if len(cfg.ManagedNodeGroups) == 1 {
logMsg("managed nodegroup")
}
} else {
logMsg := func(resource string, count int) {
logger.Info("will create a CloudFormation stack for cluster itself and %d %s stack(s)", count, resource)
}
logFiltered()
logMsg("nodegroup", len(cfg.NodeGroups))
logMsg("managed nodegroup", len(cfg.ManagedNodeGroups))
}
logger.Info("if you encounter any issues, check CloudFormation console or try 'eksctl utils describe-stacks --region=%s --cluster=%s'", meta.Region, meta.Name)
supportsManagedNodes, err := eks.VersionSupportsManagedNodes(cfg.Metadata.Version)
if err != nil {
return err
}
tasks := stackManager.NewTasksToCreateClusterWithNodeGroups(cfg.NodeGroups, cfg.ManagedNodeGroups, supportsManagedNodes)
ctl.AppendExtraClusterConfigTasks(cfg, params.InstallWindowsVPCController, tasks)
logger.Info(tasks.Describe())
if errs := tasks.DoAllSync(); len(errs) > 0 {
logger.Info("%d error(s) occurred and cluster hasn't been created properly, you may wish to check CloudFormation console", len(errs))
logger.Info("to cleanup resources, run 'eksctl delete cluster --region=%s --name=%s'", meta.Region, meta.Name)
for _, err := range errs {
logger.Critical("%s\n", err.Error())
}
return fmt.Errorf("failed to create cluster %q", meta.Name)
}
}
logger.Success("all EKS cluster resources for %q have been created", meta.Name)
// obtain cluster credentials, write kubeconfig
{ // post-creation action
var kubeconfigContextName string
if params.WriteKubeconfig {
kubectlConfig := kubeconfig.NewForKubectl(cfg, ctl.GetUsername(), params.AuthenticatorRoleARN, ctl.Provider.Profile())
kubeconfigContextName = kubectlConfig.CurrentContext
params.KubeconfigPath, err = kubeconfig.Write(params.KubeconfigPath, *kubectlConfig, params.SetContext)
if err != nil {
logger.Warning("unable to write kubeconfig %s, please retry with 'eksctl utils write-kubeconfig -n %s': %v", params.KubeconfigPath, meta.Name, err)
} else {
logger.Success("saved kubeconfig as %q", params.KubeconfigPath)
}
} else {
params.KubeconfigPath = ""
}
// create Kubernetes client
clientSet, err := ctl.NewStdClientSet(cfg)
if err != nil {
return err
}
if err = ctl.WaitForControlPlane(meta, clientSet); err != nil {
return err
}
for _, ng := range cfg.NodeGroups {
// authorise nodes to join
if err = authconfigmap.AddNodeGroup(clientSet, ng); err != nil {
return err
}
// wait for nodes to join
if err = ctl.WaitForNodes(clientSet, ng); err != nil {
return err
}
// if GPU instance type, give instructions
if utils.IsGPUInstanceType(ng.InstanceType) || (ng.InstancesDistribution != nil && utils.HasGPUInstanceType(ng.InstancesDistribution.InstanceTypes)) {
logger.Info("as you are using a GPU optimized instance type you will need to install NVIDIA Kubernetes device plugin.")
logger.Info("\t see the following page for instructions: https://github.com/NVIDIA/k8s-device-plugin")
}
}
for _, ng := range cfg.ManagedNodeGroups {
if err := ctl.WaitForNodes(clientSet, ng); err != nil {
return err
}
}
if cfg.IsFargateEnabled() {
if err := doCreateFargateProfiles(cmd, ctl); err != nil {
return err
}
if err := scheduleCoreDNSOnFargateIfRelevant(cmd, clientSet); err != nil {
return err
}
}
// check kubectl version, and offer install instructions if missing or old
// also check heptio-authenticator
// TODO: https://github.com/weaveworks/eksctl/issues/30
env, err := ctl.GetCredentialsEnv()
if err != nil {
return err
}
if err := utils.CheckAllCommands(params.KubeconfigPath, params.SetContext, kubeconfigContextName, env); err != nil {
logger.Critical("%s\n", err.Error())
logger.Info("cluster should be functional despite missing (or misconfigured) client binaries")
}
}
logger.Success("%s is ready", meta.LogString())
if err := printer.LogObj(logger.Debug, "cfg.json = \\\n%s\n", cfg); err != nil {
return err
}
return nil
}