...

Source file src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel/model/adaptor.go

Documentation: k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel/model

     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 model
    18  
    19  import (
    20  	"k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
    21  	"k8s.io/apiserver/pkg/cel/common"
    22  )
    23  
    24  var _ common.Schema = (*Structural)(nil)
    25  var _ common.SchemaOrBool = (*StructuralOrBool)(nil)
    26  
    27  type Structural struct {
    28  	Structural *schema.Structural
    29  }
    30  
    31  type StructuralOrBool struct {
    32  	StructuralOrBool *schema.StructuralOrBool
    33  }
    34  
    35  func (sb *StructuralOrBool) Schema() common.Schema {
    36  	if sb.StructuralOrBool.Structural == nil {
    37  		return nil
    38  	}
    39  	return &Structural{Structural: sb.StructuralOrBool.Structural}
    40  }
    41  
    42  func (sb *StructuralOrBool) Allows() bool {
    43  	return sb.StructuralOrBool.Bool
    44  }
    45  
    46  func (s *Structural) Type() string {
    47  	return s.Structural.Type
    48  }
    49  
    50  func (s *Structural) Format() string {
    51  	if s.Structural.ValueValidation == nil {
    52  		return ""
    53  	}
    54  	return s.Structural.ValueValidation.Format
    55  }
    56  
    57  func (s *Structural) Pattern() string {
    58  	if s.Structural.ValueValidation == nil {
    59  		return ""
    60  	}
    61  	return s.Structural.ValueValidation.Pattern
    62  }
    63  
    64  func (s *Structural) Items() common.Schema {
    65  	return &Structural{Structural: s.Structural.Items}
    66  }
    67  
    68  func (s *Structural) Properties() map[string]common.Schema {
    69  	if s.Structural.Properties == nil {
    70  		return nil
    71  	}
    72  	res := make(map[string]common.Schema, len(s.Structural.Properties))
    73  	for n, prop := range s.Structural.Properties {
    74  		s := prop
    75  		res[n] = &Structural{Structural: &s}
    76  	}
    77  	return res
    78  }
    79  
    80  func (s *Structural) AdditionalProperties() common.SchemaOrBool {
    81  	if s.Structural.AdditionalProperties == nil {
    82  		return nil
    83  	}
    84  	return &StructuralOrBool{StructuralOrBool: s.Structural.AdditionalProperties}
    85  }
    86  
    87  func (s *Structural) Default() any {
    88  	return s.Structural.Default.Object
    89  }
    90  
    91  func (s *Structural) Minimum() *float64 {
    92  	if s.Structural.ValueValidation == nil {
    93  		return nil
    94  	}
    95  	return s.Structural.ValueValidation.Minimum
    96  }
    97  
    98  func (s *Structural) IsExclusiveMinimum() bool {
    99  	if s.Structural.ValueValidation == nil {
   100  		return false
   101  	}
   102  	return s.Structural.ValueValidation.ExclusiveMinimum
   103  }
   104  
   105  func (s *Structural) Maximum() *float64 {
   106  	if s.Structural.ValueValidation == nil {
   107  		return nil
   108  	}
   109  	return s.Structural.ValueValidation.Maximum
   110  }
   111  
   112  func (s *Structural) IsExclusiveMaximum() bool {
   113  	if s.Structural.ValueValidation == nil {
   114  		return false
   115  	}
   116  	return s.Structural.ValueValidation.ExclusiveMaximum
   117  }
   118  
   119  func (s *Structural) MultipleOf() *float64 {
   120  	if s.Structural.ValueValidation == nil {
   121  		return nil
   122  	}
   123  	return s.Structural.ValueValidation.MultipleOf
   124  }
   125  
   126  func (s *Structural) MinItems() *int64 {
   127  	if s.Structural.ValueValidation == nil {
   128  		return nil
   129  	}
   130  	return s.Structural.ValueValidation.MinItems
   131  }
   132  
   133  func (s *Structural) MaxItems() *int64 {
   134  	if s.Structural.ValueValidation == nil {
   135  		return nil
   136  	}
   137  	return s.Structural.ValueValidation.MaxItems
   138  }
   139  
   140  func (s *Structural) MinLength() *int64 {
   141  	if s.Structural.ValueValidation == nil {
   142  		return nil
   143  	}
   144  	return s.Structural.ValueValidation.MinLength
   145  }
   146  
   147  func (s *Structural) MaxLength() *int64 {
   148  	if s.Structural.ValueValidation == nil {
   149  		return nil
   150  	}
   151  	return s.Structural.ValueValidation.MaxLength
   152  }
   153  
   154  func (s *Structural) MinProperties() *int64 {
   155  	if s.Structural.ValueValidation == nil {
   156  		return nil
   157  	}
   158  	return s.Structural.ValueValidation.MinProperties
   159  }
   160  
   161  func (s *Structural) MaxProperties() *int64 {
   162  	if s.Structural.ValueValidation == nil {
   163  		return nil
   164  	}
   165  	return s.Structural.ValueValidation.MaxProperties
   166  }
   167  
   168  func (s *Structural) Required() []string {
   169  	if s.Structural.ValueValidation == nil {
   170  		return nil
   171  	}
   172  	return s.Structural.ValueValidation.Required
   173  }
   174  
   175  func (s *Structural) UniqueItems() bool {
   176  	// This field is forbidden in structural schema.
   177  	// but you can just you x-kubernetes-list-type:set to get around it :)
   178  	return false
   179  }
   180  
   181  func (s *Structural) Enum() []any {
   182  	if s.Structural.ValueValidation == nil {
   183  		return nil
   184  	}
   185  	ret := make([]any, 0, len(s.Structural.ValueValidation.Enum))
   186  	for _, e := range s.Structural.ValueValidation.Enum {
   187  		ret = append(ret, e.Object)
   188  	}
   189  	return ret
   190  }
   191  
   192  func (s *Structural) Nullable() bool {
   193  	return s.Structural.Nullable
   194  }
   195  
   196  func (s *Structural) IsXIntOrString() bool {
   197  	return s.Structural.XIntOrString
   198  }
   199  
   200  func (s *Structural) IsXEmbeddedResource() bool {
   201  	return s.Structural.XEmbeddedResource
   202  }
   203  
   204  func (s *Structural) IsXPreserveUnknownFields() bool {
   205  	return s.Structural.XPreserveUnknownFields
   206  }
   207  
   208  func (s *Structural) XListType() string {
   209  	if s.Structural.XListType == nil {
   210  		return ""
   211  	}
   212  	return *s.Structural.XListType
   213  }
   214  
   215  func (s *Structural) XMapType() string {
   216  	if s.Structural.XMapType == nil {
   217  		return ""
   218  	}
   219  	return *s.Structural.XMapType
   220  }
   221  
   222  func (s *Structural) XListMapKeys() []string {
   223  	return s.Structural.XListMapKeys
   224  }
   225  
   226  func (s *Structural) AllOf() []common.Schema {
   227  	var res []common.Schema
   228  	for _, subSchema := range s.Structural.ValueValidation.AllOf {
   229  		subSchema := subSchema
   230  		res = append(res, nestedValueValidationToStructural(&subSchema))
   231  	}
   232  	return res
   233  }
   234  
   235  func (s *Structural) AnyOf() []common.Schema {
   236  	var res []common.Schema
   237  	for _, subSchema := range s.Structural.ValueValidation.AnyOf {
   238  		subSchema := subSchema
   239  		res = append(res, nestedValueValidationToStructural(&subSchema))
   240  	}
   241  	return res
   242  }
   243  
   244  func (s *Structural) OneOf() []common.Schema {
   245  	var res []common.Schema
   246  	for _, subSchema := range s.Structural.ValueValidation.OneOf {
   247  		subSchema := subSchema
   248  		res = append(res, nestedValueValidationToStructural(&subSchema))
   249  	}
   250  	return res
   251  }
   252  
   253  func (s *Structural) Not() common.Schema {
   254  	if s.Structural.ValueValidation.Not == nil {
   255  		return nil
   256  	}
   257  	return nestedValueValidationToStructural(s.Structural.ValueValidation.Not)
   258  }
   259  
   260  // nestedValueValidationToStructural converts a nested value validation to
   261  // an equivalent structural schema instance.
   262  //
   263  // This lets us avoid needing a separate adaptor for the nested value
   264  // validations, and doesn't cost too much since since we are usually exploring the
   265  // entire schema anyway.
   266  func nestedValueValidationToStructural(nvv *schema.NestedValueValidation) *Structural {
   267  	var newItems *schema.Structural
   268  	if nvv.Items != nil {
   269  		newItems = nestedValueValidationToStructural(nvv.Items).Structural
   270  	}
   271  
   272  	var newProperties map[string]schema.Structural
   273  	for k, v := range nvv.Properties {
   274  		if newProperties == nil {
   275  			newProperties = make(map[string]schema.Structural)
   276  		}
   277  
   278  		v := v
   279  		newProperties[k] = *nestedValueValidationToStructural(&v).Structural
   280  	}
   281  
   282  	return &Structural{
   283  		Structural: &schema.Structural{
   284  			Items:           newItems,
   285  			Properties:      newProperties,
   286  			ValueValidation: &nvv.ValueValidation,
   287  		},
   288  	}
   289  }
   290  
   291  type StructuralValidationRule struct {
   292  	rule, message, messageExpression, fieldPath string
   293  }
   294  
   295  func (s *StructuralValidationRule) Rule() string {
   296  	return s.rule
   297  }
   298  func (s *StructuralValidationRule) Message() string {
   299  	return s.message
   300  }
   301  func (s *StructuralValidationRule) FieldPath() string {
   302  	return s.fieldPath
   303  }
   304  func (s *StructuralValidationRule) MessageExpression() string {
   305  	return s.messageExpression
   306  }
   307  
   308  func (s *Structural) XValidations() []common.ValidationRule {
   309  	if len(s.Structural.XValidations) == 0 {
   310  		return nil
   311  	}
   312  	result := make([]common.ValidationRule, len(s.Structural.XValidations))
   313  	for i, v := range s.Structural.XValidations {
   314  		result[i] = &StructuralValidationRule{rule: v.Rule, message: v.Message, messageExpression: v.MessageExpression, fieldPath: v.FieldPath}
   315  	}
   316  	return result
   317  }
   318  
   319  func (s *Structural) WithTypeAndObjectMeta() common.Schema {
   320  	return &Structural{Structural: WithTypeAndObjectMeta(s.Structural)}
   321  }
   322  

View as plain text