Add more information when throwing errors in discoverying 3rd party resources

This commit is contained in:
Janet Kuo 2016-05-31 16:42:43 -07:00
parent 517dedd419
commit 4615d1e24c
3 changed files with 22 additions and 17 deletions

View File

@ -251,14 +251,14 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
// have been dynamically added to the apiserver // have been dynamically added to the apiserver
Object: func(discoverDynamicAPIs bool) (meta.RESTMapper, runtime.ObjectTyper) { Object: func(discoverDynamicAPIs bool) (meta.RESTMapper, runtime.ObjectTyper) {
cfg, err := clientConfig.ClientConfig() cfg, err := clientConfig.ClientConfig()
CheckErr(err) checkErrWithPrefix("failed to get client config: ", err)
cmdApiVersion := unversioned.GroupVersion{} cmdApiVersion := unversioned.GroupVersion{}
if cfg.GroupVersion != nil { if cfg.GroupVersion != nil {
cmdApiVersion = *cfg.GroupVersion cmdApiVersion = *cfg.GroupVersion
} }
if discoverDynamicAPIs { if discoverDynamicAPIs {
client, err := clients.ClientForVersion(&unversioned.GroupVersion{Version: "v1"}) client, err := clients.ClientForVersion(&unversioned.GroupVersion{Version: "v1"})
CheckErr(err) checkErrWithPrefix("failed to find client for version v1: ", err)
var versions []unversioned.GroupVersion var versions []unversioned.GroupVersion
var gvks []unversioned.GroupVersionKind var gvks []unversioned.GroupVersionKind
@ -272,7 +272,7 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
break break
} }
} }
CheckErr(err) checkErrWithPrefix("failed to get third-party group versions: ", err)
if len(versions) > 0 { if len(versions) > 0 {
priorityMapper, ok := mapper.RESTMapper.(meta.PriorityRESTMapper) priorityMapper, ok := mapper.RESTMapper.(meta.PriorityRESTMapper)
if !ok { if !ok {
@ -292,7 +292,7 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
preferredExternalVersion := versionList[0] preferredExternalVersion := versionList[0]
thirdPartyMapper, err := kubectl.NewThirdPartyResourceMapper(versionList, getGroupVersionKinds(gvks, group)) thirdPartyMapper, err := kubectl.NewThirdPartyResourceMapper(versionList, getGroupVersionKinds(gvks, group))
CheckErr(err) checkErrWithPrefix("failed to create third party resource mapper: ", err)
accessor := meta.NewAccessor() accessor := meta.NewAccessor()
groupMeta := apimachinery.GroupMeta{ groupMeta := apimachinery.GroupMeta{
GroupVersion: preferredExternalVersion, GroupVersion: preferredExternalVersion,
@ -302,7 +302,7 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
InterfacesFor: makeInterfacesFor(versionList), InterfacesFor: makeInterfacesFor(versionList),
} }
CheckErr(registered.RegisterGroup(groupMeta)) checkErrWithPrefix("failed to register group: ", registered.RegisterGroup(groupMeta))
registered.AddThirdPartyAPIGroupVersions(versionList...) registered.AddThirdPartyAPIGroupVersions(versionList...)
multiMapper = append(meta.MultiRESTMapper{thirdPartyMapper}, multiMapper...) multiMapper = append(meta.MultiRESTMapper{thirdPartyMapper}, multiMapper...)
} }

View File

@ -105,17 +105,22 @@ func fatal(msg string) {
// This method is generic to the command in use and may be used by non-Kubectl // This method is generic to the command in use and may be used by non-Kubectl
// commands. // commands.
func CheckErr(err error) { func CheckErr(err error) {
checkErr(err, fatalErrHandler) checkErr("", err, fatalErrHandler)
} }
func checkErr(err error, handleErr func(string)) { // checkErrWithPrefix works like CheckErr, but adds a caller-defined prefix to non-nil errors
func checkErrWithPrefix(prefix string, err error) {
checkErr(prefix, err, fatalErrHandler)
}
func checkErr(pref string, err error, handleErr func(string)) {
if err == nil { if err == nil {
return return
} }
if errors.IsInvalid(err) { if errors.IsInvalid(err) {
details := err.(*errors.StatusError).Status().Details details := err.(*errors.StatusError).Status().Details
prefix := fmt.Sprintf("The %s %q is invalid.\n", details.Kind, details.Name) prefix := fmt.Sprintf("%sThe %s %q is invalid.\n", pref, details.Kind, details.Name)
errs := statusCausesToAggrError(details.Causes) errs := statusCausesToAggrError(details.Causes)
handleErr(MultilineError(prefix, errs)) handleErr(MultilineError(prefix, errs))
} }
@ -125,23 +130,23 @@ func checkErr(err error, handleErr func(string)) {
switch { switch {
case len(noMatch.PartialResource.Group) > 0 && len(noMatch.PartialResource.Version) > 0: case len(noMatch.PartialResource.Group) > 0 && len(noMatch.PartialResource.Version) > 0:
handleErr(fmt.Sprintf("the server doesn't have a resource type %q in group %q and version %q", noMatch.PartialResource.Resource, noMatch.PartialResource.Group, noMatch.PartialResource.Version)) handleErr(fmt.Sprintf("%sthe server doesn't have a resource type %q in group %q and version %q", pref, noMatch.PartialResource.Resource, noMatch.PartialResource.Group, noMatch.PartialResource.Version))
case len(noMatch.PartialResource.Group) > 0: case len(noMatch.PartialResource.Group) > 0:
handleErr(fmt.Sprintf("the server doesn't have a resource type %q in group %q", noMatch.PartialResource.Resource, noMatch.PartialResource.Group)) handleErr(fmt.Sprintf("%sthe server doesn't have a resource type %q in group %q", pref, noMatch.PartialResource.Resource, noMatch.PartialResource.Group))
case len(noMatch.PartialResource.Version) > 0: case len(noMatch.PartialResource.Version) > 0:
handleErr(fmt.Sprintf("the server doesn't have a resource type %q in version %q", noMatch.PartialResource.Resource, noMatch.PartialResource.Version)) handleErr(fmt.Sprintf("%sthe server doesn't have a resource type %q in version %q", pref, noMatch.PartialResource.Resource, noMatch.PartialResource.Version))
default: default:
handleErr(fmt.Sprintf("the server doesn't have a resource type %q", noMatch.PartialResource.Resource)) handleErr(fmt.Sprintf("%sthe server doesn't have a resource type %q", pref, noMatch.PartialResource.Resource))
} }
return return
} }
// handle multiline errors // handle multiline errors
if clientcmd.IsConfigurationInvalid(err) { if clientcmd.IsConfigurationInvalid(err) {
handleErr(MultilineError("Error in configuration: ", err)) handleErr(MultilineError(fmt.Sprintf("%sError in configuration: ", pref), err))
} }
if agg, ok := err.(utilerrors.Aggregate); ok && len(agg.Errors()) > 0 { if agg, ok := err.(utilerrors.Aggregate); ok && len(agg.Errors()) > 0 {
handleErr(MultipleErrors("", agg.Errors())) handleErr(MultipleErrors(pref, agg.Errors()))
} }
msg, ok := StandardErrorMessage(err) msg, ok := StandardErrorMessage(err)
@ -151,7 +156,7 @@ func checkErr(err error, handleErr func(string)) {
msg = fmt.Sprintf("error: %s", msg) msg = fmt.Sprintf("error: %s", msg)
} }
} }
handleErr(msg) handleErr(fmt.Sprintf("%s%s", pref, msg))
} }
func statusCausesToAggrError(scs []unversioned.StatusCause) utilerrors.Aggregate { func statusCausesToAggrError(scs []unversioned.StatusCause) utilerrors.Aggregate {

View File

@ -238,7 +238,7 @@ func TestCheckInvalidErr(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
checkErr(test.err, errHandle) checkErr("", test.err, errHandle)
if errReturned != test.expected { if errReturned != test.expected {
t.Fatalf("Got: %s, expected: %s", errReturned, test.expected) t.Fatalf("Got: %s, expected: %s", errReturned, test.expected)
@ -275,7 +275,7 @@ func TestCheckNoResourceMatchError(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
checkErr(test.err, errHandle) checkErr("", test.err, errHandle)
if errReturned != test.expected { if errReturned != test.expected {
t.Fatalf("Got: %s, expected: %s", errReturned, test.expected) t.Fatalf("Got: %s, expected: %s", errReturned, test.expected)