k8s.io/apiserver@v0.31.1/pkg/cel/common/adaptor.go (about) 1 /* 2 Copyright 2023 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 common 18 19 // Schema is the adapted type for an OpenAPI schema that CEL uses. 20 // This schema does not cover all OpenAPI fields but only these CEL requires 21 // are exposed as getters. 22 type Schema interface { 23 // Type returns the OpenAPI type. 24 // Multiple types are not supported. It should return 25 // empty string if no type is specified. 26 Type() string 27 28 // Format returns the OpenAPI format. May be empty 29 Format() string 30 31 // Items returns the OpenAPI items. or nil of this field does not exist or 32 // contains no schema. 33 Items() Schema 34 35 // Properties returns the OpenAPI properties, or nil if this field does not 36 // exist. 37 // The values of the returned map are of the adapted type. 38 Properties() map[string]Schema 39 40 // AdditionalProperties returns the OpenAPI additional properties field, 41 // or nil if this field does not exist. 42 AdditionalProperties() SchemaOrBool 43 44 // Default returns the OpenAPI default field, or nil if this field does not exist. 45 Default() any 46 47 Validations 48 KubeExtensions 49 50 // WithTypeAndObjectMeta returns a schema that has the type and object meta set. 51 // the type includes "kind", "apiVersion" field 52 // the "metadata" field requires "name" and "generateName" to be set 53 // The original schema must not be mutated. Make a copy if necessary. 54 WithTypeAndObjectMeta() Schema 55 } 56 57 // Validations contains OpenAPI validation that the CEL library uses. 58 type Validations interface { 59 Pattern() string 60 Minimum() *float64 61 IsExclusiveMinimum() bool 62 Maximum() *float64 63 IsExclusiveMaximum() bool 64 MultipleOf() *float64 65 MinItems() *int64 66 MaxItems() *int64 67 MinLength() *int64 68 MaxLength() *int64 69 MinProperties() *int64 70 MaxProperties() *int64 71 Required() []string 72 Enum() []any 73 Nullable() bool 74 UniqueItems() bool 75 76 AllOf() []Schema 77 OneOf() []Schema 78 AnyOf() []Schema 79 Not() Schema 80 } 81 82 // KubeExtensions contains Kubernetes-specific extensions to the OpenAPI schema. 83 type KubeExtensions interface { 84 IsXIntOrString() bool 85 IsXEmbeddedResource() bool 86 IsXPreserveUnknownFields() bool 87 XListType() string 88 XListMapKeys() []string 89 XMapType() string 90 XValidations() []ValidationRule 91 } 92 93 // ValidationRule represents a single x-kubernetes-validations rule. 94 type ValidationRule interface { 95 Rule() string 96 Message() string 97 MessageExpression() string 98 FieldPath() string 99 } 100 101 // SchemaOrBool contains either a schema or a boolean indicating if the object 102 // can contain any fields. 103 type SchemaOrBool interface { 104 Schema() Schema 105 Allows() bool 106 }