fix ShortcutRESTMapper and prevent it from ever silently failing again

pull/6/head
deads2k 2016-02-09 09:03:12 -05:00
parent 995b72798c
commit 94d683e89b
1 changed files with 49 additions and 31 deletions

View File

@ -26,7 +26,6 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/apis/extensions"
)
const kubectlAnnotationPrefix = "kubectl.kubernetes.io/"
@ -75,53 +74,72 @@ func (m OutputVersionMapper) RESTMapping(gk unversioned.GroupKind, versions ...s
}
// ShortcutExpander is a RESTMapper that can be used for Kubernetes
// resources.
// resources. It expands the resource first, then invokes the wrapped RESTMapper
type ShortcutExpander struct {
meta.RESTMapper
RESTMapper meta.RESTMapper
}
var _ meta.RESTMapper = &ShortcutExpander{}
// KindFor implements meta.RESTMapper. It expands the resource first, then invokes the wrapped
// mapper.
func (e ShortcutExpander) KindFor(resource unversioned.GroupVersionResource) (unversioned.GroupVersionKind, error) {
resource = expandResourceShortcut(resource)
return e.RESTMapper.KindFor(resource)
return e.RESTMapper.KindFor(expandResourceShortcut(resource))
}
func (e ShortcutExpander) KindsFor(resource unversioned.GroupVersionResource) ([]unversioned.GroupVersionKind, error) {
return e.RESTMapper.KindsFor(expandResourceShortcut(resource))
}
func (e ShortcutExpander) ResourcesFor(resource unversioned.GroupVersionResource) ([]unversioned.GroupVersionResource, error) {
return e.RESTMapper.ResourcesFor(expandResourceShortcut(resource))
}
func (e ShortcutExpander) ResourceFor(resource unversioned.GroupVersionResource) (unversioned.GroupVersionResource, error) {
return e.RESTMapper.ResourceFor(expandResourceShortcut(resource))
}
// ResourceSingularizer expands the named resource and then singularizes it.
func (e ShortcutExpander) ResourceSingularizer(resource string) (string, error) {
return e.RESTMapper.ResourceSingularizer(expandResourceShortcut(unversioned.GroupVersionResource{Resource: resource}).Resource)
}
func (e ShortcutExpander) RESTMapping(gk unversioned.GroupKind, versions ...string) (*meta.RESTMapping, error) {
return e.RESTMapper.RESTMapping(gk, versions...)
}
func (e ShortcutExpander) AliasesForResource(resource string) ([]string, bool) {
return e.RESTMapper.AliasesForResource(expandResourceShortcut(unversioned.GroupVersionResource{Resource: resource}).Resource)
}
// shortForms is the list of short names to their expanded names
var shortForms = map[string]string{
// Please keep this alphabetized
// If you add an entry here, please also take a look at pkg/kubectl/cmd/cmd.go
// and add an entry to valid_resources when appropriate.
"cs": "componentstatuses",
"ds": "daemonsets",
"ep": "endpoints",
"ev": "events",
"hpa": "horizontalpodautoscalers",
"ing": "ingresses",
"limits": "limitranges",
"no": "nodes",
"ns": "namespaces",
"po": "pods",
"psp": "podSecurityPolicies",
"pvc": "persistentvolumeclaims",
"pv": "persistentvolumes",
"quota": "resourcequotas",
"rc": "replicationcontrollers",
"rs": "replicasets",
"svc": "services",
}
// 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 unversioned.GroupVersionResource) unversioned.GroupVersionResource {
shortForms := map[string]unversioned.GroupVersionResource{
// Please keep this alphabetized
// If you add an entry here, please also take a look at pkg/kubectl/cmd/cmd.go
// and add an entry to valid_resources when appropriate.
"cs": api.SchemeGroupVersion.WithResource("componentstatuses"),
"ds": extensions.SchemeGroupVersion.WithResource("daemonsets"),
"ep": api.SchemeGroupVersion.WithResource("endpoints"),
"ev": api.SchemeGroupVersion.WithResource("events"),
"hpa": extensions.SchemeGroupVersion.WithResource("horizontalpodautoscalers"),
"ing": extensions.SchemeGroupVersion.WithResource("ingresses"),
"limits": api.SchemeGroupVersion.WithResource("limitranges"),
"no": api.SchemeGroupVersion.WithResource("nodes"),
"ns": api.SchemeGroupVersion.WithResource("namespaces"),
"po": api.SchemeGroupVersion.WithResource("pods"),
"psp": api.SchemeGroupVersion.WithResource("podSecurityPolicies"),
"pvc": api.SchemeGroupVersion.WithResource("persistentvolumeclaims"),
"pv": api.SchemeGroupVersion.WithResource("persistentvolumes"),
"quota": api.SchemeGroupVersion.WithResource("resourcequotas"),
"rc": api.SchemeGroupVersion.WithResource("replicationcontrollers"),
"rs": extensions.SchemeGroupVersion.WithResource("replicasets"),
"svc": api.SchemeGroupVersion.WithResource("services"),
}
if expanded, ok := shortForms[resource.Resource]; ok {
return expanded
// don't change the group or version that's already been specified
resource.Resource = expanded
}
return resource
}