k8s.io/apiserver@v0.31.1/pkg/util/openapi/enablement.go (about)

     1  /*
     2  Copyright 2021 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  	"strings"
    21  
    22  	genericfeatures "k8s.io/apiserver/pkg/features"
    23  	utilfeature "k8s.io/apiserver/pkg/util/feature"
    24  	"k8s.io/kube-openapi/pkg/common"
    25  	"k8s.io/kube-openapi/pkg/schemamutation"
    26  	"k8s.io/kube-openapi/pkg/validation/spec"
    27  )
    28  
    29  // enumTypeDescriptionHeader is the header of enum section in schema description.
    30  const enumTypeDescriptionHeader = "Possible enum values:"
    31  
    32  // GetOpenAPIDefinitionsWithoutDisabledFeatures wraps a GetOpenAPIDefinitions to revert
    33  // any change to the schema that was made by disabled features.
    34  func GetOpenAPIDefinitionsWithoutDisabledFeatures(GetOpenAPIDefinitions common.GetOpenAPIDefinitions) common.GetOpenAPIDefinitions {
    35  	return func(ref common.ReferenceCallback) map[string]common.OpenAPIDefinition {
    36  		defs := GetOpenAPIDefinitions(ref)
    37  		restoreDefinitions(defs)
    38  		return defs
    39  	}
    40  }
    41  
    42  // restoreDefinitions restores any changes by disabled features from definition map.
    43  func restoreDefinitions(defs map[string]common.OpenAPIDefinition) {
    44  	// revert changes from OpenAPIEnums
    45  	if !utilfeature.DefaultFeatureGate.Enabled(genericfeatures.OpenAPIEnums) {
    46  		for gvk, def := range defs {
    47  			orig := &def.Schema
    48  			if ret := pruneEnums(orig); ret != orig {
    49  				def.Schema = *ret
    50  				defs[gvk] = def
    51  			}
    52  		}
    53  	}
    54  }
    55  
    56  func pruneEnums(schema *spec.Schema) *spec.Schema {
    57  	walker := schemamutation.Walker{
    58  		SchemaCallback: func(schema *spec.Schema) *spec.Schema {
    59  			orig := schema
    60  			clone := func() {
    61  				if orig == schema { // if schema has not been mutated yet
    62  					schema = new(spec.Schema)
    63  					*schema = *orig // make a clone from orig to schema
    64  				}
    65  			}
    66  			if headerIndex := strings.Index(schema.Description, enumTypeDescriptionHeader); headerIndex != -1 {
    67  				// remove the enum section from description.
    68  				// note that the new lines before the header should be removed too,
    69  				// thus the slice range.
    70  				clone()
    71  				schema.Description = strings.TrimSpace(schema.Description[:headerIndex])
    72  			}
    73  			if len(schema.Enum) != 0 {
    74  				// remove the enum field
    75  				clone()
    76  				schema.Enum = nil
    77  			}
    78  			return schema
    79  		},
    80  		RefCallback: schemamutation.RefCallbackNoop,
    81  	}
    82  	return walker.WalkSchema(schema)
    83  }