@@ -3,6 +3,8 @@ package clientcmd
3
3
import (
4
4
"errors"
5
5
"fmt"
6
+ "os"
7
+ "reflect"
6
8
"sort"
7
9
"strconv"
8
10
"time"
@@ -13,6 +15,7 @@ import (
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,58 @@ 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 kclientcmd .IsEmptyCluster (err ) {
78
+ return nil , fmt .Errorf (`No configuration file found, please login or point to an existing file:
79
+
80
+ 1. Via the command-line flag --config
81
+ 2. Via the KUBECONFIG environment variable
82
+ 3. In your home directory as ~/.kube/config
83
+
84
+ To view or setup config directly use the 'config' command.` )
85
+ }
86
+ return nil , err
87
+ }
88
+ return cfg , nil
89
+ }
90
+
91
+ func isDefaultConfig (cfg * kclient.Config ) bool {
92
+ if len (os .Getenv ("KUBERNETES_MASTER" )) > 0 {
93
+ return false
94
+ }
95
+ defaultConfig , err := kclientcmd .DefaultClientConfig .ClientConfig ()
96
+ if err != nil {
97
+ return false
98
+ }
99
+ return reflect .DeepEqual (cfg , defaultConfig )
100
+ }
101
+
54
102
// Factory provides common options for OpenShift commands
55
103
type Factory struct {
56
104
* cmdutil.Factory
0 commit comments