forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_cloud-config-script.sh
107 lines (94 loc) · 3.53 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
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
{{- 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
}
{{- if .worker.kubeletDataVolume }}
function format-data-device() {
LABEL=KUBEDEV
if ! blkid --label $LABEL >/dev/null; then
DEVICES=$(lsblk -dbsnP -o NAME,PARTTYPE,FSTYPE,SIZE)
MATCHING_DEVICES=$(echo "$DEVICES" | grep 'PARTTYPE="".*FSTYPE="".*SIZE="{{.worker.kubeletDataVolume.size}}"')
echo "Matching kubelet data device by size : {{.worker.kubeletDataVolume.size}}"
TARGET_DEVICE_NAME=$(echo "$MATCHING_DEVICES" | head -n1 | cut -f2 -d\")
echo "detected kubelet data device $TARGET_DEVICE_NAME"
mkfs.ext4 -L $LABEL -O quota -E lazy_itable_init=0,lazy_journal_init=0,quotatype=usrquota:grpquota:prjquota /dev/$TARGET_DEVICE_NAME
echo "formatted and labeled data device $TARGET_DEVICE_NAME"
mkdir /tmp/varlibcp
mount LABEL=$LABEL /tmp/varlibcp
echo "mounted temp copy dir on data device $TARGET_DEVICE_NAME"
cp -a /var/lib/* /tmp/varlibcp/
umount /tmp/varlibcp
echo "copied /var/lib to data device $TARGET_DEVICE_NAME"
mount LABEL=$LABEL /var/lib -o defaults,prjquota,errors=remount-ro
echo "mounted /var/lib on data device $TARGET_DEVICE_NAME"
fi
}
format-data-device
{{- end}}
{{ 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" ]] || [[ ! -f "$DIR_KUBELET/pki/kubelet-client-current.pem" ]]; 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 and (ne $name "docker.service") (ne $name "var-lib.mount") }}
systemctl enable {{ $name }} && systemctl restart --no-block {{ $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}}