Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 72e4e06

Browse files
committedNov 5, 2015
Allow in-cluster config for oc
Because we set the default env value to empty, we can't use the default in cluster config for 'oc' (when you run inside a container, oc works). It's really important for container integration scenarios that oc uses the service account token by default, just like kubeconfig.
1 parent d512cd0 commit 72e4e06

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed
 

‎hack/test-cmd.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ atomic-enterprise start \
187187
"${NODE_CONFIG_DIR}/node-config.yaml"
188188

189189
# test client not configured
190-
[ "$(oc get services 2>&1 | grep 'Error in configuration')" ]
190+
[ "$(oc get services 2>&1 | grep 'No configuration file found, please login')" ]
191191

192192
# Set KUBERNETES_MASTER for oc from now on
193193
export KUBERNETES_MASTER="${API_SCHEME}://${API_HOST}:${API_PORT}"

‎pkg/cmd/util/clientcmd/factory.go

+42-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package clientcmd
33
import (
44
"errors"
55
"fmt"
6+
"reflect"
67
"sort"
78
"strconv"
89
"time"
@@ -13,6 +14,7 @@ import (
1314
"k8s.io/kubernetes/pkg/api/meta"
1415
kclient "k8s.io/kubernetes/pkg/client/unversioned"
1516
kclientcmd "k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
17+
kclientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
1618
"k8s.io/kubernetes/pkg/fields"
1719
"k8s.io/kubernetes/pkg/kubectl"
1820
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
@@ -33,24 +35,62 @@ import (
3335
routegen "github.com/openshift/origin/pkg/route/generator"
3436
)
3537

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+
3642
// New creates a default Factory for commands that should share identical server
3743
// connection behavior. Most commands should use this method to get a factory.
3844
func New(flags *pflag.FlagSet) *Factory {
39-
// Override global default to "" so we force the client to ask for user input
4045
// TODO refactor this upstream:
4146
// DefaultCluster should not be a global
4247
// A call to ClientConfig() should always return the best clientCfg possible
4348
// even if an error was returned, and let the caller decide what to do
44-
kclientcmd.DefaultCluster.Server = ""
49+
kclientcmd.DefaultCluster.Server = defaultClusterConfigURL
4550

4651
// TODO: there should be two client configs, one for OpenShift, and one for Kubernetes
4752
clientConfig := DefaultClientConfig(flags)
53+
clientConfig = defaultingClientConfig{clientConfig}
4854
f := NewFactory(clientConfig)
4955
f.BindFlags(flags)
5056

5157
return f
5258
}
5359

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+
5494
// Factory provides common options for OpenShift commands
5595
type Factory struct {
5696
*cmdutil.Factory

0 commit comments

Comments
 (0)
Please sign in to comment.