diff options
author | Ilya Mashchenko <ilya@netdata.cloud> | 2022-04-11 13:19:54 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-11 13:19:54 +0300 |
commit | b320d2b36325e8cedcd0bea43020a7483d8361fe (patch) | |
tree | 545cd12136c99570526e3765216488528e811b00 /daemon | |
parent | f896148a33f1ac01db3c90856b60f5ec5ff39c9a (diff) |
feat: add k8s_cluster_name host tag (GKE only) (#12638)
Diffstat (limited to 'daemon')
-rw-r--r-- | daemon/get-kubernetes-labels.sh.in | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/daemon/get-kubernetes-labels.sh.in b/daemon/get-kubernetes-labels.sh.in index 5aa89ab9da..7e11ba3dd5 100644 --- a/daemon/get-kubernetes-labels.sh.in +++ b/daemon/get-kubernetes-labels.sh.in @@ -1,6 +1,8 @@ #!/usr/bin/env bash -# Checks if netdata is running in a kubernetes pod and fetches that pod's labels +# Checks if netdata is running in a kubernetes pod and fetches: +# - pod's labels +# - kubernetes cluster name (GKE only) if [ -z "${KUBERNETES_SERVICE_HOST}" ] || [ -z "${KUBERNETES_PORT_443_TCP_PORT}" ] || [ -z "${MY_POD_NAMESPACE}" ] || [ -z "${MY_POD_NAME}" ]; then exit 0 @@ -37,5 +39,20 @@ if ! KUBE_SYSTEM_NS_UID=$(jq -r '.metadata.uid' <<< "$KUBE_SYSTEM_NS_DATA" 2>&1) exit 1 fi -echo -e "$POD_LABELS\nk8s_cluster_id:$KUBE_SYSTEM_NS_UID" +LABELS="$POD_LABELS\nk8s_cluster_id:$KUBE_SYSTEM_NS_UID" + +GCP_META_HEADER="Metadata-Flavor: Google" +GCP_META_URL="http://metadata/computeMetadata/v1" +GKE_CLUSTER_NAME="" + +if id=$(curl --fail -s -m 5 --noproxy "*" -H "$GCP_META_HEADER" "$GCP_META_URL/project/project-id"); then + loc=$(curl --fail -s -m 5 --noproxy "*" -H "$GCP_META_HEADER" "$GCP_META_URL/instance/attributes/cluster-location") + name=$(curl --fail -s -m 5 --noproxy "*" -H "$GCP_META_HEADER" "$GCP_META_URL/instance/attributes/cluster-name") + [ -n "$id" ] && [ -n "$loc" ] && [ -n "$name" ] && GKE_CLUSTER_NAME="gke_${id}_${loc}_${name}" +fi + +[ -n "$GKE_CLUSTER_NAME" ] && LABELS+="\nk8s_cluster_name:$GKE_CLUSTER_NAME" + +echo -e "$LABELS" + exit 0 |