From df9620c9f6f6a60f7cbcacb3ad9fa40d79d1d73e Mon Sep 17 00:00:00 2001 From: Jiahui Feng Date: Tue, 23 Jan 2024 16:07:39 -0800 Subject: [PATCH] add support for equality check. --- staging/src/k8s.io/apiserver/pkg/cel/mutation/common/val.go | 5 ++++- .../pkg/cel/mutation/unstructured/typeresolver_test.go | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/staging/src/k8s.io/apiserver/pkg/cel/mutation/common/val.go b/staging/src/k8s.io/apiserver/pkg/cel/mutation/common/val.go index 0055623dda8..16cca9dd261 100644 --- a/staging/src/k8s.io/apiserver/pkg/cel/mutation/common/val.go +++ b/staging/src/k8s.io/apiserver/pkg/cel/mutation/common/val.go @@ -77,7 +77,10 @@ func (v *ObjectVal) ConvertToType(typeValue ref.Type) ref.Val { // Equal returns true if the `other` value has the same type and content as the implementing struct. func (v *ObjectVal) Equal(other ref.Val) ref.Val { - return types.Bool(v == other) + if rhs, ok := other.(*ObjectVal); ok { + return types.Bool(reflect.DeepEqual(v.fields, rhs.fields)) + } + return types.Bool(false) } // Type returns the TypeValue of the value. diff --git a/staging/src/k8s.io/apiserver/pkg/cel/mutation/unstructured/typeresolver_test.go b/staging/src/k8s.io/apiserver/pkg/cel/mutation/unstructured/typeresolver_test.go index d249d53d3f2..b46fc273eaf 100644 --- a/staging/src/k8s.io/apiserver/pkg/cel/mutation/unstructured/typeresolver_test.go +++ b/staging/src/k8s.io/apiserver/pkg/cel/mutation/unstructured/typeresolver_test.go @@ -101,6 +101,11 @@ func TestTypeProvider(t *testing.T) { }.intList.sum()`, expectedValue: int64(6), }, + { + name: "equality check", + expression: "Object{spec: Object.spec{replicas: 3}} == Object{spec: Object.spec{replicas: 1 + 2}}", + expectedValue: true, + }, } { t.Run(tc.name, func(t *testing.T) { _, option := mutation.NewTypeProviderAndEnvOption(&TypeResolver{})