Skip to content

Commit

Permalink
Improve Docker image identifier parsing (influxdata#5838)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmcken authored and danielnelson committed May 17, 2019
1 parent f76cca3 commit 633a468
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 11 deletions.
45 changes: 34 additions & 11 deletions plugins/inputs/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,39 @@ func (d *Docker) gatherInfo(acc telegraf.Accumulator) error {
return nil
}

func parseImage(image string) (string, string) {
// Adapts some of the logic from the actual Docker library's image parsing
// routines:
// https://github.com/docker/distribution/blob/release/2.7/reference/normalize.go
domain := ""
remainder := ""

i := strings.IndexRune(image, '/')

if i == -1 || (!strings.ContainsAny(image[:i], ".:") && image[:i] != "localhost") {
remainder = image
} else {
domain, remainder = image[:i], image[i+1:]
}

imageName := ""
imageVersion := "unknown"

i = strings.LastIndex(remainder, ":")
if i > -1 {
imageVersion = remainder[i+1:]
imageName = remainder[:i]
} else {
imageName = remainder
}

if domain != "" {
imageName = domain + "/" + imageName
}

return imageName, imageVersion
}

func (d *Docker) gatherContainer(
container types.Container,
acc telegraf.Accumulator,
Expand All @@ -366,17 +399,7 @@ func (d *Docker) gatherContainer(
return nil
}

// 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"
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
}
imageName, imageVersion := parseImage(container.Image)

tags := map[string]string{
"engine_host": d.engine_host,
Expand Down
51 changes: 51 additions & 0 deletions plugins/inputs/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,3 +815,54 @@ func TestContainerName(t *testing.T) {
})
}
}

func TestParseImage(t *testing.T) {
tests := []struct {
image string
parsedName string
parsedVersion string
}{
{
image: "postgres",
parsedName: "postgres",
parsedVersion: "unknown",
},
{
image: "postgres:latest",
parsedName: "postgres",
parsedVersion: "latest",
},
{
image: "coreos/etcd",
parsedName: "coreos/etcd",
parsedVersion: "unknown",
},
{
image: "coreos/etcd:latest",
parsedName: "coreos/etcd",
parsedVersion: "latest",
},
{
image: "quay.io/postgres",
parsedName: "quay.io/postgres",
parsedVersion: "unknown",
},
{
image: "quay.io:4443/coreos/etcd",
parsedName: "quay.io:4443/coreos/etcd",
parsedVersion: "unknown",
},
{
image: "quay.io:4443/coreos/etcd:latest",
parsedName: "quay.io:4443/coreos/etcd",
parsedVersion: "latest",
},
}
for _, tt := range tests {
t.Run("parse name "+tt.image, func(t *testing.T) {
imageName, imageVersion := parseImage(tt.image)
require.Equal(t, tt.parsedName, imageName)
require.Equal(t, tt.parsedVersion, imageVersion)
})
}
}

0 comments on commit 633a468

Please sign in to comment.