...

Source file src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/kubeopenapi.go

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

     1  /*
     2  Copyright 2020 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 schema
    18  
    19  import (
    20  	"k8s.io/kube-openapi/pkg/validation/spec"
    21  )
    22  
    23  // ToKubeOpenAPI converts a structural schema to go-openapi schema. It is faithful and roundtrippable.
    24  func (s *Structural) ToKubeOpenAPI() *spec.Schema {
    25  	if s == nil {
    26  		return nil
    27  	}
    28  
    29  	ret := &spec.Schema{}
    30  
    31  	if s.Items != nil {
    32  		ret.Items = &spec.SchemaOrArray{Schema: s.Items.ToKubeOpenAPI()}
    33  	}
    34  	if s.Properties != nil {
    35  		ret.Properties = make(map[string]spec.Schema, len(s.Properties))
    36  		for k, v := range s.Properties {
    37  			ret.Properties[k] = *v.ToKubeOpenAPI()
    38  		}
    39  	}
    40  	s.Generic.toKubeOpenAPI(ret)
    41  	s.Extensions.toKubeOpenAPI(ret)
    42  	s.ValueValidation.toKubeOpenAPI(ret)
    43  
    44  	return ret
    45  }
    46  
    47  func (g *Generic) toKubeOpenAPI(ret *spec.Schema) {
    48  	if g == nil {
    49  		return
    50  	}
    51  
    52  	if len(g.Type) != 0 {
    53  		ret.Type = spec.StringOrArray{g.Type}
    54  	}
    55  	ret.Nullable = g.Nullable
    56  	if g.AdditionalProperties != nil {
    57  		ret.AdditionalProperties = &spec.SchemaOrBool{
    58  			Allows: g.AdditionalProperties.Bool,
    59  			Schema: g.AdditionalProperties.Structural.ToKubeOpenAPI(),
    60  		}
    61  	}
    62  	ret.Description = g.Description
    63  	ret.Title = g.Title
    64  	ret.Default = g.Default.Object
    65  }
    66  
    67  func (x *Extensions) toKubeOpenAPI(ret *spec.Schema) {
    68  	if x == nil {
    69  		return
    70  	}
    71  
    72  	if x.XPreserveUnknownFields {
    73  		ret.VendorExtensible.AddExtension("x-kubernetes-preserve-unknown-fields", true)
    74  	}
    75  	if x.XEmbeddedResource {
    76  		ret.VendorExtensible.AddExtension("x-kubernetes-embedded-resource", true)
    77  	}
    78  	if x.XIntOrString {
    79  		ret.VendorExtensible.AddExtension("x-kubernetes-int-or-string", true)
    80  	}
    81  	if len(x.XListMapKeys) > 0 {
    82  		ret.VendorExtensible.AddExtension("x-kubernetes-list-map-keys", x.XListMapKeys)
    83  	}
    84  	if x.XListType != nil {
    85  		ret.VendorExtensible.AddExtension("x-kubernetes-list-type", *x.XListType)
    86  	}
    87  	if x.XMapType != nil {
    88  		ret.VendorExtensible.AddExtension("x-kubernetes-map-type", *x.XMapType)
    89  	}
    90  	if len(x.XValidations) > 0 {
    91  		ret.VendorExtensible.AddExtension("x-kubernetes-validations", x.XValidations)
    92  	}
    93  }
    94  
    95  func (v *ValueValidation) toKubeOpenAPI(ret *spec.Schema) {
    96  	if v == nil {
    97  		return
    98  	}
    99  
   100  	ret.Format = v.Format
   101  	ret.Maximum = v.Maximum
   102  	ret.ExclusiveMaximum = v.ExclusiveMaximum
   103  	ret.Minimum = v.Minimum
   104  	ret.ExclusiveMinimum = v.ExclusiveMinimum
   105  	ret.MaxLength = v.MaxLength
   106  	ret.MinLength = v.MinLength
   107  	ret.Pattern = v.Pattern
   108  	ret.MaxItems = v.MaxItems
   109  	ret.MinItems = v.MinItems
   110  	ret.UniqueItems = v.UniqueItems
   111  	ret.MultipleOf = v.MultipleOf
   112  	if v.Enum != nil {
   113  		ret.Enum = make([]interface{}, 0, len(v.Enum))
   114  		for i := range v.Enum {
   115  			ret.Enum = append(ret.Enum, v.Enum[i].Object)
   116  		}
   117  	}
   118  	ret.MaxProperties = v.MaxProperties
   119  	ret.MinProperties = v.MinProperties
   120  	ret.Required = v.Required
   121  	for i := range v.AllOf {
   122  		ret.AllOf = append(ret.AllOf, *v.AllOf[i].toKubeOpenAPI())
   123  	}
   124  	for i := range v.AnyOf {
   125  		ret.AnyOf = append(ret.AnyOf, *v.AnyOf[i].toKubeOpenAPI())
   126  	}
   127  	for i := range v.OneOf {
   128  		ret.OneOf = append(ret.OneOf, *v.OneOf[i].toKubeOpenAPI())
   129  	}
   130  	ret.Not = v.Not.toKubeOpenAPI()
   131  }
   132  
   133  func (vv *NestedValueValidation) toKubeOpenAPI() *spec.Schema {
   134  	if vv == nil {
   135  		return nil
   136  	}
   137  
   138  	ret := &spec.Schema{}
   139  
   140  	vv.ValueValidation.toKubeOpenAPI(ret)
   141  	if vv.Items != nil {
   142  		ret.Items = &spec.SchemaOrArray{Schema: vv.Items.toKubeOpenAPI()}
   143  	}
   144  	if vv.Properties != nil {
   145  		ret.Properties = make(map[string]spec.Schema, len(vv.Properties))
   146  		for k, v := range vv.Properties {
   147  			ret.Properties[k] = *v.toKubeOpenAPI()
   148  		}
   149  	}
   150  	vv.ForbiddenGenerics.toKubeOpenAPI(ret)   // normally empty. Exception: int-or-string
   151  	vv.ForbiddenExtensions.toKubeOpenAPI(ret) // shouldn't do anything
   152  
   153  	return ret
   154  }
   155  

View as plain text