This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
clean.go
165 lines (154 loc) · 4.03 KB
/
clean.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2018 Datadog, Inc.
package setup
import (
"os"
"os/exec"
"strings"
"syscall"
"github.com/DataDog/pupernetes/pkg/util"
"github.com/coreos/go-systemd/dbus"
"github.com/golang/glog"
)
func remove(path string) error {
glog.V(4).Infof("Trying to remove %s ...", path)
_, err := os.Lstat(path)
if err != nil && os.IsNotExist(err) {
glog.V(4).Infof("Not existing path: %s", path)
return nil
}
glog.V(4).Infof("Removing %s ...", path)
err = os.RemoveAll(path)
if err != nil {
glog.Errorf("Unexpected error during cleanup of %s: %v", path, err)
return err
}
glog.Infof("Removed %s", path)
return nil
}
func (e *Environment) cleanMounts() error {
b, err := exec.Command("mount").CombinedOutput()
output := string(b)
if err != nil {
glog.Errorf("Cannot get mount list: %v, %s", err, output)
return err
}
for _, line := range strings.Split(output, "\n") {
fields := strings.Fields(line)
if len(fields) < 3 {
continue
}
mountPath := fields[2]
if !strings.HasPrefix(mountPath, e.kubeletRootDir) {
continue
}
glog.V(4).Infof("Removing mount %s", mountPath)
err := syscall.Unmount(mountPath, 0)
if err != nil {
glog.Errorf("Cannot umount %s: %v", mountPath, err)
continue
}
glog.Infof("Removed mount %s", mountPath)
}
return nil
}
func (e *Environment) Clean() error {
var toRemove []string
if e.cleanOptions.None {
glog.V(4).Infof("Cleanup skipped")
return nil
}
_, err := os.Stat(e.GetHyperkubePath())
if e.cleanOptions.Kubectl && err != nil {
glog.Warningf("Cannot use kubectl: %v", err)
}
if e.cleanOptions.Kubectl && err == nil {
for _, elt := range [][]string{
{"delete-context", defaultKubectlContextName},
{"delete-cluster", defaultKubectlClusterName},
{"unset", "current-context"},
} {
b, err := exec.Command(e.GetHyperkubePath(),
"kubectl",
"config",
elt[0],
elt[1],
).CombinedOutput()
output := string(b)
if err != nil && !strings.Contains(output, ", not in ") {
glog.Errorf("Cannot exec kubectl: %s", output)
}
}
}
if e.cleanOptions.Iptables && err == nil {
// this command can fail, it's a non issue
b, err := exec.Command(e.GetHyperkubePath(), "proxy", "--cleanup").CombinedOutput()
glog.V(5).Infof("Iptables clean: %s, %v", string(b), err)
}
if e.cleanOptions.Etcd {
toRemove = append(toRemove, e.etcdDataABSPath)
}
if e.cleanOptions.Manifests {
toRemove = append(toRemove,
e.manifestTemplatesABSPath,
e.manifestStaticPodABSPath,
e.manifestAPIABSPath,
e.manifestConfigABSPath,
e.manifestSystemdUnit,
)
}
if e.cleanOptions.Binaries {
toRemove = append(toRemove, e.binABSPath)
}
if e.cleanOptions.Secrets {
toRemove = append(toRemove, e.secretsABSPath)
}
if e.cleanOptions.Network {
toRemove = append(toRemove, e.networkABSPath)
}
if e.cleanOptions.Systemd {
glog.V(3).Infof("Shutting down systemd units")
conn, err := dbus.NewSystemdConnection()
if err != nil {
glog.Errorf("Cannot connect to dbus: %v", err)
return err
}
defer conn.Reload()
defer conn.Close()
for _, u := range e.systemdUnitNames {
toRemove = append(toRemove, UnitPath+u)
err = util.StopUnit(conn, u)
if err != nil {
glog.Errorf("Cannot stop systemd unit %s: %v", u, err)
// don't return
}
}
}
if e.cleanOptions.All {
toRemove = append(toRemove, e.rootABSPath)
}
if e.cleanOptions.Mounts {
e.cleanMounts()
}
if e.cleanOptions.Kubelet {
// don't do it twice
if !e.cleanOptions.Mounts {
e.cleanMounts()
}
toRemove = append(toRemove, e.kubeletRootDir, KubeletCRILogPath)
}
for _, elt := range toRemove {
err := remove(elt)
if err != nil {
if elt == e.kubeletRootDir && strings.Contains(err.Error(), "device or resource busy") {
glog.Warningf("Mounts still present in %s?", e.kubeletRootDir)
continue
}
return err
}
}
glog.Infof("Cleanup finished")
return nil
}