-
Notifications
You must be signed in to change notification settings - Fork 785
/
create_cluster.go
158 lines (124 loc) · 4.84 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
package create
import (
"fmt"
"github.com/jenkins-x/jx/v2/pkg/cmd/create/options"
"github.com/jenkins-x/jx/v2/pkg/cmd/initcmd"
"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/spf13/cobra"
)
type KubernetesProvider string
// CreateClusterOptions the flags for running create cluster
type CreateClusterOptions struct {
options.CreateOptions
InstallOptions InstallOptions
Flags initcmd.InitFlags
Provider string
SkipInstallation bool `mapstructure:"skip-installation"`
}
const (
optionKubernetesVersion = "kubernetes-version"
optionNodes = "nodes"
optionClusterName = "cluster-name"
optionSkipInstallation = "skip-installation"
)
const (
ValidKubernetesProviders = `Valid Kubernetes providers include:
* aks (Azure Container Service - https://docs.microsoft.com/en-us/azure/aks)
* 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)
# icp (IBM Cloud Private) - https://www.ibm.com/cloud/private
* iks (IBM Cloud Kubernetes Service - https://console.bluemix.net/docs/containers)
* kubernetes for custom installations of Kubernetes
* 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)
- 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(`
# create a cluster on Google Cloud
jx create cluster gke --skip-installation
# create a cluster on AWS via EKS
jx create cluster eks --skip-installation
`)
)
// 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(commonOpts *opts.CommonOptions) *cobra.Command {
options := createCreateClusterOptions(commonOpts, "")
cmd := &cobra.Command{
Use: "cluster [kubernetes provider]",
Short: "Create a new Kubernetes cluster",
Long: fmt.Sprintf(CreateClusterLong, ValidKubernetesProviders),
Example: CreateClusterExample,
Run: func(cmd2 *cobra.Command, args []string) {
options.Cmd = cmd2
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
cmd.AddCommand(NewCmdCreateClusterAKS(commonOpts))
cmd.AddCommand(NewCmdCreateClusterEKS(commonOpts))
cmd.AddCommand(NewCmdCreateClusterGKE(commonOpts))
cmd.AddCommand(NewCmdCreateClusterIKS(commonOpts))
return cmd
}
func (o *CreateClusterOptions) addCreateClusterFlags(cmd *cobra.Command) {
o.InstallOptions.AddInstallFlags(cmd, true)
cmd.Flags().BoolVarP(&o.SkipInstallation, optionSkipInstallation, "", false, "Provision cluster only, don't install Jenkins X into it")
_ = viper.BindPFlag(optionSkipInstallation, cmd.Flags().Lookup(optionSkipInstallation))
}
func createCreateClusterOptions(commonOpts *opts.CommonOptions, cloudProvider string) CreateClusterOptions {
options := CreateClusterOptions{
CreateOptions: options.CreateOptions{
CommonOptions: commonOpts,
},
Provider: cloudProvider,
InstallOptions: CreateInstallOptions(commonOpts),
}
return options
}
func (o *CreateClusterOptions) initAndInstall(provider string) error {
err := o.GetConfiguration(&o)
if err != nil {
return errors.Wrap(err, "getting create cluster configuration")
}
if o.SkipInstallation {
log.Logger().Infof("%s cluster created. Skipping Jenkins X installation.", 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()
}