Skip to content

Commit 04b10a4

Browse files
committed
use the upstream construction for kubeconfig evaluation
1 parent 27ae008 commit 04b10a4

File tree

13 files changed

+20
-158
lines changed

13 files changed

+20
-158
lines changed

pkg/client/cmd/clientcmd.go

-37
This file was deleted.

pkg/client/config/loader.go

-5
This file was deleted.

pkg/cmd/infra/router/clientcmd.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717

1818
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
1919

20-
"github.com/openshift/origin/pkg/client/config"
2120
"github.com/openshift/origin/pkg/cmd/flagtypes"
2221
"github.com/openshift/origin/pkg/cmd/util"
2322
)
@@ -199,8 +198,8 @@ func (cfg *Config) bindEnv() error {
199198

200199
func DefaultClientConfig(flags *pflag.FlagSet) kclientcmd.ClientConfig {
201200
loadingRules := kclientcmd.NewDefaultClientConfigLoadingRules()
202-
flags.StringVar(&loadingRules.ExplicitPath, config.OpenShiftConfigFlagName, "", "Path to the config file to use for CLI requests.")
203-
cobra.MarkFlagFilename(flags, config.OpenShiftConfigFlagName)
201+
flags.StringVar(&loadingRules.ExplicitPath, kclientcmd.OpenShiftKubeConfigFlagName, "", "Path to the config file to use for CLI requests.")
202+
cobra.MarkFlagFilename(flags, kclientcmd.OpenShiftKubeConfigFlagName)
204203

205204
// set our explicit defaults
206205
defaultOverrides := &kclientcmd.ConfigOverrides{ClusterDefaults: kclientcmdapi.Cluster{Server: os.Getenv("KUBERNETES_MASTER")}}

pkg/oc/admin/diagnostics/config.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
kclientcmd "k8s.io/client-go/tools/clientcmd"
1111
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
1212

13-
"github.com/openshift/origin/pkg/client/config"
1413
"github.com/openshift/origin/pkg/oc/admin/diagnostics/diagnostics/util"
1514
)
1615

@@ -33,7 +32,7 @@ func (o DiagnosticsOptions) detectClientConfig() (expected bool, detected bool)
3332
return false, false
3433
}
3534
o.Logger().Notice("CED2011", "Determining if client configuration exists for client/cluster diagnostics")
36-
confFlagName := config.OpenShiftConfigFlagName
35+
confFlagName := kclientcmd.OpenShiftKubeConfigFlagName
3736
confFlagValue := o.ClientFlags.Lookup(confFlagName).Value.String()
3837
successfulLoad := false
3938

