-
Notifications
You must be signed in to change notification settings - Fork 5
/
podsec-u7s-functions
executable file
·306 lines (279 loc) · 6.44 KB
/
podsec-u7s-functions
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
#!/bin/sh
# Common functions
# Customizable environment variables:
# * $U7S_DEBUG: enable debug mode if set to "1"
# Environment variables set by this script:
# * $XDG_DATA_HOME: ~u7s-admin/.local/share if not set
# * $XDG_CONFIG_HOME: ~u7s-admin/.config if not set
# * $XDG_CACHE_HOME: ~u7s-admin/.cache if not set
USERNAME='u7s-admin'
set -eo pipefail
# logging utilities
debug_enabled() {
: ${U7S_DEBUG=0}
[[ $U7S_DEBUG == 1 ]] || [[ $U7S_DEBUG == true ]]
}
log_debug() {
if debug_enabled; then
echo -e "\e[102m\e[97m[DEBUG]\e[49m\e[39m $@" >&2
fi
}
log_info() {
echo -e "\e[104m\e[97m[INFO]\e[49m\e[39m $@" >&2
}
log_info_n() {
echo -n -e "\e[104m\e[97m[INFO]\e[49m\e[39m $@" >&2
}
log_warning() {
echo -e "\e[101m\e[97m[WARN]\e[49m\e[39m $@" >&2
}
log_error() {
echo -e "\e[101m\e[97m[ERROR]\e[49m\e[39m $@" >&2
}
# nsenter utilities
nsenter_main() {
: ${_U7S_NSENTER_CHILD=0}
if [[ $_U7S_NSENTER_CHILD == 0 ]]; then
_U7S_NSENTER_CHILD=1
export _U7S_NSENTER_CHILD
nsenter__nsenter_retry_loop
rc=0
nsenter__nsenter $@ || rc=$?
exit $rc
fi
}
nsenter__nsenter_retry_loop() {
local max_trial=10
log_info_n "Entering RootlessKit namespaces: "
for ((i = 0; i < max_trial; i++)); do
rc=0
nsenter__nsenter echo OK 2>/dev/null || rc=$?
if [[ rc -eq 0 ]]; then
return 0
fi
echo -n .
sleep 1
done
echo ""
log_error "nsenter failed after ${max_trial} attempts, RootlessKit not running?"
return 1
}
nsenter__nsenter() {
local pidfile=$XDG_RUNTIME_DIR/usernetes/rootlesskit/child_pid
if ! [[ -f $pidfile ]]; then
return 1
fi
# workaround for https://github.com/rootless-containers/rootlesskit/issues/37
# see the corresponding code in rootlesskit
local pidreadyfile=$XDG_RUNTIME_DIR/usernetes/rootlesskit/_child_pid.u7s-ready
if ! [[ -f $pidreadyfile ]]; then
return 1
fi
if ! [[ $(cat $pidfile) -eq $(cat $pidreadyfile) ]]; then
return 1
fi
export ROOTLESSKIT_STATE_DIR=$XDG_RUNTIME_DIR/usernetes/rootlesskit
# TODO(AkihiroSuda): ping to $XDG_RUNTIME_DIR/usernetes/rootlesskit/api.sock
nsenter --user --preserve-credential --mount --net --cgroup --pid --ipc --uts -t $(cat $pidfile) --wd=$PWD -- $@
}
setEnvsByYaml() {
yamlFile=$1
ifs=$IFS
for assign in $(yq '.spec.containers[0].command' $yamlFile | grep -- '"--')
do
if [ "${assign:0-1}" = ',' ]
then
assign=${assign:3:-2}
else
assign=${assign:3:-1}
fi
IFS==
set -- $assign
IFS=$ifs
var=$1
value=$2
IFS=-
set -- $var
IFS=$ifs
varsh=$1
shift
for p
do
varsh+="_$p"
done
echo "export $varsh=$value"
done
}
# Применение маски сети к указанному адресу
maskAddr() {
set +e
addr=$1
mask=$2
if [ $mask -eq 0 -o $mask -gt 32 ]
then
echo "Неверная маска $mask" >&2
exit
fi
let tail=32-$mask
ifs=$IFS
IFS=.
set -- $addr
IFS=$ifs
if [ $# -ne 4 ]
then
echo "Неверный адрес $addr" >&2
exit
fi
Val=0
for val
do
let Val=$Val*256+$val
done
let "Val=$Val>>$tail"
let "Val=$Val<<$tail"
let "ret=$Val&255"
for i in 1 2 3
do
let "Val=$Val>>8"
let "val=$Val&255"
ret="$val.$ret"
done
echo $ret
}
# Получение внешнего IP
getExtIP() {
set -- $(ip r | grep default)
export router=$3
ip a | grep 'inet ' |
(
while read inet
do
set -- $inet
AddrMask=$2
ifs=$IFS
IFS=/
set -- $AddrMask
IFS=$ifs
addr=$1
mask=$2
maskedAddr=$(maskAddr $addr $mask)
maskedRoute=$(maskAddr $router $mask)
if [ "$maskedAddr" = "$maskedRoute" ]
then
echo $addr
break
fi
done
)
}
# Получение имени внешнего устройства
getExtDev() {
export extIP=$1
ifs=$IFS
IFS=.
set -- $extIP
IFS=$ifs
if [ $# -ne 4 ]
then
echo "Некорректный адрес '$extIP'" >&2
exit
fi
ip a | grep $extIP |
(
while read inet
do
set -- $inet
if [ "$1" != 'inet' ]
then
continue
fi
addr=$2
l=${#extIP}
if [ "${addr:0:$l}" = "$extIP" -a "${addr:$l:1}" == '/' ]
then
while [ $# -gt 1 ]; do shift; done
echo $1
break
fi
done
)
}
getCidr() {
export CIDRIP CIDRMASK
cidr=$1
# shift; shift
IFS=/
set -- $cidr
IFS=$ifs
CIDRIP=$1
CIDRMASK=$2
IFS=.
set -- $cidr
IFS=$ifs
if [ $# -ne 4 ]
then
echo "Некорректный адрес --service-cidr '$cidr'" >&1
exit 1
fi
# echo $cidr $serviceMask
}
# Функция формирует ClusterIP как cidr с 1-чкой в последнем кварте
getKubernetesClusterIP() {
cidr=$1
ifs=$IFS
IFS=.
set -- $cidr
IFS=$ifs
echo "${1}.${2}.${3}.1"
}
# Функция модифицирует файл /kubernetes/manifests/kube-apiserver.yaml запуска kube-apiserver
# для настройки аудита
function tuneAudit() {
haveAudit=$(cat /etc/kubernetes/manifests/kube-apiserver.yaml | yq '[.spec.volumes[].hostPath][].path | select(. == "/etc/kubernetes/audit")')
if [ -z "$haveAudit" ]
then
TMPFILE="/tmp/kube-api-server.$$"
confFile="/etc/kubernetes/manifests/kube-apiserver.yaml"
cat $confFile |
yq -y '.spec.containers[].command |= . +
["--audit-policy-file=/etc/kubernetes/audit/policy.yaml"] +
["--audit-log-path=/etc/kubernetes/audit/audit.log"] +
["--audit-log-maxsize=500"] +
["--audit-log-maxbackup=3"]
' |
yq -y '.spec.containers[].volumeMounts |= . +
[{ "mountPath": "/etc/kubernetes/audit", "name": "audit" }]
' |
yq -y '.spec.volumes |= . +
[{ "hostPath": {"path": "/etc/kubernetes/audit" , "type": "DirectoryOrCreate" }, "name": "audit" }]
' > $TMPFILE
if [ -s $TMPFILE ]
then
mv $TMPFILE $confFile
fi
fi
}
export U7S_REGISTRY_PLATFORM U7S_REGISTRY
# Функция устанавливает имя рягистратора обрвзов в переменнную U7S_REGISTRY_PLATFORM
function setRegistryName() {
if [ ! -z ${U7S_REGISTRY_PLATFORM+x} ];
then
export U7S_REGISTRY="registry.k8s.io"
export U7S_REGISTRY_PLATFORM=$U7S_REGISTRY
return;
fi
source /etc/podsec/u7s/env/platform
if grep registry.local /etc/hosts >/dev/null 2>&1
then
export U7S_REGISTRY="registry.local"
else
U7S_REGISTRY="registry.altlinux.org"
fi
export U7S_REGISTRY_PLATFORM="$U7S_REGISTRY/$U7S_PLATFORM"
}
# export XDG_{DATA,CONFIG,CACHE}_HOME
: ${XDG_DATA_HOME=~u7s-admin/.local/share}
: ${XDG_CONFIG_HOME=~u7s-admin/.config}
: ${XDG_CACHE_HOME=~u7s-admin/.cache}
envFile="${XDG_CONFIG_HOME}/usernetes/env"
export XDG_DATA_HOME XDG_CONFIG_HOME XDG_CACHE_HOME envFile