-
Notifications
You must be signed in to change notification settings - Fork 7
/
install.go
240 lines (220 loc) · 7.88 KB
/
install.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
package utils
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
"github.com/Jeffail/gabs"
log "github.com/platform9/nodeadm/pkg/logrus"
"github.com/platform9/nodeadm/apis"
"github.com/platform9/nodeadm/constants"
"github.com/platform9/nodeadm/systemd"
)
func InstallMasterComponents(config *apis.InitConfiguration) {
PopulateCache()
placeKubeComponents()
placeCNIPlugin()
if err := systemd.StopIfActive("kubelet.service"); err != nil {
log.Fatalf("Failed to install kubelet service: %v", err)
}
if err := systemd.DisableIfEnabled("kubelet.service"); err != nil {
log.Fatalf("Failed to install kubelet service: %v", err)
}
placeAndModifyKubeletServiceFile()
placeAndModifyKubeadmKubeletSystemdDropin()
if err := systemd.Enable("kubelet.service"); err != nil {
log.Fatalf("Failed to install kubelet service: %v", err)
}
if err := systemd.Start("kubelet.service"); err != nil {
log.Fatalf("Failed to install kubelet service: %v", err)
}
if config.VIPConfiguration == nil {
return
}
if err := systemd.StopIfActive("keepalived.service"); err != nil {
log.Fatalf("Failed to install keepalived service: %v", err)
}
if err := systemd.DisableIfEnabled("keepalived.service"); err != nil {
log.Fatalf("Failed to install keepalived service: %v", err)
}
if err := writeKeepAlivedServiceFiles(config); err != nil {
log.Fatalf("Failed to configure keepalived: %v", err)
}
if err := systemd.Enable("keepalived.service"); err != nil {
log.Fatalf("Failed to install keepalived service: %v", err)
}
if err := systemd.Start("keepalived.service"); err != nil {
log.Fatalf("Failed to install keepalived service: %v", err)
}
}
func InstallNodeComponents() {
PopulateCache()
placeKubeComponents()
placeCNIPlugin()
if err := systemd.StopIfActive("kubelet.service"); err != nil {
log.Fatalf("Failed to install kubelet service: %v", err)
}
if err := systemd.DisableIfEnabled("kubelet.service"); err != nil {
log.Fatalf("Failed to install kubelet service: %v", err)
}
placeAndModifyKubeletServiceFile()
placeAndModifyKubeadmKubeletSystemdDropin()
if err := systemd.Enable("kubelet.service"); err != nil {
log.Fatalf("Failed to install kubelet service: %v", err)
}
if err := systemd.Start("kubelet.service"); err != nil {
log.Fatalf("Failed to install kubelet service: %v", err)
}
}
func placeAndModifyKubeletServiceFile() {
serviceFile := filepath.Join(constants.SystemdDir, "kubelet.service")
_, err := copyFile(filepath.Join(constants.CacheDir, constants.KubeDirName, "kubelet.service"), serviceFile)
checkError(err, "Unable to copy file")
ReplaceString(serviceFile, "/usr/bin", constants.BaseInstallDir)
}
func placeAndModifyKubeadmKubeletSystemdDropin() {
err := os.MkdirAll(filepath.Join(constants.SystemdDir, "kubelet.service.d"), constants.Execute)
if err != nil {
log.Fatalf("\nFailed to create dir with error %v", err)
}
confFile := filepath.Join(constants.SystemdDir, "kubelet.service.d", constants.KubeadmKubeletSystemdDropinFilename)
_, err = copyFile(filepath.Join(constants.CacheDir, constants.KubeDirName, constants.KubeadmKubeletSystemdDropinFilename), confFile)
checkError(err, "Unable to copy file")
ReplaceString(confFile, "/usr/bin", constants.BaseInstallDir)
}
func placeKubeComponents() {
_, err := copyFile(filepath.Join(constants.CacheDir, constants.KubeDirName, "kubectl"), filepath.Join(constants.BaseInstallDir, "kubectl"))
checkError(err, "Unable to copy file")
_, err = copyFile(filepath.Join(constants.CacheDir, constants.KubeDirName, "kubeadm"), filepath.Join(constants.BaseInstallDir, "kubeadm"))
checkError(err, "Unable to copy file")
_, err = copyFile(filepath.Join(constants.CacheDir, constants.KubeDirName, "kubelet"), filepath.Join(constants.BaseInstallDir, "kubelet"))
checkError(err, "Unable to copy file")
}
func checkError(err error, message string) {
if err != nil {
log.Fatalf("%s: %s", message, err)
}
}
func copyFile(src string, dst string) ([]byte, error) {
cmd := exec.Command("cp", src, dst)
out, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("failed to run %q: %s", strings.Join(cmd.Args, " "), err)
}
return out, err
}
func placeCNIPlugin() {
tmpFile := fmt.Sprintf("cni-plugins-amd64-%s.tgz", constants.CNIVersion)
_, err := copyFile(filepath.Join(constants.CacheDir, constants.CNIDirName, tmpFile), filepath.Join("/tmp", tmpFile))
checkError(err, "Unable to copy file")
if _, err = os.Stat(constants.CniVersionInstallDir); os.IsNotExist(err) {
err := os.MkdirAll(constants.CniVersionInstallDir, constants.Execute)
if err != nil {
log.Fatalf("\nFailed to create dir %s with error %v", constants.CniVersionInstallDir, err)
}
cmd := exec.Command("tar", "-xvf", filepath.Join("/tmp", tmpFile), "-C", constants.CniVersionInstallDir)
err = cmd.Run()
if err != nil {
log.Fatalf("Failed to run %q: %s", strings.Join(cmd.Args, " "), err)
}
CreateSymLinks(constants.CniVersionInstallDir, constants.CNIBaseDir, true)
}
}
func writeTemplateIntoFile(tmpl, name, file string, data interface{}) {
err := os.MkdirAll(filepath.Dir(file), constants.Read)
if err != nil {
log.Fatalf("Failed to create dirs for path %s with error %v", filepath.Dir(file), err)
}
f, err := os.Create(file)
if err != nil {
log.Fatalf("Failed to create file %q: %v", file, err)
}
defer f.Close()
w := bufio.NewWriter(f)
t := template.Must(template.New(name).Parse(tmpl))
t.Execute(w, data)
err = w.Flush()
if err != nil {
log.Fatalf("Failed to write to file %q: %v", file, err)
}
}
func writeKeepAlivedServiceFiles(config *apis.InitConfiguration) error {
p, err := gabs.Consume(config.MasterConfiguration)
if err != nil {
return fmt.Errorf("unable to parse masterConfiguration: %s", err)
}
v := p.Path("api.bindPort").Data()
if v == nil {
return fmt.Errorf("masterConfiguration.api.bindPort is not defined. This is a bug, please file an issue on github.com/platform9/nodeadm")
}
apiBindPort, ok := v.(int)
if !ok {
return fmt.Errorf("unable to parse masterConfiguration.api.bindPort")
}
configTemplateVals := struct {
VIPConfiguration *apis.VIPConfiguration
APIBindPort int
VRRPScriptInterval int
VRRPScriptRise int
VRRPScriptFall int
WgetTimeout int
}{
VIPConfiguration: config.VIPConfiguration,
APIBindPort: apiBindPort,
VRRPScriptInterval: constants.VRRPScriptInterval,
VRRPScriptRise: constants.VRRPScriptRise,
VRRPScriptFall: constants.VRRPScriptFall,
WgetTimeout: constants.WgetTimeout,
}
kaConfFileTemplate := `global_defs {
enable_script_security
}
vrrp_script chk_apiserver {
script "/usr/bin/wget -T {{.WgetTimeout}} -qO /dev/null https://127.0.0.1:{{.APIBindPort}}/healthz"
interval {{.VRRPScriptInterval}}
fall {{.VRRPScriptFall}}
rise {{.VRRPScriptRise}}
}
vrrp_instance K8S_APISERVER {
interface {{.VIPConfiguration.NetworkInterface}}
state BACKUP
virtual_router_id {{.VIPConfiguration.RouterID}}
nopreempt
virtual_ipaddress {
{{.VIPConfiguration.IP}}
}
track_script {
chk_apiserver
}
}`
writeTemplateIntoFile(kaConfFileTemplate, "vipConfFileTemplate", constants.KeepalivedConfigFilename, configTemplateVals)
kaSvcFileTemplate := `
[Unit]
Description= Keepalived service
After=network.target docker.service
Requires=docker.service
[Service]
Type=simple
ExecStart=/usr/bin/docker run --cap-add=NET_ADMIN \
--net=host --name vip \
-v {{.ConfigFile}}:/usr/local/etc/keepalived/keepalived.conf \
{{.KeepAlivedImg}}
ExecStartPre=-/usr/bin/docker kill vip
ExecStartPre=-/usr/bin/docker rm vip
ExecStop=/usr/bin/docker stop vip
ExecStopPost=/usr/bin/docker rm vip
Restart=on-failure
MemoryLow=10M
[Install]
WantedBy=multi-user.target
`
type KaServiceData struct {
ConfigFile, KeepAlivedImg string
}
kaServiceData := KaServiceData{constants.KeepalivedConfigFilename, constants.KeepalivedImage}
writeTemplateIntoFile(kaSvcFileTemplate, "kaSvcFileTemplate", filepath.Join(constants.SystemdDir, "keepalived.service"), kaServiceData)
return nil
}