Skip to content

Commit 3f80863

Browse files
authored
Merge pull request kubernetes#131559 from jpbetz/fix-unstructured-to-val-equality
Fix UnstructuredToVal map equality to respect nil fields
2 parents 07704ee + 66b8a84 commit 3f80863

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

staging/src/k8s.io/apiserver/pkg/cel/common/values_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,16 @@ func TestToValue(t *testing.T) {
318318
"s2": struct2,
319319
},
320320
},
321+
{
322+
name: "compare: identical complex structs",
323+
expression: "c1 == c2",
324+
activation: map[string]typedValue{"c1": complex1, "c2": complex1Again},
325+
},
326+
{
327+
name: "compare: different complex structs",
328+
expression: "c1 != c2",
329+
activation: map[string]typedValue{"c1": complex1, "c2": complex2},
330+
},
321331
{
322332
name: "compare: struct and pointer to identical struct",
323333
expression: "s1 == s1_ptr",

staging/src/k8s.io/apiserver/pkg/cel/common/valuesunstructured.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,13 +598,17 @@ func (t *unstructuredMap) Equal(other ref.Val) ref.Val {
598598
if t.Size() != oMap.Size() {
599599
return types.False
600600
}
601+
601602
for key, value := range t.value {
602-
if propSchema, ok := t.propSchema(key); ok {
603-
ov, found := oMap.Find(types.String(key))
604-
if !found {
603+
if _, ok := t.propSchema(key); ok {
604+
v, found := t.Find(types.String(key))
605+
ov, oFound := oMap.Find(types.String(key))
606+
if found != oFound {
605607
return types.False
606608
}
607-
v := UnstructuredToVal(value, propSchema)
609+
if !found {
610+
continue
611+
}
608612
vEq := v.Equal(ov)
609613
if vEq != types.True {
610614
return vEq // either false or error

0 commit comments

Comments
 (0)