Debian Codegraph @

pkg/cel/mutation/typeresolver.go

raw
1 /*
2 Copyright 2024 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package mutation
18
19 import (
20 "strings"
21
22 "k8s.io/apiserver/pkg/cel/common"
23 "k8s.io/apiserver/pkg/cel/mutation/dynamic"
24 )
25
26 // ObjectTypeName is the name of Object types that are used to declare the types of
27 // Kubernetes objects in CEL dynamically using the naming scheme "Object.<fieldName>...<fieldName>".
28 // For example "Object.spec.containers" is the type of the spec.containers field of the object in scope.
29 const ObjectTypeName = "Object"
30
31 // JSONPatchTypeName is the name of the JSONPatch type. This type is typically used to create JSON patches
32 // in CEL expressions.
33 const JSONPatchTypeName = "JSONPatch"
34
35 // DynamicTypeResolver resolves the Object and JSONPatch types when compiling
36 // CEL expressions without schema information about the object.
37 type DynamicTypeResolver struct{}
38
39 func (r *DynamicTypeResolver) Resolve(name string) (common.ResolvedType, bool) {
40 if name == JSONPatchTypeName {
41 return &JSONPatchType{}, true
42 }
43 if name == ObjectTypeName || strings.HasPrefix(name, ObjectTypeName+".") {
44 return dynamic.NewObjectType(name), true
45 }
46 return nil, false
47 }