forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_cloud-config-script.sh
82 lines (71 loc) · 2.31 KB
/
_cloud-config-script.sh
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
{{- define "shoot-cloud-config.execution-script" -}}
#!/bin/bash -eu
DIR_KUBELET="/var/lib/kubelet"
DIR_CLOUDCONFIG_DOWNLOADER="/var/lib/cloud-config-downloader"
DIR_CLOUDCONFIG="$DIR_CLOUDCONFIG_DOWNLOADER/downloads"
PATH_CLOUDCONFIG_DOWNLOADER_SERVER="$DIR_CLOUDCONFIG_DOWNLOADER/credentials/server"
PATH_CLOUDCONFIG_DOWNLOADER_CA_CERT="$DIR_CLOUDCONFIG_DOWNLOADER/credentials/ca.crt"
PATH_CLOUDCONFIG="{{ .configFilePath }}"
PATH_CLOUDCONFIG_OLD="${PATH_CLOUDCONFIG}.old"
mkdir -p "$DIR_CLOUDCONFIG" "$DIR_KUBELET"
function docker-preload() {
name="$1"
image="$2"
echo "Checking whether to preload $name from $image"
if [ -z $(docker images -q "$image") ]; then
echo "Preloading $name from $image"
docker pull "$image"
else
echo "No need to preload $name from $image"
fi
}
{{ range $name, $image := (required ".images is required" .images) -}}
docker-preload "{{ $name }}" "{{ $image }}"
{{ end }}
cat << 'EOF' | base64 -d > "$PATH_CLOUDCONFIG"
{{ .worker.cloudConfig | b64enc }}
EOF
if [ ! -f "$PATH_CLOUDCONFIG_OLD" ]; then
touch "$PATH_CLOUDCONFIG_OLD"
fi
if [[ ! -f "$DIR_KUBELET/kubeconfig-real" ]]; then
cat <<EOF > "$DIR_KUBELET/kubeconfig-bootstrap"
---
apiVersion: v1
kind: Config
current-context: kubelet-bootstrap@default
clusters:
- cluster:
certificate-authority-data: $(cat "$PATH_CLOUDCONFIG_DOWNLOADER_CA_CERT" | base64 | tr -d '\n')
server: $(cat "$PATH_CLOUDCONFIG_DOWNLOADER_SERVER")
name: default
contexts:
- context:
cluster: default
user: kubelet-bootstrap
name: kubelet-bootstrap@default
users:
- name: kubelet-bootstrap
user:
as-user-extra: {}
token: {{ required ".bootstrapToken is required" .bootstrapToken }}
EOF
else
rm -f "$DIR_KUBELET/kubeconfig-bootstrap"
fi
if ! diff "$PATH_CLOUDCONFIG" "$PATH_CLOUDCONFIG_OLD" >/dev/null; then
echo "Seen newer cloud config version"
if {{ .worker.command }}; then
echo "Successfully applied new cloud config version"
systemctl daemon-reload
{{- range $name := (required ".worker.units is required" .worker.units) }}
{{- if ne $name "docker.service" }}
systemctl enable {{ $name }} && systemctl restart {{ $name }}
{{- end }}
{{- end }}
echo "Successfully restarted all units referenced in the cloud config."
cp "$PATH_CLOUDCONFIG" "$PATH_CLOUDCONFIG_OLD"
fi
fi
rm "$PATH_CLOUDCONFIG"
{{- end}}