Skip to content

Commit db999c9

Browse files
authored
Merge pull request kubernetes#89913 from zhouya0/fix_kubectl_version_should_print_version_info
Fix kubectl version should print version info without config file
2 parents 495b0dd + 948f4de commit db999c9

File tree

4 files changed

+71
-7
lines changed

4 files changed

+71
-7
lines changed

staging/src/k8s.io/cli-runtime/pkg/genericclioptions/config_flags.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package genericclioptions
1818

1919
import (
20-
"errors"
2120
"os"
2221
"path/filepath"
2322
"regexp"
@@ -58,8 +57,7 @@ const (
5857

5958
var (
6059
defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "http-cache")
61-
62-
ErrEmptyConfig = errors.New(`Missing or incomplete configuration info. Please point to an existing, complete config file:
60+
ErrEmptyConfig = clientcmd.NewEmptyConfigError(`Missing or incomplete configuration info. Please point to an existing, complete config file:
6361
6462
1. Via the command-line flag --kubeconfig
6563
2. Via the KUBECONFIG environment variable

staging/src/k8s.io/client-go/tools/clientcmd/validation.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,24 @@ import (
3030

3131
var (
3232
ErrNoContext = errors.New("no context chosen")
33-
ErrEmptyConfig = errors.New("no configuration has been provided, try setting KUBERNETES_MASTER environment variable")
33+
ErrEmptyConfig = NewEmptyConfigError("no configuration has been provided, try setting KUBERNETES_MASTER environment variable")
3434
// message is for consistency with old behavior
3535
ErrEmptyCluster = errors.New("cluster has no server defined")
3636
)
3737

38+
// NewEmptyConfigError returns an error wrapping the given message which IsEmptyConfig() will recognize as an empty config error
39+
func NewEmptyConfigError(message string) error {
40+
return &errEmptyConfig{message}
41+
}
42+
43+
type errEmptyConfig struct {
44+
message string
45+
}
46+
47+
func (e *errEmptyConfig) Error() string {
48+
return e.message
49+
}
50+
3851
type errContextNotFound struct {
3952
ContextName string
4053
}
@@ -60,9 +73,14 @@ func IsContextNotFound(err error) bool {
6073
func IsEmptyConfig(err error) bool {
6174
switch t := err.(type) {
6275
case errConfigurationInvalid:
63-
return len(t) == 1 && t[0] == ErrEmptyConfig
76+
if len(t) != 1 {
77+
return false
78+
}
79+
_, ok := t[0].(*errEmptyConfig)
80+
return ok
6481
}
65-
return err == ErrEmptyConfig
82+
_, ok := err.(*errEmptyConfig)
83+
return ok
6684
}
6785

6886
// errConfigurationInvalid is a set of errors indicating the configuration is invalid.

staging/src/k8s.io/kubectl/pkg/cmd/version/BUILD

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@io_bazel_rules_go//go:def.bzl", "go_library")
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
22

33
go_library(
44
name = "go_default_library",
@@ -20,6 +20,16 @@ go_library(
2020
],
2121
)
2222

23+
go_test(
24+
name = "go_default_test",
25+
srcs = ["version_test.go"],
26+
embed = [":go_default_library"],
27+
deps = [
28+
"//staging/src/k8s.io/cli-runtime/pkg/genericclioptions:go_default_library",
29+
"//staging/src/k8s.io/kubectl/pkg/cmd/util:go_default_library",
30+
],
31+
)
32+
2333
filegroup(
2434
name = "package-srcs",
2535
srcs = glob(["**"]),
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package version
18+
19+
import (
20+
"strings"
21+
"testing"
22+
23+
"k8s.io/cli-runtime/pkg/genericclioptions"
24+
cmdutil "k8s.io/kubectl/pkg/cmd/util"
25+
)
26+
27+
func TestNewCmdVersionWithoutConfigFile(t *testing.T) {
28+
tf := cmdutil.NewFactory(&genericclioptions.ConfigFlags{})
29+
streams, _, buf, _ := genericclioptions.NewTestIOStreams()
30+
cmd := NewCmdVersion(tf, streams)
31+
cmd.SetOutput(buf)
32+
if err := cmd.Execute(); err != nil {
33+
t.Errorf("Cannot execute version command: %v", err)
34+
}
35+
if !strings.Contains(buf.String(), "Client Version") {
36+
t.Errorf("unexpected output: %s", buf.String())
37+
}
38+
}

0 commit comments

Comments
 (0)