Skip to content

Commit db665a7

Browse files
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 f3a2c25 commit db665a7

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

hack/test-cmd.sh

+6-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,12 @@ 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')" ]
191+
unused_port=33333
192+
# setting env bypasses the not configured message
193+
[ "$(KUBERNETES_MASTER='http://${API_HOST}:${unused_port}' oc get services 2>&1 | grep 'did you specify the right host or port')" ]
194+
# setting --server bypasses the not configured message
195+
[ "$(oc get services --server=http://${API_HOST}:${unused_port} 2>&1 | grep 'did you specify the right host or port')" ]
191196

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

pkg/cmd/util/clientcmd/factory.go

+36-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"k8s.io/kubernetes/pkg/api/meta"
1414
kclient "k8s.io/kubernetes/pkg/client/unversioned"
1515
kclientcmd "k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
16+
kclientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
1617
"k8s.io/kubernetes/pkg/fields"
1718
"k8s.io/kubernetes/pkg/kubectl"
1819
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
@@ -36,7 +37,6 @@ import (
3637
// New creates a default Factory for commands that should share identical server
3738
// connection behavior. Most commands should use this method to get a factory.
3839
func New(flags *pflag.FlagSet) *Factory {
39-
// Override global default to "" so we force the client to ask for user input
4040
// TODO refactor this upstream:
4141
// DefaultCluster should not be a global
4242
// A call to ClientConfig() should always return the best clientCfg possible
@@ -45,12 +45,47 @@ func New(flags *pflag.FlagSet) *Factory {
4545

4646
// TODO: there should be two client configs, one for OpenShift, and one for Kubernetes
4747
clientConfig := DefaultClientConfig(flags)
48+
clientConfig = defaultingClientConfig{clientConfig}
4849
f := NewFactory(clientConfig)
4950
f.BindFlags(flags)
5051

5152
return f
5253
}
5354

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+
5489
// Factory provides common options for OpenShift commands
5590
type Factory struct {
5691
*cmdutil.Factory

0 commit comments

Comments
 (0)