k8s.io/apiserver@v0.31.1/pkg/cel/openapi/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 openapi 18 19 import ( 20 "github.com/google/cel-go/common/types/ref" 21 22 apiservercel "k8s.io/apiserver/pkg/cel" 23 "k8s.io/apiserver/pkg/cel/common" 24 "k8s.io/kube-openapi/pkg/validation/spec" 25 ) 26 27 var _ common.Schema = (*Schema)(nil) 28 var _ common.SchemaOrBool = (*SchemaOrBool)(nil) 29 30 type Schema struct { 31 Schema *spec.Schema 32 } 33 34 type SchemaOrBool struct { 35 SchemaOrBool *spec.SchemaOrBool 36 } 37 38 func (sb *SchemaOrBool) Schema() common.Schema { 39 return &Schema{Schema: sb.SchemaOrBool.Schema} 40 } 41 42 func (sb *SchemaOrBool) Allows() bool { 43 return sb.SchemaOrBool.Allows 44 } 45 46 func (s *Schema) Type() string { 47 if len(s.Schema.Type) == 0 { 48 return "" 49 } 50 return s.Schema.Type[0] 51 } 52 53 func (s *Schema) Format() string { 54 return s.Schema.Format 55 } 56 57 func (s *Schema) Pattern() string { 58 return s.Schema.Pattern 59 } 60 61 func (s *Schema) Items() common.Schema { 62 if s.Schema.Items == nil || s.Schema.Items.Schema == nil { 63 return nil 64 } 65 return &Schema{Schema: s.Schema.Items.Schema} 66 } 67 68 func (s *Schema) Properties() map[string]common.Schema { 69 if s.Schema.Properties == nil { 70 return nil 71 } 72 res := make(map[string]common.Schema, len(s.Schema.Properties)) 73 for n, prop := range s.Schema.Properties { 74 // map value is unaddressable, create a shallow copy 75 // this is a shallow non-recursive copy 76 s := prop 77 res[n] = &Schema{Schema: &s} 78 } 79 return res 80 } 81 82 func (s *Schema) AdditionalProperties() common.SchemaOrBool { 83 if s.Schema.AdditionalProperties == nil { 84 return nil 85 } 86 return &SchemaOrBool{SchemaOrBool: s.Schema.AdditionalProperties} 87 } 88 89 func (s *Schema) Default() any { 90 return s.Schema.Default 91 } 92 93 func (s *Schema) Minimum() *float64 { 94 return s.Schema.Minimum 95 } 96 97 func (s *Schema) IsExclusiveMinimum() bool { 98 return s.Schema.ExclusiveMinimum 99 } 100 101 func (s *Schema) Maximum() *float64 { 102 return s.Schema.Maximum 103 } 104 105 func (s *Schema) IsExclusiveMaximum() bool { 106 return s.Schema.ExclusiveMaximum 107 } 108 109 func (s *Schema) MultipleOf() *float64 { 110 return s.Schema.MultipleOf 111 } 112 113 func (s *Schema) UniqueItems() bool { 114 return s.Schema.UniqueItems 115 } 116 117 func (s *Schema) MinItems() *int64 { 118 return s.Schema.MinItems 119 } 120 121 func (s *Schema) MaxItems() *int64 { 122 return s.Schema.MaxItems 123 } 124 125 func (s *Schema) MinLength() *int64 { 126 return s.Schema.MinLength 127 } 128 129 func (s *Schema) MaxLength() *int64 { 130 return s.Schema.MaxLength 131 } 132 133 func (s *Schema) MinProperties() *int64 { 134 return s.Schema.MinProperties 135 } 136 137 func (s *Schema) MaxProperties() *int64 { 138 return s.Schema.MaxProperties 139 } 140 141 func (s *Schema) Required() []string { 142 return s.Schema.Required 143 } 144 145 func (s *Schema) Enum() []any { 146 return s.Schema.Enum 147 } 148 149 func (s *Schema) Nullable() bool { 150 return s.Schema.Nullable 151 } 152 153 func (s *Schema) AllOf() []common.Schema { 154 var res []common.Schema 155 for _, nestedSchema := range s.Schema.AllOf { 156 nestedSchema := nestedSchema 157 res = append(res, &Schema{&nestedSchema}) 158 } 159 return res 160 } 161 162 func (s *Schema) AnyOf() []common.Schema { 163 var res []common.Schema 164 for _, nestedSchema := range s.Schema.AnyOf { 165 nestedSchema := nestedSchema 166 res = append(res, &Schema{&nestedSchema}) 167 } 168 return res 169 } 170 171 func (s *Schema) OneOf() []common.Schema { 172 var res []common.Schema 173 for _, nestedSchema := range s.Schema.OneOf { 174 nestedSchema := nestedSchema 175 res = append(res, &Schema{&nestedSchema}) 176 } 177 return res 178 } 179 180 func (s *Schema) Not() common.Schema { 181 if s.Schema.Not == nil { 182 return nil 183 } 184 return &Schema{s.Schema.Not} 185 } 186 187 func (s *Schema) IsXIntOrString() bool { 188 return isXIntOrString(s.Schema) 189 } 190 191 func (s *Schema) IsXEmbeddedResource() bool { 192 return isXEmbeddedResource(s.Schema) 193 } 194 195 func (s *Schema) IsXPreserveUnknownFields() bool { 196 return isXPreserveUnknownFields(s.Schema) 197 } 198 199 func (s *Schema) XListType() string { 200 return getXListType(s.Schema) 201 } 202 203 func (s *Schema) XMapType() string { 204 return getXMapType(s.Schema) 205 } 206 207 func (s *Schema) XListMapKeys() []string { 208 return getXListMapKeys(s.Schema) 209 } 210 211 func (s *Schema) XValidations() []common.ValidationRule { 212 return getXValidations(s.Schema) 213 } 214 215 func (s *Schema) WithTypeAndObjectMeta() common.Schema { 216 return &Schema{common.WithTypeAndObjectMeta(s.Schema)} 217 } 218 219 func UnstructuredToVal(unstructured any, schema *spec.Schema) ref.Val { 220 return common.UnstructuredToVal(unstructured, &Schema{schema}) 221 } 222 223 func SchemaDeclType(s *spec.Schema, isResourceRoot bool) *apiservercel.DeclType { 224 return common.SchemaDeclType(&Schema{Schema: s}, isResourceRoot) 225 } 226 227 func MakeMapList(sts *spec.Schema, items []interface{}) (rv common.MapList) { 228 return common.MakeMapList(&Schema{Schema: sts}, items) 229 }