mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Merge pull request #20511 from deads2k/fix-singularizer
Auto commit by PR queue bot
This commit is contained in:
commit
54b13b4c24
@ -165,18 +165,33 @@ func KindToResource(kind unversioned.GroupVersionKind) ( /*plural*/ unversioned.
|
||||
|
||||
// ResourceSingularizer implements RESTMapper
|
||||
// It converts a resource name from plural to singular (e.g., from pods to pod)
|
||||
// It must have exactly one match and it must match case perfectly. This is congruent with old functionality
|
||||
func (m *DefaultRESTMapper) ResourceSingularizer(resourceType string) (string, error) {
|
||||
partialResource := unversioned.GroupVersionResource{Resource: resourceType}
|
||||
resource, err := m.ResourceFor(partialResource)
|
||||
resources, err := m.ResourcesFor(partialResource)
|
||||
if err != nil {
|
||||
return resourceType, err
|
||||
}
|
||||
|
||||
singular, ok := m.pluralToSingular[resource]
|
||||
if !ok {
|
||||
return resourceType, fmt.Errorf("no singular of resource %v has been defined", resource)
|
||||
singular := unversioned.GroupVersionResource{}
|
||||
for _, curr := range resources {
|
||||
currSingular, ok := m.pluralToSingular[curr]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if singular.IsEmpty() {
|
||||
singular = currSingular
|
||||
continue
|
||||
}
|
||||
|
||||
if currSingular.Resource != singular.Resource {
|
||||
return resourceType, fmt.Errorf("multiple possibile singular resources (%v) found for %v", resources, resourceType)
|
||||
}
|
||||
}
|
||||
|
||||
if singular.IsEmpty() {
|
||||
return resourceType, fmt.Errorf("no singular of resource %v has been defined", resourceType)
|
||||
}
|
||||
|
||||
return singular.Resource, nil
|
||||
}
|
||||
|
||||
|
@ -56,6 +56,10 @@ type GroupVersionResource struct {
|
||||
Resource string
|
||||
}
|
||||
|
||||
func (gvr GroupVersionResource) IsEmpty() bool {
|
||||
return len(gvr.Group) == 0 && len(gvr.Version) == 0 && len(gvr.Resource) == 0
|
||||
}
|
||||
|
||||
func (gvr GroupVersionResource) GroupResource() GroupResource {
|
||||
return GroupResource{Group: gvr.Group, Resource: gvr.Resource}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user