k8s.io/apiserver@v0.31.1/pkg/cel/openapi/extensions.go (about)

     1  /*
     2  Copyright 2022 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  	"k8s.io/apimachinery/pkg/util/intstr"
    21  	"k8s.io/apiserver/pkg/cel/common"
    22  	"k8s.io/kube-openapi/pkg/validation/spec"
    23  )
    24  
    25  var intOrStringFormat = intstr.IntOrString{}.OpenAPISchemaFormat()
    26  
    27  func isExtension(schema *spec.Schema, key string) bool {
    28  	v, ok := schema.Extensions.GetBool(key)
    29  	return v && ok
    30  }
    31  
    32  func isXIntOrString(schema *spec.Schema) bool {
    33  	// built-in types have the Format while CRDs use extension
    34  	// both are valid, checking both
    35  	return schema.Format == intOrStringFormat || isExtension(schema, extIntOrString)
    36  }
    37  
    38  func isXEmbeddedResource(schema *spec.Schema) bool {
    39  	return isExtension(schema, extEmbeddedResource)
    40  }
    41  
    42  func isXPreserveUnknownFields(schema *spec.Schema) bool {
    43  	return isExtension(schema, extPreserveUnknownFields)
    44  }
    45  
    46  func getXListType(schema *spec.Schema) string {
    47  	s, _ := schema.Extensions.GetString(extListType)
    48  	return s
    49  }
    50  
    51  func getXMapType(schema *spec.Schema) string {
    52  	s, _ := schema.Extensions.GetString(extMapType)
    53  	return s
    54  }
    55  
    56  func getXListMapKeys(schema *spec.Schema) []string {
    57  	mapKeys, ok := schema.Extensions.GetStringSlice(extListMapKeys)
    58  	if !ok {
    59  		return nil
    60  	}
    61  	return mapKeys
    62  }
    63  
    64  type ValidationRule struct {
    65  	RuleField              string `json:"rule"`
    66  	MessageField           string `json:"message"`
    67  	MessageExpressionField string `json:"messageExpression"`
    68  	PathField              string `json:"fieldPath"`
    69  }
    70  
    71  func (v ValidationRule) Rule() string {
    72  	return v.RuleField
    73  }
    74  
    75  func (v ValidationRule) Message() string {
    76  	return v.MessageField
    77  }
    78  
    79  func (v ValidationRule) FieldPath() string {
    80  	return v.PathField
    81  }
    82  
    83  func (v ValidationRule) MessageExpression() string {
    84  	return v.MessageExpressionField
    85  }
    86  
    87  // TODO: simplify
    88  func getXValidations(schema *spec.Schema) []common.ValidationRule {
    89  	var rules []ValidationRule
    90  	err := schema.Extensions.GetObject(extValidations, &rules)
    91  	if err != nil {
    92  		return nil
    93  	}
    94  	results := make([]common.ValidationRule, len(rules))
    95  	for i, rule := range rules {
    96  		results[i] = rule
    97  	}
    98  	return results
    99  }
   100  
   101  const extIntOrString = "x-kubernetes-int-or-string"
   102  const extEmbeddedResource = "x-kubernetes-embedded-resource"
   103  const extPreserveUnknownFields = "x-kubernetes-preserve-unknown-fields"
   104  const extListType = "x-kubernetes-list-type"
   105  const extMapType = "x-kubernetes-map-type"
   106  const extListMapKeys = "x-kubernetes-list-map-keys"
   107  const extValidations = "x-kubernetes-validations"