Merge pull request #3183 from carlisia/c-name

Better name format for init containers
This commit is contained in:
JenTing Hsiao
2020-12-16 23:16:48 +08:00
committed by GitHub
2 changed files with 43 additions and 11 deletions

View File

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

View File

@@ -30,28 +30,53 @@ func TestGetName(t *testing.T) {
{
name: "image name with registry hostname and tag",
image: "gcr.io/my-repo/my-image:latest",
expected: "my-image",
expected: "my-repo-my-image",
},
{
name: "image name with registry hostname, without tag",
image: "gcr.io/my-repo/my-image",
expected: "my-image",
expected: "my-repo-my-image",
},
{
name: "image name without registry hostname, with tag",
image: "my-repo/my-image:latest",
expected: "my-image",
expected: "my-repo-my-image",
},
{
name: "image name without registry hostname, without tag",
image: "my-repo/my-image",
expected: "my-image",
expected: "my-repo-my-image",
},
{
name: "image name with registry hostname and port, and tag",
image: "mycustomregistry.io:8080/my-repo/my-image:latest",
expected: "my-repo-my-image",
},
{
name: "image name with no / in it",
image: "my-image",
expected: "my-image",
},
{
name: "image name starting with / in it",
image: "/my-image",
expected: "my-image",
},
{
name: "image name with repo starting with a / as first char",
image: "/my-repo/my-image",
expected: "my-repo-my-image",
},
{
name: "image name with registry hostname, etoomany slashes, without tag",
image: "gcr.io/my-repo/mystery/another/my-image",
expected: "my-repo-mystery-another-my-image",
},
{
name: "image name with registry hostname starting with a / will include the registry name ¯\\_(ツ)_/¯",
image: "/gcr.io/my-repo/mystery/another/my-image",
expected: "gcr.io-my-repo-mystery-another-my-image",
},
}
for _, test := range tests {