mirror of
https://github.com/kubernetes/client-go.git
synced 2025-08-14 05:25:33 +00:00
Fix discovery restmapper finding resources in non-preferred versions
Kubernetes-commit: a6316fb3a525a6a88ca74cfa76c2892455fcf832
This commit is contained in:
parent
c6f8cf2c47
commit
7a2d69e09e
@ -49,6 +49,7 @@ func NewRESTMapper(groupResources []*APIGroupResources, versionInterfaces meta.V
|
||||
for _, group := range groupResources {
|
||||
groupPriority = append(groupPriority, group.Group.Name)
|
||||
|
||||
// Make sure the preferred version comes first
|
||||
if len(group.Group.PreferredVersion.Version) != 0 {
|
||||
preferred := group.Group.PreferredVersion.Version
|
||||
if _, ok := group.VersionedResources[preferred]; ok {
|
||||
@ -72,6 +73,21 @@ func NewRESTMapper(groupResources []*APIGroupResources, versionInterfaces meta.V
|
||||
continue
|
||||
}
|
||||
|
||||
// Add non-preferred versions after the preferred version, in case there are resources that only exist in those versions
|
||||
if discoveryVersion.Version != group.Group.PreferredVersion.Version {
|
||||
resourcePriority = append(resourcePriority, schema.GroupVersionResource{
|
||||
Group: group.Group.Name,
|
||||
Version: discoveryVersion.Version,
|
||||
Resource: meta.AnyResource,
|
||||
})
|
||||
|
||||
kindPriority = append(kindPriority, schema.GroupVersionKind{
|
||||
Group: group.Group.Name,
|
||||
Version: discoveryVersion.Version,
|
||||
Kind: meta.AnyKind,
|
||||
})
|
||||
}
|
||||
|
||||
gv := schema.GroupVersion{Group: group.Group.Name, Version: discoveryVersion.Version}
|
||||
versionMapper := meta.NewDefaultRESTMapper([]schema.GroupVersion{gv}, versionInterfaces)
|
||||
|
||||
|
@ -67,6 +67,32 @@ func TestRESTMapper(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
// This group tests finding and prioritizing resources that only exist in non-preferred versions
|
||||
{
|
||||
Group: metav1.APIGroup{
|
||||
Name: "unpreferred",
|
||||
Versions: []metav1.GroupVersionForDiscovery{
|
||||
{Version: "v1"},
|
||||
{Version: "v2beta1"},
|
||||
{Version: "v2alpha1"},
|
||||
},
|
||||
PreferredVersion: metav1.GroupVersionForDiscovery{Version: "v1"},
|
||||
},
|
||||
VersionedResources: map[string][]metav1.APIResource{
|
||||
"v1": {
|
||||
{Name: "broccoli", Namespaced: true, Kind: "Broccoli"},
|
||||
},
|
||||
"v2beta1": {
|
||||
{Name: "broccoli", Namespaced: true, Kind: "Broccoli"},
|
||||
{Name: "peas", Namespaced: true, Kind: "Pea"},
|
||||
},
|
||||
"v2alpha1": {
|
||||
{Name: "broccoli", Namespaced: true, Kind: "Broccoli"},
|
||||
{Name: "peas", Namespaced: true, Kind: "Pea"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
restMapper := NewRESTMapper(resources, nil)
|
||||
@ -123,6 +149,16 @@ func TestRESTMapper(t *testing.T) {
|
||||
Kind: "Job",
|
||||
},
|
||||
},
|
||||
{
|
||||
input: schema.GroupVersionResource{
|
||||
Resource: "peas",
|
||||
},
|
||||
want: schema.GroupVersionKind{
|
||||
Group: "unpreferred",
|
||||
Version: "v2beta1",
|
||||
Kind: "Pea",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range kindTCs {
|
||||
|
Loading…
Reference in New Issue
Block a user