feat: add k8s_cluster_name host tag (GKE only) (#12638)
Ilya Mashchenko committed
Apr 11, 2022 at 13:19 UTC
b320d2b36325e8cedcd0bea43020a7483d8361fe
1 file changed
+19
-2
daemon/get-kubernetes-labels.sh.in
+19
-2
@@ -1,6 +1,8 @@
1
#!/usr/bin/env bash
2
3
-# Checks if netdata is running in a kubernetes pod and fetches that pod's labels
3
+# Checks if netdata is running in a kubernetes pod and fetches:
4
+# - pod's labels
5
+# - kubernetes cluster name (GKE only)
6
7
if [ -z "${KUBERNETES_SERVICE_HOST}" ] || [ -z "${KUBERNETES_PORT_443_TCP_PORT}" ] || [ -z "${MY_POD_NAMESPACE}" ] || [ -z "${MY_POD_NAME}" ]; then
8
exit 0
@@ -37,5 +39,20 @@ if ! KUBE_SYSTEM_NS_UID=$(jq -r '.metadata.uid' <<< "$KUBE_SYSTEM_NS_DATA" 2>&1)
39
exit 1
40
fi
41
40
-echo -e "$POD_LABELS\nk8s_cluster_id:$KUBE_SYSTEM_NS_UID"
42
+LABELS="$POD_LABELS\nk8s_cluster_id:$KUBE_SYSTEM_NS_UID"
43
+
44
+GCP_META_HEADER="Metadata-Flavor: Google"
45
+GCP_META_URL="http://metadata/computeMetadata/v1"
46
+GKE_CLUSTER_NAME=""
47
+
48
+if id=$(curl --fail -s -m 5 --noproxy "*" -H "$GCP_META_HEADER" "$GCP_META_URL/project/project-id"); then
49
+ loc=$(curl --fail -s -m 5 --noproxy "*" -H "$GCP_META_HEADER" "$GCP_META_URL/instance/attributes/cluster-location")
50
+ name=$(curl --fail -s -m 5 --noproxy "*" -H "$GCP_META_HEADER" "$GCP_META_URL/instance/attributes/cluster-name")
51
+ [ -n "$id" ] && [ -n "$loc" ] && [ -n "$name" ] && GKE_CLUSTER_NAME="gke_${id}_${loc}_${name}"
52
+fi
53
+
54
+[ -n "$GKE_CLUSTER_NAME" ] && LABELS+="\nk8s_cluster_name:$GKE_CLUSTER_NAME"
55
+
56
+echo -e "$LABELS"
57
+
58
exit 0