ShortcutExpander has been extended in a way that it will examine a hardcoded list of tuples anticipated from the server when
searching for an alternative name for the resource. Note that the list is ordered and the first match will yield the extended resource's name. One important thing to highlight is that the ShortcutExpander will fall back to PriorityRestMaper to determine the group for the resource. Also this PR introduces a new shortcut namely sc which will resolve to storageclasses within storage.k8s.io group In addition the type of kubectl.ShortForms has been changed to ResourceShortcuts struct. It has got a brand new name, it was also extended and ordered by group.
This commit is contained in:
@@ -71,23 +71,23 @@ func (e ShortcutExpander) getAll() []schema.GroupResource {
|
||||
}
|
||||
|
||||
func (e ShortcutExpander) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
|
||||
return e.RESTMapper.KindFor(expandResourceShortcut(resource))
|
||||
return e.RESTMapper.KindFor(e.expandResourceShortcut(resource))
|
||||
}
|
||||
|
||||
func (e ShortcutExpander) KindsFor(resource schema.GroupVersionResource) ([]schema.GroupVersionKind, error) {
|
||||
return e.RESTMapper.KindsFor(expandResourceShortcut(resource))
|
||||
return e.RESTMapper.KindsFor(e.expandResourceShortcut(resource))
|
||||
}
|
||||
|
||||
func (e ShortcutExpander) ResourcesFor(resource schema.GroupVersionResource) ([]schema.GroupVersionResource, error) {
|
||||
return e.RESTMapper.ResourcesFor(expandResourceShortcut(resource))
|
||||
return e.RESTMapper.ResourcesFor(e.expandResourceShortcut(resource))
|
||||
}
|
||||
|
||||
func (e ShortcutExpander) ResourceFor(resource schema.GroupVersionResource) (schema.GroupVersionResource, error) {
|
||||
return e.RESTMapper.ResourceFor(expandResourceShortcut(resource))
|
||||
return e.RESTMapper.ResourceFor(e.expandResourceShortcut(resource))
|
||||
}
|
||||
|
||||
func (e ShortcutExpander) ResourceSingularizer(resource string) (string, error) {
|
||||
return e.RESTMapper.ResourceSingularizer(expandResourceShortcut(schema.GroupVersionResource{Resource: resource}).Resource)
|
||||
return e.RESTMapper.ResourceSingularizer(e.expandResourceShortcut(schema.GroupVersionResource{Resource: resource}).Resource)
|
||||
}
|
||||
|
||||
func (e ShortcutExpander) RESTMapping(gk schema.GroupKind, versions ...string) (*meta.RESTMapping, error) {
|
||||
@@ -124,17 +124,60 @@ func (e ShortcutExpander) AliasesForResource(resource string) ([]string, bool) {
|
||||
}
|
||||
return aliases, true
|
||||
}
|
||||
expanded := expandResourceShortcut(schema.GroupVersionResource{Resource: resource}).Resource
|
||||
expanded := e.expandResourceShortcut(schema.GroupVersionResource{Resource: resource}).Resource
|
||||
return []string{expanded}, (expanded != resource)
|
||||
}
|
||||
|
||||
// getShortcutMappings returns a hardcoded set of tuples.
|
||||
// First the list of potential resources will be taken from the instance variable
|
||||
// which holds the anticipated result of the discovery API.
|
||||
// Next we will fall back to the hardcoded list of resources.
|
||||
// Note that the list is ordered by group priority.
|
||||
// TODO: Wire this to discovery API.
|
||||
func (e ShortcutExpander) getShortcutMappings() ([]kubectl.ResourceShortcuts, error) {
|
||||
res := []kubectl.ResourceShortcuts{
|
||||
{
|
||||
ShortForm: schema.GroupResource{Group: "storage.k8s.io", Resource: "sc"},
|
||||
LongForm: schema.GroupResource{Group: "storage.k8s.io", Resource: "storageclasses"},
|
||||
},
|
||||
}
|
||||
|
||||
// append hardcoded short forms at the end of the list
|
||||
res = append(res, kubectl.ResourcesShortcutStatic...)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// expandResourceShortcut will return the expanded version of resource
|
||||
// (something that a pkg/api/meta.RESTMapper can understand), if it is
|
||||
// indeed a shortcut. Otherwise, will return resource unmodified.
|
||||
func expandResourceShortcut(resource schema.GroupVersionResource) schema.GroupVersionResource {
|
||||
if expanded, ok := kubectl.ShortForms[resource.Resource]; ok {
|
||||
resource.Resource = expanded
|
||||
return resource
|
||||
// indeed a shortcut. If no match has been found, we will match on group prefixing.
|
||||
// Lastly we will return resource unmodified.
|
||||
func (e ShortcutExpander) expandResourceShortcut(resource schema.GroupVersionResource) schema.GroupVersionResource {
|
||||
// get the shortcut mappings and return on first match.
|
||||
if resources, err := e.getShortcutMappings(); err == nil {
|
||||
for _, item := range resources {
|
||||
if len(resource.Group) != 0 && resource.Group != item.ShortForm.Group {
|
||||
continue
|
||||
}
|
||||
if resource.Resource == item.ShortForm.Resource {
|
||||
resource.Resource = item.LongForm.Resource
|
||||
return resource
|
||||
}
|
||||
}
|
||||
|
||||
// we didn't find exact match so match on group prefixing. This allows autoscal to match autoscaling
|
||||
if len(resource.Group) == 0 {
|
||||
return resource
|
||||
}
|
||||
for _, item := range resources {
|
||||
if !strings.HasPrefix(item.ShortForm.Group, resource.Group) {
|
||||
continue
|
||||
}
|
||||
if resource.Resource == item.ShortForm.Resource {
|
||||
resource.Resource = item.LongForm.Resource
|
||||
return resource
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return resource
|
||||
}
|
||||
|
Reference in New Issue
Block a user