Skip to content

Commit a420428

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 f9d8378 commit a420428

File tree

3 files changed

+53
-2
lines changed

3 files changed

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

+42-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ import (
77
"strconv"
88
"time"
99

10+
"github.com/golang/glog"
11+
1012
"github.com/spf13/pflag"
1113
"k8s.io/kubernetes/pkg/api"
1214
kerrors "k8s.io/kubernetes/pkg/api/errors"
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,51 @@ 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 icc, err := kclient.InClusterConfig(); err == nil {
78+
glog.V(4).Infof("Using in-cluster configuration")
79+
return icc, nil
80+
}
81+
if kclientcmd.IsEmptyConfig(err) {
82+
return nil, fmt.Errorf(`No configuration file found, please login or point to an existing file:
83+
84+
1. Via the command-line flag --config
85+
2. Via the KUBECONFIG environment variable
86+
3. In your home directory as ~/.kube/config
87+
88+
To view or setup config directly use the 'config' command.`)
89+
}
90+
return nil, err
91+
}
92+
return cfg, nil
93+
}
94+
5495
// Factory provides common options for OpenShift commands
5596
type Factory struct {
5697
*cmdutil.Factory

test/end-to-end/core.sh

+5
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ oc login -u e2e-user
137137
oc project test
138138
oc whoami
139139

140+
echo "[INFO] Running a CLI command in a container using the service account"
141+
out=$(oc run cli-with-token --attach --image=openshift/origin:${TAG} --restart=Never -- cli status --loglevel=4 2>&1)
142+
echo $out
143+
[ "$(echo $out | grep 'Using in-cluster configuration')" ]
144+
140145
echo "[INFO] Streaming the logs from a deployment twice..."
141146
oc create -f test/fixtures/failing-dc.yaml
142147
tryuntil oc get rc/failing-dc-1

0 commit comments

Comments
 (0)