Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated cherry pick of #73482: Always select the in-memory group/version as a target when #73564

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions staging/src/k8s.io/apimachinery/pkg/runtime/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ load(
go_test(
name = "go_default_test",
srcs = [
"codec_test.go",
"local_scheme_test.go",
"swagger_doc_generator_test.go",
],
Expand Down
20 changes: 20 additions & 0 deletions staging/src/k8s.io/apimachinery/pkg/runtime/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ var _ GroupVersioner = multiGroupVersioner{}
type multiGroupVersioner struct {
target schema.GroupVersion
acceptedGroupKinds []schema.GroupKind
coerce bool
}

// NewMultiGroupVersioner returns the provided group version for any kind that matches one of the provided group kinds.
Expand All @@ -312,6 +313,22 @@ func NewMultiGroupVersioner(gv schema.GroupVersion, groupKinds ...schema.GroupKi
return multiGroupVersioner{target: gv, acceptedGroupKinds: groupKinds}
}

// NewCoercingMultiGroupVersioner returns the provided group version for any incoming kind.
// Incoming kinds that match the provided groupKinds are preferred.
// Kind may be empty in the provided group kind, in which case any kind will match.
// Examples:
// gv=mygroup/__internal, groupKinds=mygroup/Foo, anothergroup/Bar
// KindForGroupVersionKinds(yetanother/v1/Baz, anothergroup/v1/Bar) -> mygroup/__internal/Bar (matched preferred group/kind)
//
// gv=mygroup/__internal, groupKinds=mygroup, anothergroup
// KindForGroupVersionKinds(yetanother/v1/Baz, anothergroup/v1/Bar) -> mygroup/__internal/Bar (matched preferred group)
//
// gv=mygroup/__internal, groupKinds=mygroup, anothergroup
// KindForGroupVersionKinds(yetanother/v1/Baz, yetanother/v1/Bar) -> mygroup/__internal/Baz (no preferred group/kind match, uses first kind in list)
func NewCoercingMultiGroupVersioner(gv schema.GroupVersion, groupKinds ...schema.GroupKind) GroupVersioner {
return multiGroupVersioner{target: gv, acceptedGroupKinds: groupKinds, coerce: true}
}

// KindForGroupVersionKinds returns the target group version if any kind matches any of the original group kinds. It will
// use the originating kind where possible.
func (v multiGroupVersioner) KindForGroupVersionKinds(kinds []schema.GroupVersionKind) (schema.GroupVersionKind, bool) {
Expand All @@ -326,5 +343,8 @@ func (v multiGroupVersioner) KindForGroupVersionKinds(kinds []schema.GroupVersio
return v.target.WithKind(src.Kind), true
}
}
if v.coerce && len(kinds) > 0 {
return v.target.WithKind(kinds[0].Kind), true
}
return schema.GroupVersionKind{}, false
}
78 changes: 78 additions & 0 deletions staging/src/k8s.io/apimachinery/pkg/runtime/codec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright 2019 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 runtime

import (
"testing"

"k8s.io/apimachinery/pkg/runtime/schema"
)

func gv(group, version string) schema.GroupVersion {
return schema.GroupVersion{Group: group, Version: version}
}
func gvk(group, version, kind string) schema.GroupVersionKind {
return schema.GroupVersionKind{Group: group, Version: version, Kind: kind}
}
func gk(group, kind string) schema.GroupKind {
return schema.GroupKind{Group: group, Kind: kind}
}

func TestCoercingMultiGroupVersioner(t *testing.T) {
testcases := []struct {
name string
target schema.GroupVersion
preferredKinds []schema.GroupKind
kinds []schema.GroupVersionKind
expectKind schema.GroupVersionKind
}{
{
name: "matched preferred group/kind",
target: gv("mygroup", "__internal"),
preferredKinds: []schema.GroupKind{gk("mygroup", "Foo"), gk("anothergroup", "Bar")},
kinds: []schema.GroupVersionKind{gvk("yetanother", "v1", "Baz"), gvk("anothergroup", "v1", "Bar")},
expectKind: gvk("mygroup", "__internal", "Bar"),
},
{
name: "matched preferred group",
target: gv("mygroup", "__internal"),
preferredKinds: []schema.GroupKind{gk("mygroup", ""), gk("anothergroup", "")},
kinds: []schema.GroupVersionKind{gvk("yetanother", "v1", "Baz"), gvk("anothergroup", "v1", "Bar")},
expectKind: gvk("mygroup", "__internal", "Bar"),
},
{
name: "no preferred group/kind match, uses first kind in list",
target: gv("mygroup", "__internal"),
preferredKinds: []schema.GroupKind{gk("mygroup", ""), gk("anothergroup", "")},
kinds: []schema.GroupVersionKind{gvk("yetanother", "v1", "Baz"), gvk("yetanother", "v1", "Bar")},
expectKind: gvk("mygroup", "__internal", "Baz"),
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
v := NewCoercingMultiGroupVersioner(tc.target, tc.preferredKinds...)
kind, ok := v.KindForGroupVersionKinds(tc.kinds)
if !ok {
t.Error("got no kind")
}
if kind != tc.expectKind {
t.Errorf("expected %#v, got %#v", tc.expectKind, kind)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func NewStorageCodec(opts StorageCodecConfig) (runtime.Codec, error) {
)
decoder := opts.StorageSerializer.DecoderToVersion(
recognizer.NewDecoder(decoders...),
runtime.NewMultiGroupVersioner(
runtime.NewCoercingMultiGroupVersioner(
opts.MemoryVersion,
schema.GroupKind{Group: opts.MemoryVersion.Group},
schema.GroupKind{Group: opts.StorageVersion.Group},
Expand Down