Merge pull request #89913 from zhouya0/fix_kubectl_version_should_print_version_info

Fix kubectl version should print version info without config file
This commit is contained in:
Kubernetes Prow Robot 2020-04-15 09:24:03 -07:00 committed by GitHub
commit db999c9634
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 7 deletions

View File

@ -17,7 +17,6 @@ limitations under the License.
package genericclioptions package genericclioptions
import ( import (
"errors"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -58,8 +57,7 @@ const (
var ( var (
defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "http-cache") defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "http-cache")
ErrEmptyConfig = clientcmd.NewEmptyConfigError(`Missing or incomplete configuration info. Please point to an existing, complete config file:
ErrEmptyConfig = errors.New(`Missing or incomplete configuration info. Please point to an existing, complete config file:
1. Via the command-line flag --kubeconfig 1. Via the command-line flag --kubeconfig
2. Via the KUBECONFIG environment variable 2. Via the KUBECONFIG environment variable

View File

@ -30,11 +30,24 @@ import (
var ( var (
ErrNoContext = errors.New("no context chosen") 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 // message is for consistency with old behavior
ErrEmptyCluster = errors.New("cluster has no server defined") 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 { type errContextNotFound struct {
ContextName string ContextName string
} }
@ -60,9 +73,14 @@ func IsContextNotFound(err error) bool {
func IsEmptyConfig(err error) bool { func IsEmptyConfig(err error) bool {
switch t := err.(type) { switch t := err.(type) {
case errConfigurationInvalid: case errConfigurationInvalid:
return len(t) == 1 && t[0] == ErrEmptyConfig if len(t) != 1 {
return false
} }
return err == ErrEmptyConfig _, ok := t[0].(*errEmptyConfig)
return ok
}
_, ok := err.(*errEmptyConfig)
return ok
} }
// errConfigurationInvalid is a set of errors indicating the configuration is invalid. // errConfigurationInvalid is a set of errors indicating the configuration is invalid.

View File

@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library") load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library( go_library(
name = "go_default_library", name = "go_default_library",
@ -20,6 +20,16 @@ go_library(
], ],
) )
go_test(
name = "go_default_test",
srcs = ["version_test.go"],
embed = [":go_default_library"],
deps = [
"//staging/src/k8s.io/cli-runtime/pkg/genericclioptions:go_default_library",
"//staging/src/k8s.io/kubectl/pkg/cmd/util:go_default_library",
],
)
filegroup( filegroup(
name = "package-srcs", name = "package-srcs",
srcs = glob(["**"]), srcs = glob(["**"]),

View File

@ -0,0 +1,38 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package version
import (
"strings"
"testing"
"k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
)
func TestNewCmdVersionWithoutConfigFile(t *testing.T) {
tf := cmdutil.NewFactory(&genericclioptions.ConfigFlags{})
streams, _, buf, _ := genericclioptions.NewTestIOStreams()
cmd := NewCmdVersion(tf, streams)
cmd.SetOutput(buf)
if err := cmd.Execute(); err != nil {
t.Errorf("Cannot execute version command: %v", err)
}
if !strings.Contains(buf.String(), "Client Version") {
t.Errorf("unexpected output: %s", buf.String())
}
}