fix(describe): Dont panic when trying to describe an unknown resource

mine
Michael Cristina 2019-07-17 12:40:31 -05:00
parent fe11d334c7
commit 861b440de0
4 changed files with 23 additions and 6 deletions

View File

@ -243,4 +243,16 @@ var resMap = map[string]*meta.RESTMapping{
GroupVersionKind: schema.GroupVersionKind{Group: "apiextensions.k8s.io", Version: "v1beta1", Kind: "CustomResourceDefinition"}, GroupVersionKind: schema.GroupVersionKind{Group: "apiextensions.k8s.io", Version: "v1beta1", Kind: "CustomResourceDefinition"},
Scope: RestMapping, Scope: RestMapping,
}, },
"Events": {
Resource: schema.GroupVersionResource{Group: "events.k8s.io", Version: "v1beta1", Resource: "events"},
GroupVersionKind: schema.GroupVersionKind{Group: "events.k8s.io", Version: "v1beta1", Kind: "Event"},
Scope: RestMapping,
},
"PodDisruptionBudgets": {
Resource: schema.GroupVersionResource{Group: "policy", Version: "v1beta1", Resource: "poddisruptionbudgets"},
GroupVersionKind: schema.GroupVersionKind{Group: "policy", Version: "v1beta1", Kind: "PodDisruptionBudget"},
Scope: RestMapping,
},
} }

View File

@ -131,13 +131,18 @@ func (b *Base) List(ns string) (Columnars, error) {
func (b *Base) Describe(kind, pa string) (string, error) { func (b *Base) Describe(kind, pa string) (string, error) {
mapping, err := k8s.RestMapping.Find(kind) mapping, err := k8s.RestMapping.Find(kind)
if err != nil { if err != nil {
g, v, n := b.Resource.(*k8s.Resource).GetInfo() resource, ok := b.Resource.(*k8s.Resource)
mapper := k8s.RestMapper{b.Connection} if !ok {
log.Debug().Msgf("resource not a (*k8s.Resource) and %s", err)
return "", fmt.Errorf("resource not a (*k8s.Resource) and %s", err)
}
g, v, n := resource.GetInfo()
mapper := k8s.RestMapper{Connection: b.Connection}
var e error var e error
mapping, e = mapper.ResourceFor(fmt.Sprintf("%s.%s.%s", n, v, g)) mapping, e = mapper.ResourceFor(fmt.Sprintf("%s.%s.%s", n, v, g))
if e != nil { if e != nil {
log.Debug().Err(err).Msgf("Unable to find mapper for %s %s", kind, pa) log.Debug().Err(e).Msgf("Unable to find mapper for %s %s", kind, pa)
return "", err return "", e
} }
} }

View File

@ -38,7 +38,7 @@ func NewCustomList(c k8s.Connection, ns, group, version, name string) List {
func NewCustom(c k8s.Connection, group, version, name string) *Custom { func NewCustom(c k8s.Connection, group, version, name string) *Custom {
cr := &Custom{Base: &Base{Connection: c, Resource: k8s.NewResource(c, group, version, name)}} cr := &Custom{Base: &Base{Connection: c, Resource: k8s.NewResource(c, group, version, name)}}
cr.Factory = cr cr.Factory = cr
cr.group, cr.version, cr.name = cr.Resource.(*k8s.Resource).GetInfo() cr.group, cr.version, cr.name = group, version, name
return cr return cr
} }

View File

@ -189,7 +189,7 @@ func (v *resourceView) defaultEnter(ns, _, selection string) {
yaml, err := v.list.Resource().Describe(v.masterPage().baseTitle, selection) yaml, err := v.list.Resource().Describe(v.masterPage().baseTitle, selection)
if err != nil { if err != nil {
v.app.flash().errf("Describe command failed %s", err) v.app.flash().errf("Describe command failed: %s", err)
return return
} }