-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubernetes_controller_resources.py
239 lines (223 loc) · 8.29 KB
/
kubernetes_controller_resources.py
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
import base64
import os
first_rootfs_script = (
f"DEBIAN_APT_GET_PROXY={os.environ.get('DEBIAN_APT_GET_PROXY', '')}"
+ """
set -exo pipefail
if [[ -n "$DEBIAN_APT_GET_PROXY" ]]; then
echo 'Acquire::HTTP::Proxy "'"$DEBIAN_APT_GET_PROXY"'";' > /etc/apt/apt.conf.d/99-build-proxy
fi
apt-get update
apt-get install -y debootstrap tar
http_proxy=$DEBIAN_APT_GET_PROXY https_proxy=$DEBIAN_APT_GET_PROXY debootstrap --include=debconf-utils,openssh-client,openssl,openssh-server,sudo,python3,bash-completion,unattended-upgrades,xz-utils,file,curl,ca-certificates --exclude apparmor,ifupdown "$DEBIAN_RELEASE" "/mnt"
mkdir -p /mnt/proc /mnt/dev /mnt/sys
rm -rf /mnt/var/cache/apt/archives/*deb
tar cf /srv/vauban/rootfs/rootfs.tar -C /mnt .
"""
)
bash_script = (
f"DEBIAN_APT_GET_PROXY={os.environ.get('DEBIAN_APT_GET_PROXY', '')}"
+ """
set -exo pipefail
FROM_SCRATCH=${FROM_SCRATCH:-false}
function update_linux() {
echo "Updating linux"
echo "true" > /usr/bin/linux-check-removal
apt-get update
apt list --installed 2> /dev/null | grep -E 'linux-(header|image)' | cut -d/ -f1 | xargs apt-mark unhold || true
apt-get install -y linux-headers-amd64 linux-image-amd64
apt-get autoremove -y
apt-get purge -y $(apt list --installed 2> /dev/null | grep linux-head | grep -v linux-headers-amd64/ | head -n-2 | cut -f1 -d'/')
apt-get purge -y $(apt list --installed 2> /dev/null | grep linux-image | grep -v linux-image-amd64/ | head -n-1 | cut -f1 -d'/')
}
function run_sshd() {
mkdir -p /root/.ssh
"""
+ f"""echo "{os.environ.get('VAUBAN_KUBERNETES_ENGINE_PUB_KEY')}" >> /root/.ssh/authorized_keys"""
+ """
ssh-keygen -A
echo -e "PermitRootLogin yes\\nPasswordAuthentication no\\nPubkeyAuthentication yes\\nSubsystem sftp /usr/lib/openssh/sftp-server" > /tmp/vauban_sshd
mkdir -p /run/sshd
{ /usr/sbin/sshd -D -e -f /tmp/vauban_sshd ; } &
}
function from_scratch_metadata_and_leave() {
echo "Debian release imported" # Important log line as it will be used as a marker of success
# FIXME: add /imginfo and /packages files
:
for i in $(seq 1 3600); do
if [[ -f /tmp/vauban_success ]]; then
set +e
rm /tmp/vauban_*
echo -e "\n\
---\n\
packages:\n\
\n\
- source: debootstrap\n\
hostname: ${HOST_NAME}\n\
packages: |" >> /packages
apt list --installed | sed -e 's/stable,stable/stable/g' | tail -n +2 > /tmp/apt-after
cat /tmp/apt-after | sed "s/^/ + /g" >> /packages
rm -rf /tmp/* /etc/apt/apt.conf.d/99-build-proxy
exit 0 ;
fi ;
sleep 1 ;
done ;
exit 1
}
export INITRD=No
rm -rf /tmp/vauban_success
apt list --installed | sed -e 's/stable,stable/stable/g' > /tmp/apt-before
printf 'Package: linux-*-rt-*\nPin: release *\nPin-Priority: -1\n' > /etc/apt/preferences.d/block-kernel-rt
if [[ -n "$DEBIAN_APT_GET_PROXY" ]]; then
echo 'Acquire::HTTP::Proxy "'"$DEBIAN_APT_GET_PROXY"'";' > /etc/apt/apt.conf.d/99-build-proxy
fi
[[ "${IN_CONFFS:-no}" != "yes" ]] && update_linux
apt list --installed 2> /dev/null | grep -E 'linux-(header|image)' | cut -d/ -f1 | xargs apt-mark hold
rm -f /etc/apt/apt.conf.d/99-build-proxy || true
[[ $FROM_SCRATCH == "false" ]] && run_sshd
[[ $FROM_SCRATCH == "true" ]] && from_scratch_metadata_and_leave
for i in $(seq 1 3600); do
if [[ -f /tmp/vauban_success ]]; then
set +e
rm /tmp/vauban_*
sed -i "/vauban_build/d" /root/.ssh/authorized_keys ;
echo -e "\n\
- playbook: ${PLAYBOOK:-fixme}\n\
hostname: ${HOST_NAME}\n\
packages: |" >> /packages
apt list --installed | sed -e 's/stable,stable/stable/g' > /tmp/apt-after
diff /tmp/apt-before /tmp/apt-after | grep '^[<>]' | sed 's/</ -/g' | sed 's/>/ +/g' >> /packages
rm -rf /root/.ansible /tmp/* /etc/apt/apt.conf.d/99-build-proxy
exit 0 ;
fi ;
sleep 1 ;
done ;
exit 1
"""
)
dockerfile_txt = """ARG SOURCE
FROM $SOURCE
ARG HOST_NAME
ARG IN_CONFFS
RUN bash /srv/vauban/entrypoint.sh
"""
dockerfile_scratch_txt = """
FROM scratch
ARG HOST_NAME
WORKDIR /srv/vauban/
ADD ./rootfs/rootfs.tar /
RUN bash -c "FROM_SCRATCH=true bash /srv/vauban/entrypoint.sh"
"""
cm_dockerfile = {
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {"name": "vauban-dockerfile"},
"data": {
"Dockerfile": dockerfile_txt,
"Dockerfile_scratch": dockerfile_scratch_txt,
"entrypoint.sh": bash_script,
"first_rootfs.sh": first_rootfs_script,
},
}
def get_pod_kaniko_manifest(name, source, debian_release, tags, in_conffs, uuid):
pod_kaniko = {
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": name,
"labels": {
"vauban.corp.dblc.io/uuid": str(uuid),
"vauban.corp.dblc.io/vauban-type": "kaniko",
"vauban.corp.dblc.io/vauban-job-id": os.environ.get(
"VAUBAN_BUILD_JOB_ULID", "VAUBAN_BUILD_JOB_ULID_var_undefined"
),
},
},
"spec": {
"containers": [
{
"name": "kaniko",
"image": "gcr.io/kaniko-project/executor:latest",
"args": [
"--dockerfile=./Dockerfile",
"--single-snapshot",
"--ignore-var-run=false",
"--context=dir:///srv/vauban",
]
+ (
["--build-arg", "SOURCE=" + source]
if source is not None
else []
)
+ [
"--build-arg",
"IN_CONFFS=" + in_conffs,
"--build-arg",
"HOST_NAME=" + name,
]
+ ["--destination=" + tag for tag in tags],
"volumeMounts": [
{"name": "root", "mountPath": "/srv/vauban/rootfs"},
{"name": "dockerfile", "mountPath": "/srv/vauban"},
{
"name": "registryconfig",
"mountPath": "/kaniko/.docker",
},
],
"ports": [{"containerPort": 22}],
"env": [
{
"name": "PATH",
"value": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
}
],
}
],
"restartPolicy": "Never",
"automountServiceAccountToken": False,
"volumes": [
{
"name": "dockerfile",
"configMap": {
"name": "vauban-dockerfile",
"items": [
{
"key": (
"Dockerfile"
if debian_release is None
else "Dockerfile_scratch"
),
"path": "Dockerfile",
},
{"key": "entrypoint.sh", "path": "entrypoint.sh"},
{"key": "first_rootfs.sh", "path": "first_rootfs.sh"},
],
},
},
{
"name": "registryconfig",
"secret": {"secretName": "vauban-registryconfig"},
},
{
"name": "root",
"emptyDir": {},
},
],
},
}
if debian_release is not None:
init_container = {
"name": "init",
"image": "debian:12-slim",
"args": [
"bash",
"-c",
f"DEBIAN_RELEASE={debian_release} bash /srv/vauban/first_rootfs.sh",
],
"volumeMounts": [
{"name": "dockerfile", "mountPath": "/srv/vauban"},
{"name": "root", "mountPath": "/srv/vauban/rootfs"},
],
}
pod_kaniko["spec"]["initContainers"] = [init_container]
return pod_kaniko