-
Notifications
You must be signed in to change notification settings - Fork 14
/
create.go
225 lines (200 loc) · 6.46 KB
/
create.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
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package infrastructure
import (
"fmt"
"os"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/common"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/connector"
"github.com/spf13/cobra"
versionutil "k8s.io/apimachinery/pkg/util/version"
"k8s.io/cli-runtime/pkg/genericiooptions"
"k8s.io/kubectl/pkg/util/templates"
"github.com/apecloud/kubeblocks/pkg/configuration/container"
cfgcore "github.com/apecloud/kubeblocks/pkg/configuration/core"
"github.com/apecloud/kbcli/pkg/cmd/infrastructure/constant"
"github.com/apecloud/kbcli/pkg/cmd/infrastructure/tasks"
"github.com/apecloud/kbcli/pkg/cmd/infrastructure/types"
"github.com/apecloud/kbcli/pkg/util"
)
type createOptions struct {
clusterOptions
version types.InfraVersionInfo
criType string
debug bool
sandBoxImage string
securityEnhancement bool
outputKubeconfig string
}
var createExamples = templates.Examples(`
# Create kubernetes cluster with specified config yaml
kbcli infra create -c cluster.yaml
# example cluster.yaml
cat cluster.yaml
metadata:
name: kb-k8s-test-cluster
user:
name: user1
privateKeyPath: ~/.ssh/test.pem
nodes:
- name: kb-infra-node-0
address: 1.1.1.1
internalAddress: 10.128.0.19
- name: kb-infra-node-1
address: 1.1.1.2
internalAddress: 10.128.0.20
- name: kb-infra-node-2
address: 1.1.1.3
internalAddress: 10.128.0.21
options:
hugePageFeature:
hugePageSize: 10GB
roleGroup:
etcd:
- kb-infra-node-0
- kb-infra-node-1
- kb-infra-node-2
master:
- kb-infra-node-0
worker:
- kb-infra-node-1
- kb-infra-node-2
kubernetes:
containerManager: containerd
# apis/kubeadm/types.Networking
networking:
plugin: cilium
dnsDomain: cluster.local
podSubnet: 10.233.64.0/18
serviceSubnet: 10.233.0.0/18
controlPlaneEndpoint:
domain: lb.kubeblocks.local
port: 6443
cri:
containerRuntimeType: "containerd"
containerRuntimeEndpoint: "unix:///run/containerd/containerd.sock"
sandBoxImage: "k8s.gcr.io/pause:3.8"
addons:
- name: openebs
namespace: kube-blocks
sources:
chart:
name: openebs
version: 3.7.0
repo: https://openebs.github.io/charts
options:
values:
- "localprovisioner.basePath=/mnt/disks"
- "localprovisioner.hostpathClass.isDefaultClass=true"
`)
func (o *createOptions) Run() error {
const minKubernetesVersion = "v1.24.0"
v, err := versionutil.ParseSemantic(o.version.KubernetesVersion)
if err != nil {
return err
}
c, err := v.Compare(minKubernetesVersion)
if err != nil {
return err
}
if c < 0 {
return cfgcore.MakeError("kubernetes version must be greater than %s", minKubernetesVersion)
}
o.Cluster.Kubernetes.AutoDefaultFill()
o.version = o.Version
o.checkAndSetDefaultVersion()
cluster, err := createClusterWithOptions(buildTemplateParams(o))
if err != nil {
return err
}
yes, err := o.confirm(fmt.Sprintf("install kubernetes using version: %v", o.version.KubernetesVersion))
if err != nil {
return err
}
if !yes {
return nil
}
runtime := &common.KubeRuntime{
BaseRuntime: connector.NewBaseRuntime(o.clusterName, connector.NewDialer(), o.debug, false),
Cluster: cluster,
ClusterName: o.clusterName,
}
syncClusterNodeRole(cluster, runtime)
checkAndUpdateZone()
pipelineRunner := tasks.NewPipelineRunner("CreateCluster", NewCreatePipeline(o), runtime)
if err := pipelineRunner.Do(o.IOStreams.Out); err != nil {
return err
}
fmt.Fprintf(o.IOStreams.Out, "Kubernetes Installation is complete.\n\n")
return nil
}
func checkAndUpdateZone() {
const ZoneName = "KKZONE"
if location, _ := util.GetIPLocation(); location == "CN" {
os.Setenv(ZoneName, "cn")
}
fmt.Printf("current zone: %s\n", os.Getenv(ZoneName))
}
func NewCreateKubernetesCmd(streams genericiooptions.IOStreams) *cobra.Command {
o := &createOptions{
clusterOptions: clusterOptions{
IOStreams: streams,
}}
o.checkAndSetDefaultVersion()
cmd := &cobra.Command{
Use: "create",
Short: "create kubernetes cluster.",
Example: createExamples,
Run: func(cmd *cobra.Command, args []string) {
util.CheckErr(o.Complete())
util.CheckErr(o.Validate())
util.CheckErr(o.Run())
},
}
o.buildCreateInfraFlags(cmd)
o.Version = o.version
return cmd
}
func (o *createOptions) buildCreateInfraFlags(cmd *cobra.Command) {
buildCommonFlags(cmd, &o.clusterOptions)
cmd.Flags().StringVarP(&o.version.KubernetesVersion, "version", "", o.version.KubernetesVersion, fmt.Sprintf("Specify install kubernetes version. default version is %s", o.version.KubernetesVersion))
cmd.Flags().StringVarP(&o.sandBoxImage, "sandbox-image", "", constant.DefaultSandBoxImage, "Specified sandbox-image will not be used by the cri. [option]")
cmd.Flags().StringVarP(&o.criType, "container-runtime", "", string(container.ContainerdType), "Specify kubernetes container runtime. default is containerd")
cmd.Flags().BoolVarP(&o.debug, "debug", "", false, "set debug mode")
cmd.Flags().StringVarP(&o.outputKubeconfig, "output-kubeconfig", "", tasks.GetDefaultConfig(), "Specified output kubeconfig. [option]")
}
func (o *createOptions) checkAndSetDefaultVersion() {
if o.version.KubernetesVersion == "" {
o.version.KubernetesVersion = constant.DefaultK8sVersion
}
if o.version.EtcdVersion == "" {
o.version.EtcdVersion = constant.DefaultEtcdVersion
}
if o.version.ContainerVersion == "" {
o.version.ContainerVersion = constant.DefaultContainerdVersion
}
if o.version.HelmVersion == "" {
o.version.HelmVersion = constant.DefaultHelmVersion
}
if o.version.CRICtlVersion == "" {
o.version.CRICtlVersion = constant.DefaultCRICtlVersion
}
if o.version.CniVersion == "" {
o.version.CniVersion = constant.DefaultCniVersion
}
if o.version.RuncVersion == "" {
o.version.RuncVersion = constant.DefaultRuncVersion
}
}