forked from rancher/rke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hosts.go
356 lines (330 loc) · 10.4 KB
/
hosts.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package hosts
import (
"context"
"fmt"
"path"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/sirupsen/logrus"
"github.com/docker/docker/client"
"github.com/rancher/rke/docker"
"github.com/rancher/rke/k8s"
"github.com/rancher/rke/log"
"github.com/rancher/types/apis/management.cattle.io/v3"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/kubernetes"
)
type Host struct {
v3.RKEConfigNode
DClient *client.Client
LocalConnPort int
IsControl bool
IsWorker bool
IsEtcd bool
IgnoreDockerVersion bool
ToAddEtcdMember bool
ExistingEtcdCluster bool
SavedKeyPhrase string
ToAddLabels map[string]string
ToDelLabels map[string]string
ToAddTaints []string
ToDelTaints []string
DockerInfo types.Info
UpdateWorker bool
PrefixPath string
BastionHost v3.BastionHost
}
const (
ToCleanEtcdDir = "/var/lib/etcd/"
ToCleanSSLDir = "/etc/kubernetes/"
ToCleanCNIConf = "/etc/cni/"
ToCleanCNIBin = "/opt/cni/"
ToCleanCNILib = "/var/lib/cni/"
ToCleanCalicoRun = "/var/run/calico/"
ToCleanTempCertPath = "/etc/kubernetes/.tmp/"
CleanerContainerName = "kube-cleaner"
LogCleanerContainerName = "rke-log-cleaner"
RKELogsPath = "/var/lib/rancher/rke/log"
B2DOS = "Boot2Docker"
B2DPrefixPath = "/mnt/sda1/rke"
ROS = "RancherOS"
ROSPrefixPath = "/opt/rke"
CoreOS = "CoreOS"
CoreOSPrefixPath = "/opt/rke"
)
func (h *Host) CleanUpAll(ctx context.Context, cleanerImage string, prsMap map[string]v3.PrivateRegistry, externalEtcd bool) error {
log.Infof(ctx, "[hosts] Cleaning up host [%s]", h.Address)
toCleanPaths := []string{
path.Join(h.PrefixPath, ToCleanSSLDir),
ToCleanCNIConf,
ToCleanCNIBin,
ToCleanCalicoRun,
path.Join(h.PrefixPath, ToCleanTempCertPath),
path.Join(h.PrefixPath, ToCleanCNILib),
}
if !externalEtcd {
toCleanPaths = append(toCleanPaths, path.Join(h.PrefixPath, ToCleanEtcdDir))
}
return h.CleanUp(ctx, toCleanPaths, cleanerImage, prsMap)
}
func (h *Host) CleanUpWorkerHost(ctx context.Context, cleanerImage string, prsMap map[string]v3.PrivateRegistry) error {
if h.IsControl || h.IsEtcd {
log.Infof(ctx, "[hosts] Host [%s] is already a controlplane or etcd host, skipping cleanup.", h.Address)
return nil
}
toCleanPaths := []string{
path.Join(h.PrefixPath, ToCleanSSLDir),
ToCleanCNIConf,
ToCleanCNIBin,
ToCleanCalicoRun,
path.Join(h.PrefixPath, ToCleanCNILib),
}
return h.CleanUp(ctx, toCleanPaths, cleanerImage, prsMap)
}
func (h *Host) CleanUpControlHost(ctx context.Context, cleanerImage string, prsMap map[string]v3.PrivateRegistry) error {
if h.IsWorker || h.IsEtcd {
log.Infof(ctx, "[hosts] Host [%s] is already a worker or etcd host, skipping cleanup.", h.Address)
return nil
}
toCleanPaths := []string{
path.Join(h.PrefixPath, ToCleanSSLDir),
ToCleanCNIConf,
ToCleanCNIBin,
ToCleanCalicoRun,
path.Join(h.PrefixPath, ToCleanCNILib),
}
return h.CleanUp(ctx, toCleanPaths, cleanerImage, prsMap)
}
func (h *Host) CleanUpEtcdHost(ctx context.Context, cleanerImage string, prsMap map[string]v3.PrivateRegistry) error {
toCleanPaths := []string{
path.Join(h.PrefixPath, ToCleanEtcdDir),
path.Join(h.PrefixPath, ToCleanSSLDir),
}
if h.IsWorker || h.IsControl {
log.Infof(ctx, "[hosts] Host [%s] is already a worker or control host, skipping cleanup certs.", h.Address)
toCleanPaths = []string{
path.Join(h.PrefixPath, ToCleanEtcdDir),
}
}
return h.CleanUp(ctx, toCleanPaths, cleanerImage, prsMap)
}
func (h *Host) CleanUp(ctx context.Context, toCleanPaths []string, cleanerImage string, prsMap map[string]v3.PrivateRegistry) error {
log.Infof(ctx, "[hosts] Cleaning up host [%s]", h.Address)
imageCfg, hostCfg := buildCleanerConfig(h, toCleanPaths, cleanerImage)
log.Infof(ctx, "[hosts] Running cleaner container on host [%s]", h.Address)
if err := docker.DoRunContainer(ctx, h.DClient, imageCfg, hostCfg, CleanerContainerName, h.Address, CleanerContainerName, prsMap); err != nil {
return err
}
if _, err := docker.WaitForContainer(ctx, h.DClient, h.Address, CleanerContainerName); err != nil {
return err
}
log.Infof(ctx, "[hosts] Removing cleaner container on host [%s]", h.Address)
if err := docker.RemoveContainer(ctx, h.DClient, h.Address, CleanerContainerName); err != nil {
return err
}
log.Infof(ctx, "[hosts] Removing dead container logs on host [%s]", h.Address)
if err := DoRunLogCleaner(ctx, h, cleanerImage, prsMap); err != nil {
return err
}
log.Infof(ctx, "[hosts] Successfully cleaned up host [%s]", h.Address)
return nil
}
func DeleteNode(ctx context.Context, toDeleteHost *Host, kubeClient *kubernetes.Clientset, hasAnotherRole bool, cloudProvider string) error {
if hasAnotherRole {
log.Infof(ctx, "[hosts] host [%s] has another role, skipping delete from kubernetes cluster", toDeleteHost.Address)
return nil
}
log.Infof(ctx, "[hosts] Cordoning host [%s]", toDeleteHost.Address)
if _, err := k8s.GetNode(kubeClient, toDeleteHost.HostnameOverride); err != nil {
if apierrors.IsNotFound(err) {
log.Warnf(ctx, "[hosts] Can't find node by name [%s]", toDeleteHost.Address)
return nil
}
return err
}
if err := k8s.CordonUncordon(kubeClient, toDeleteHost.HostnameOverride, true); err != nil {
return err
}
log.Infof(ctx, "[hosts] Deleting host [%s] from the cluster", toDeleteHost.Address)
if err := k8s.DeleteNode(kubeClient, toDeleteHost.HostnameOverride, cloudProvider); err != nil {
return err
}
log.Infof(ctx, "[hosts] Successfully deleted host [%s] from the cluster", toDeleteHost.Address)
return nil
}
func RemoveTaintFromHost(ctx context.Context, host *Host, taintKey string, kubeClient *kubernetes.Clientset) error {
log.Infof(ctx, "[hosts] removing taint [%s] from host [%s]", taintKey, host.Address)
if err := k8s.RemoveTaintFromNodeByKey(kubeClient, host.HostnameOverride, taintKey); err != nil {
return err
}
log.Infof(ctx, "[hosts] Successfully deleted taint [%s] from host [%s]", taintKey, host.Address)
return nil
}
func GetToDeleteHosts(currentHosts, configHosts, inactiveHosts []*Host) []*Host {
toDeleteHosts := []*Host{}
for _, currentHost := range currentHosts {
found := false
for _, newHost := range configHosts {
if currentHost.Address == newHost.Address {
found = true
}
}
if !found {
inactive := false
for _, inactiveHost := range inactiveHosts {
if inactiveHost.Address == currentHost.Address {
inactive = true
break
}
}
if !inactive {
toDeleteHosts = append(toDeleteHosts, currentHost)
}
}
}
return toDeleteHosts
}
func GetToAddHosts(currentHosts, configHosts []*Host) []*Host {
toAddHosts := []*Host{}
for _, configHost := range configHosts {
found := false
for _, currentHost := range currentHosts {
if currentHost.Address == configHost.Address {
found = true
break
}
}
if !found {
toAddHosts = append(toAddHosts, configHost)
}
}
return toAddHosts
}
func IsHostListChanged(currentHosts, configHosts []*Host) bool {
changed := false
for _, host := range currentHosts {
found := false
for _, configHost := range configHosts {
if host.Address == configHost.Address {
found = true
break
}
}
if !found {
return true
}
}
for _, host := range configHosts {
found := false
for _, currentHost := range currentHosts {
if host.Address == currentHost.Address {
found = true
break
}
}
if !found {
return true
}
}
return changed
}
func buildCleanerConfig(host *Host, toCleanDirs []string, cleanerImage string) (*container.Config, *container.HostConfig) {
cmd := append([]string{"rm", "-rf"}, toCleanDirs...)
imageCfg := &container.Config{
Image: cleanerImage,
Cmd: cmd,
}
bindMounts := []string{}
for _, vol := range toCleanDirs {
bindMounts = append(bindMounts, fmt.Sprintf("%s:%s:z", vol, vol))
}
hostCfg := &container.HostConfig{
Binds: bindMounts,
}
return imageCfg, hostCfg
}
func NodesToHosts(rkeNodes []v3.RKEConfigNode, nodeRole string) []*Host {
hostList := make([]*Host, 0)
for _, node := range rkeNodes {
for _, role := range node.Role {
if role == nodeRole {
newHost := Host{
RKEConfigNode: node,
}
hostList = append(hostList, &newHost)
break
}
}
}
return hostList
}
func GetUniqueHostList(etcdHosts, cpHosts, workerHosts []*Host) []*Host {
hostList := []*Host{}
hostList = append(hostList, etcdHosts...)
hostList = append(hostList, cpHosts...)
hostList = append(hostList, workerHosts...)
// little trick to get a unique host list
uniqHostMap := make(map[*Host]bool)
for _, host := range hostList {
uniqHostMap[host] = true
}
uniqHostList := []*Host{}
for host := range uniqHostMap {
uniqHostList = append(uniqHostList, host)
}
return uniqHostList
}
func GetPrefixPath(osType, ClusterPrefixPath string) string {
var prefixPath string
switch {
case ClusterPrefixPath != "/":
prefixPath = ClusterPrefixPath
case strings.Contains(osType, B2DOS):
prefixPath = B2DPrefixPath
case strings.Contains(osType, ROS):
prefixPath = ROSPrefixPath
case strings.Contains(osType, CoreOS):
prefixPath = CoreOSPrefixPath
default:
prefixPath = ClusterPrefixPath
}
return prefixPath
}
func DoRunLogCleaner(ctx context.Context, host *Host, alpineImage string, prsMap map[string]v3.PrivateRegistry) error {
logrus.Debugf("[cleanup] Starting log link cleanup on host [%s]", host.Address)
imageCfg := &container.Config{
Image: alpineImage,
Tty: true,
Cmd: []string{
"sh",
"-c",
fmt.Sprintf("find %s -type l ! -exec test -e {} \\; -print -delete", RKELogsPath),
},
}
hostCfg := &container.HostConfig{
Binds: []string{
"/var/lib:/var/lib",
},
Privileged: true,
}
if err := docker.DoRemoveContainer(ctx, host.DClient, LogCleanerContainerName, host.Address); err != nil {
return err
}
if err := docker.DoRunContainer(ctx, host.DClient, imageCfg, hostCfg, LogCleanerContainerName, host.Address, "cleanup", prsMap); err != nil {
return err
}
if err := docker.DoRemoveContainer(ctx, host.DClient, LogCleanerContainerName, host.Address); err != nil {
return err
}
logrus.Debugf("[cleanup] Successfully cleaned up log links on host [%s]", host.Address)
return nil
}
func IsNodeInList(host *Host, hostList []*Host) bool {
for _, h := range hostList {
if h.HostnameOverride == host.HostnameOverride {
return true
}
}
return false
}