-
Notifications
You must be signed in to change notification settings - Fork 787
/
create_cluster.go
188 lines (153 loc) · 6.16 KB
/
create_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
package cmd
import (
"fmt"
"io"
"sort"
"strings"
"github.com/jenkins-x/jx/pkg/log"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/spf13/cobra"
)
type KubernetesProvider string
// CreateClusterOptions the flags for running create cluster
type CreateClusterOptions struct {
CreateOptions
InstallOptions InstallOptions
Flags InitFlags
Provider string
SkipInstallation bool
}
const (
GKE = "gke"
OKE = "oke"
EKS = "eks"
AKS = "aks"
AWS = "aws"
PKS = "pks"
IKS = "iks"
MINIKUBE = "minikube"
MINISHIFT = "minishift"
KUBERNETES = "kubernetes"
OPENSHIFT = "openshift"
ORACLE = "oracle"
ICP = "icp"
JX_INFRA = "jx-infra"
optionKubernetesVersion = "kubernetes-version"
optionNodes = "nodes"
optionClusterName = "cluster-name"
)
var KUBERNETES_PROVIDERS = []string{MINIKUBE, GKE, OKE, AKS, AWS, EKS, KUBERNETES, IKS, OPENSHIFT, MINISHIFT, JX_INFRA, PKS, ICP}
const (
stableKubeCtlVersionURL = "https://storage.googleapis.com/kubernetes-release/release/stable.txt"
valid_providers = `Valid Kubernetes providers include:
* aks (Azure Container Service - https://docs.microsoft.com/en-us/azure/aks)
* aws (Amazon Web Services via kops - https://github.com/aws-samples/aws-workshop-for-kubernetes/blob/master/readme.adoc)
* eks (Amazon Web Services Elastic Container Service for Kubernetes - https://docs.aws.amazon.com/eks/latest/userguide/getting-started.html)
* gke (Google Container Engine - https://cloud.google.com/kubernetes-engine)
* oke (Oracle Cloud Infrastructure Container Engine for Kubernetes - https://docs.cloud.oracle.com/iaas/Content/ContEng/Concepts/contengoverview.htm)
# icp (IBM Cloud Private) - https://www.ibm.com/cloud/private
* iks (IBM Cloud Kubernetes Service - https://console.bluemix.net/docs/containers)
* oke (Oracle Cloud Infrastructure Container Engine for Kubernetes - https://docs.cloud.oracle.com/iaas/Content/ContEng/Concepts/contengoverview.htm)
* kubernetes for custom installations of Kubernetes
* minikube (single-node Kubernetes cluster inside a VM on your laptop)
* minishift (single-node OpenShift cluster inside a VM on your laptop)
* openshift for installing on 3.9.x or later clusters of OpenShift
`
)
type CreateClusterFlags struct {
}
var (
createClusterLong = templates.LongDesc(`
This command creates a new Kubernetes cluster, installing required local dependencies and provisions the Jenkins X platform
You can see a demo of this command here: [https://jenkins-x.io/demos/create_cluster/](https://jenkins-x.io/demos/create_cluster/)
%s
Depending on which cloud provider your cluster is created on possible dependencies that will be installed are:
- kubectl (CLI to interact with Kubernetes clusters)
- helm (package manager for Kubernetes)
- draft (CLI that makes it easy to build applications that run on Kubernetes)
- minikube (single-node Kubernetes cluster inside a VM on your laptop )
- minishift (single-node OpenShift cluster inside a VM on your laptop)
- virtualisation drivers (to run Minikube in a VM)
- gcloud (Google Cloud CLI)
- oci (Oracle Cloud Infrastructure CLI)
- az (Azure CLI)
- ibmcloud (IBM CLoud CLI)
For more documentation see: [https://jenkins-x.io/getting-started/create-cluster/](https://jenkins-x.io/getting-started/create-cluster/)
`)
createClusterExample = templates.Examples(`
jx create cluster minikube
`)
)
// KubernetesProviderOptions returns all the Kubernetes providers as a string
func KubernetesProviderOptions() string {
values := []string{}
values = append(values, KUBERNETES_PROVIDERS...)
sort.Strings(values)
return strings.Join(values, ", ")
}
// NewCmdCreateCluster 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 NewCmdCreateCluster(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := createCreateClusterOptions(f, in, out, errOut, "")
cmd := &cobra.Command{
Use: "cluster [kubernetes provider]",
Short: "Create a new Kubernetes cluster",
Long: fmt.Sprintf(createClusterLong, valid_providers),
Example: createClusterExample,
Run: func(cmd2 *cobra.Command, args []string) {
options.Cmd = cmd2
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.AddCommand(NewCmdCreateClusterAKS(f, in, out, errOut))
cmd.AddCommand(NewCmdCreateClusterAWS(f, in, out, errOut))
cmd.AddCommand(NewCmdCreateClusterEKS(f, in, out, errOut))
cmd.AddCommand(NewCmdCreateClusterGKE(f, in, out, errOut))
cmd.AddCommand(NewCmdCreateClusterMinikube(f, in, out, errOut))
cmd.AddCommand(NewCmdCreateClusterMinishift(f, in, out, errOut))
cmd.AddCommand(NewCmdCreateClusterOKE(f, in, out, errOut))
cmd.AddCommand(NewCmdCreateClusterIKS(f, in, out, errOut))
return cmd
}
func (o *CreateClusterOptions) addCreateClusterFlags(cmd *cobra.Command) {
o.InstallOptions.addInstallFlags(cmd, true)
cmd.Flags().BoolVarP(&o.SkipInstallation, "skip-installation", "", false, "Provision cluster only, don't install Jenkins X into it")
}
func createCreateClusterOptions(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer, cloudProvider string) CreateClusterOptions {
commonOptions := CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
}
options := CreateClusterOptions{
CreateOptions: CreateOptions{
CommonOptions: commonOptions,
},
Provider: cloudProvider,
InstallOptions: CreateInstallOptions(f, in, out, errOut),
}
return options
}
func (o *CreateClusterOptions) initAndInstall(provider string) error {
if o.SkipInstallation {
log.Infof("%s cluster created. Skipping Jenkins X installation.\n", o.Provider)
return nil
}
o.InstallOptions.BatchMode = o.BatchMode
o.InstallOptions.Flags.Provider = provider
installOpts := &o.InstallOptions
// call jx install
err := installOpts.Run()
if err != nil {
return err
}
return nil
}
// Run returns help if function is run without any argument
func (o *CreateClusterOptions) Run() error {
return o.Cmd.Help()
}