Skip to content

Commit

Permalink
Fix appending SHA256 hash to image reference in ImageWithDigest (#1784)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvinkler committed Jun 6, 2023
1 parent 75a8b89 commit 109e6b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/functions/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,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)

Expand Down
10 changes: 10 additions & 0 deletions pkg/functions/function_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 109e6b6

Please sign in to comment.