This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
join.go
193 lines (174 loc) · 5.16 KB
/
join.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
package kubernetes
import (
"bytes"
"fmt"
"strings"
"time"
"github.com/pkg/errors"
"github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/core/infra"
"github.com/caos/orbos/mntr"
)
type CloudIntegration int
func join(
monitor mntr.Monitor,
clusterID string,
joining *initializedMachine,
joinAt infra.Machine,
desired DesiredV0,
kubeAPI *infra.Address,
joinToken string,
kubernetesVersion KubernetesVersion,
certKey string) (*string, error) {
monitor = monitor.WithFields(map[string]interface{}{
"machine": joining.infra.ID(),
"tier": joining.pool.tier,
})
var applyNetworkCommand string
switch desired.Spec.Networking.Network {
case "cilium":
applyNetworkCommand = "kubectl create -f https://raw.githubusercontent.com/cilium/cilium/1.6.3/install/kubernetes/quick-install.yaml"
case "calico":
applyNetworkCommand = fmt.Sprintf(`curl https://docs.projectcalico.org/v3.10/manifests/calico.yaml -O && sed -i -e "s?192.168.0.0/16?%s?g" calico.yaml && kubectl apply -f calico.yaml`, desired.Spec.Networking.PodCidr)
default:
return nil, errors.Errorf("Unknown network implementation %s", desired.Spec.Networking.Network)
}
kubeadmCfgPath := "/etc/kubeadm/config.yaml"
kubeadmCfg := fmt.Sprintf(`apiVersion: kubeadm.k8s.io/v1beta2
kind: InitConfiguration
bootstrapTokens:
- groups:
- system:bootstrappers:kubeadm:default-node-token
token: %s
ttl: 10m0s
usages:
- signing
- authentication
localAPIEndpoint:
advertiseAddress: %s
bindPort: %d
nodeRegistration:
# criSocket: /var/run/dockershim.sock
name: %s
taints:
- effect: NoSchedule
key: node-role.kubernetes.io/master
---
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
cgroupDriver: systemd
---
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
apiServer:
timeoutForControlPlane: 4m0s
certSANs:
- "%s"
certificatesDir: /etc/kubernetes/pki
clusterName: %s
controlPlaneEndpoint: %s
controllerManager: {}
dns:
type: CoreDNS
etcd:
local:
dataDir: /var/lib/etcd
extraArgs:
listen-metrics-urls: http://0.0.0.0:2381
imageRepository: k8s.gcr.io
kubernetesVersion: %s
networking:
dnsDomain: %s
podSubnet: %s
serviceSubnet: %s
scheduler: {}
`,
joinToken,
joining.infra.IP(),
kubeAPI.BackendPort,
joining.infra.ID(),
kubeAPI.Location,
clusterID,
kubeAPI,
kubernetesVersion,
desired.Spec.Networking.DNSDomain,
desired.Spec.Networking.PodCidr,
desired.Spec.Networking.ServiceCidr)
if joinAt != nil {
kubeadmCfg += fmt.Sprintf(`---
apiVersion: kubeadm.k8s.io/v1beta2
kind: JoinConfiguration
caCertPath: /etc/kubernetes/pki/ca.crt
discovery:
bootstrapToken:
apiServerEndpoint: %s:%d
token: %s
unsafeSkipCAVerification: true
timeout: 5m0s
nodeRegistration:
name: %s
`,
joinAt.IP(),
kubeAPI.BackendPort,
joinToken,
joining.infra.ID())
if joining.pool.tier == Controlplane {
kubeadmCfg += fmt.Sprintf(`controlPlane:
localAPIEndpoint:
advertiseAddress: %s
bindPort: %d
certificateKey: %s
`,
joining.infra.IP(),
kubeAPI.BackendPort,
certKey)
}
}
if err := infra.Try(monitor, time.NewTimer(7*time.Second), 2*time.Second, joining.infra, func(cmp infra.Machine) error {
return cmp.WriteFile(kubeadmCfgPath, strings.NewReader(kubeadmCfg), 600)
}); err != nil {
return nil, err
}
monitor.WithFields(map[string]interface{}{
"path": kubeadmCfgPath,
}).Debug("Written file")
cmd := "sudo kubeadm reset -f && sudo rm -rf /var/lib/etcd"
resetStdout, err := joining.infra.Execute(nil, cmd)
if err != nil {
return nil, errors.Wrapf(err, "executing %s failed", cmd)
}
monitor.WithFields(map[string]interface{}{
"stdout": string(resetStdout),
}).Debug("Cleaned up machine")
if joinAt != nil {
cmd := fmt.Sprintf("sudo kubeadm join --ignore-preflight-errors=Port-%d %s:%d --config %s", kubeAPI.BackendPort, joinAt.IP(), kubeAPI.FrontendPort, kubeadmCfgPath)
joinStdout, err := joining.infra.Execute(nil, cmd)
if err != nil {
return nil, errors.Wrapf(err, "executing %s failed", cmd)
}
monitor.WithFields(map[string]interface{}{
"stdout": string(joinStdout),
}).Debug("Executed kubeadm join")
joining.currentMachine.Joined = true
monitor.Changed("Node joined")
return nil, joining.pool.infra.EnsureMember(joining.infra)
}
if err := joining.pool.infra.EnsureMember(joining.infra); err != nil {
return nil, err
}
initCmd := fmt.Sprintf("sudo kubeadm init --ignore-preflight-errors=Port-%d --config %s && mkdir -p ${HOME}/.kube && yes | sudo cp -rf /etc/kubernetes/admin.conf ${HOME}/.kube/config && sudo chown $(id -u):$(id -g) ${HOME}/.kube/config && %s", kubeAPI.BackendPort, kubeadmCfgPath, applyNetworkCommand)
initStdout, err := joining.infra.Execute(nil, initCmd)
if err != nil {
return nil, err
}
monitor.WithFields(map[string]interface{}{
"stdout": string(initStdout),
}).Debug("Executed kubeadm init")
kubeconfigBuf := new(bytes.Buffer)
if err := joining.infra.ReadFile("${HOME}/.kube/config", kubeconfigBuf); err != nil {
return nil, err
}
joining.currentMachine.Joined = true
monitor.Changed("Cluster initialized")
kc := strings.ReplaceAll(kubeconfigBuf.String(), "kubernetes-admin", strings.Join([]string{clusterID, "admin"}, "-"))
return &kc, nil
}