Skip to content

Commit

Permalink
fix: containerId coming from system driver are correctly parsed (#34)
Browse files Browse the repository at this point in the history
* fix: containerId coming from system driver are correctly parsed
  • Loading branch information
ardias authored Oct 22, 2020
1 parent a64a06a commit 8a311fc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ BINARY_NAME = nri-kubernetes
E2E_BINARY_NAME := $(BINARY_NAME)-e2e

GOVENDOR_VERSION = 1.0.8
GOLANGCILINT_VERSION = 1.29
GOLANGCILINT_VERSION = 1.31.0

.PHONY: all
all: build
Expand Down
24 changes: 23 additions & 1 deletion src/kubelet/metric/cadvisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metric
import (
"errors"
"fmt"
"regexp"
"strings"

"github.com/newrelic/nri-kubernetes/src/client"
Expand All @@ -19,6 +20,12 @@ const (
StandaloneCAdvisorMetricsPath = "/metrics"
)

var (
dockerNativeWithoutSystemD = regexp.MustCompile("^.*([0-9a-f]+)$")
dockerNativeWithSystemD = regexp.MustCompile("^.*\\w+-([0-9a-f]+)\\.scope$")
dockerGeneric = regexp.MustCompile("^([0-9a-f]+)$")
)

// getLabel returns the first label it finds by the given names
func getLabel(labels prometheus.Labels, names ...string) (string, bool) {
for _, name := range names {
Expand Down Expand Up @@ -129,6 +136,21 @@ func createRawEntityID(m prometheus.Metric) (string, error) {
}

// /kubepods/besteffort/podba8b34d7-11a3-11e8-a084-080027352a02/a949bd136c1397b9f52905538ee11450427be33648abe38db06be2e5cfbeca49
// /docker/ae17ce6dcd2f27905cedf80609044290eccd98115b4e1ded08fcf6852cf939ae/kubepods.slice/kubepods-besteffort.slice/kubepods-besteffort-pod13118b761000f8fe2c4662d5f32d9532.slice/crio-ebccdd64bb3ef5dfa9d9b167cb5e30f9b696c2694fb7e0783af5575c28be3d1b.scope
// /docker/d44b560aba016229fd4f87a33bf81e8eaf6c81932a0623530456e8f80f9675ad/kubepods/besteffort/pod6edbcc6c66e4b5af53005f91bf0bc1fd/7588a02459ef3166ba043c5a605c9ce65e4dd250d7ee40428a28d806c4116e97
func extractContainerID(v string) string {
return v[strings.LastIndex(v, "/")+1:]
containerID := v[strings.LastIndex(v, "/")+1:]
matches := dockerNativeWithSystemD.FindStringSubmatch(containerID)
if len(matches) > 0 {
return matches[1]
}
matches = dockerNativeWithoutSystemD.FindStringSubmatch(containerID)
if len(matches) > 0 {
return matches[0]
}
matches = dockerGeneric.FindStringSubmatch(containerID)
if len(matches) > 0 {
return matches[0]
}
return containerID
}
33 changes: 33 additions & 0 deletions src/kubelet/metric/cadvisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,36 @@ container_memory_usage_bytes{container_name="influxdb",id="/kubepods/besteffort/

assert.ElementsMatch(t, expectedErrs, err.(data.ErrorGroup).Errors)
}

func TestExtractContainerIdWithoutSystemD_ReturnsCorrectId(t *testing.T) {
// given
fullContainerID := "/docker/d44b560aba016229fd4f87a33bf81e8eaf6c81932a0623530456e8f80f9675ad/kubepods/besteffort/pod6edbcc6c66e4b5af53005f91bf0bc1fd/7588a02459ef3166ba043c5a605c9ce65e4dd250d7ee40428a28d806c4116e97"
expected := "7588a02459ef3166ba043c5a605c9ce65e4dd250d7ee40428a28d806c4116e97"

// when
actual := extractContainerID(fullContainerID)

assert.Equal(t, expected, actual)
}

func TestExtractContainerIdWithSystemD_ReturnsCorrectId(t *testing.T) {
// given
fullContainerID := "/docker/ae17ce6dcd2f27905cedf80609044290eccd98115b4e1ded08fcf6852cf939ae/kubepods.slice/kubepods-besteffort.slice/kubepods-besteffort-pod13118b761000f8fe2c4662d5f32d9532.slice/crio-ebccdd64bb3ef5dfa9d9b167cb5e30f9b696c2694fb7e0783af5575c28be3d1b.scope"
expected := "ebccdd64bb3ef5dfa9d9b167cb5e30f9b696c2694fb7e0783af5575c28be3d1b"

// when
actual := extractContainerID(fullContainerID)

assert.Equal(t, expected, actual)
}

func TestExtractContainerIdDockerGeneric_ReturnsCorrectId(t *testing.T) {
// given
fullContainerID := "docker://5a4eefc5b30f6f12402665bd920ee8e889ba9e14bdcc623e2865a79d40f58412"
expected := "5a4eefc5b30f6f12402665bd920ee8e889ba9e14bdcc623e2865a79d40f58412"

// when
actual := extractContainerID(fullContainerID)

assert.Equal(t, expected, actual)
}

0 comments on commit 8a311fc

Please sign in to comment.