@@ -13,6 +13,7 @@ import (
13
13
"k8s.io/kubernetes/pkg/api/meta"
14
14
kclient "k8s.io/kubernetes/pkg/client/unversioned"
15
15
kclientcmd "k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
16
+ kclientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
16
17
"k8s.io/kubernetes/pkg/fields"
17
18
"k8s.io/kubernetes/pkg/kubectl"
18
19
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
@@ -36,7 +37,6 @@ import (
36
37
// New creates a default Factory for commands that should share identical server
37
38
// connection behavior. Most commands should use this method to get a factory.
38
39
func New (flags * pflag.FlagSet ) * Factory {
39
- // Override global default to "" so we force the client to ask for user input
40
40
// TODO refactor this upstream:
41
41
// DefaultCluster should not be a global
42
42
// A call to ClientConfig() should always return the best clientCfg possible
@@ -45,12 +45,47 @@ func New(flags *pflag.FlagSet) *Factory {
45
45
46
46
// TODO: there should be two client configs, one for OpenShift, and one for Kubernetes
47
47
clientConfig := DefaultClientConfig (flags )
48
+ clientConfig = defaultingClientConfig {clientConfig }
48
49
f := NewFactory (clientConfig )
49
50
f .BindFlags (flags )
50
51
51
52
return f
52
53
}
53
54
55
+ // defaultingClientConfig detects whether the provided config is the default, and if
56
+ // so returns an error that indicates the user should set up their config.
57
+ type defaultingClientConfig struct {
58
+ nested kclientcmd.ClientConfig
59
+ }
60
+
61
+ // RawConfig calls the nested method
62
+ func (c defaultingClientConfig ) RawConfig () (kclientcmdapi.Config , error ) {
63
+ return c .nested .RawConfig ()
64
+ }
65
+
66
+ // Namespace calls the nested method
67
+ func (c defaultingClientConfig ) Namespace () (string , bool , error ) {
68
+ return c .nested .Namespace ()
69
+ }
70
+
71
+ // ClientConfig returns a complete client config
72
+ func (c defaultingClientConfig ) ClientConfig () (* kclient.Config , error ) {
73
+ cfg , err := c .nested .ClientConfig ()
74
+ if err != nil {
75
+ if kclientcmd .IsEmptyConfig (err ) {
76
+ return nil , fmt .Errorf (`No configuration file found, please login or point to an existing file:
77
+
78
+ 1. Via the command-line flag --config
79
+ 2. Via the KUBECONFIG environment variable
80
+ 3. In your home directory as ~/.kube/config
81
+
82
+ To view or setup config directly use the 'config' command.` )
83
+ }
84
+ return nil , err
85
+ }
86
+ return cfg , nil
87
+ }
88
+
54
89
// Factory provides common options for OpenShift commands
55
90
type Factory struct {
56
91
* cmdutil.Factory
0 commit comments