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