From 5a541567decdd6f5a9a662ddc3a1b4515d40a753 Mon Sep 17 00:00:00 2001 From: Mritunjay Kumar Sharma Date: Tue, 15 Feb 2022 14:05:53 +0530 Subject: [PATCH] Fix image parsing for image referenced as digests (#3196) * fixes image break with sha256 Signed-off-by: Mritunjay Sharma * fixes priority to digest Signed-off-by: Mritunjay Sharma Co-authored-by: shuting --- pkg/engine/context/imageutils.go | 6 +++--- pkg/engine/context/imageutils_test.go | 17 ++++++++--------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/pkg/engine/context/imageutils.go b/pkg/engine/context/imageutils.go index 16b06ba85d..a74dd674e9 100644 --- a/pkg/engine/context/imageutils.go +++ b/pkg/engine/context/imageutils.go @@ -35,10 +35,10 @@ type ImageInfo struct { func (i *ImageInfo) String() string { image := i.Registry + "/" + i.Path + ":" + i.Tag + // image that needs only digest and not the tag if i.Digest != "" { - image = image + "@" + i.Digest + image = i.Registry + "/" + i.Path + "@" + i.Digest } - return image } @@ -187,7 +187,7 @@ func newImageInfo(image, jsonPointer string) (*ImageInfo, error) { } // set default tag - the domain is set via addDefaultDomain before parsing - if tag == "" { + if digest == "" && tag == "" { tag = "latest" } diff --git a/pkg/engine/context/imageutils_test.go b/pkg/engine/context/imageutils_test.go index 20bd495c3a..83030c8109 100644 --- a/pkg/engine/context/imageutils_test.go +++ b/pkg/engine/context/imageutils_test.go @@ -59,15 +59,6 @@ func Test_extractImageInfo(t *testing.T) { } func Test_ImageInfo_String(t *testing.T) { - validateImageInfo(t, - "registry.test.io/test/myapp:v1.2-21.g5523e95@sha256:31aaf12480bd08c54e7990c6b0e43d775a7a84603d2921a6de4abbc317b2fd10", - "myapp", - "test/myapp", - "registry.test.io", - "v1.2-21.g5523e95", - "sha256:31aaf12480bd08c54e7990c6b0e43d775a7a84603d2921a6de4abbc317b2fd10", - "registry.test.io/test/myapp:v1.2-21.g5523e95@sha256:31aaf12480bd08c54e7990c6b0e43d775a7a84603d2921a6de4abbc317b2fd10") - validateImageInfo(t, "nginx", "nginx", @@ -112,6 +103,14 @@ func Test_ImageInfo_String(t *testing.T) { "latest", "", "localhost:4443/test/nginx:latest") + validateImageInfo(t, + "docker.io/test/centos@sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f", + "centos", + "test/centos", + "docker.io", + "", + "sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f", + "docker.io/test/centos@sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f") } func validateImageInfo(t *testing.T, raw, name, path, registry, tag, digest, str string) {