-
Notifications
You must be signed in to change notification settings - Fork 787
/
create_cluster_minishift.go
252 lines (209 loc) · 6.42 KB
/
create_cluster_minishift.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 cmd
import (
"bytes"
"os"
"os/exec"
"runtime"
"strings"
"github.com/jenkins-x/jx/pkg/cloud"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1"
)
// CreateClusterMinishiftOptions the flags for running create cluster
type CreateClusterMinishiftOptions struct {
CreateClusterOptions
Flags CreateClusterMinishiftFlags
Provider KubernetesProvider
}
type CreateClusterMinishiftFlags struct {
Memory string
CPU string
Driver string
HyperVVirtualSwitch string
Namespace string
}
var (
createClusterMinishiftLong = templates.LongDesc(`
This command creates a new Kubernetes cluster, installing required local dependencies and provisions the
Jenkins X platform
Minishift is a tool that makes it easy to run OpenShift locally. Minishift runs a single-node OpenShift
cluster inside a VM on your laptop for users looking to try out Kubernetes or develop with it day-to-day.
`)
createClusterMinishiftExample = templates.Examples(`
jx create cluster minishift
`)
)
// NewCmdGet creates a command object for the generic "init" action, which
// installs the dependencies required to run the jenkins-x platform on a Kubernetes cluster.
func NewCmdCreateClusterMinishift(commonOpts *CommonOptions) *cobra.Command {
options := CreateClusterMinishiftOptions{
CreateClusterOptions: createCreateClusterOptions(commonOpts, cloud.MINISHIFT),
}
cmd := &cobra.Command{
Use: "minishift",
Short: "Create a new OpenShift cluster with Minishift: Runs locally",
Long: createClusterMinishiftLong,
Example: createClusterMinishiftExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addCreateClusterFlags(cmd)
cmd.Flags().StringVarP(&options.Flags.Memory, "memory", "m", "4096", "Amount of RAM allocated to the Minishift VM in MB")
cmd.Flags().StringVarP(&options.Flags.CPU, "cpu", "c", "3", "Number of CPUs allocated to the Minishift VM")
cmd.Flags().StringVarP(&options.Flags.Driver, "vm-driver", "d", "", "VM driver is one of: [virtualbox xhyve vmwarefusion hyperkit]")
cmd.Flags().StringVarP(&options.Flags.HyperVVirtualSwitch, "hyperv-virtual-switch", "v", "", "Additional options for using HyperV with Minishift")
return cmd
}
func (o *CreateClusterMinishiftOptions) Run() error {
var deps []string
d := binaryShouldBeInstalled("minishift")
if d != "" {
deps = append(deps, d)
}
d = binaryShouldBeInstalled("oc")
if d != "" {
deps = append(deps, d)
}
err := o.installMissingDependencies(deps)
if err != nil {
log.Errorf("error installing missing dependencies %v, please fix or install manually then try again", err)
os.Exit(-1)
}
if o.isExistingMinishiftRunning() {
log.Error("an existing Minishift cluster is already running, perhaps use `jx install`.\nNote: existing Minishift must have RBAC enabled, running `minishift delete` and `jx create cluster minishift` creates a new VM with RBAC enabled")
os.Exit(-1)
}
err = o.createClusterMinishift()
if err != nil {
log.Errorf("error creating cluster %v", err)
os.Exit(-1)
}
return nil
}
func (o *CreateClusterMinishiftOptions) defaultMacVMDriver() string {
return "xhyve"
}
func (o *CreateClusterMinishiftOptions) isExistingMinishiftRunning() bool {
var cmd_out bytes.Buffer
e := exec.Command("minishift", "status")
e.Stdout = &cmd_out
e.Stderr = o.Err
err := e.Run()
if err != nil {
return false
}
if strings.Contains(cmd_out.String(), "Running") {
return true
}
return false
}
func (o *CreateClusterMinishiftOptions) createClusterMinishift() error {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
mem := o.Flags.Memory
prompt := &survey.Input{
Message: "memory (MB)",
Default: mem,
Help: "Amount of RAM allocated to the Minishift VM in MB",
}
survey.AskOne(prompt, &mem, nil, surveyOpts)
cpu := o.Flags.CPU
prompt = &survey.Input{
Message: "cpu (cores)",
Default: cpu,
Help: "Number of CPUs allocated to the Minishift VM",
}
survey.AskOne(prompt, &cpu, nil, surveyOpts)
vmDriverValue := o.Flags.Driver
if len(vmDriverValue) == 0 {
switch runtime.GOOS {
case "darwin":
vmDriverValue = o.defaultMacVMDriver()
case "windows":
vmDriverValue = "hyperv"
case "linux":
vmDriverValue = "kvm"
default:
vmDriverValue = "virtualbox"
}
}
// only add drivers that are appropriate for this OS
var driver string
drivers := []string{vmDriverValue}
if vmDriverValue != "virtualbox" {
drivers = append(drivers, "virtualbox")
}
if runtime.GOOS == "darwin" {
if util.StringArrayIndex(drivers, "xhyve") < 0 {
drivers = append(drivers, "xhyve")
}
}
if runtime.GOOS == "linux" {
drivers = append(drivers, "none")
}
prompts := &survey.Select{
Message: "Select driver:",
Options: drivers,
Default: vmDriverValue,
Help: "VM driver, defaults to recommended native virtualisation",
}
err := survey.AskOne(prompts, &driver, nil, surveyOpts)
if err != nil {
return err
}
if driver != "none" {
err = o.doInstallMissingDependencies([]string{driver})
if err != nil {
log.Errorf("error installing missing dependencies %v, please fix or install manually then try again", err)
os.Exit(-1)
}
}
log.Info("Installing default addons ...\n")
err = o.RunCommand("minishift", "addons", "install", "--defaults")
if err != nil {
return err
}
log.Info("Enabling admin user...\n")
err = o.RunCommand("minishift", "addons", "enable", "admin-user")
if err != nil {
return err
}
args := []string{"start", "--memory", mem, "--cpus", cpu, "--vm-driver", driver}
hyperVVirtualSwitch := o.Flags.HyperVVirtualSwitch
if hyperVVirtualSwitch != "" {
args = append(args, "--hyperv-virtual-switch", hyperVVirtualSwitch)
}
log.Info("Creating cluster...\n")
err = o.RunCommand("minishift", args...)
if err != nil {
return err
}
ip, err := o.getCommandOutput("", "minishift", "ip")
if err != nil {
return err
}
o.InstallOptions.Flags.Domain = ip + ".nip.io"
err = o.RunCommand("oc", "login", "-u", "admin", "-p", "admin", "--insecure-skip-tls-verify=true")
if err != nil {
return err
}
ns := o.Flags.Namespace
if ns == "" {
_, ns, _ = o.KubeClientAndNamespace()
if err != nil {
return err
}
}
log.Info("Initialising cluster ...\n")
err = o.initAndInstall(cloud.MINISHIFT)
if err != nil {
return err
}
return nil
}