fix(cgroups.plugin): do not disable K8s pod/container cgroups if can't rename them (#12865)
Ilya Mashchenko committed
May 10, 2022 at 14:52 UTC
bd504c6c57ad88f376cd392c711f02cd8b58a16e
2 files changed
+15
-14
collectors/cgroups.plugin/cgroup-name.sh
+15
-10
@@ -229,14 +229,14 @@ function k8s_get_kubepod_name() {
229
230
if [ -z "$pod_uid" ] && [ -z "$cntr_id" ]; then
231
warning "${fn}: can't extract pod_uid or container_id from the cgroup '$id'."
232
- return 1
232
+ return 3
233
fi
234
235
[ -n "$pod_uid" ] && info "${fn}: cgroup '$id' is a pod(uid:$pod_uid)"
236
[ -n "$cntr_id" ] && info "${fn}: cgroup '$id' is a container(id:$cntr_id)"
237
238
if [ -n "$cntr_id" ] && k8s_is_pause_container "$cgroup_path"; then
239
- return 1
239
+ return 3
240
fi
241
242
if ! command -v jq > /dev/null 2>&1; then
@@ -276,7 +276,7 @@ function k8s_get_kubepod_name() {
276
if [ -z "$kube_system_uid" ]; then
277
url="https://$host/api/v1/namespaces/kube-system"
278
# FIX: check HTTP response code
279
- if ! kube_system_ns=$(curl -sSk -H "$header" "$url" 2>&1); then
279
+ if ! kube_system_ns=$(curl --fail -sSk -H "$header" "$url" 2>&1); then
280
warning "${fn}: error on curl '${url}': ${kube_system_ns}."
281
fi
282
fi
@@ -284,13 +284,13 @@ function k8s_get_kubepod_name() {
284
url="https://$host/api/v1/pods"
285
[ -n "$MY_NODE_NAME" ] && url+="?fieldSelector=spec.nodeName==$MY_NODE_NAME"
286
# FIX: check HTTP response code
287
- if ! pods=$(curl -sSk -H "$header" "$url" 2>&1); then
287
+ if ! pods=$(curl --fail -sSk -H "$header" "$url" 2>&1); then
288
warning "${fn}: error on curl '${url}': ${pods}."
289
return 1
290
fi
291
elif ps -C kubelet >/dev/null 2>&1 && command -v kubectl >/dev/null 2>&1; then
292
if [ -z "$kube_system_uid" ]; then
293
- if ! kube_system_ns=$(kubectl get namespaces kube-system -o json 2>&1); then
293
+ if ! kube_system_ns=$(kubectl --kubeconfig="$KUBE_CONFIG" get namespaces kube-system -o json 2>&1); then
294
warning "${fn}: error on 'kubectl': ${kube_system_ns}."
295
fi
296
fi
@@ -379,7 +379,7 @@ function k8s_get_kubepod_name() {
379
# jq filter nonexistent field and nonexistent label value is 'null'
380
if [[ $name =~ _null(_|$) ]]; then
381
warning "${fn}: invalid name: $name (cgroup '$id')"
382
- name=""
382
+ return 1
383
fi
384
385
echo "$name"
@@ -408,14 +408,19 @@ function k8s_get_name() {
408
fi
409
EXIT_CODE=$EXIT_SUCCESS
410
;;
411
+ 1)
412
+ NAME="k8s_${id}"
413
+ warning "${fn}: cannot find the name of cgroup with id '${id}'. Setting name to ${NAME} and enabling it."
414
+ EXIT_CODE=$EXIT_SUCCESS
415
+ ;;
416
2)
412
- warning "${fn}: cannot find the name of cgroup with id '${id}'. Setting name to ${id} and asking for retry."
413
- NAME="${id}"
417
+ NAME="k8s_${id}"
418
+ warning "${fn}: cannot find the name of cgroup with id '${id}'. Setting name to ${NAME} and asking for retry."
419
EXIT_CODE=$EXIT_RETRY
420
;;
421
*)
417
- warning "${fn}: cannot find the name of cgroup with id '${id}'. Setting name to ${id} and disabling it."
418
- NAME="${id}"
422
+ NAME="k8s_${id}"
423
+ warning "${fn}: cannot find the name of cgroup with id '${id}'. Setting name to ${NAME} and disabling it."
424
EXIT_CODE=$EXIT_DISABLE
425
;;
426
esac
collectors/cgroups.plugin/sys_fs_cgroup.c
-4
@@ -1848,10 +1848,6 @@ static inline void discovery_rename_cgroup(struct cgroup *cg) {
1848
cg->pending_renames = 0;
1849
cg->processed = 1;
1850
break;
1851
- default:
1852
- if (!cg->pending_renames && is_inside_k8s) {
1853
- cg->processed = 1;
1854
- }
1851
}
1852
1853
if (cg->pending_renames || cg->processed) {