summaryrefslogtreecommitdiffstats
path: root/daemon
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--daemon/get-kubernetes-labels.sh.in49
-rwxr-xr-xdaemon/system-info.sh22
2 files changed, 48 insertions, 23 deletions
diff --git a/daemon/get-kubernetes-labels.sh.in b/daemon/get-kubernetes-labels.sh.in
index 805d027b..5aa89ab9 100644
--- a/daemon/get-kubernetes-labels.sh.in
+++ b/daemon/get-kubernetes-labels.sh.in
@@ -2,17 +2,40 @@
# Checks if netdata is running in a kubernetes pod and fetches that pod's labels
-if [ -n "${KUBERNETES_SERVICE_HOST}" ] && [ -n "${KUBERNETES_PORT_443_TCP_PORT}" ] && [ -n "${MY_POD_NAMESPACE}" ] && [ -n "${MY_POD_NAME}" ]; then
- if command -v jq >/dev/null 2>&1; then
- KUBE_TOKEN="$(</var/run/secrets/kubernetes.io/serviceaccount/token)"
- URL="https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/$MY_POD_NAMESPACE/pods/$MY_POD_NAME"
- curl -sSk -H "Authorization: Bearer $KUBE_TOKEN" "$URL" |
- jq -r '.metadata.labels' | grep ':' | tr -d '," '
- exit 0
- else
- echo "jq command not available. Please install jq to get host labels for kubernetes pods."
- exit 1
- fi
-else
- exit 0
+if [ -z "${KUBERNETES_SERVICE_HOST}" ] || [ -z "${KUBERNETES_PORT_443_TCP_PORT}" ] || [ -z "${MY_POD_NAMESPACE}" ] || [ -z "${MY_POD_NAME}" ]; then
+ exit 0
fi
+
+if ! command -v jq > /dev/null 2>&1; then
+ echo "jq command not available. Please install jq to get host labels for kubernetes pods."
+ exit 1
+fi
+
+TOKEN="$(< /var/run/secrets/kubernetes.io/serviceaccount/token)"
+HEADER="Authorization: Bearer $TOKEN"
+HOST="$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT"
+
+URL="https://$HOST/api/v1/namespaces/$MY_POD_NAMESPACE/pods/$MY_POD_NAME"
+if ! POD_DATA=$(curl -sSk -H "$HEADER" "$URL" 2>&1); then
+ echo "error on curl '${URL}': ${POD_DATA}."
+ exit 1
+fi
+
+URL="https://$HOST/api/v1/namespaces/kube-system"
+if ! KUBE_SYSTEM_NS_DATA=$(curl -sSk -H "$HEADER" "$URL" 2>&1); then
+ echo "error on curl '${URL}': ${KUBE_SYSTEM_NS_DATA}."
+ exit 1
+fi
+
+if ! POD_LABELS=$(jq -r '.metadata.labels' <<< "$POD_DATA" | grep ':' | tr -d '," ' 2>&1); then
+ echo "error on 'jq' parse pod data: ${POD_LABELS}."
+ exit 1
+fi
+
+if ! KUBE_SYSTEM_NS_UID=$(jq -r '.metadata.uid' <<< "$KUBE_SYSTEM_NS_DATA" 2>&1); then
+ echo "error on 'jq' parse kube_system_ns: ${KUBE_SYSTEM_NS_UID}."
+ exit 1
+fi
+
+echo -e "$POD_LABELS\nk8s_cluster_id:$KUBE_SYSTEM_NS_UID"
+exit 0
diff --git a/daemon/system-info.sh b/daemon/system-info.sh
index 80eb82f8..05d8667c 100755
--- a/daemon/system-info.sh
+++ b/daemon/system-info.sh
@@ -108,9 +108,9 @@ else
fi
# shellcheck disable=SC2153
- if [ "${NAME}" = "unknown" ] || [ "${VERSION}" = "unknown" ] || [ "${ID}" = "unknown" ]; then
+ if [ "${CONTAINER_NAME}" = "unknown" ] || [ "${CONTAINER_VERSION}" = "unknown" ] || [ "${CONTAINER_ID}" = "unknown" ]; then
if [ -f "/etc/lsb-release" ]; then
- if [ "${OS_DETECTION}" = "unknown" ]; then
+ if [ "${CONTAINER_OS_DETECTION}" = "unknown" ]; then
CONTAINER_OS_DETECTION="/etc/lsb-release"
else
CONTAINER_OS_DETECTION="Mixed"
@@ -119,19 +119,19 @@ else
DISTRIB_RELEASE="unknown"
DISTRIB_CODENAME="unknown"
eval "$(grep -E "^(DISTRIB_ID|DISTRIB_RELEASE|DISTRIB_CODENAME)=" < /etc/lsb-release)"
- if [ "${NAME}" = "unknown" ]; then CONTAINER_NAME="${DISTRIB_ID}"; fi
- if [ "${VERSION}" = "unknown" ]; then CONTAINER_VERSION="${DISTRIB_RELEASE}"; fi
- if [ "${ID}" = "unknown" ]; then CONTAINER_ID="${DISTRIB_CODENAME}"; fi
+ if [ "${CONTAINER_NAME}" = "unknown" ]; then CONTAINER_NAME="${DISTRIB_ID}"; fi
+ if [ "${CONTAINER_VERSION}" = "unknown" ]; then CONTAINER_VERSION="${DISTRIB_RELEASE}"; fi
+ if [ "${CONTAINER_ID}" = "unknown" ]; then CONTAINER_ID="${DISTRIB_CODENAME}"; fi
fi
if [ -n "$(command -v lsb_release 2> /dev/null)" ]; then
- if [ "${OS_DETECTION}" = "unknown" ]; then
+ if [ "${CONTAINER_OS_DETECTION}" = "unknown" ]; then
CONTAINER_OS_DETECTION="lsb_release"
else
CONTAINER_OS_DETECTION="Mixed"
fi
- if [ "${NAME}" = "unknown" ]; then CONTAINER_NAME="$(lsb_release -is 2> /dev/null)"; fi
- if [ "${VERSION}" = "unknown" ]; then CONTAINER_VERSION="$(lsb_release -rs 2> /dev/null)"; fi
- if [ "${ID}" = "unknown" ]; then CONTAINER_ID="$(lsb_release -cs 2> /dev/null)"; fi
+ if [ "${CONTAINER_NAME}" = "unknown" ]; then CONTAINER_NAME="$(lsb_release -is 2> /dev/null)"; fi
+ if [ "${CONTAINER_VERSION}" = "unknown" ]; then CONTAINER_VERSION="$(lsb_release -rs 2> /dev/null)"; fi
+ if [ "${CONTAINER_ID}" = "unknown" ]; then CONTAINER_ID="$(lsb_release -cs 2> /dev/null)"; fi
fi
fi
fi
@@ -143,7 +143,9 @@ HOST_VERSION="unknown"
HOST_VERSION_ID="unknown"
HOST_ID="unknown"
HOST_ID_LIKE="unknown"
-if [ "${CONTAINER}" = "unknown" ]; then
+
+# 'systemd-detect-virt' returns 'none' if there is no hardware/container virtualization.
+if [ "${CONTAINER}" = "unknown" ] || [ "${CONTAINER}" = "none" ]; then
for v in NAME ID ID_LIKE VERSION VERSION_ID OS_DETECTION; do
eval "HOST_$v=\$CONTAINER_$v; CONTAINER_$v=none"
done