Better name format for init containers

Signed-off-by: Carlisia <carlisia@vmware.com>
pull/3183/head
Carlisia 2020-12-14 14:14:12 -08:00
parent 2f635e14ce
commit 2de7c7924c
2 changed files with 17 additions and 11 deletions

View File

@ -44,23 +44,29 @@ func ForPluginContainer(image string, pullPolicy corev1api.PullPolicy) *Containe
} }
// getName returns the 'name' component of a docker // getName returns the 'name' component of a docker
// image (i.e. everything after the last '/' and before // image that includes its reposiroty name, and transforms the combined
// any subsequent ':') // string into a DNS-1123 compatible name.
func getName(image string) string { func getName(image string) string {
slashIndex := strings.LastIndex(image, "/") slashIndex := strings.Index(image, "/")
slashCount := strings.Count(image[slashIndex:], "/")
colonIndex := strings.LastIndex(image, ":") colonIndex := strings.LastIndex(image, ":")
// this removes the registry name when there is one, but keeps the repository name
start := 0 start := 0
if slashIndex > 0 { if slashCount == 1 {
start = 0
} else {
// this will be the first character after the first found slash
start = slashIndex + 1 start = slashIndex + 1
} }
// this removes the tag
end := len(image) end := len(image)
if colonIndex > slashIndex { if colonIndex > 0 {
end = colonIndex end = colonIndex
} }
return image[start:end] return strings.Replace(image[start:end], "/", "-", 1) // this makes it DNS-1123 compatible
} }
// Result returns the built Container. // Result returns the built Container.

View File

@ -30,27 +30,27 @@ func TestGetName(t *testing.T) {
{ {
name: "image name with registry hostname and tag", name: "image name with registry hostname and tag",
image: "gcr.io/my-repo/my-image:latest", image: "gcr.io/my-repo/my-image:latest",
expected: "my-image", expected: "my-repo-my-image",
}, },
{ {
name: "image name with registry hostname, without tag", name: "image name with registry hostname, without tag",
image: "gcr.io/my-repo/my-image", image: "gcr.io/my-repo/my-image",
expected: "my-image", expected: "my-repo-my-image",
}, },
{ {
name: "image name without registry hostname, with tag", name: "image name without registry hostname, with tag",
image: "my-repo/my-image:latest", image: "my-repo/my-image:latest",
expected: "my-image", expected: "my-repo-my-image",
}, },
{ {
name: "image name without registry hostname, without tag", name: "image name without registry hostname, without tag",
image: "my-repo/my-image", image: "my-repo/my-image",
expected: "my-image", expected: "my-repo-my-image",
}, },
{ {
name: "image name with registry hostname and port, and tag", name: "image name with registry hostname and port, and tag",
image: "mycustomregistry.io:8080/my-repo/my-image:latest", image: "mycustomregistry.io:8080/my-repo/my-image:latest",
expected: "my-image", expected: "my-repo-my-image",
}, },
} }