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