Merge pull request #123538 from jiahuif-forks/fix/cel/mutation-library-map-support
CEL mutation library: add support for map Kubernetes-commit: 286cdad32d7967a5f3b84a8924448ea914d44c00
This commit is contained in:
commit
f663919323
4
go.mod
4
go.mod
|
|
@ -42,7 +42,7 @@ require (
|
|||
google.golang.org/protobuf v1.31.0
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1
|
||||
gopkg.in/square/go-jose.v2 v2.6.0
|
||||
k8s.io/api v0.0.0-20240221202343-ffee488e7bd8
|
||||
k8s.io/api v0.0.0-20240228104440-d6ea7e8f9bfc
|
||||
k8s.io/apimachinery v0.0.0-20240221202133-0f2e9357997f
|
||||
k8s.io/client-go v0.0.0-20240221202651-8c4efe8d079e
|
||||
k8s.io/component-base v0.0.0-20240227002902-6c2a49d37aa4
|
||||
|
|
@ -125,7 +125,7 @@ require (
|
|||
)
|
||||
|
||||
replace (
|
||||
k8s.io/api => k8s.io/api v0.0.0-20240221202343-ffee488e7bd8
|
||||
k8s.io/api => k8s.io/api v0.0.0-20240228104440-d6ea7e8f9bfc
|
||||
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20240221202133-0f2e9357997f
|
||||
k8s.io/client-go => k8s.io/client-go v0.0.0-20240221202651-8c4efe8d079e
|
||||
k8s.io/component-base => k8s.io/component-base v0.0.0-20240227002902-6c2a49d37aa4
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -385,8 +385,8 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
|||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
k8s.io/api v0.0.0-20240221202343-ffee488e7bd8 h1:K3vOhiu2OcPzv/gdniojRhysSxAyc5iSFtp9nVbS7m4=
|
||||
k8s.io/api v0.0.0-20240221202343-ffee488e7bd8/go.mod h1:kCYq4mrNBxRaHCZeTveL6//1FveIDm3wxJZeFNKF6b8=
|
||||
k8s.io/api v0.0.0-20240228104440-d6ea7e8f9bfc h1:7IBpQCcIsPKGiK8R2xVKsFYdcqvt5UIiA4ARPHdlUoQ=
|
||||
k8s.io/api v0.0.0-20240228104440-d6ea7e8f9bfc/go.mod h1:kCYq4mrNBxRaHCZeTveL6//1FveIDm3wxJZeFNKF6b8=
|
||||
k8s.io/apimachinery v0.0.0-20240221202133-0f2e9357997f h1:IvhurYeUUiUsNbDhVJHvOwhgfpUoobqtGQGat2VxfcQ=
|
||||
k8s.io/apimachinery v0.0.0-20240221202133-0f2e9357997f/go.mod h1:/862Kkwje5hhHGJWPKiaHuov2c6mw6uCXWikV9kOIP4=
|
||||
k8s.io/client-go v0.0.0-20240221202651-8c4efe8d079e h1:ucy8O9ziYYysZM3YZ+c1e/oltPPmvz+flFBae/0FtG8=
|
||||
|
|
|
|||
|
|
@ -55,7 +55,11 @@ func (v *ObjectVal) ConvertToNative(typeDesc reflect.Type) (any, error) {
|
|||
}
|
||||
result = make(map[string]any, len(v.fields))
|
||||
for k, v := range v.fields {
|
||||
result[k] = convertField(v)
|
||||
converted, err := convertField(v)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fail to convert field %q: %w", k, err)
|
||||
}
|
||||
result[k] = converted
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
|
@ -104,8 +108,12 @@ func (v *ObjectVal) IsZeroValue() bool {
|
|||
// convertField converts a referred ref.Val to its expected type.
|
||||
// For objects, the expected type is map[string]any
|
||||
// For lists, the expected type is []any
|
||||
// For maps, the expected type is map[string]any
|
||||
// For anything else, it is converted via value.Value()
|
||||
func convertField(value ref.Val) any {
|
||||
//
|
||||
// It will return an error if the request type is a map but the key
|
||||
// is not a string.
|
||||
func convertField(value ref.Val) (any, error) {
|
||||
// special handling for lists, where the elements are converted with Value() instead of ConvertToNative
|
||||
// to allow them to become native value of any type.
|
||||
if listOfVal, ok := value.Value().([]ref.Val); ok {
|
||||
|
|
@ -113,7 +121,20 @@ func convertField(value ref.Val) any {
|
|||
for _, v := range listOfVal {
|
||||
result = append(result, v.Value())
|
||||
}
|
||||
return result
|
||||
return result, nil
|
||||
}
|
||||
return value.Value()
|
||||
// unstructured maps, as seen in annotations
|
||||
// map keys must be strings
|
||||
if mapOfVal, ok := value.Value().(map[ref.Val]ref.Val); ok {
|
||||
result := make(map[string]any)
|
||||
for k, v := range mapOfVal {
|
||||
stringKey, ok := k.Value().(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("map key %q is of type %t, not string", k, k)
|
||||
}
|
||||
result[stringKey] = v.Value()
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
return value.Value(), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -94,6 +94,17 @@ func TestTypeProvider(t *testing.T) {
|
|||
"intList": []any{int64(1), int64(2), int64(3)},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "map string-to-string",
|
||||
expression: `Object{
|
||||
annotations: {"foo": "bar"}
|
||||
}`,
|
||||
expectedValue: map[string]any{
|
||||
"annotations": map[string]any{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "field access",
|
||||
expression: `Object{
|
||||
|
|
|
|||
Loading…
Reference in New Issue