From 1c7ee45b9bc86111e14a955d4922b85d5ca9e8b4 Mon Sep 17 00:00:00 2001 From: zhouya0 Date: Tue, 7 Apr 2020 15:18:38 +0800 Subject: [PATCH] Fix kubectl version should print version info Kubernetes-commit: 948f4de2dbba3affca2de460d836158cbde5db78 --- tools/clientcmd/validation.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/tools/clientcmd/validation.go b/tools/clientcmd/validation.go index afe6f80b..a480bdf0 100644 --- a/tools/clientcmd/validation.go +++ b/tools/clientcmd/validation.go @@ -30,11 +30,24 @@ import ( var ( ErrNoContext = errors.New("no context chosen") - ErrEmptyConfig = errors.New("no configuration has been provided, try setting KUBERNETES_MASTER environment variable") + ErrEmptyConfig = NewEmptyConfigError("no configuration has been provided, try setting KUBERNETES_MASTER environment variable") // message is for consistency with old behavior ErrEmptyCluster = errors.New("cluster has no server defined") ) +// NewEmptyConfigError returns an error wrapping the given message which IsEmptyConfig() will recognize as an empty config error +func NewEmptyConfigError(message string) error { + return &errEmptyConfig{message} +} + +type errEmptyConfig struct { + message string +} + +func (e *errEmptyConfig) Error() string { + return e.message +} + type errContextNotFound struct { ContextName string } @@ -60,9 +73,14 @@ func IsContextNotFound(err error) bool { func IsEmptyConfig(err error) bool { switch t := err.(type) { case errConfigurationInvalid: - return len(t) == 1 && t[0] == ErrEmptyConfig + if len(t) != 1 { + return false + } + _, ok := t[0].(*errEmptyConfig) + return ok } - return err == ErrEmptyConfig + _, ok := err.(*errEmptyConfig) + return ok } // errConfigurationInvalid is a set of errors indicating the configuration is invalid.