cgroups: add option to use Kubelet for pods metadata (#14891)
Ilya Mashchenko committed
Apr 13, 2023 at 01:02 UTC
068ae1cfac921313c7d60f8a3fea5f19600c1f38
1 file changed
+12
-2
collectors/cgroups.plugin/cgroup-name.sh
+12
-2
@@ -306,8 +306,14 @@ function k8s_get_kubepod_name() {
306
fi
307
fi
308
309
- url="https://$host/api/v1/pods"
310
- [ -n "$MY_NODE_NAME" ] && url+="?fieldSelector=spec.nodeName==$MY_NODE_NAME"
309
+ local url
310
+ if [ -n "${USE_KUBELET_FOR_PODS_METADATA}" ]; then
311
+ url="${KUBELET_URL:-https://localhost:10250}/pods"
312
+ else
313
+ url="https://$host/api/v1/pods"
314
+ [ -n "$MY_NODE_NAME" ] && url+="?fieldSelector=spec.nodeName==$MY_NODE_NAME"
315
+ fi
316
+
317
# FIX: check HTTP response code
318
if ! pods=$(curl --fail -sSk -H "$header" "$url" 2>&1); then
319
warning "${fn}: error on curl '${url}': ${pods}."
@@ -404,6 +410,10 @@ function k8s_get_kubepod_name() {
410
# jq filter nonexistent field and nonexistent label value is 'null'
411
if [[ $name =~ _null(_|$) ]]; then
412
warning "${fn}: invalid name: $name (cgroup '$id')"
413
+ if [ -n "${USE_KUBELET_FOR_PODS_METADATA}" ]; then
414
+ # local data is cached and may not contain the correct id
415
+ return 2
416
+ fi
417
return 1
418
fi
419