/
k3s_status.go
124 lines (107 loc) Β· 3.68 KB
/
k3s_status.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
/*
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 k3s
import (
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"strings"
"github.com/pkg/errors"
"github.com/kubesphere/kubekey/v2/pkg/common"
"github.com/kubesphere/kubekey/v2/pkg/core/connector"
)
type K3sStatus struct {
Version string
ClusterInfo string
NodeToken string
KubeConfig string
NodesInfo map[string]string
}
func NewK3sStatus() *K3sStatus {
return &K3sStatus{NodesInfo: make(map[string]string)}
}
func (k *K3sStatus) SearchVersion(runtime connector.Runtime) error {
cmd := "k3s --version | grep 'k3s' | awk '{print $3}'"
if output, err := runtime.GetRunner().Cmd(cmd, true); err != nil {
return errors.Wrap(errors.WithStack(err), "search current version failed")
} else {
k.Version = output
}
return nil
}
func (k *K3sStatus) SearchKubeConfig(runtime connector.Runtime) error {
kubeCfgCmd := "cat /etc/rancher/k3s/k3s.yaml"
if kubeConfigStr, err := runtime.GetRunner().SudoCmd(kubeCfgCmd, false); err != nil {
return errors.Wrap(errors.WithStack(err), "search cluster kubeconfig failed")
} else {
k.KubeConfig = kubeConfigStr
}
return nil
}
func (k *K3sStatus) SearchNodeToken(runtime connector.Runtime) error {
nodeTokenBase64Cmd := "cat /var/lib/rancher/k3s/server/node-token"
output, err := runtime.GetRunner().SudoCmd(nodeTokenBase64Cmd, true)
if err != nil {
return errors.Wrap(errors.WithStack(err), "get cluster node token failed")
}
k.NodeToken = output
return nil
}
func (k *K3sStatus) SearchInfo(runtime connector.Runtime) error {
output, err := runtime.GetRunner().SudoCmd(
"/usr/local/bin/kubectl --no-headers=true get nodes -o custom-columns=:metadata.name,:status.nodeInfo.kubeletVersion,:status.addresses",
true)
if err != nil {
return errors.Wrap(errors.WithStack(err), "get k3s cluster info failed")
}
k.ClusterInfo = output
return nil
}
func (k *K3sStatus) SearchNodesInfo(_ connector.Runtime) error {
ipv4Regexp, err := regexp.Compile(common.IPv4Regexp)
if err != nil {
return err
}
ipv6Regexp, err := regexp.Compile(common.IPv6Regexp)
if err != nil {
return err
}
tmp := strings.Split(k.ClusterInfo, "\r\n")
if len(tmp) >= 1 {
for i := 0; i < len(tmp); i++ {
if ipv4 := ipv4Regexp.FindStringSubmatch(tmp[i]); len(ipv4) != 0 {
k.NodesInfo[ipv4[0]] = ipv4[0]
}
if ipv6 := ipv6Regexp.FindStringSubmatch(tmp[i]); len(ipv6) != 0 {
k.NodesInfo[ipv6[0]] = ipv6[0]
}
if len(strings.Fields(tmp[i])) > 3 {
k.NodesInfo[strings.Fields(tmp[i])[0]] = strings.Fields(tmp[i])[1]
} else {
k.NodesInfo[strings.Fields(tmp[i])[0]] = ""
}
}
}
return nil
}
func (k *K3sStatus) LoadKubeConfig(runtime connector.Runtime, kubeConf *common.KubeConf) error {
kubeConfigPath := filepath.Join(runtime.GetWorkDir(), fmt.Sprintf("config-%s", runtime.GetObjName()))
oldServer := "server: https://127.0.0.1:6443"
newServer := fmt.Sprintf("server: https://%s:%d", kubeConf.Cluster.ControlPlaneEndpoint.Address, kubeConf.Cluster.ControlPlaneEndpoint.Port)
newKubeConfigStr := strings.Replace(k.KubeConfig, oldServer, newServer, -1)
if err := ioutil.WriteFile(kubeConfigPath, []byte(newKubeConfigStr), 0644); err != nil {
return err
}
return nil
}