mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 13:37:30 +00:00
Merge pull request #32140 from deads2k/restmapper-02-match-partial-groups
Automatic merge from submit-queue add group prefix matching for kubectl usage Adds prefix matching for groups so that `kubectl get storageclass.storage` matches `storageclass.storage.k8s.io`. @kubernetes/kubectl
This commit is contained in:
commit
0764aa0b68
@ -947,6 +947,9 @@ __EOF__
|
|||||||
kube::test::if_has_string "${output_message}" 'extensions/v1beta1'
|
kube::test::if_has_string "${output_message}" 'extensions/v1beta1'
|
||||||
output_message=$(kubectl get hpa.autoscaling -o=jsonpath='{.items[0].apiVersion}' 2>&1 "${kube_flags[@]}")
|
output_message=$(kubectl get hpa.autoscaling -o=jsonpath='{.items[0].apiVersion}' 2>&1 "${kube_flags[@]}")
|
||||||
kube::test::if_has_string "${output_message}" 'autoscaling/v1'
|
kube::test::if_has_string "${output_message}" 'autoscaling/v1'
|
||||||
|
# tests kubectl group prefix matching
|
||||||
|
output_message=$(kubectl get hpa.autoscal -o=jsonpath='{.items[0].apiVersion}' 2>&1 "${kube_flags[@]}")
|
||||||
|
kube::test::if_has_string "${output_message}" 'autoscaling/v1'
|
||||||
# Clean up
|
# Clean up
|
||||||
# Note that we should delete hpa first, otherwise it may fight with the rc reaper.
|
# Note that we should delete hpa first, otherwise it may fight with the rc reaper.
|
||||||
kubectl delete hpa frontend "${kube_flags[@]}"
|
kubectl delete hpa frontend "${kube_flags[@]}"
|
||||||
|
@ -223,8 +223,8 @@ func (m *DefaultRESTMapper) ResourcesFor(input unversioned.GroupVersionResource)
|
|||||||
|
|
||||||
ret := []unversioned.GroupVersionResource{}
|
ret := []unversioned.GroupVersionResource{}
|
||||||
switch {
|
switch {
|
||||||
// fully qualified. Find the exact match
|
|
||||||
case hasGroup && hasVersion:
|
case hasGroup && hasVersion:
|
||||||
|
// fully qualified. Find the exact match
|
||||||
for plural, singular := range m.pluralToSingular {
|
for plural, singular := range m.pluralToSingular {
|
||||||
if singular == resource {
|
if singular == resource {
|
||||||
ret = append(ret, plural)
|
ret = append(ret, plural)
|
||||||
@ -237,16 +237,37 @@ func (m *DefaultRESTMapper) ResourcesFor(input unversioned.GroupVersionResource)
|
|||||||
}
|
}
|
||||||
|
|
||||||
case hasGroup:
|
case hasGroup:
|
||||||
|
// given a group, prefer an exact match. If you don't find one, resort to a prefix match on group
|
||||||
|
foundExactMatch := false
|
||||||
requestedGroupResource := resource.GroupResource()
|
requestedGroupResource := resource.GroupResource()
|
||||||
for plural, singular := range m.pluralToSingular {
|
for plural, singular := range m.pluralToSingular {
|
||||||
if singular.GroupResource() == requestedGroupResource {
|
if singular.GroupResource() == requestedGroupResource {
|
||||||
|
foundExactMatch = true
|
||||||
ret = append(ret, plural)
|
ret = append(ret, plural)
|
||||||
}
|
}
|
||||||
if plural.GroupResource() == requestedGroupResource {
|
if plural.GroupResource() == requestedGroupResource {
|
||||||
|
foundExactMatch = true
|
||||||
ret = append(ret, plural)
|
ret = append(ret, plural)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if you didn't find an exact match, match on group prefixing. This allows storageclass.storage to match
|
||||||
|
// storageclass.storage.k8s.io
|
||||||
|
if !foundExactMatch {
|
||||||
|
for plural, singular := range m.pluralToSingular {
|
||||||
|
if !strings.HasPrefix(plural.Group, requestedGroupResource.Group) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if singular.Resource == requestedGroupResource.Resource {
|
||||||
|
ret = append(ret, plural)
|
||||||
|
}
|
||||||
|
if plural.Resource == requestedGroupResource.Resource {
|
||||||
|
ret = append(ret, plural)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
case hasVersion:
|
case hasVersion:
|
||||||
for plural, singular := range m.pluralToSingular {
|
for plural, singular := range m.pluralToSingular {
|
||||||
if singular.Version == resource.Version && singular.Resource == resource.Resource {
|
if singular.Version == resource.Version && singular.Resource == resource.Resource {
|
||||||
@ -309,13 +330,29 @@ func (m *DefaultRESTMapper) KindsFor(input unversioned.GroupVersionResource) ([]
|
|||||||
}
|
}
|
||||||
|
|
||||||
case hasGroup:
|
case hasGroup:
|
||||||
|
foundExactMatch := false
|
||||||
requestedGroupResource := resource.GroupResource()
|
requestedGroupResource := resource.GroupResource()
|
||||||
for currResource, currKind := range m.resourceToKind {
|
for currResource, currKind := range m.resourceToKind {
|
||||||
if currResource.GroupResource() == requestedGroupResource {
|
if currResource.GroupResource() == requestedGroupResource {
|
||||||
|
foundExactMatch = true
|
||||||
ret = append(ret, currKind)
|
ret = append(ret, currKind)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if you didn't find an exact match, match on group prefixing. This allows storageclass.storage to match
|
||||||
|
// storageclass.storage.k8s.io
|
||||||
|
if !foundExactMatch {
|
||||||
|
for currResource, currKind := range m.resourceToKind {
|
||||||
|
if !strings.HasPrefix(currResource.Group, requestedGroupResource.Group) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if currResource.Resource == requestedGroupResource.Resource {
|
||||||
|
ret = append(ret, currKind)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
case hasVersion:
|
case hasVersion:
|
||||||
for currResource, currKind := range m.resourceToKind {
|
for currResource, currKind := range m.resourceToKind {
|
||||||
if currResource.Version == resource.Version && currResource.Resource == resource.Resource {
|
if currResource.Version == resource.Version && currResource.Resource == resource.Resource {
|
||||||
|
@ -130,6 +130,62 @@ func TestRESTMapperKindsFor(t *testing.T) {
|
|||||||
ExpectedKinds []unversioned.GroupVersionKind
|
ExpectedKinds []unversioned.GroupVersionKind
|
||||||
ExpectedKindErr string
|
ExpectedKindErr string
|
||||||
}{
|
}{
|
||||||
|
{
|
||||||
|
// exact matches are prefered
|
||||||
|
Name: "groups, with group exact",
|
||||||
|
PreferredOrder: []unversioned.GroupVersion{
|
||||||
|
{Group: "first-group-1", Version: "first-version"},
|
||||||
|
{Group: "first-group", Version: "first-version"},
|
||||||
|
},
|
||||||
|
KindsToRegister: []unversioned.GroupVersionKind{
|
||||||
|
{Group: "first-group-1", Version: "first-version", Kind: "my-kind"},
|
||||||
|
{Group: "first-group", Version: "first-version", Kind: "my-kind"},
|
||||||
|
},
|
||||||
|
PartialResourceToRequest: unversioned.GroupVersionResource{Group: "first-group", Resource: "my-kind"},
|
||||||
|
|
||||||
|
ExpectedKinds: []unversioned.GroupVersionKind{
|
||||||
|
{Group: "first-group", Version: "first-version", Kind: "my-kind"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
// group prefixes work
|
||||||
|
Name: "groups, with group prefix",
|
||||||
|
PreferredOrder: []unversioned.GroupVersion{
|
||||||
|
{Group: "second-group", Version: "first-version"},
|
||||||
|
{Group: "first-group", Version: "first-version"},
|
||||||
|
},
|
||||||
|
KindsToRegister: []unversioned.GroupVersionKind{
|
||||||
|
{Group: "first-group", Version: "first-version", Kind: "my-kind"},
|
||||||
|
{Group: "second-group", Version: "first-version", Kind: "my-kind"},
|
||||||
|
},
|
||||||
|
PartialResourceToRequest: unversioned.GroupVersionResource{Group: "first", Resource: "my-kind"},
|
||||||
|
|
||||||
|
ExpectedKinds: []unversioned.GroupVersionKind{
|
||||||
|
{Group: "first-group", Version: "first-version", Kind: "my-kind"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
// group prefixes can be ambiguous
|
||||||
|
Name: "groups, with ambiguous group prefix",
|
||||||
|
PreferredOrder: []unversioned.GroupVersion{
|
||||||
|
{Group: "first-group-1", Version: "first-version"},
|
||||||
|
{Group: "first-group", Version: "first-version"},
|
||||||
|
},
|
||||||
|
KindsToRegister: []unversioned.GroupVersionKind{
|
||||||
|
{Group: "first-group-1", Version: "first-version", Kind: "my-kind"},
|
||||||
|
{Group: "first-group", Version: "first-version", Kind: "my-kind"},
|
||||||
|
},
|
||||||
|
PartialResourceToRequest: unversioned.GroupVersionResource{Group: "first", Resource: "my-kind"},
|
||||||
|
|
||||||
|
ExpectedKinds: []unversioned.GroupVersionKind{
|
||||||
|
{Group: "first-group-1", Version: "first-version", Kind: "my-kind"},
|
||||||
|
{Group: "first-group", Version: "first-version", Kind: "my-kind"},
|
||||||
|
},
|
||||||
|
ExpectedKindErr: " matches multiple kinds ",
|
||||||
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
Name: "ambiguous groups, with preference order",
|
Name: "ambiguous groups, with preference order",
|
||||||
PreferredOrder: []unversioned.GroupVersion{
|
PreferredOrder: []unversioned.GroupVersion{
|
||||||
@ -243,6 +299,65 @@ func TestRESTMapperResourcesFor(t *testing.T) {
|
|||||||
ExpectedResources []unversioned.GroupVersionResource
|
ExpectedResources []unversioned.GroupVersionResource
|
||||||
ExpectedResourceErr string
|
ExpectedResourceErr string
|
||||||
}{
|
}{
|
||||||
|
{
|
||||||
|
// exact matches are prefered
|
||||||
|
Name: "groups, with group exact",
|
||||||
|
PreferredOrder: []unversioned.GroupVersion{
|
||||||
|
{Group: "first-group-1", Version: "first-version"},
|
||||||
|
{Group: "first-group", Version: "first-version"},
|
||||||
|
},
|
||||||
|
KindsToRegister: []unversioned.GroupVersionKind{
|
||||||
|
{Group: "first-group-1", Version: "first-version", Kind: "my-kind"},
|
||||||
|
{Group: "first-group", Version: "first-version", Kind: "my-kind"},
|
||||||
|
},
|
||||||
|
PluralPartialResourceToRequest: unversioned.GroupVersionResource{Group: "first-group", Resource: "my-kinds"},
|
||||||
|
SingularPartialResourceToRequest: unversioned.GroupVersionResource{Group: "first-group", Resource: "my-kind"},
|
||||||
|
|
||||||
|
ExpectedResources: []unversioned.GroupVersionResource{
|
||||||
|
{Group: "first-group", Version: "first-version", Resource: "my-kinds"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
// group prefixes work
|
||||||
|
Name: "groups, with group prefix",
|
||||||
|
PreferredOrder: []unversioned.GroupVersion{
|
||||||
|
{Group: "second-group", Version: "first-version"},
|
||||||
|
{Group: "first-group", Version: "first-version"},
|
||||||
|
},
|
||||||
|
KindsToRegister: []unversioned.GroupVersionKind{
|
||||||
|
{Group: "first-group", Version: "first-version", Kind: "my-kind"},
|
||||||
|
{Group: "second-group", Version: "first-version", Kind: "my-kind"},
|
||||||
|
},
|
||||||
|
PluralPartialResourceToRequest: unversioned.GroupVersionResource{Group: "first", Resource: "my-kinds"},
|
||||||
|
SingularPartialResourceToRequest: unversioned.GroupVersionResource{Group: "first", Resource: "my-kind"},
|
||||||
|
|
||||||
|
ExpectedResources: []unversioned.GroupVersionResource{
|
||||||
|
{Group: "first-group", Version: "first-version", Resource: "my-kinds"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
// group prefixes can be ambiguous
|
||||||
|
Name: "groups, with ambiguous group prefix",
|
||||||
|
PreferredOrder: []unversioned.GroupVersion{
|
||||||
|
{Group: "first-group-1", Version: "first-version"},
|
||||||
|
{Group: "first-group", Version: "first-version"},
|
||||||
|
},
|
||||||
|
KindsToRegister: []unversioned.GroupVersionKind{
|
||||||
|
{Group: "first-group-1", Version: "first-version", Kind: "my-kind"},
|
||||||
|
{Group: "first-group", Version: "first-version", Kind: "my-kind"},
|
||||||
|
},
|
||||||
|
PluralPartialResourceToRequest: unversioned.GroupVersionResource{Group: "first", Resource: "my-kinds"},
|
||||||
|
SingularPartialResourceToRequest: unversioned.GroupVersionResource{Group: "first", Resource: "my-kind"},
|
||||||
|
|
||||||
|
ExpectedResources: []unversioned.GroupVersionResource{
|
||||||
|
{Group: "first-group-1", Version: "first-version", Resource: "my-kinds"},
|
||||||
|
{Group: "first-group", Version: "first-version", Resource: "my-kinds"},
|
||||||
|
},
|
||||||
|
ExpectedResourceErr: " matches multiple resources ",
|
||||||
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
Name: "ambiguous groups, with preference order",
|
Name: "ambiguous groups, with preference order",
|
||||||
PreferredOrder: []unversioned.GroupVersion{
|
PreferredOrder: []unversioned.GroupVersion{
|
||||||
|
Loading…
Reference in New Issue
Block a user