-
Notifications
You must be signed in to change notification settings - Fork 529
/
tasks.go
342 lines (293 loc) Β· 10 KB
/
tasks.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
Copyright 2021 The KubeSphere Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package precheck
import (
"fmt"
"regexp"
"strings"
"github.com/pkg/errors"
versionutil "k8s.io/apimachinery/pkg/util/version"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/common"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/action"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/connector"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/logger"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/version/kubernetes"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/version/kubesphere"
)
type GreetingsTask struct {
action.BaseAction
}
func (h *GreetingsTask) Execute(runtime connector.Runtime) error {
hello, err := runtime.GetRunner().SudoCmd("echo 'Greetings, KubeKey!'", false)
if err != nil {
return err
}
logger.Log.Messagef(runtime.RemoteHost().GetName(), hello)
return nil
}
type NodePreCheck struct {
common.KubeAction
}
func (n *NodePreCheck) Execute(runtime connector.Runtime) error {
var results = make(map[string]string)
results["name"] = runtime.RemoteHost().GetName()
for _, software := range baseSoftware {
var (
cmd string
)
switch software {
case docker:
cmd = "docker version --format '{{.Server.Version}}'"
case containerd:
cmd = "containerd --version | cut -d ' ' -f 3"
default:
cmd = fmt.Sprintf("which %s", software)
}
switch software {
case sudo:
// sudo skip sudo prefix
default:
cmd = connector.SudoPrefix(cmd)
}
res, err := runtime.GetRunner().Cmd(cmd, false)
switch software {
case showmount:
software = nfs
case rbd:
software = ceph
case glusterfs:
software = glusterfs
}
if err != nil || strings.Contains(res, "not found") {
results[software] = ""
} else {
// software in path
if strings.Contains(res, "bin/") {
results[software] = "y"
} else {
// get software version, e.g. docker, containerd, etc.
results[software] = res
}
}
}
output, err := runtime.GetRunner().Cmd("date +\"%Z %H:%M:%S\"", false)
if err != nil {
results["time"] = ""
} else {
results["time"] = strings.TrimSpace(output)
}
host := runtime.RemoteHost()
if res, ok := host.GetCache().Get(common.NodePreCheck); ok {
m := res.(map[string]string)
m = results
host.GetCache().Set(common.NodePreCheck, m)
} else {
host.GetCache().Set(common.NodePreCheck, results)
}
return nil
}
type GetKubeConfig struct {
common.KubeAction
}
func (g *GetKubeConfig) Execute(runtime connector.Runtime) error {
if exist, err := runtime.GetRunner().FileExist("$HOME/.kube/config"); err != nil {
return err
} else {
if exist {
return nil
} else {
if exist, err := runtime.GetRunner().FileExist("/etc/kubernetes/admin.conf"); err != nil {
return err
} else {
if exist {
if _, err := runtime.GetRunner().Cmd("mkdir -p $HOME/.kube", false); err != nil {
return err
}
if _, err := runtime.GetRunner().SudoCmd("cp /etc/kubernetes/admin.conf $HOME/.kube/config", false); err != nil {
return err
}
userId, err := runtime.GetRunner().Cmd("echo $(id -u)", false)
if err != nil {
return errors.Wrap(errors.WithStack(err), "get user id failed")
}
userGroupId, err := runtime.GetRunner().Cmd("echo $(id -g)", false)
if err != nil {
return errors.Wrap(errors.WithStack(err), "get user group id failed")
}
chownKubeConfig := fmt.Sprintf("chown -R %s:%s $HOME/.kube", userId, userGroupId)
if _, err := runtime.GetRunner().SudoCmd(chownKubeConfig, false); err != nil {
return errors.Wrap(errors.WithStack(err), "chown user kube config failed")
}
}
}
}
}
return errors.New("kube config not found")
}
type GetAllNodesK8sVersion struct {
common.KubeAction
}
func (g *GetAllNodesK8sVersion) Execute(runtime connector.Runtime) error {
var nodeK8sVersion string
kubeletVersionInfo, err := runtime.GetRunner().SudoCmd("/usr/local/bin/kubelet --version", false)
if err != nil {
return errors.Wrap(err, "get current kubelet version failed")
}
nodeK8sVersion = strings.Split(kubeletVersionInfo, " ")[1]
host := runtime.RemoteHost()
host.GetCache().Set(common.NodeK8sVersion, nodeK8sVersion)
if host.IsRole(common.Master) {
apiserverVersion, err := runtime.GetRunner().SudoCmd(
"cat /etc/kubernetes/manifests/kube-apiserver.yaml | grep 'image:' | rev | cut -d ':' -f1 | rev",
false)
if err != nil {
return errors.Wrap(err, "get current kube-apiserver version failed")
}
apiserverSemanticVersion, err := versionutil.ParseSemantic(apiserverVersion)
if err != nil {
return errors.Wrap(err, "parse kube-apiserver version failed")
}
kubeletSemanticVersion, err := versionutil.ParseSemantic(nodeK8sVersion)
if err != nil {
return errors.Wrap(err, "parse kubelet version failed")
}
if apiserverSemanticVersion.LessThan(kubeletSemanticVersion) {
host.GetCache().Set(common.NodeK8sVersion, apiserverVersion)
}
}
return nil
}
type CalculateMinK8sVersion struct {
common.KubeAction
}
func (g *CalculateMinK8sVersion) Execute(runtime connector.Runtime) error {
versionList := make([]*versionutil.Version, 0, len(runtime.GetHostsByRole(common.K8s)))
for _, host := range runtime.GetHostsByRole(common.K8s) {
version, ok := host.GetCache().GetMustString(common.NodeK8sVersion)
if !ok {
return errors.Errorf("get node %s Kubernetes version failed by host cache", host.GetName())
}
if versionObj, err := versionutil.ParseSemantic(version); err != nil {
return errors.Wrap(err, "parse node version failed")
} else {
versionList = append(versionList, versionObj)
}
}
minVersion := versionList[0]
for _, version := range versionList {
if !minVersion.LessThan(version) {
minVersion = version
}
}
g.PipelineCache.Set(common.K8sVersion, fmt.Sprintf("v%s", minVersion))
return nil
}
type CheckDesiredK8sVersion struct {
common.KubeAction
}
func (k *CheckDesiredK8sVersion) Execute(_ connector.Runtime) error {
if ok := kubernetes.VersionSupport(k.KubeConf.Cluster.Kubernetes.Version); !ok {
return errors.New(fmt.Sprintf("does not support upgrade to Kubernetes %s",
k.KubeConf.Cluster.Kubernetes.Version))
}
k.PipelineCache.Set(common.DesiredK8sVersion, k.KubeConf.Cluster.Kubernetes.Version)
return nil
}
type KsVersionCheck struct {
common.KubeAction
}
func (k *KsVersionCheck) Execute(runtime connector.Runtime) error {
ksVersionStr, err := runtime.GetRunner().SudoCmd(
"/usr/local/bin/kubectl get deploy -n kubesphere-system ks-console -o jsonpath='{.metadata.labels.version}'",
false)
if err != nil {
if k.KubeConf.Cluster.KubeSphere.Enabled {
return errors.Wrap(err, "get kubeSphere version failed")
} else {
ksVersionStr = ""
}
}
ccKsVersionStr, ccErr := runtime.GetRunner().SudoCmd(
"/usr/local/bin/kubectl get ClusterConfiguration ks-installer -n kubesphere-system -o jsonpath='{.metadata.labels.version}'",
false)
if ccErr == nil && versionutil.MustParseSemantic(ccKsVersionStr).AtLeast(versionutil.MustParseSemantic("v3.1.0")) {
ksVersionStr = ccKsVersionStr
}
k.PipelineCache.Set(common.KubeSphereVersion, ksVersionStr)
return nil
}
type DependencyCheck struct {
common.KubeAction
}
func (d *DependencyCheck) Execute(_ connector.Runtime) error {
currentKsVersion, ok := d.PipelineCache.GetMustString(common.KubeSphereVersion)
if !ok {
return errors.New("get current KubeSphere version failed by pipeline cache")
}
desiredVersion := d.KubeConf.Cluster.KubeSphere.Version
if d.KubeConf.Cluster.KubeSphere.Enabled {
var version string
if latest, ok := kubesphere.LatestRelease(desiredVersion); ok {
version = latest.Version
} else if ks, ok := kubesphere.DevRelease(desiredVersion); ok {
version = ks.Version
} else {
r := regexp.MustCompile("v(\\d+\\.)?(\\d+\\.)?(\\*|\\d+)")
version = r.FindString(desiredVersion)
}
ksInstaller, ok := kubesphere.VersionMap[version]
if !ok {
return errors.New(fmt.Sprintf("Unsupported version: %s", desiredVersion))
}
if currentKsVersion != desiredVersion {
if ok := ksInstaller.UpgradeSupport(currentKsVersion); !ok {
return errors.New(fmt.Sprintf("Unsupported upgrade plan: %s to %s", currentKsVersion, desiredVersion))
}
}
if ok := ksInstaller.K8sSupport(d.KubeConf.Cluster.Kubernetes.Version); !ok {
return errors.New(fmt.Sprintf("KubeSphere %s does not support running on Kubernetes %s",
version, d.KubeConf.Cluster.Kubernetes.Version))
}
} else {
ksInstaller, ok := kubesphere.VersionMap[currentKsVersion]
if !ok {
return errors.New(fmt.Sprintf("Unsupported version: %s", currentKsVersion))
}
if ok := ksInstaller.K8sSupport(d.KubeConf.Cluster.Kubernetes.Version); !ok {
return errors.New(fmt.Sprintf("KubeSphere %s does not support running on Kubernetes %s",
currentKsVersion, d.KubeConf.Cluster.Kubernetes.Version))
}
}
return nil
}
type GetKubernetesNodesStatus struct {
common.KubeAction
}
func (g *GetKubernetesNodesStatus) Execute(runtime connector.Runtime) error {
nodeStatus, err := runtime.GetRunner().SudoCmd("/usr/local/bin/kubectl get node -o wide", false)
if err != nil {
return err
}
g.PipelineCache.Set(common.ClusterNodeStatus, nodeStatus)
cri, err := runtime.GetRunner().SudoCmd("/usr/local/bin/kubectl get node -o jsonpath=\"{.items[*].status.nodeInfo.containerRuntimeVersion}\"", false)
if err != nil {
return err
}
g.PipelineCache.Set(common.ClusterNodeCRIRuntimes, cri)
featureGates, err := runtime.GetRunner().SudoCmd("cat /etc/kubernetes/manifests/kube-apiserver.yaml | grep feature-gates", false)
if err != nil {
return err
}
g.PipelineCache.Set(common.ClusterFeatureGates, featureGates)
return nil
}