Skip to content

Commit b7998ec

Browse files
authored
When running from within a k8s pod, use the k8s API to get the cgroup name (netdata#5576)
* When running from within a k8s pod, use the k8s API to get the pod name * Check the last part of an underscore-delimited or slash-delimited container id * Add a docker image builder that adds a single image to a user-specified registry, for use in k8s * When running in k8s, disable cgroups that the pod API does not return * Use longer name for k8s containers * Add reference to build-test.sh to packaging/docker/README.md * Anonymous statistics should not break when /proc/1/sched is not available
1 parent 97699c5 commit b7998ec

5 files changed

Lines changed: 144 additions & 22 deletions

File tree

collectors/cgroups.plugin/cgroup-name.sh.in

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ function docker_get_name_api() {
6464
return 0
6565
}
6666

67+
function k8s_get_name() {
68+
# Take the last part of the delimited path identifier (expecting either _ or / as a delimiter).
69+
local id="${1##*_}"
70+
if [ "${id}" == "${1}" ]; then
71+
id="${1##*/}"
72+
fi
73+
KUBE_TOKEN="$(</var/run/secrets/kubernetes.io/serviceaccount/token)"
74+
NAME="$(
75+
curl -sSk -H "Authorization: Bearer $KUBE_TOKEN" "https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/pods" |
76+
jq -r '.items[] | "k8s_\(.metadata.namespace)_\(.metadata.name)_\(.metadata.uid)_\(.status.containerStatuses[0].name) \(.status.containerStatuses[0].containerID)"' |
77+
grep "$id" |
78+
cut -d' ' -f1
79+
)"
80+
if [ -z "${NAME}" ]; then
81+
warning "cannot find the name of k8s pod with containerID '${id}'. Setting name to ${id} and disabling it"
82+
NAME="${id}"
83+
NAME_NOT_FOUND=3
84+
else
85+
info "k8s containerID '${id}' has chart name (namespace_podname_poduid_containername) '${NAME}'"
86+
fi
87+
}
88+
6789
function docker_get_name() {
6890
local id="${1}"
6991
if hash docker 2>/dev/null; then
@@ -73,7 +95,7 @@ function docker_get_name() {
7395
fi
7496
if [ -z "${NAME}" ]; then
7597
warning "cannot find the name of docker container '${id}'"
76-
NAME_NOT_FOUND=1
98+
NAME_NOT_FOUND=2
7799
NAME="${id:0:12}"
78100
else
79101
info "docker container '${id}' is named '${NAME}'"
@@ -89,6 +111,7 @@ function docker_validate_id() {
89111
fi
90112
}
91113

114+
92115
# -----------------------------------------------------------------------------
93116

94117
[ -z "${NETDATA_USER_CONFIG_DIR}" ] && NETDATA_USER_CONFIG_DIR="@configdir_POST@"
@@ -118,25 +141,22 @@ for CONFIG in "${NETDATA_USER_CONFIG_DIR}/cgroups-names.conf" "${NETDATA_STOCK_C
118141
fi
119142
done
120143

144+
if [ -z "${NAME}" ] && [ -n "${KUBERNETES_SERVICE_HOST}" ] && [ -n "${KUBERNETES_PORT_443_TCP_PORT}" ] && [[ ${CGROUP} =~ ^.*kubepods.* ]]; then
145+
k8s_get_name "${CGROUP}"
146+
fi
147+
121148
if [ -z "${NAME}" ]; then
122149
if [[ ${CGROUP} =~ ^.*docker[-_/\.][a-fA-F0-9]+[-_\.]?.*$ ]]; then
123150
# docker containers
124151
#shellcheck disable=SC1117
125152
DOCKERID="$(echo "${CGROUP}" | sed "s|^.*docker[-_/]\([a-fA-F0-9]\+\)[-_\.]\?.*$|\1|")"
126153
docker_validate_id "${DOCKERID}"
127-
128154
elif [[ ${CGROUP} =~ ^.*ecs[-_/\.][a-fA-F0-9]+[-_\.]?.*$ ]]; then
129155
# ECS
130156
#shellcheck disable=SC1117
131157
DOCKERID="$(echo "${CGROUP}" | sed "s|^.*ecs[-_/].*[-_/]\([a-fA-F0-9]\+\)[-_\.]\?.*$|\1|")"
132158
docker_validate_id "${DOCKERID}"
133159

134-
elif [[ ${CGROUP} =~ ^.*kubepods[_/].*[_/]pod[a-fA-F0-9-]+[_/][a-fA-F0-9]+$ ]]; then
135-
# kubernetes
136-
#shellcheck disable=SC1117
137-
DOCKERID="$(echo "${CGROUP}" | sed "s|^.*kubepods[_/].*[_/]pod[a-fA-F0-9-]\+[_/]\([a-fA-F0-9]\+\)$|\1|")"
138-
docker_validate_id "${DOCKERID}"
139-
140160
elif [[ ${CGROUP} =~ machine.slice[_/].*\.service ]]; then
141161
# systemd-nspawn
142162
NAME="$(echo "${CGROUP}" | sed 's/.*machine.slice[_\/]\(.*\)\.service/\1/g')"
@@ -177,8 +197,5 @@ fi
177197
info "cgroup '${CGROUP}' is called '${NAME}'"
178198
echo "${NAME}"
179199

180-
if [ "${NAME_NOT_FOUND}" -eq 1 ]; then
181-
exit 2
182-
else
183-
exit 0
184-
fi
200+
exit ${NAME_NOT_FOUND}
201+

collectors/cgroups.plugin/sys_fs_cgroup.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ static inline void cgroup_get_chart_name(struct cgroup *cg) {
893893

894894
snprintfz(command, CGROUP_CHARTID_LINE_MAX, "exec %s '%s'", cgroups_rename_script, cg->chart_id);
895895

896-
debug(D_CGROUP, "executing command \"%s\" for cgroup '%s'", command, cg->id);
896+
debug(D_CGROUP, "executing command \"%s\" for cgroup '%s'", command, cg->chart_id);
897897
FILE *fp = mypopen(command, &cgroup_pid);
898898
if(fp) {
899899
// debug(D_CGROUP, "reading from command '%s' for cgroup '%s'", command, cg->id);
@@ -904,12 +904,16 @@ static inline void cgroup_get_chart_name(struct cgroup *cg) {
904904
// debug(D_CGROUP, "closed command for cgroup '%s'", cg->id);
905905

906906
if(s && *s && *s != '\n') {
907-
debug(D_CGROUP, "cgroup '%s' should be renamed to '%s'", cg->id, s);
907+
debug(D_CGROUP, "cgroup '%s' should be renamed to '%s'", cg->chart_id, s);
908908

909909
s = trim(s);
910910
if (s) {
911-
if(likely(!name_error))
911+
if(likely(name_error==0))
912912
cg->pending_renames = 0;
913+
else if (unlikely(name_error==3)) {
914+
debug(D_CGROUP, "cgroup '%s' disabled based due to rename command output", cg->chart_id);
915+
cg->enabled = 0;
916+
}
913917

914918
if(likely(cg->pending_renames < 2)) {
915919
freez(cg->chart_title);

daemon/anonymous-statistics.sh.in

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,14 @@ fi
8585
# -------------------------------------------------------------------------------------------------
8686
# detect containers with heuristics
8787

88-
if [ "${CONTAINER}" = "unknown" ] ; then
89-
IFS='(, ' read -r process _ </proc/1/sched
90-
if [ "${process}" = "netdata" ]; then
91-
CONTAINER="container"
92-
CONT_DETECTION="process"
88+
if [ "${CONTAINER}" = "unknown" ]; then
89+
if [ -f /proc/1/sched ] ; then
90+
IFS='(, ' read -r process _ </proc/1/sched
91+
if [ "${process}" = "netdata" ]; then
92+
CONTAINER="container"
93+
CONT_DETECTION="process"
94+
fi
9395
fi
94-
9596
# ubuntu and debian supply /bin/running-in-container
9697
# https://www.apt-browse.org/browse/ubuntu/trusty/main/i386/upstart/1.12.1-0ubuntu4/file/bin/running-in-container
9798
if /bin/running-in-container >/dev/null 2>&1; then

packaging/docker/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,35 @@ services:
124124
You can restrict access by following [official caddy guide](https://caddyserver.com/docs/basicauth) and adding lines to Caddyfile.
125125

126126
[![analytics](https://www.google-analytics.com/collect?v=1&aip=1&t=pageview&_s=1&ds=github&dr=https%3A%2F%2Fgithub.com%2Fnetdata%2Fnetdata&dl=https%3A%2F%2Fmy-netdata.io%2Fgithub%2Fpackaging%2Fdocker%2FREADME&_u=MAC~&cid=5792dfd7-8dc4-476b-af31-da2fdb9f93d2&tid=UA-64295674-3)]()
127+
128+
### Publish a test image to your own repository
129+
130+
The script `packaging/docker/build-test.sh` can be used to create an image and upload it to a repository of your choosing.
131+
132+
```
133+
Usage: packaging/docker/build-test.sh -r <REPOSITORY> -v <VERSION> -u <DOCKER_USERNAME> -p <DOCKER_PASSWORD> [-s]
134+
-s skip build, just push the image
135+
Builds an amd64 image and pushes it to the docker hub repository REPOSITORY
136+
```
137+
138+
This is especially useful when testing a Pull Request for Kubernetes, since you can set `image` to an immutable repository and tag, set the `imagePullPolicy` to `Always` and just keep uploading new images.
139+
140+
Example:
141+
142+
We get a local copy of the Helm chart at https://github.com/netdata/helmchart. We modify `values.yaml` to have the following:
143+
144+
```
145+
image:
146+
repository: cakrit/netdata-prs
147+
tag: PR5576
148+
pullPolicy: Always
149+
```
150+
151+
We check out PR5576 and run the following:
152+
```
153+
./packaging/docker/build-test.sh -r cakrit/netdata-prs -v PR5576 -u cakrit -p 'XXX'
154+
```
155+
156+
Then we can run `helm install [path to our helmchart clone]`.
157+
158+
If we make changes to the code, we execute the same `build-test.sh` command, followed by `helm upgrade [name] [path to our helmchart clone]`

packaging/docker/build-test.sh

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-3.0-or-later
3+
# Author : Chris Akritidis (cakrit)
4+
# Cross-arch docker build helper script
5+
6+
printhelp() {
7+
echo "Usage: packaging/docker/build-test.sh -r <REPOSITORY> -v <VERSION> -u <DOCKER_USERNAME> -p <DOCKER_PASSWORD> [-s]
8+
-s skip build, just push the image
9+
Builds an amd64 image and pushes it to the docker hub repository REPOSITORY"
10+
}
11+
12+
set -e
13+
14+
if [ ! -f .gitignore ]; then
15+
echo "Run as ./packaging/docker/$(basename "$0") from top level directory of git repository"
16+
exit 1
17+
fi
18+
19+
DOBUILD=1
20+
while getopts :r:v:u:p:s option
21+
do
22+
case "$option" in
23+
r)
24+
REPOSITORY=$OPTARG
25+
;;
26+
v)
27+
VERSION=$OPTARG
28+
;;
29+
u)
30+
DOCKER_USERNAME=$OPTARG
31+
;;
32+
p)
33+
DOCKER_PASSWORD=$OPTARG
34+
;;
35+
s)
36+
DOBUILD=0
37+
;;
38+
*)
39+
printhelp
40+
exit 1
41+
;;
42+
esac
43+
done
44+
45+
if [ -n "${REPOSITORY}" ] && [ -n "${VERSION}" ] && [ -n "${DOCKER_USERNAME}" ] && [ -n "${DOCKER_PASSWORD}" ] ; then
46+
if [ $DOBUILD -eq 1 ] ; then
47+
echo "Building ${VERSION} of ${REPOSITORY} container"
48+
docker run --rm --privileged multiarch/qemu-user-static:register --reset
49+
50+
# Build images using multi-arch Dockerfile.
51+
eval docker build --build-arg ARCH="amd64" --tag "${REPOSITORY}:${VERSION}" --file packaging/docker/Dockerfile ./
52+
53+
# Create temporary docker CLI config with experimental features enabled (manifests v2 need it)
54+
mkdir -p /tmp/docker
55+
#echo '{"experimental":"enabled"}' > /tmp/docker/config.json
56+
fi
57+
58+
# Login to docker hub to allow futher operations
59+
echo "Logging into docker"
60+
echo "$DOCKER_PASSWORD" | docker --config /tmp/docker login -u "$DOCKER_USERNAME" --password-stdin
61+
62+
echo "Pushing ${REPOSITORY}:${VERSION}"
63+
docker --config /tmp/docker push "${REPOSITORY}:${VERSION}"
64+
else
65+
echo "Missing parameter. REPOSITORY=${REPOSITORY} VERSION=${VERSION} DOCKER_USERNAME=${DOCKER_USERNAME} DOCKER_PASSWORD=${DOCKER_PASSWORD}"
66+
printhelp
67+
exit 1
68+
fi

0 commit comments

Comments
 (0)