-
Notifications
You must be signed in to change notification settings - Fork 124
/
provider.go
337 lines (282 loc) · 9.78 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
/*
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 RHEL.
//
package rhel
import (
"bytes"
"errors"
"fmt"
"text/template"
"github.com/Masterminds/semver"
"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(req plugin.UserDataRequest) (string, error) {
tmpl, err := template.New("user-data").Funcs(userdatahelper.TxtFuncMap()).Parse(userDataTemplate)
if err != nil {
return "", fmt.Errorf("failed to parse user-data template: %v", err)
}
kubeletVersion, err := semver.NewVersion(req.MachineSpec.Versions.Kubelet)
if err != nil {
return "", fmt.Errorf("invalid kubelet version: %v", err)
}
dockerVersion, err := userdatahelper.DockerVersionYum(kubeletVersion)
if err != nil {
return "", fmt.Errorf("invalid docker version: %v", err)
}
pconfig, err := providerconfigtypes.GetConfig(req.MachineSpec.ProviderSpec)
if err != nil {
return "", fmt.Errorf("failed to get provider config: %v", err)
}
if pconfig.OverwriteCloudConfig != nil {
req.CloudConfig = *pconfig.OverwriteCloudConfig
}
if pconfig.Network != nil {
return "", errors.New("static IP config is not supported with RHEL")
}
rhelConfig, err := LoadConfig(pconfig.OperatingSystemSpec)
if err != nil {
return "", fmt.Errorf("failed to parse OperatingSystemSpec: %v", err)
}
serverAddr, err := userdatahelper.GetServerAddressFromKubeconfig(req.Kubeconfig)
if err != nil {
return "", fmt.Errorf("error extracting server address from kubeconfig: %v", 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: %v", err)
}
data := struct {
plugin.UserDataRequest
ProviderSpec *providerconfigtypes.Config
OSConfig *Config
KubeletVersion string
DockerVersion string
ServerAddr string
Kubeconfig string
KubernetesCACert string
NodeIPScript string
}{
UserDataRequest: req,
ProviderSpec: pconfig,
OSConfig: rhelConfig,
KubeletVersion: kubeletVersion.String(),
DockerVersion: dockerVersion,
ServerAddr: serverAddr,
Kubeconfig: kubeconfigString,
KubernetesCACert: kubernetesCACert,
NodeIPScript: userdatahelper.SetupNodeIPEnvScript(),
}
b := &bytes.Buffer{}
err = tmpl.Execute(b, data)
if err != nil {
return "", fmt.Errorf("failed to execute user-data template: %v", err)
}
return userdatahelper.CleanupTemplateOutput(b.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: no
{{- 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
{{- /* Make sure we always disable swap - Otherwise the kubelet won't start */}}
sed -i.orig '/.*swap.*/d' /etc/fstab
swapoff -a
{{ 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 }}
yum install -y yum-utils
yum-config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
{{- /* Due to DNF modules we have to do this on docker-ce repo
More info at: https://bugzilla.redhat.com/show_bug.cgi?id=1756473 */}}
sed -i 's/\$releasever/7/g' /etc/yum.repos.d/docker-ce.repo
yum-config-manager --save --setopt=docker-ce-stable.module_hotfixes=true
DOCKER_VERSION='{{ .DockerVersion }}'
yum install -y docker-ce-${DOCKER_VERSION} \
docker-ce-cli-${DOCKER_VERSION} \
ebtables \
ethtool \
nfs-utils \
bash-completion \
sudo \
socat \
wget \
curl \
yum-plugin-versionlock \
{{- if eq .CloudProviderName "vsphere" }}
open-vm-tools \
{{- end }}
ipvsadm
yum versionlock add docker-ce-*
{{ safeDownloadBinariesScript .KubeletVersion | indent 4 }}
# set kubelet nodeip environment variable
mkdir -p /etc/systemd/system/kubelet.service.d/
/opt/bin/setup_net_env.sh
{{ if eq .CloudProviderName "vsphere" }}
systemctl enable --now vmtoolsd.service
{{ end -}}
{{- /* Without this, the conformance tests fail with differing tests causing it, the common denominator: They look for some string in container logs and get an empty log */ -}}
systemctl enable --now docker
systemctl enable --now kubelet
systemctl enable --now --no-block kubelet-healthcheck.service
systemctl enable --now --no-block docker-healthcheck.service
- path: "/opt/bin/supervise.sh"
permissions: "0755"
content: |
#!/bin/bash
set -xeuo pipefail
while ! "$@"; do
sleep 1
done
- path: "/etc/systemd/system/kubelet.service"
content: |
{{ kubeletSystemdUnit .KubeletVersion .CloudProviderName .MachineSpec.Name .DNSIPs .ExternalCloudProvider .PauseImage .MachineSpec.Taints | indent 4 }}
- path: "/etc/kubernetes/cloud-config"
permissions: "0600"
content: |
{{ .CloudConfig | indent 4 }}
- 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 | 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: /etc/docker/daemon.json
permissions: "0644"
content: |
{{ dockerConfig .InsecureRegistries .RegistryMirrors | indent 4 }}
- path: /etc/systemd/system/kubelet-healthcheck.service
permissions: "0644"
content: |
{{ kubeletHealthCheckSystemdUnit | indent 4 }}
- path: /etc/systemd/system/docker-healthcheck.service
permissions: "0644"
content: |
{{ containerRuntimeHealthCheckSystemdUnit | indent 4 }}
{{- if or .InsecureRegistries .RegistryMirrors }}
- path: /run/containers/registries.conf
permissions: "0644"
content: |
{{- if .InsecureRegistries}}
INSECURE_REGISTRY="{{range .InsecureRegistries}}--insecure-registry {{.}} {{end}}"
{{- end}}
{{- if .RegistryMirrors}}
REGISTRIES="{{range .RegistryMirrors}}--registry-mirror {{.}} {{end}}"
{{- end}}
{{- end}}
- path: /etc/systemd/system/docker.service.d/environment.conf
permissions: "0644"
content: |
[Service]
EnvironmentFile=-/etc/environment
rh_subscription:
{{- if .OSConfig.RHELUseSatelliteServer }}
org: "{{.OSConfig.RHELOrganizationName}}"
activation-key: "{{.OSConfig.RHELActivationKey}}"
server-hostname: {{ .OSConfig.RHELSatelliteServer }}
rhsm-baseurl: https://{{ .OSConfig.RHELSatelliteServer }}/pulp/repos
{{- else }}
username: "{{.OSConfig.RHELSubscriptionManagerUser}}"
password: "{{.OSConfig.RHELSubscriptionManagerPassword}}"
auto-attach: true
{{- end }}
runcmd:
- systemctl start setup.service
`