in kubectl-utils/pkg/kube/discovery.go [37:69]
func (c *Client) FindResource(ctx context.Context, name string) (*metav1.APIResource, error) {
var matches []metav1.APIResource
resourceLists, err := c.DiscoveryClient.ServerPreferredResources()
if err != nil {
return nil, fmt.Errorf("doing server discovery: %w", err)
}
for _, resourceList := range resourceLists {
gv, err := schema.ParseGroupVersion(resourceList.GroupVersion)
if err != nil {
return nil, fmt.Errorf("parsing group version %q: %w", resourceList.GroupVersion, err)
}
for _, resource := range resourceList.APIResources {
if resource.Kind == name {
if resource.Group == "" {
resource.Group = gv.Group
}
if resource.Version == "" {
resource.Version = gv.Version
}
matches = append(matches, resource)
}
}
}
if len(matches) == 0 {
return nil, fmt.Errorf("no match for resource %q", name)
}
if len(matches) > 1 {
return nil, fmt.Errorf("found multiple matches for resource %q", name)
}
resource := matches[0]
return &resource, nil
}