Skip to content

Commit 4ce574f

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 d512cd0 commit 4ce574f

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-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

+49-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package clientcmd
33
import (
44
"errors"
55
"fmt"
6+
"os"
7+
"reflect"
68
"sort"
79
"strconv"
810
"time"
@@ -13,6 +15,7 @@ import (
1315
"k8s.io/kubernetes/pkg/api/meta"
1416
kclient "k8s.io/kubernetes/pkg/client/unversioned"
1517
kclientcmd "k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
18+
kclientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
1619
"k8s.io/kubernetes/pkg/fields"
1720
"k8s.io/kubernetes/pkg/kubectl"
1821
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
@@ -36,7 +39,6 @@ import (
3639
// New creates a default Factory for commands that should share identical server
3740
// connection behavior. Most commands should use this method to get a factory.
3841
func New(flags *pflag.FlagSet) *Factory {
39-
// Override global default to "" so we force the client to ask for user input
4042
// TODO refactor this upstream:
4143
// DefaultCluster should not be a global
4244
// A call to ClientConfig() should always return the best clientCfg possible
@@ -45,12 +47,58 @@ func New(flags *pflag.FlagSet) *Factory {
4547

4648
// TODO: there should be two client configs, one for OpenShift, and one for Kubernetes
4749
clientConfig := DefaultClientConfig(flags)
50+
clientConfig = defaultingClientConfig{clientConfig}
4851
f := NewFactory(clientConfig)
4952
f.BindFlags(flags)
5053

5154
return f
5255
}
5356

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+
54102
// Factory provides common options for OpenShift commands
55103
type Factory struct {
56104
*cmdutil.Factory

0 commit comments

Comments
 (0)