Remove warning log for merging meta and scale type

This commit is contained in:
Jeffrey Ying 2022-05-07 00:53:08 -04:00 committed by Jefftree
parent 9d0bb09fae
commit b1e9162182
2 changed files with 191 additions and 11 deletions

View File

@ -17,12 +17,17 @@ limitations under the License.
package builder
import (
"k8s.io/klog/v2"
"fmt"
"strings"
"k8s.io/kube-openapi/pkg/aggregator"
"k8s.io/kube-openapi/pkg/spec3"
"k8s.io/kube-openapi/pkg/validation/spec"
)
const metadataGV = "io.k8s.apimachinery.pkg.apis.meta.v1"
const autoscalingGV = "io.k8s.api.autoscaling.v1"
// MergeSpecs aggregates all OpenAPI specs, reusing the metadata of the first, static spec as the basis.
// The static spec has the highest priority, and its paths and definitions won't get overlapped by
// user-defined CRDs. None of the input is mutated, but input and output share data structures.
@ -83,28 +88,29 @@ func mergeSpec(dest, source *spec.Swagger) {
}
// MergeSpecsV3 merges OpenAPI v3 specs for CRDs
// For V3, the static spec is never merged with the individual CRD specs so no conflict resolution is necessary
// Conflicts belonging to the meta.v1 or autoscaling.v1 group versions are skipped as all CRDs reference those types
// Other conflicts will result in an error
func MergeSpecsV3(crdSpecs ...*spec3.OpenAPI) (*spec3.OpenAPI, error) {
// create shallow copy of staticSpec, but replace paths and definitions because we modify them.
crdSpec := &spec3.OpenAPI{}
if len(crdSpecs) > 0 {
crdSpec.Version = crdSpecs[0].Version
crdSpec.Info = crdSpecs[0].Info
}
for _, s := range crdSpecs {
// merge specs without checking conflicts, since the naming controller prevents
// conflicts between user-defined CRDs
mergeSpecV3(crdSpec, s)
err := mergeSpecV3(crdSpec, s)
if err != nil {
return nil, err
}
}
return crdSpec, nil
}
// mergeSpecV3 copies paths and definitions from source to dest, mutating dest, but not source.
// We assume that conflicts do not matter.
func mergeSpecV3(dest, source *spec3.OpenAPI) {
// Conflicts belonging to the meta.v1 or autoscaling.v1 group versions are skipped as all CRDs reference those types
// Other conflicts will result in an error
func mergeSpecV3(dest, source *spec3.OpenAPI) error {
if source == nil || source.Paths == nil {
return
return nil
}
if dest.Paths == nil {
dest.Paths = &spec3.Paths{}
@ -118,7 +124,10 @@ func mergeSpecV3(dest, source *spec3.OpenAPI) {
dest.Components.Schemas = map[string]*spec.Schema{}
}
if _, exists := dest.Components.Schemas[k]; exists {
klog.Warningf("Should not happen: OpenAPI V3 merge schema conflict on %s", k)
if strings.HasPrefix(k, metadataGV) || strings.HasPrefix(k, autoscalingGV) {
continue
}
return fmt.Errorf("OpenAPI V3 merge schema conflict on %s", k)
}
dest.Components.Schemas[k] = v
}
@ -128,4 +137,5 @@ func mergeSpecV3(dest, source *spec3.OpenAPI) {
}
dest.Paths.Paths[k] = v
}
return nil
}

View File

@ -0,0 +1,170 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package builder
import (
"reflect"
"testing"
"k8s.io/kube-openapi/pkg/spec3"
"k8s.io/kube-openapi/pkg/validation/spec"
)
func TestMergeSpecV3(t *testing.T) {
tests := []struct {
name string
specs []*spec3.OpenAPI
expected *spec3.OpenAPI
}{
{
name: "oneCRD",
specs: []*spec3.OpenAPI{{
Paths: &spec3.Paths{
Paths: map[string]*spec3.Path{
"/apis/stable.example.com/v1/crd1": {},
},
},
Components: &spec3.Components{
Schemas: map[string]*spec.Schema{
"io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta": {},
},
},
},
},
expected: &spec3.OpenAPI{
Paths: &spec3.Paths{
Paths: map[string]*spec3.Path{
"/apis/stable.example.com/v1/crd1": {},
},
},
Components: &spec3.Components{
Schemas: map[string]*spec.Schema{
"io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta": {},
},
},
},
},
{
name: "two CRDs same gv",
specs: []*spec3.OpenAPI{{
Paths: &spec3.Paths{
Paths: map[string]*spec3.Path{
"/apis/stable.example.com/v1/crd1": {},
},
},
Components: &spec3.Components{
Schemas: map[string]*spec.Schema{
"com.example.stable.v1.CRD1": {},
"io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta": {},
},
},
},
{
Paths: &spec3.Paths{
Paths: map[string]*spec3.Path{
"/apis/stable.example.com/v1/crd2": {},
},
},
Components: &spec3.Components{
Schemas: map[string]*spec.Schema{
"com.example.stable.v1.CRD2": {},
"io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta": {},
},
},
},
},
expected: &spec3.OpenAPI{
Paths: &spec3.Paths{
Paths: map[string]*spec3.Path{
"/apis/stable.example.com/v1/crd1": {},
"/apis/stable.example.com/v1/crd2": {},
},
},
Components: &spec3.Components{
Schemas: map[string]*spec.Schema{
"io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta": {},
"com.example.stable.v1.CRD1": {},
"com.example.stable.v1.CRD2": {},
},
},
},
},
{
name: "two CRDs with scale",
specs: []*spec3.OpenAPI{{
Paths: &spec3.Paths{
Paths: map[string]*spec3.Path{
"/apis/stable.example.com/v1/crd1": {},
},
},
Components: &spec3.Components{
Schemas: map[string]*spec.Schema{
"com.example.stable.v1.CRD1": {},
"io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta": {},
"io.k8s.api.autoscaling.v1.Scale": {},
},
},
},
{
Paths: &spec3.Paths{
Paths: map[string]*spec3.Path{
"/apis/stable.example.com/v1/crd2": {},
},
},
Components: &spec3.Components{
Schemas: map[string]*spec.Schema{
"com.example.stable.v1.CRD2": {},
"io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta": {},
"io.k8s.api.autoscaling.v1.Scale": {},
},
},
},
},
expected: &spec3.OpenAPI{
Paths: &spec3.Paths{
Paths: map[string]*spec3.Path{
"/apis/stable.example.com/v1/crd1": {},
"/apis/stable.example.com/v1/crd2": {},
},
},
Components: &spec3.Components{
Schemas: map[string]*spec.Schema{
"io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta": {},
"com.example.stable.v1.CRD1": {},
"com.example.stable.v1.CRD2": {},
"io.k8s.api.autoscaling.v1.Scale": {},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
merged, err := MergeSpecsV3(tt.specs...)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(merged, tt.expected) {
t.Error("Merged spec is different from expected spec")
}
})
}
}