-
Notifications
You must be signed in to change notification settings - Fork 124
/
provider.go
384 lines (319 loc) · 11.5 KB
/
provider.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
Copyright 2019 The Machine Controller Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//
// UserData plugin for CentOS.
//
package centos
import (
"errors"
"fmt"
"strings"
"text/template"
"github.com/Masterminds/semver/v3"
"go.uber.org/zap"
"github.com/kubermatic/machine-controller/pkg/apis/plugin"
providerconfigtypes "github.com/kubermatic/machine-controller/pkg/providerconfig/types"
userdatahelper "github.com/kubermatic/machine-controller/pkg/userdata/helper"
)
// Provider is a pkg/userdata/plugin.Provider implementation.
type Provider struct{}
// UserData renders user-data template to string.
func (p Provider) UserData(log *zap.SugaredLogger, req plugin.UserDataRequest) (string, error) {
tmpl, err := template.New("user-data").Funcs(userdatahelper.TxtFuncMap(log)).Parse(userDataTemplate)
if err != nil {
return "", fmt.Errorf("failed to parse user-data template: %w", err)
}
kubeletVersion, err := semver.NewVersion(req.MachineSpec.Versions.Kubelet)
if err != nil {
return "", fmt.Errorf("invalid kubelet version: %w", err)
}
pconfig, err := providerconfigtypes.GetConfig(req.MachineSpec.ProviderSpec)
if err != nil {
return "", fmt.Errorf("failed to get provider config: %w", err)
}
if pconfig.OverwriteCloudConfig != nil {
req.CloudConfig = *pconfig.OverwriteCloudConfig
}
if pconfig.Network.IsStaticIPConfig() {
return "", errors.New("static IP config is not supported with CentOS")
}
centosConfig, err := LoadConfig(pconfig.OperatingSystemSpec)
if err != nil {
return "", fmt.Errorf("failed to parse OperatingSystemSpec: %w", err)
}
kubeconfigString, err := userdatahelper.StringifyKubeconfig(req.Kubeconfig)
if err != nil {
return "", err
}
kubernetesCACert, err := userdatahelper.GetCACert(req.Kubeconfig)
if err != nil {
return "", fmt.Errorf("error extracting cacert: %w", err)
}
crEngine := req.ContainerRuntime.Engine()
crScript, err := crEngine.ScriptFor(providerconfigtypes.OperatingSystemCentOS)
if err != nil {
return "", fmt.Errorf("failed to generate container runtime install script: %w", err)
}
crConfig, err := crEngine.Config()
if err != nil {
return "", fmt.Errorf("failed to generate container runtime config: %w", err)
}
crAuthConfig, err := crEngine.AuthConfig()
if err != nil {
return "", fmt.Errorf("failed to generate container runtime auth config: %w", err)
}
data := struct {
plugin.UserDataRequest
ProviderSpec *providerconfigtypes.Config
OSConfig *Config
KubeletVersion string
Kubeconfig string
KubernetesCACert string
NodeIPScript string
ExtraKubeletFlags []string
ContainerRuntimeScript string
ContainerRuntimeConfigFileName string
ContainerRuntimeConfig string
ContainerRuntimeAuthConfigFileName string
ContainerRuntimeAuthConfig string
ContainerRuntimeName string
}{
UserDataRequest: req,
ProviderSpec: pconfig,
OSConfig: centosConfig,
KubeletVersion: kubeletVersion.String(),
Kubeconfig: kubeconfigString,
KubernetesCACert: kubernetesCACert,
NodeIPScript: userdatahelper.SetupNodeIPEnvScript(pconfig.Network.GetIPFamily()),
ExtraKubeletFlags: crEngine.KubeletFlags(),
ContainerRuntimeScript: crScript,
ContainerRuntimeConfigFileName: crEngine.ConfigFileName(),
ContainerRuntimeConfig: crConfig,
ContainerRuntimeAuthConfigFileName: crEngine.AuthConfigFileName(),
ContainerRuntimeAuthConfig: crAuthConfig,
ContainerRuntimeName: crEngine.String(),
}
buf := strings.Builder{}
if err = tmpl.Execute(&buf, data); err != nil {
return "", fmt.Errorf("failed to execute user-data template: %w", err)
}
return userdatahelper.CleanupTemplateOutput(buf.String())
}
// UserData template.
const userDataTemplate = `#cloud-config
{{ if ne .CloudProviderName "aws" }}
hostname: {{ .MachineSpec.Name }}
{{- /* Never set the hostname on AWS nodes. Kubernetes(kube-proxy) requires the hostname to be the private dns name */}}
{{ end }}
{{- if .OSConfig.DistUpgradeOnBoot }}
package_upgrade: true
package_reboot_if_required: true
{{- end }}
ssh_pwauth: false
{{- if ne (len .ProviderSpec.SSHPublicKeys) 0 }}
ssh_authorized_keys:
{{- range .ProviderSpec.SSHPublicKeys }}
- "{{ . }}"
{{- end }}
{{- end }}
write_files:
{{- if .HTTPProxy }}
- path: "/etc/environment"
content: |
{{ proxyEnvironment .HTTPProxy .NoProxy | indent 4 }}
{{- end }}
- path: "/etc/systemd/journald.conf.d/max_disk_use.conf"
content: |
{{ journalDConfig | indent 4 }}
- path: "/opt/load-kernel-modules.sh"
permissions: "0755"
content: |
{{ kernelModulesScript | indent 4 }}
- path: "/etc/sysctl.d/k8s.conf"
content: |
{{ kernelSettings | indent 4 }}
- path: /etc/selinux/config
content: |
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=permissive
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
- path: "/opt/bin/setup"
permissions: "0755"
content: |
#!/bin/bash
set -xeuo pipefail
setenforce 0 || true
{{- /* As we added some modules and don't want to reboot, restart the service */}}
systemctl restart systemd-modules-load.service
sysctl --system
{{ if ne .CloudProviderName "aws" }}
{{- /* The normal way of setting it via cloud-init is broken, see */}}
{{- /* https://bugs.launchpad.net/cloud-init/+bug/1662542 */}}
hostnamectl set-hostname {{ .MachineSpec.Name }}
{{ end }}
{{- /* CentOS 8 has reached EOL and all packages were moved to vault.centos.org -- https://www.centos.org/centos-linux-eol/ */}}
source /etc/os-release
if [ "$ID" == "centos" ] && [ "$VERSION_ID" == "8" ]; then
sudo sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sudo sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
fi
yum install -y \
device-mapper-persistent-data \
lvm2 \
ebtables \
ethtool \
nfs-utils \
bash-completion \
sudo \
socat \
wget \
curl \
{{- if or (eq .CloudProviderName "vsphere") (eq .CloudProviderName "vmware-cloud-director") }}
open-vm-tools \
{{- end }}
{{- if eq .CloudProviderName "nutanix" }}
iscsi-initiator-utils \
{{- end }}
ipvsadm
{{- /* iscsid service is required on Nutanix machines for CSI driver to attach volumes. */}}
{{- if eq .CloudProviderName "nutanix" }}
systemctl enable --now iscsid
{{ end }}
{{ .ContainerRuntimeScript | indent 4 }}
{{ safeDownloadBinariesScript .KubeletVersion | indent 4 }}
# set kubelet nodeip environment variable
mkdir -p /etc/systemd/system/kubelet.service.d/
/opt/bin/setup_net_env.sh
systemctl disable --now firewalld || true
{{ if eq .CloudProviderName "vsphere" }}
systemctl enable --now vmtoolsd.service
{{ end -}}
systemctl enable --now kubelet
systemctl enable --now --no-block kubelet-healthcheck.service
{{- if eq .CloudProviderName "kubevirt" }}
systemctl enable --now --no-block restart-kubelet.service
{{ end }}
systemctl disable setup.service
- path: "/opt/bin/supervise.sh"
permissions: "0755"
content: |
#!/bin/bash
set -xeuo pipefail
while ! "$@"; do
sleep 1
done
- path: "/opt/disable-swap.sh"
permissions: "0755"
content: |
# Make sure we always disable swap - Otherwise the kubelet won't start as for some cloud
# providers swap gets enabled on reboot or after the setup script has finished executing.
sed -i.orig '/.*swap.*/d' /etc/fstab
swapoff -a
- path: "/etc/systemd/system/kubelet.service"
content: |
{{ kubeletSystemdUnit .ContainerRuntimeName .KubeletVersion .KubeletCloudProviderName .MachineSpec.Name .DNSIPs .ExternalCloudProvider .ProviderSpec.Network.GetIPFamily .PauseImage .MachineSpec.Taints .ExtraKubeletFlags true | indent 4 }}
{{- if ne (len .CloudConfig) 0 }}
- path: "/etc/kubernetes/cloud-config"
permissions: "0600"
content: |
{{ .CloudConfig | indent 4 }}
{{- end }}
- path: "/opt/bin/setup_net_env.sh"
permissions: "0755"
content: |
{{ .NodeIPScript | indent 4 }}
- path: "/etc/kubernetes/bootstrap-kubelet.conf"
permissions: "0600"
content: |
{{ .Kubeconfig | indent 4 }}
- path: "/etc/kubernetes/kubelet.conf"
content: |
{{ kubeletConfiguration "cluster.local" .DNSIPs .KubeletFeatureGates .KubeletConfigs | indent 4 }}
- path: "/etc/kubernetes/pki/ca.crt"
content: |
{{ .KubernetesCACert | indent 4 }}
- path: "/etc/systemd/system/setup.service"
permissions: "0644"
content: |
[Install]
WantedBy=multi-user.target
[Unit]
Requires=network-online.target
After=network-online.target
[Service]
Type=oneshot
RemainAfterExit=true
EnvironmentFile=-/etc/environment
ExecStart=/opt/bin/supervise.sh /opt/bin/setup
- path: "/etc/profile.d/opt-bin-path.sh"
permissions: "0644"
content: |
export PATH="/opt/bin:$PATH"
- path: {{ .ContainerRuntimeConfigFileName }}
permissions: "0644"
content: |
{{ .ContainerRuntimeConfig | indent 4 }}
- path: /etc/systemd/system/kubelet-healthcheck.service
permissions: "0644"
content: |
{{ kubeletHealthCheckSystemdUnit | indent 4 }}
{{- with .ProviderSpec.CAPublicKey }}
- path: "/etc/ssh/trusted-user-ca-keys.pem"
content: |
{{ . | indent 4 }}
- path: "/etc/ssh/sshd_config"
content: |
{{ sshConfigAddendum | indent 4 }}
append: true
{{- end }}
{{- if eq .CloudProviderName "kubevirt" }}
- path: "/opt/bin/restart-kubelet.sh"
permissions: "0744"
content: |
#!/bin/bash
# Needed for Kubevirt provider because if the virt-launcher pod is deleted,
# the VM and DataVolume states are kept and VM is rebooted. We need to restart the kubelet
# with the new config (new IP) and run this at every boot.
set -xeuo pipefail
# This helps us avoid an unnecessary restart for kubelet on the first boot
if [ -f /etc/kubelet_needs_restart ]; then
# restart kubelet since it's not the first boot
systemctl daemon-reload
systemctl restart kubelet.service
else
touch /etc/kubelet_needs_restart
fi
- path: "/etc/systemd/system/restart-kubelet.service"
permissions: "0644"
content: |
[Unit]
Requires=kubelet.service
After=kubelet.service
Description=Service responsible for restarting kubelet when the machine is rebooted
[Service]
Type=oneshot
ExecStart=/opt/bin/restart-kubelet.sh
[Install]
WantedBy=multi-user.target
{{- end }}
runcmd:
- systemctl enable --now setup.service
`