...

Source file src/k8s.io/apiextensions-apiserver/pkg/controller/openapi/builder/merge.go

Documentation: k8s.io/apiextensions-apiserver/pkg/controller/openapi/builder

     1  /*
     2  Copyright 2019 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 builder
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  
    23  	"k8s.io/kube-openapi/pkg/aggregator"
    24  	"k8s.io/kube-openapi/pkg/spec3"
    25  	"k8s.io/kube-openapi/pkg/validation/spec"
    26  )
    27  
    28  const metadataGV = "io.k8s.apimachinery.pkg.apis.meta.v1"
    29  const autoscalingGV = "io.k8s.api.autoscaling.v1"
    30  
    31  // MergeSpecs aggregates all OpenAPI specs, reusing the metadata of the first, static spec as the basis.
    32  // The static spec has the highest priority, and its paths and definitions won't get overlapped by
    33  // user-defined CRDs. None of the input is mutated, but input and output share data structures.
    34  func MergeSpecs(staticSpec *spec.Swagger, crdSpecs ...*spec.Swagger) (*spec.Swagger, error) {
    35  	// create shallow copy of staticSpec, but replace paths and definitions because we modify them.
    36  	specToReturn := *staticSpec
    37  	if staticSpec.Definitions != nil {
    38  		specToReturn.Definitions = make(spec.Definitions, len(staticSpec.Definitions))
    39  		for k, s := range staticSpec.Definitions {
    40  			specToReturn.Definitions[k] = s
    41  		}
    42  	}
    43  	if staticSpec.Parameters != nil {
    44  		specToReturn.Parameters = make(map[string]spec.Parameter, len(staticSpec.Parameters))
    45  		for k, s := range staticSpec.Parameters {
    46  			specToReturn.Parameters[k] = s
    47  		}
    48  	}
    49  	if staticSpec.Paths != nil {
    50  		specToReturn.Paths = &spec.Paths{
    51  			Paths: make(map[string]spec.PathItem, len(staticSpec.Paths.Paths)),
    52  		}
    53  		for k, p := range staticSpec.Paths.Paths {
    54  			specToReturn.Paths.Paths[k] = p
    55  		}
    56  	}
    57  
    58  	crdSpec := &spec.Swagger{}
    59  	for _, s := range crdSpecs {
    60  		// merge specs without checking conflicts, since the naming controller prevents
    61  		// conflicts between user-defined CRDs
    62  		mergeSpec(crdSpec, s)
    63  	}
    64  
    65  	// The static spec has the highest priority. Resolve conflicts to prevent user-defined
    66  	// CRDs potentially overlapping the built-in apiextensions API
    67  	if err := aggregator.MergeSpecsIgnorePathConflictRenamingDefinitionsAndParameters(&specToReturn, crdSpec); err != nil {
    68  		return nil, err
    69  	}
    70  	return &specToReturn, nil
    71  }
    72  
    73  // mergeSpec copies paths, parameters and definitions from source to dest, mutating dest, but not source.
    74  // We assume that conflicts do not matter.
    75  func mergeSpec(dest, source *spec.Swagger) {
    76  	if source == nil || source.Paths == nil {
    77  		return
    78  	}
    79  	if dest.Paths == nil {
    80  		dest.Paths = &spec.Paths{}
    81  	}
    82  	for k, v := range source.Definitions {
    83  		if dest.Definitions == nil {
    84  			dest.Definitions = make(spec.Definitions, len(source.Definitions))
    85  		}
    86  		dest.Definitions[k] = v
    87  	}
    88  	for k, v := range source.Parameters {
    89  		if dest.Parameters == nil {
    90  			dest.Parameters = make(map[string]spec.Parameter, len(source.Parameters))
    91  		}
    92  		dest.Parameters[k] = v
    93  	}
    94  	for k, v := range source.Paths.Paths {
    95  		if dest.Paths.Paths == nil {
    96  			dest.Paths.Paths = make(map[string]spec.PathItem, len(source.Paths.Paths))
    97  		}
    98  		dest.Paths.Paths[k] = v
    99  	}
   100  }
   101  
   102  // MergeSpecsV3 merges OpenAPI v3 specs for CRDs
   103  // Conflicts belonging to the meta.v1 or autoscaling.v1 group versions are skipped as all CRDs reference those types
   104  // Other conflicts will result in an error
   105  func MergeSpecsV3(crdSpecs ...*spec3.OpenAPI) (*spec3.OpenAPI, error) {
   106  	crdSpec := &spec3.OpenAPI{}
   107  	if len(crdSpecs) > 0 {
   108  		crdSpec.Version = crdSpecs[0].Version
   109  		crdSpec.Info = crdSpecs[0].Info
   110  	}
   111  	for _, s := range crdSpecs {
   112  		err := mergeSpecV3(crdSpec, s)
   113  		if err != nil {
   114  			return nil, err
   115  		}
   116  	}
   117  	return crdSpec, nil
   118  }
   119  
   120  // mergeSpecV3 copies paths and definitions from source to dest, mutating dest, but not source.
   121  // Conflicts belonging to the meta.v1 or autoscaling.v1 group versions are skipped as all CRDs reference those types
   122  // Other conflicts will result in an error
   123  func mergeSpecV3(dest, source *spec3.OpenAPI) error {
   124  	if source == nil || source.Paths == nil {
   125  		return nil
   126  	}
   127  	if dest.Paths == nil {
   128  		dest.Paths = &spec3.Paths{}
   129  	}
   130  
   131  	for k, v := range source.Components.Schemas {
   132  		if dest.Components == nil {
   133  			dest.Components = &spec3.Components{}
   134  		}
   135  		if dest.Components.Schemas == nil {
   136  			dest.Components.Schemas = map[string]*spec.Schema{}
   137  		}
   138  		if _, exists := dest.Components.Schemas[k]; exists {
   139  			if strings.HasPrefix(k, metadataGV) || strings.HasPrefix(k, autoscalingGV) {
   140  				continue
   141  			}
   142  			return fmt.Errorf("OpenAPI V3 merge schema conflict on %s", k)
   143  		}
   144  		dest.Components.Schemas[k] = v
   145  	}
   146  	for k, v := range source.Paths.Paths {
   147  		if dest.Paths.Paths == nil {
   148  			dest.Paths.Paths = map[string]*spec3.Path{}
   149  		}
   150  		dest.Paths.Paths[k] = v
   151  	}
   152  	return nil
   153  }
   154  

View as plain text