-
Notifications
You must be signed in to change notification settings - Fork 787
/
create_cluster_eks.go
174 lines (151 loc) · 5.77 KB
/
create_cluster_eks.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
package cmd
import (
"github.com/jenkins-x/jx/pkg/cloud/amazon"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"io"
"os"
"strconv"
"strings"
"time"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
logger "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
// CreateClusterEKSOptions contains the CLI flags
type CreateClusterEKSOptions struct {
CreateClusterOptions
Flags CreateClusterEKSFlags
}
type CreateClusterEKSFlags struct {
ClusterName string
NodeType string
NodeCount int
NodesMin int
NodesMax int
Region string
Zones string
Profile string
SshPublicKey string
Verbose int
AWSOperationTimeout time.Duration
}
var (
createClusterEKSLong = templates.LongDesc(`
This command creates a new Kubernetes cluster on Amazon Web Services (AWS) using EKS, installing required local dependencies and provisions the
Jenkins X platform
EKS is a managed Kubernetes service on AWS.
`)
createClusterEKSExample = templates.Examples(`
# to create a new Kubernetes cluster with Jenkins X in your default zones (from $EKS_AVAILABILITY_ZONES)
jx create cluster eks
# to specify the zones
jx create cluster eks --zones us-west-2a,us-west-2b,us-west-2c
`)
)
// NewCmdCreateClusterEKS creates the command
func NewCmdCreateClusterEKS(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := CreateClusterEKSOptions{
CreateClusterOptions: createCreateClusterOptions(f, in, out, errOut, AKS),
}
cmd := &cobra.Command{
Use: "eks",
Short: "Create a new Kubernetes cluster on AWS using EKS",
Long: createClusterEKSLong,
Example: createClusterEKSExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addCreateClusterFlags(cmd)
options.addCommonFlags(cmd)
cmd.Flags().StringVarP(&options.Flags.ClusterName, optionClusterName, "n", "", "The name of this cluster.")
cmd.Flags().StringVarP(&options.Flags.NodeType, "node-type", "", "m5.large", "node instance type")
cmd.Flags().IntVarP(&options.Flags.NodeCount, optionNodes, "o", -1, "number of nodes")
cmd.Flags().IntVarP(&options.Flags.NodesMin, "nodes-min", "", -1, "minimum number of nodes")
cmd.Flags().IntVarP(&options.Flags.NodesMax, "nodes-max", "", -1, "maximum number of nodes")
cmd.Flags().IntVarP(&options.Flags.Verbose, "eksctl-log-level", "", -1, "set log level, use 0 to silence, 4 for debugging and 5 for debugging with AWS debug logging (default 3)")
cmd.Flags().DurationVarP(&options.Flags.AWSOperationTimeout, "aws-api-timeout", "", 20*time.Minute, "Duration of AWS API timeout")
cmd.Flags().StringVarP(&options.Flags.Region, "region", "r", "", "The region to use. Default: us-west-2")
cmd.Flags().StringVarP(&options.Flags.Zones, optionZones, "z", "", "Availability Zones. Auto-select if not specified. If provided, this overrides the $EKS_AVAILABILITY_ZONES environment variable")
cmd.Flags().StringVarP(&options.Flags.Profile, "profile", "p", "", "AWS profile to use. If provided, this overrides the AWS_PROFILE environment variable")
cmd.Flags().StringVarP(&options.Flags.SshPublicKey, "ssh-public-key", "", "", "SSH public key to use for nodes (import from local path, or use existing EC2 key pair) (default \"~/.ssh/id_rsa.pub\")")
return cmd
}
// Runs the command logic (including installing required binaries, parsing options and aggregating eksctl command)
func (o *CreateClusterEKSOptions) Run() error {
log.ConfigureLog(o.LogLevel)
var deps []string
d := binaryShouldBeInstalled("eksctl")
if d != "" {
deps = append(deps, d)
}
d = binaryShouldBeInstalled("heptio-authenticator-aws")
if d != "" {
deps = append(deps, d)
}
logger.Debugf("Dependencies to be installed: %s", strings.Join(deps, ", "))
err := o.installMissingDependencies(deps)
if err != nil {
logger.Errorf("%v\nPlease fix the error or install manually then try again", err)
os.Exit(-1)
}
flags := &o.Flags
zones := flags.Zones
if zones == "" {
zones = os.Getenv("EKS_AVAILABILITY_ZONES")
}
args := []string{"create", "cluster", "--full-ecr-access"}
if flags.ClusterName != "" {
args = append(args, "--name", flags.ClusterName)
}
region, err := amazon.ResolveRegion("", flags.Region)
if err != nil {
return err
}
args = append(args, "--region", region)
if zones != "" {
args = append(args, "--zones", zones)
}
if flags.Profile != "" {
args = append(args, "--profile", flags.Profile)
}
if flags.SshPublicKey != "" {
args = append(args, "--ssh-public-key", flags.SshPublicKey)
}
args = append(args, "--node-type", flags.NodeType)
if flags.NodeCount >= 0 {
args = append(args, "--nodes", strconv.Itoa(flags.NodeCount))
}
if flags.NodesMin >= 0 {
args = append(args, "--nodes-min", strconv.Itoa(flags.NodesMin))
}
if flags.NodesMax >= 0 {
args = append(args, "--nodes-max", strconv.Itoa(flags.NodesMax))
}
if flags.Verbose >= 0 {
args = append(args, "--verbose", strconv.Itoa(flags.Verbose))
}
args = append(args, "--aws-api-timeout", flags.AWSOperationTimeout.String())
logger.Info("Creating EKS cluster - this can take a while so please be patient...")
logger.Infof("You can watch progress in the CloudFormation console: %s", util.ColorInfo("https://console.aws.amazon.com/cloudformation/"))
logger.Debugf("Running command: %s", util.ColorInfo("eksctl "+strings.Join(args, " ")))
if logger.GetLevel() == logger.DebugLevel {
err = o.runCommandVerbose("eksctl", args...)
if err != nil {
return err
}
log.Blank()
} else {
err = o.runCommandQuietly("eksctl", args...)
if err != nil {
return err
}
}
logger.Info("Initialising cluster ...\n")
return o.initAndInstall(EKS)
}