From 3876e36bc02292a60464e8fcfe536206d451531c Mon Sep 17 00:00:00 2001 From: Alex Sherwin Date: Fri, 16 Dec 2016 08:53:16 -0500 Subject: [PATCH] fixes #1987 custom docker repos with non-standard port (#2018) * fixed parsing of docker image name/version now accounts for custom docker repo's which contain a colon for a non-default port * 1978: modifying docker test case to have a custom repo with non-standard port * using a temp var to store index, ran gofmt * fixes #1987, renaming iterator to 'i' --- plugins/inputs/docker/docker.go | 16 ++++++++++------ plugins/inputs/docker/docker_test.go | 6 +++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/plugins/inputs/docker/docker.go b/plugins/inputs/docker/docker.go index e2c488dc85cff..7fc48689fc2a0 100644 --- a/plugins/inputs/docker/docker.go +++ b/plugins/inputs/docker/docker.go @@ -221,14 +221,18 @@ func (d *Docker) gatherContainer( cname = strings.TrimPrefix(container.Names[0], "/") } - // the image name sometimes has a version part. - // ie, rabbitmq:3-management - imageParts := strings.Split(container.Image, ":") - imageName := imageParts[0] + // the image name sometimes has a version part, or a private repo + // ie, rabbitmq:3-management or docker.someco.net:4443/rabbitmq:3-management + imageName := "" imageVersion := "unknown" - if len(imageParts) > 1 { - imageVersion = imageParts[1] + i := strings.LastIndex(container.Image, ":") // index of last ':' character + if i > -1 { + imageVersion = container.Image[i+1:] + imageName = container.Image[:i] + } else { + imageName = container.Image } + tags := map[string]string{ "engine_host": d.engine_host, "container_name": cname, diff --git a/plugins/inputs/docker/docker_test.go b/plugins/inputs/docker/docker_test.go index 21960a4d831d9..a60203af55e45 100644 --- a/plugins/inputs/docker/docker_test.go +++ b/plugins/inputs/docker/docker_test.go @@ -340,7 +340,7 @@ func (d FakeDockerClient) ContainerList(octx context.Context, options types.Cont container2 := types.Container{ ID: "b7dfbb9478a6ae55e237d4d74f8bbb753f0817192b5081334dc78476296e2173", Names: []string{"/etcd2"}, - Image: "quay.io/coreos/etcd:v2.2.2", + Image: "quay.io:4443/coreos/etcd:v2.2.2", Command: "/etcd -name etcd2 -advertise-client-urls http://localhost:2379 -listen-client-urls http://0.0.0.0:2379", Created: 1455941933, Status: "Up 4 hours", @@ -429,7 +429,7 @@ func TestDockerGatherInfo(t *testing.T) { }, map[string]string{ "container_name": "etcd2", - "container_image": "quay.io/coreos/etcd", + "container_image": "quay.io:4443/coreos/etcd", "cpu": "cpu3", "container_version": "v2.2.2", "engine_host": "absol", @@ -477,7 +477,7 @@ func TestDockerGatherInfo(t *testing.T) { map[string]string{ "engine_host": "absol", "container_name": "etcd2", - "container_image": "quay.io/coreos/etcd", + "container_image": "quay.io:4443/coreos/etcd", "container_version": "v2.2.2", }, )