From 5b2904834b361d6f1dabc124fb3e05d693cafcc9 Mon Sep 17 00:00:00 2001 From: Knative Prow Robot Date: Thu, 6 Jul 2023 01:40:39 +0100 Subject: [PATCH] Fix appending SHA256 hash to image reference in ImageWithDigest (#1846) Co-authored-by: Michal Vinkler --- pkg/functions/function.go | 6 ++++++ pkg/functions/function_unit_test.go | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/pkg/functions/function.go b/pkg/functions/function.go index 7a2fab6fc3..4a54a62bee 100644 --- a/pkg/functions/function.go +++ b/pkg/functions/function.go @@ -416,6 +416,12 @@ func (f Function) ImageWithDigest() string { return f.Image } + // Return image with new Digest if image already contains SHA256 Digest + shaIndex := strings.Index(f.Image, "@sha256:") + if shaIndex > 0 { + return f.Image[:shaIndex] + "@" + f.ImageDigest + } + lastSlashIdx := strings.LastIndexAny(f.Image, "/") imageAsBytes := []byte(f.Image) diff --git a/pkg/functions/function_unit_test.go b/pkg/functions/function_unit_test.go index 08eeeff652..b0fc6fd18e 100644 --- a/pkg/functions/function_unit_test.go +++ b/pkg/functions/function_unit_test.go @@ -105,6 +105,16 @@ func TestFunction_ImageWithDigest(t *testing.T) { fields: fields{Image: "bar:latest", ImageDigest: "42"}, want: "bar@42", }, + { + name: "Full path with port and SHA256 Digest", + fields: fields{Image: "image-registry.openshift-image-registry.svc.cluster.local:50000/default/bar@sha256:42", ImageDigest: "sha256:42"}, + want: "image-registry.openshift-image-registry.svc.cluster.local:50000/default/bar@sha256:42", + }, + { + name: "Full path with port and SHA256 Digest with empty ImageDigest", + fields: fields{Image: "image-registry.openshift-image-registry.svc.cluster.local:50000/default/bar@sha256:42", ImageDigest: ""}, + want: "image-registry.openshift-image-registry.svc.cluster.local:50000/default/bar@sha256:42", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {