Skip to content

Commit aeb70b3

Browse files
committed
fix: tag CEL Function return correct tag when tag and digest are present
1 parent b97f888 commit aeb70b3

4 files changed

Lines changed: 29 additions & 7 deletions

File tree

cel/libs/image/impl.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package image
22

33
import (
4+
"strings"
5+
46
"github.com/google/cel-go/common/types"
57
"github.com/google/cel-go/common/types/ref"
68
"github.com/google/go-containerregistry/pkg/name"
@@ -67,6 +69,16 @@ func imageTag(arg ref.Val) ref.Val {
6769
if !ok {
6870
return types.MaybeNoSuchOverloadErr(arg)
6971
}
72+
73+
// Workaround for upstream not detecting the tag correctly when tag and digest are both present.
74+
// See https://github.com/google/go-containerregistry/issues/2069
75+
original := v.String()
76+
if strings.Contains(original, "@") {
77+
parts := strings.Split(original, "@")
78+
tagWithoutDigest := parts[0]
79+
v, _ = name.NewTag(tagWithoutDigest, name.StrictValidation)
80+
}
81+
7082
var tag string
7183
if v, ok := v.(name.Tag); ok {
7284
tag = v.TagStr()

cel/libs/image/lib_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ func TestImage(t *testing.T) {
186186
expr: `parseImageReference("registry.k8s.io/kube-apiserver-arm64:testtag").tag()`,
187187
expectValue: types.String("testtag"),
188188
},
189+
{
190+
name: "tag_with_digest",
191+
expr: `parseImageReference("registry.k8s.io/kube-apiserver-arm64:testtag@sha256:6aefddb645ee6963afd681b1845c661d0ea4c3b20ab9db86d9e753b203d385f2").tag()`,
192+
expectValue: types.String("testtag"),
193+
},
189194
{
190195
name: "default_tag",
191196
expr: `parseImageReference("registry.k8s.io/kube-apiserver-arm64").tag()`,

core/resulters/transformer_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,20 @@ func TestTransformer_ImplementsResulter(t *testing.T) {
7878
P string
7979
I int
8080
O bool
81-
} { return struct {
82-
P string
83-
I int
84-
O bool
85-
}{policy, in, out} }
81+
} {
82+
return struct {
83+
P string
84+
I int
85+
O bool
86+
}{policy, in, out}
87+
}
8688
result := func(s []struct {
8789
P string
8890
I int
8991
O bool
90-
}) int { return len(s) }
92+
}) int {
93+
return len(s)
94+
}
9195
inner := NewAppender[string, int, struct {
9296
P string
9397
I int

extensions/policy/engine_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ func TestNewEngine_Handle_PropagatesEvaluationError(t *testing.T) {
6060
}
6161

6262
// ensureEngineResultType compiles only if the engine has the expected result type.
63-
func ensureEngineResultType(_ core.Engine[map[string]bool, string, defaults.Result[accessPolicy, map[string]bool, string, Evaluation[bool]]]) {}
63+
func ensureEngineResultType(_ core.Engine[map[string]bool, string, defaults.Result[accessPolicy, map[string]bool, string, Evaluation[bool]]]) {
64+
}
6465

6566
func TestNewEngine_ResultTypeMatchesDefaults(t *testing.T) {
6667
src := core.MakeSource(accessPolicy{})

0 commit comments

Comments
 (0)