/
supkube.go
143 lines (125 loc) · 4.08 KB
/
supkube.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
package core
import (
"fmt"
"strings"
"github.com/currycan/supkube/cni"
"github.com/currycan/supkube/pkg/logger"
)
type CleanCluster interface {
Check
Clean
}
type JoinNodeAndMaster interface {
Check
Send
Join
}
type Init interface {
Check
Send
PreInit
Join
Print
}
type Install interface {
Check
Send
Apply
}
var (
JoinToken string
TokenCaCertHash string
CertificateKey string
)
//Installer is
type Installer struct {
Hosts []string
Masters []string
Nodes []string
Network string
APIServer string
}
type CommandType string
//command type
const InitMaster CommandType = "initMaster"
const JoinMaster CommandType = "joinMaster"
const JoinNode CommandType = "joinNode"
func (s *Installer) Command(version string, name CommandType) (cmd string) {
// Please convert your v1beta1 configuration files to v1beta2 using the
// "kubeadm config migrate" command of kubeadm v1.15.x, 因此1.14 版本不支持双网卡.
commands := map[CommandType]string{
InitMaster: `kubeadm init --config=/root/kubeadm-config.yaml --experimental-upload-certs` + vlogToStr(),
JoinMaster: fmt.Sprintf("kubeadm join %s:6443 --token %s --discovery-token-ca-cert-hash %s --experimental-control-plane --certificate-key %s"+vlogToStr(), IPFormat(s.Masters[0]), JoinToken, TokenCaCertHash, CertificateKey),
JoinNode: fmt.Sprintf("kubeadm join %s:6443 --token %s --discovery-token-ca-cert-hash %s"+vlogToStr(), VIP, JoinToken, TokenCaCertHash),
}
//other version >= 1.15.x
//todo
if VersionToInt(version) >= 115 {
commands[InitMaster] = `kubeadm init --config=/root/kubeadm-config.yaml --upload-certs` + vlogToStr()
commands[JoinMaster] = "kubeadm join --config=/root/kubeadm-join-config.yaml " + vlogToStr()
commands[JoinNode] = "kubeadm join --config=/root/kubeadm-join-config.yaml " + vlogToStr()
}
// version >= 1.16.x support kubeadm init --skip-phases=addon/kube-proxy
// version <= 115
// kubectl -n kube-system delete ds kube-proxy
// # Run on each node:
// iptables-restore <(iptables-save | grep -v KUBE)
if s.Network == cni.CILIUM {
if VersionToInt(version) >= 116 {
commands[InitMaster] = `kubeadm init --skip-phases=addon/kube-proxy --config=/root/kubeadm-config.yaml --upload-certs` + vlogToStr()
} else {
commands[InitMaster] = `kubeadm init --config=/root/kubeadm-config.yaml --upload-certs` + vlogToStr()
}
}
v, ok := commands[name]
defer func() {
if r := recover(); r != nil {
logger.Error("[globals]fetch command error")
}
}()
if !ok {
panic(1)
}
return v
}
//decode output to join token hash and key
func decodeOutput(output []byte) {
s0 := string(output)
logger.Debug("[globals]decodeOutput: %s", s0)
slice := strings.Split(s0, "kubeadm join")
slice1 := strings.Split(slice[1], "Please note")
logger.Info("[globals]join command is: %s", slice1[0])
decodeJoinCmd(slice1[0])
}
// 192.168.0.200:6443 --token 9vr73a.a8uxyaju799qwdjv --discovery-token-ca-cert-hash sha256:7c2e69131a36ae2a042a339b33381c6d0d43887e2de83720eff5359e26aec866 --experimental-control-plane --certificate-key f8902e114ef118304e561c3ecd4d0b543adc226b7a07f675f56564185ffe0c07
func decodeJoinCmd(cmd string) {
logger.Debug("[globals]decodeJoinCmd: %s", cmd)
stringSlice := strings.Split(cmd, " ")
for i, r := range stringSlice {
r = strings.ReplaceAll(r, "\t", "")
r = strings.ReplaceAll(r, "\n", "")
r = strings.ReplaceAll(r, "\\", "")
r = strings.TrimSpace(r)
logger.Debug("[####]%d :%s:", i, r)
// switch r {
// case "--token":
// JoinToken = stringSlice[i+1]
// case "--discovery-token-ca-cert-hash":
// TokenCaCertHash = stringSlice[i+1]
// case "--certificate-key":
// CertificateKey = stringSlice[i+1][:64]
// }
if strings.Contains(r, "--token") {
JoinToken = stringSlice[i+1]
}
if strings.Contains(r, "--discovery-token-ca-cert-hash") {
TokenCaCertHash = stringSlice[i+1]
}
if strings.Contains(r, "--certificate-key") {
CertificateKey = stringSlice[i+1][:64]
}
}
logger.Debug("[####]JoinToken :%s", JoinToken)
logger.Debug("[####]TokenCaCertHash :%s", TokenCaCertHash)
logger.Debug("[####]CertificateKey :%s", CertificateKey)
}