@cryptotaxi247 / netdata-1 / commits / fce6650a6

Added ability to get pod name from cgroup with kubectl in bare metal deployment (#7416)

* Ability to get pod name with kubectl in cgroup * added $KUBE_CONFIG variable * shellcheck disable=SC2086

Chris Akritidis committed Mar 5, 2020 at 06:30 UTC fce6650a689625f3a591cf79046395b62c9687f9
1 file changed +31 -16
collectors/cgroups.plugin/cgroup-name.sh.in
+31 -16
@@ -77,19 +77,36 @@ function docker_get_name_api() {
77 }
78
79 function k8s_get_name() {
80 - # Take the last part of the delimited path identifier (expecting either _ or / as a delimiter).
81 - local id="${1##*_}"
82 - if [ "${id}" == "${1}" ]; then
83 - id="${1##*/}"
80 + if [[ "${1}" =~ ^kube.*_pod.*$ ]]; then
81 + local id="${1##*_pod}"
82 + else
83 + # Take the last part of the delimited path identifier (expecting either _ or / as a delimiter).
84 + local id="${1##*_}"
85 + if [ "${id}" == "${1}" ]; then
86 + id="${1##*/}"
87 + fi
88 fi
85 - KUBE_TOKEN="$(</var/run/secrets/kubernetes.io/serviceaccount/token)"
89 if command -v jq >/dev/null 2>&1; then
87 - NAME="$(
88 - curl -sSk -H "Authorization: Bearer $KUBE_TOKEN" "https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/pods" |
89 - jq -r '.items[] | "k8s_\(.metadata.namespace)_\(.metadata.name)_\(.metadata.uid)_" + (.status.containerStatuses[]? | "\(.name) \(.containerID)")' |
90 - grep "$id" |
91 - cut -d' ' -f1
92 - )"
90 + if [ -n "${KUBERNETES_SERVICE_HOST}" ] && [ -n "${KUBERNETES_PORT_443_TCP_PORT}" ]; then
91 + KUBE_TOKEN="$(</var/run/secrets/kubernetes.io/serviceaccount/token)"
92 + NAME="$(
93 + curl -sSk -H "Authorization: Bearer $KUBE_TOKEN" "https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/pods" |
94 + jq -r '.items[] | "k8s_\(.metadata.namespace)_\(.metadata.name)_\(.metadata.uid)_\(.status.containerStatuses[0].name) \(.status.containerStatuses[0].containerID)"' |
95 + grep "$id" |
96 + cut -d' ' -f1
97 + )"
98 + elif ps -C kubelet >/dev/null 2>&1 && command -v kubectl >/dev/null 2>&1; then
99 + if [[ -z ${KUBE_CONFIG+x} ]]; then
100 + KUBE_CONFIG="/etc/kubernetes/admin.conf"
101 + fi
102 + if kubectl --kubeconfig=$KUBE_CONFIG get pod --all-namespaces >/dev/null 2>&1; then
103 + #shellcheck disable=SC2086
104 + NAME="$(kubectl --kubeconfig=$KUBE_CONFIG get pod --all-namespaces --output='json' | \
105 + jq -r '.items[] | select(.metadata.uid == "'$id'") | .metadata.name')"
106 + else
107 + warning "kubectl cannot get pod list, check for configuration file in $KUBE_CONFIG, or set this path to env \$KUBE_CONFIG"
108 + fi
109 + fi
110 else
111 warning "jq command not available, k8s_get_name() cannot execute. Please install jq should you wish for k8s to be fully functional"
112 fi
@@ -158,12 +175,10 @@ for CONFIG in "${NETDATA_USER_CONFIG_DIR}/cgroups-names.conf" "${NETDATA_STOCK_C
175 fi
176 done
177
161 -if [ -z "${NAME}" ] && [ -n "${KUBERNETES_SERVICE_HOST}" ] && [ -n "${KUBERNETES_PORT_443_TCP_PORT}" ] && [[ ${CGROUP} =~ ^.*kubepods.* ]]; then
162 - k8s_get_name "${CGROUP}"
163 -fi
164 -
178 if [ -z "${NAME}" ]; then
166 - if [[ ${CGROUP} =~ ^.*docker[-_/\.][a-fA-F0-9]+[-_\.]?.*$ ]]; then
179 + if [[ ${CGROUP} =~ ^.*kubepods.* ]]; then
180 + k8s_get_name "${CGROUP}"
181 + elif [[ ${CGROUP} =~ ^.*docker[-_/\.][a-fA-F0-9]+[-_\.]?.*$ ]]; then
182 # docker containers
183 #shellcheck disable=SC1117
184 DOCKERID="$(echo "${CGROUP}" | sed "s|^.*docker[-_/]\([a-fA-F0-9]\+\)[-_\.]\?.*$|\1|")"