pkg/oc/admin/diagnostics/diagnostics.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import (
1212

1313
kutilerrors "k8s.io/apimachinery/pkg/util/errors"
1414
"k8s.io/apimachinery/pkg/util/sets"
15+
kclientcmd "k8s.io/client-go/tools/clientcmd"
1516
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
1617

17-
"github.com/openshift/origin/pkg/client/config"
1818
"github.com/openshift/origin/pkg/cmd/flagtypes"
1919
poddiag "github.com/openshift/origin/pkg/oc/admin/diagnostics/diagnostics/client/pod/in_pod"
2020
networkpoddiag "github.com/openshift/origin/pkg/oc/admin/diagnostics/diagnostics/cluster/network/in_pod"
@@ -223,7 +223,7 @@ func (o *DiagnosticsOptions) bindCommonFlags(flags *flag.FlagSet) {
223223
func (o *DiagnosticsOptions) bindClientFlags(flags *flag.FlagSet) {
224224
o.ClientFlags = flag.NewFlagSet("client", flag.ContinueOnError) // hide the extensive set of client flags
225225
o.Factory = osclientcmd.New(o.ClientFlags) // that would otherwise be added to this command
226-
flags.AddFlag(o.ClientFlags.Lookup(config.OpenShiftConfigFlagName))
226+
flags.AddFlag(o.ClientFlags.Lookup(kclientcmd.OpenShiftKubeConfigFlagName))
227227
flags.AddFlag(o.ClientFlags.Lookup("context")) // TODO: find k8s constant
228228
flags.StringVar(&o.ClientClusterContext, options.FlagClusterContextName, "", "Client context to use for cluster administrator")
229229
flags.BoolVar(&o.PreventModification, options.FlagPreventModificationName, false, "If true, may be set to prevent diagnostics making any changes via the API")

pkg/oc/admin/prune/images.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ func getClientAndMasterVersions(client discovery.DiscoveryInterface, timeout tim
763763

764764
select {
765765
case err, closed := <-done:
766-
if strings.HasSuffix(fmt.Sprintf("%v", err), "connection refused") || clientcmd.IsConfigurationMissing(err) || kclientcmd.IsConfigurationInvalid(err) {
766+
if strings.HasSuffix(fmt.Sprintf("%v", err), "connection refused") || kclientcmd.IsEmptyConfig(err) || kclientcmd.IsConfigurationInvalid(err) {
767767
return nil, nil, err
768768
}
769769
if closed && err != nil {

pkg/oc/bootstrap/docker/up.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
3636

3737
userv1client "github.com/openshift/client-go/user/clientset/versioned"
38-
osclientcmd "github.com/openshift/origin/pkg/client/cmd"
3938
cmdutil "github.com/openshift/origin/pkg/cmd/util"
4039
"github.com/openshift/origin/pkg/cmd/util/variable"
4140
oauthclientinternal "github.com/openshift/origin/pkg/oauth/generated/internalclientset"
@@ -228,7 +227,7 @@ func (c *ClusterUpConfig) Complete(cmd *cobra.Command, out io.Writer) error {
228227
// Get the default client config for login
229228
var err error
230229
flags := pflag.NewFlagSet("", pflag.ContinueOnError)
231-
c.defaultClientConfig, err = osclientcmd.DefaultClientConfig(flags).RawConfig()
230+
c.defaultClientConfig, err = kcmdutil.DefaultClientConfig(flags).RawConfig()
232231
if err != nil {
233232
if !os.IsNotExist(err) {
234233
return err

pkg/oc/cli/cmd/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func (o VersionOptions) RunVersion() error {
178178

179179
select {
180180
case err, closed := <-done:
181-
if strings.HasSuffix(fmt.Sprintf("%v", err), "connection refused") || clientcmd.IsConfigurationMissing(err) || kclientcmd.IsConfigurationInvalid(err) {
181+
if strings.HasSuffix(fmt.Sprintf("%v", err), "connection refused") || kclientcmd.IsEmptyConfig(err) || kclientcmd.IsConfigurationInvalid(err) {
182182
return nil
183183
}
184184
if closed && err != nil {

pkg/oc/cli/cmd/wrappers.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
2121
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
2222

23-
cmdconfig "github.com/openshift/origin/pkg/client/config"
2423
cmdutil "github.com/openshift/origin/pkg/cmd/util"
2524
"github.com/openshift/origin/pkg/oc/cli/cmd/create"
2625
"github.com/openshift/origin/pkg/oc/cli/describe"
@@ -738,7 +737,7 @@ func NewCmdConfig(parentName, name string, f *clientcmd.Factory, out, errOut io.
738737
pathOptions := &kclientcmd.PathOptions{
739738
GlobalFile: kclientcmd.RecommendedHomeFile,
740739
EnvVar: kclientcmd.RecommendedConfigPathEnvVar,
741-
ExplicitFileFlag: cmdconfig.OpenShiftConfigFlagName,
740+
ExplicitFileFlag: kclientcmd.OpenShiftKubeConfigFlagName,
742741

743742
GlobalFileSubpath: path.Join(kclientcmd.RecommendedHomeDir, kclientcmd.RecommendedFileName),
744743

pkg/oc/cli/config/loader.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@ import (
55

66
kclientcmd "k8s.io/client-go/tools/clientcmd"
77
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
8-
9-
"github.com/openshift/origin/pkg/client/config"
108
)
119

1210
func NewPathOptions(cmd *cobra.Command) *kclientcmd.PathOptions {
13-
return NewPathOptionsWithConfig(kcmdutil.GetFlagString(cmd, config.OpenShiftConfigFlagName))
11+
return NewPathOptionsWithConfig(kcmdutil.GetFlagString(cmd, kclientcmd.OpenShiftKubeConfigFlagName))
1412
}
1513

1614
func NewPathOptionsWithConfig(configPath string) *kclientcmd.PathOptions {
1715
return &kclientcmd.PathOptions{
1816
GlobalFile: kclientcmd.RecommendedHomeFile,
1917

2018
EnvVar: kclientcmd.RecommendedConfigPathEnvVar,
21-
ExplicitFileFlag: config.OpenShiftConfigFlagName,
19+
ExplicitFileFlag: kclientcmd.OpenShiftKubeConfigFlagName,
2220

2321
LoadingRules: &kclientcmd.ClientConfigLoadingRules{
2422
ExplicitPath: configPath,

pkg/oc/cli/util/clientcmd/factory_client_access.go

+5-96
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,24 @@ package clientcmd
22

33
import (
44
"errors"
5-
"fmt"
6-
"io/ioutil"
75
"net/http"
8-
"os"
96
"path/filepath"
107
"regexp"
118
"strconv"
129
"strings"
1310
"time"
1411

15-
"github.com/golang/glog"
1612
"github.com/spf13/cobra"
1713
"github.com/spf13/pflag"
1814
"k8s.io/kubernetes/pkg/api/legacyscheme"
1915

20-
osclientcmd "github.com/openshift/origin/pkg/client/cmd"
2116
corev1 "k8s.io/api/core/v1"
22-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2317
"k8s.io/apimachinery/pkg/runtime"
2418
"k8s.io/apimachinery/pkg/runtime/schema"
2519
"k8s.io/client-go/discovery"
2620
"k8s.io/client-go/kubernetes"
2721
restclient "k8s.io/client-go/rest"
2822
kclientcmd "k8s.io/client-go/tools/clientcmd"
29-
kclientcmdapi "k8s.io/client-go/tools/clientcmd/api"
3023
"k8s.io/client-go/util/homedir"
3124
kapi "k8s.io/kubernetes/pkg/apis/core"
3225
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
@@ -59,12 +52,14 @@ type ClientAccessFactory interface {
5952
}
6053

6154
func NewClientAccessFactory(optionalClientConfig kclientcmd.ClientConfig) ClientAccessFactory {
55+
// if we call this factory construction method, we want the openshift style config loading
56+
kclientcmd.UseOpenShiftKubeConfigValues = true
57+
kclientcmd.ErrEmptyConfig = kclientcmd.NewErrConfigurationMissing()
58+
6259
flags := pflag.NewFlagSet("", pflag.ContinueOnError)
6360
clientConfig := optionalClientConfig
6461
if optionalClientConfig == nil {
65-
// TODO: there should be two client configs, one for OpenShift, and one for Kubernetes
66-
clientConfig = osclientcmd.DefaultClientConfig(flags)
67-
clientConfig = defaultingClientConfig{clientConfig}
62+
clientConfig = kcmdutil.DefaultClientConfig(flags)
6863
}
6964
factory := &ring0Factory{
7065
clientConfig: clientConfig,
@@ -389,92 +384,6 @@ func (f *ring0Factory) EditorEnvs() []string {
389384
return []string{"OC_EDITOR", "EDITOR"}
390385
}
391386

392-
// defaultingClientConfig detects whether the provided config is the default, and if
393-
// so returns an error that indicates the user should set up their config.
394-
type defaultingClientConfig struct {
395-
nested kclientcmd.ClientConfig
396-
}
397-
398-
// RawConfig calls the nested method
399-
func (c defaultingClientConfig) RawConfig() (kclientcmdapi.Config, error) {
400-
return c.nested.RawConfig()
401-
}
402-
403-
// Namespace calls the nested method, and if an empty config error is returned
404-
// it checks for the same default as kubectl - the value of POD_NAMESPACE or
405-
// "default".
406-
func (c defaultingClientConfig) Namespace() (string, bool, error) {
407-
namespace, ok, err := c.nested.Namespace()
408-
if err == nil {
409-
return namespace, ok, nil
410-
}
411-
if !kclientcmd.IsEmptyConfig(err) {
412-
return "", false, err
413-
}
414-
415-
// This way assumes you've set the POD_NAMESPACE environment variable using the downward API.
416-
// This check has to be done first for backwards compatibility with the way InClusterConfig was originally set up
417-
if ns := os.Getenv("POD_NAMESPACE"); ns != "" {
418-
return ns, true, nil
419-
}
420-
421-
// Fall back to the namespace associated with the service account token, if available
422-
if data, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); err == nil {
423-
if ns := strings.TrimSpace(string(data)); len(ns) > 0 {
424-
return ns, true, nil
425-
}
426-
}
427-
428-
return metav1.NamespaceDefault, false, nil
429-
}
430-
431-
// ConfigAccess implements ClientConfig
432-
func (c defaultingClientConfig) ConfigAccess() kclientcmd.ConfigAccess {
433-
return c.nested.ConfigAccess()
434-
}
435-
436-
type errConfigurationMissing struct {
437-
err error
438-
}
439-
440-
func (e errConfigurationMissing) Error() string {
441-
return fmt.Sprintf("%v", e.err)
442-
}
443-
444-
func IsConfigurationMissing(err error) bool {
445-
switch err.(type) {
446-
case errConfigurationMissing:
447-
return true
448-
}
449-
return kclientcmd.IsContextNotFound(err)
450-
}
451-
452-
// ClientConfig returns a complete client config
453-
func (c defaultingClientConfig) ClientConfig() (*restclient.Config, error) {
454-
cfg, err := c.nested.ClientConfig()
455-
if err == nil {
456-
return cfg, nil
457-
}
458-
459-
if !kclientcmd.IsEmptyConfig(err) {
460-
return nil, err
461-
}
462-
463-
// TODO: need to expose inClusterConfig upstream and use that
464-
if icc, err := restclient.InClusterConfig(); err == nil {
465-
glog.V(4).Infof("Using in-cluster configuration")
466-
return icc, nil
467-
}
468-
469-
return nil, errConfigurationMissing{fmt.Errorf(`Missing or incomplete configuration info. Please login or point to an existing, complete config file:
470-
471-
1. Via the command-line flag --config
472-
2. Via the KUBECONFIG environment variable
473-
3. In your home directory as ~/.kube/config
474-
475-
To view or setup config directly use the 'config' command.`)}
476-
}
477-
478387
// computeDiscoverCacheDir takes the parentDir and the host and comes up with a "usually non-colliding" name.
479388
func computeDiscoverCacheDir(parentDir, host string) string {
480389
// strip the optional scheme from host if its there:

test/cmd/config.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ os::cmd::expect_success_and_not_text 'oc get bc' 'does not exist'
3131

3232
os::cmd::expect_failure_and_text 'env -u KUBERNETES_SERVICE_HOST oc get buildconfigs --context="test"' 'context was not found for specified context: test'
3333
os::cmd::expect_failure_and_text 'env -u KUBERNETES_SERVICE_HOST oc get buildconfigs --cluster="test"' 'no server found for cluster "test"'
34-
os::cmd::expect_failure_and_text 'env -u KUBERNETES_SERVICE_HOST oc get buildconfigs --user="test"' 'auth info "test" does not exist'
34+
# need some level of default (both upstream and here) to get the pretty auth message because you fail on namespace first.
35+
os::cmd::expect_failure_and_text 'KUBERNETES_MASTER=anything env -u KUBERNETES_SERVICE_HOST oc get buildconfigs --user="test"' 'auth info "test" does not exist'
3536

3637
os::cmd::expect_failure_and_text 'oc get bc --config=missing' 'missing: no such file or directory'
3738

test/integration/login_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88

99
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1010
"k8s.io/client-go/tools/clientcmd"
11+
kclientcmd "k8s.io/client-go/tools/clientcmd"
1112

1213
authorizationclient "github.com/openshift/origin/pkg/authorization/generated/internalclientset"
13-
"github.com/openshift/origin/pkg/client/config"
1414
"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"
1515
newproject "github.com/openshift/origin/pkg/oc/admin/project"
1616
"github.com/openshift/origin/pkg/oc/cli/cmd"
@@ -149,7 +149,7 @@ func newLoginOptions(server string, username string, password string, insecure b
149149
func defaultClientConfig(flags *pflag.FlagSet) clientcmd.ClientConfig {
150150
loadingRules := &clientcmd.ClientConfigLoadingRules{ExplicitPath: ""}
151151

152-
flags.StringVar(&loadingRules.ExplicitPath, config.OpenShiftConfigFlagName, "", "Path to the config file to use for CLI requests.")
152+
flags.StringVar(&loadingRules.ExplicitPath, kclientcmd.OpenShiftKubeConfigFlagName, "", "Path to the config file to use for CLI requests.")
153153

154154
overrides := &clientcmd.ConfigOverrides{}
155155
overrideFlags := clientcmd.RecommendedConfigOverrideFlags("")

0 commit comments

Comments
 (0)