Skip to content

Commit d849d68

Browse files
committed
PRODENG-3225 more image tag detection use cases
- add tagless iamges to the image tag detection and refactor the tag detection to handled them Signed-off-by: James Nesbitt <james.r.nesbitt@gmail.com>
1 parent d5f2e51 commit d849d68

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

pkg/docker/image.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,27 @@ var errInvalidVersion = errors.New("invalid image version")
154154
//
155155
// e.g. `dtr.efzp.com:9026/mirantis/ucp-agent:3.8.10` => `dtr.efzp.com:9026/mirantis/ucp-agent`, `3.8.10`
156156
func ImageRepoAndTag(image string) (string, string, error) {
157-
vparts := strings.Split(image, ":")
158-
vpartslen := len(vparts)
159-
if vpartslen < 2 || vpartslen > 3 {
160-
return "", "", fmt.Errorf("%w: malformed version output: %s", errInvalidVersion, image)
157+
lastColon := strings.LastIndexByte(image, ':')
158+
lastSlash := strings.LastIndexByte(image, '/')
159+
160+
// If there is no colon, tag is implicitly "latest"
161+
if lastColon == -1 {
162+
return image, "latest", nil
163+
}
164+
165+
// If the last colon is part of the registry host (before the first slash),
166+
// and there are no more colons, then the tag is also implicitly "latest".
167+
// e.g. "localhost:5000/my-image"
168+
if lastColon < lastSlash {
169+
return image, "latest", nil
170+
}
171+
172+
repo := image[:lastColon]
173+
tag := image[lastColon+1:]
174+
175+
if tag == "" {
176+
return "", "", fmt.Errorf("%w: empty tag in version output: %s", errInvalidVersion, image)
161177
}
162178

163-
repo := strings.Join(vparts[0:vpartslen-1], ":")
164-
version := vparts[vpartslen-1]
165-
return repo, version, nil
179+
return repo, tag, nil
166180
}

pkg/docker/image_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ func TestImageVersions(t *testing.T) {
8383
{"localhost:5000/my-image:1.0.0", "localhost:5000/my-image", "1.0.0"},
8484
{"registry.mirantis.com/mirantiseng/ucp:3.9.0-rc2", "registry.mirantis.com/mirantiseng/ucp", "3.9.0-rc2"},
8585
{"registry.ci.mirantis.com/mirantiseng/ecp", "registry.ci.mirantis.com/mirantiseng/ecp", "latest"},
86+
{"localhost:5000/my-image", "localhost:5000/my-image", "latest"},
87+
{"my-complex-repo:5000/org/image:v1", "my-complex-repo:5000/org/image", "v1"},
8688
}
8789

8890
for _, tc := range cases {

0 commit comments

Comments
 (0)