mirror of
https://github.com/weaveworks/scope.git
synced 2026-08-18 20:07:06 +00:00
Addressed @LiliC's feedback.
This commit is contained in:
@@ -467,3 +467,27 @@ func ContainerIsStopped(c Container) bool {
|
||||
state := c.StateString()
|
||||
return (state != StateRunning && state != StateRestarting && state != StatePaused)
|
||||
}
|
||||
|
||||
// splitImageName returns parts of the full image name (image name, image tag).
|
||||
func splitImageName(imageName string) []string {
|
||||
parts := strings.SplitN(imageName, "/", 3)
|
||||
if len(parts) == 3 {
|
||||
imageName = fmt.Sprintf("%s/%s", parts[1], parts[2])
|
||||
}
|
||||
return strings.SplitN(imageName, ":", 2)
|
||||
}
|
||||
|
||||
// ImageNameWithoutTag splits the image name apart, returning the name
|
||||
// without the version, if possible
|
||||
func ImageNameWithoutTag(imageName string) string {
|
||||
return splitImageName(imageName)[0]
|
||||
}
|
||||
|
||||
// ImageNameTag splits the image name apart, returning the version tag, if possible
|
||||
func ImageNameTag(imageName string) string {
|
||||
imageNameParts := splitImageName(imageName)
|
||||
if len(imageNameParts) < 2 {
|
||||
return ""
|
||||
}
|
||||
return imageNameParts[1]
|
||||
}
|
||||
|
||||
@@ -104,3 +104,18 @@ func TestPipes(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestDockerImageName(t *testing.T) {
|
||||
for _, input := range []struct{ in, name string }{
|
||||
{"foo/bar", "foo/bar"},
|
||||
{"foo/bar:baz", "foo/bar"},
|
||||
{"reg:123/foo/bar:baz", "foo/bar"},
|
||||
{"docker-registry.domain.name:5000/repo/image1:ver", "repo/image1"},
|
||||
{"foo", "foo"},
|
||||
} {
|
||||
name := docker.ImageNameWithoutTag(input.in)
|
||||
if name != input.name {
|
||||
t.Fatalf("%s: %s != %s", input.in, name, input.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -471,27 +469,3 @@ func (r *registry) WalkNetworks(f func(docker_client.Network)) {
|
||||
f(network)
|
||||
}
|
||||
}
|
||||
|
||||
// ImageNameParts returns parts of the full image name (image name, image tag).
|
||||
func ImageNameParts(name string) []string {
|
||||
parts := strings.SplitN(name, "/", 3)
|
||||
if len(parts) == 3 {
|
||||
name = fmt.Sprintf("%s/%s", parts[1], parts[2])
|
||||
}
|
||||
return strings.SplitN(name, ":", 2)
|
||||
}
|
||||
|
||||
// ImageNameWithoutVersion splits the image name apart, returning the name
|
||||
// without the version, if possible
|
||||
func ImageNameWithoutVersion(name string) string {
|
||||
return ImageNameParts(name)[0]
|
||||
}
|
||||
|
||||
// ImageNameVersion splits the image name apart, returning the version, if possible
|
||||
func ImageNameVersion(name string) string {
|
||||
imageNameParts := ImageNameParts(name)
|
||||
if len(imageNameParts) < 2 {
|
||||
return ""
|
||||
}
|
||||
return imageNameParts[1]
|
||||
}
|
||||
|
||||
@@ -515,18 +515,3 @@ func TestRegistryDelete(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestDockerImageName(t *testing.T) {
|
||||
for _, input := range []struct{ in, name string }{
|
||||
{"foo/bar", "foo/bar"},
|
||||
{"foo/bar:baz", "foo/bar"},
|
||||
{"reg:123/foo/bar:baz", "foo/bar"},
|
||||
{"docker-registry.domain.name:5000/repo/image1:ver", "repo/image1"},
|
||||
{"foo", "foo"},
|
||||
} {
|
||||
name := docker.ImageNameWithoutVersion(input.in)
|
||||
if name != input.name {
|
||||
t.Fatalf("%s: %s != %s", input.in, name, input.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,8 +283,8 @@ func (r *Reporter) containerImageTopology() report.Topology {
|
||||
}
|
||||
if len(image.RepoTags) > 0 {
|
||||
imageFullName := image.RepoTags[0]
|
||||
latests[ImageName] = ImageNameWithoutVersion(imageFullName)
|
||||
latests[ImageTag] = ImageNameVersion(imageFullName)
|
||||
latests[ImageName] = ImageNameWithoutTag(imageFullName)
|
||||
latests[ImageTag] = ImageNameTag(imageFullName)
|
||||
}
|
||||
nodeID := report.MakeContainerImageNodeID(imageID)
|
||||
node := report.MakeNodeWith(nodeID, latests)
|
||||
|
||||
@@ -123,7 +123,7 @@ func (t *Tagger) tag(tree process.Tree, topology *report.Topology) {
|
||||
// If we can work out the image name, add a parent tag for it
|
||||
image, ok := t.registry.GetContainerImage(c.Image())
|
||||
if ok && len(image.RepoTags) > 0 {
|
||||
imageName := ImageNameWithoutVersion(image.RepoTags[0])
|
||||
imageName := ImageNameWithoutTag(image.RepoTags[0])
|
||||
node = node.WithParents(report.MakeSets().
|
||||
Add(report.ContainerImage, report.MakeStringSet(report.MakeContainerImageNodeID(imageName))),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user