Skip to content

Commit f7d9853

Browse files
Merge pull request #19335 from deads2k/cli-25-configpatch
use the upstream construction for kubeconfig evaluation
2 parents 39cd4a0 + 156c979 commit f7d9853

File tree

23 files changed

+1723
-4561
lines changed

23 files changed

+1723
-4561
lines changed

contrib/completions/bash/oc

+825-2,162
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contrib/completions/zsh/oc

+825-2,162
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hack/import-restrictions.json

-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,6 @@
461461
"github.com/openshift/origin/pkg/build/registry/buildconfig",
462462
"github.com/openshift/origin/pkg/build/util",
463463
"github.com/openshift/origin/pkg/bulk",
464-
"github.com/openshift/origin/pkg/client/cmd",
465464
"github.com/openshift/origin/pkg/client/config",
466465
"github.com/openshift/origin/pkg/cmd/flagtypes",
467466
"github.com/openshift/origin/pkg/cmd/server/admin",

pkg/client/config/loader.go

-55
This file was deleted.

pkg/client/cmd/clientcmd.go pkg/cmd/infra/router/clientcmd.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmd
1+
package router
22

33
import (
44
"fmt"
@@ -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
)
@@ -64,13 +63,6 @@ func (cfg *Config) Bind(flags *pflag.FlagSet) {
6463
}
6564
}
6665

67-
// BindToFile is used when this config will not be bound to flags, but should load the config file
68-
// from disk if available.
69-
func (cfg *Config) BindToFile() *Config {
70-
cfg.clientConfig = DefaultClientConfig(pflag.NewFlagSet("empty", pflag.ContinueOnError))
71-
return cfg
72-
}
73-
7466
// OpenShiftConfig returns the OpenShift configuration
7567
func (cfg *Config) OpenShiftConfig() *restclient.Config {
7668
err := cfg.bindEnv()
@@ -205,9 +197,9 @@ func (cfg *Config) bindEnv() error {
205197
}
206198

207199
func DefaultClientConfig(flags *pflag.FlagSet) kclientcmd.ClientConfig {
208-
loadingRules := config.NewOpenShiftClientConfigLoadingRules()
209-
flags.StringVar(&loadingRules.ExplicitPath, config.OpenShiftConfigFlagName, "", "Path to the config file to use for CLI requests.")
210-
cobra.MarkFlagFilename(flags, config.OpenShiftConfigFlagName)
200+
loadingRules := kclientcmd.NewDefaultClientConfigLoadingRules()
201+
flags.StringVar(&loadingRules.ExplicitPath, kclientcmd.OpenShiftKubeConfigFlagName, "", "Path to the config file to use for CLI requests.")
202+
cobra.MarkFlagFilename(flags, kclientcmd.OpenShiftKubeConfigFlagName)
211203

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

pkg/cmd/infra/router/f5.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
1515
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
1616

17-
clientcmd "github.com/openshift/origin/pkg/client/cmd"
1817
"github.com/openshift/origin/pkg/cmd/util"
1918
cmdversion "github.com/openshift/origin/pkg/cmd/version"
2019
projectinternalclientset "github.com/openshift/origin/pkg/project/generated/internalclientset"
@@ -42,7 +41,7 @@ var (
4241
// F5RouterOptions represent the complete structure needed to start an F5 router
4342
// sync process.
4443
type F5RouterOptions struct {
45-
Config *clientcmd.Config
44+
Config *Config
4645

4746
F5Router
4847
RouterSelection
@@ -138,12 +137,12 @@ func (o *F5Router) Validate() error {
138137
// NewCommandF5Router provides CLI handler for the F5 router sync plugin.
139138
func NewCommandF5Router(name string) *cobra.Command {
140139
options := &F5RouterOptions{
141-
Config: clientcmd.NewConfig(),
140+
Config: NewConfig(),
142141
}
143142
options.Config.FromFile = true
144143

145144
cmd := &cobra.Command{
146-
Use: fmt.Sprintf("%s%s", name, clientcmd.ConfigSyntax),
145+
Use: fmt.Sprintf("%s%s", name, ConfigSyntax),
147146
Short: "Start an F5 route synchronizer",
148147
Long: f5Long,
149148
Run: func(c *cobra.Command, args []string) {

pkg/cmd/infra/router/template.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
2828
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
2929

30-
clientcmd "github.com/openshift/origin/pkg/client/cmd"
3130
"github.com/openshift/origin/pkg/cmd/server/crypto"
3231
"github.com/openshift/origin/pkg/cmd/util"
3332
cmdversion "github.com/openshift/origin/pkg/cmd/version"
@@ -63,7 +62,7 @@ var routerLong = templates.LongDesc(`
6362
that you must have a cluster-wide administrative role to view all namespaces.`)
6463

6564
type TemplateRouterOptions struct {
66-
Config *clientcmd.Config
65+
Config *Config
6766

6867
TemplateRouter
6968
RouterStats
@@ -138,12 +137,12 @@ func (o *RouterStats) Bind(flag *pflag.FlagSet) {
138137
// NewCommndTemplateRouter provides CLI handler for the template router backend
139138
func NewCommandTemplateRouter(name string) *cobra.Command {
140139
options := &TemplateRouterOptions{
141-
Config: clientcmd.NewConfig(),
140+
Config: NewConfig(),
142141
}
143142
options.Config.FromFile = true
144143

145144
cmd := &cobra.Command{
146-
Use: fmt.Sprintf("%s%s", name, clientcmd.ConfigSyntax),
145+
Use: fmt.Sprintf("%s%s", name, ConfigSyntax),
147146
Short: "Start a router",
148147
Long: routerLong,
149148
Run: func(c *cobra.Command, args []string) {

pkg/oc/admin/diagnostics/config.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"os"
88

99
"k8s.io/client-go/tools/clientcmd"
10+
kclientcmd "k8s.io/client-go/tools/clientcmd"
1011
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
1112

12-
"github.com/openshift/origin/pkg/client/config"
1313
"github.com/openshift/origin/pkg/oc/admin/diagnostics/diagnostics/util"
1414
)
1515

@@ -32,12 +32,12 @@ func (o DiagnosticsOptions) detectClientConfig() (expected bool, detected bool)
3232
return false, false
3333
}
3434
o.Logger().Notice("CED2011", "Determining if client configuration exists for client/cluster diagnostics")
35-
confFlagName := config.OpenShiftConfigFlagName
35+
confFlagName := kclientcmd.OpenShiftKubeConfigFlagName
3636
confFlagValue := o.ClientFlags.Lookup(confFlagName).Value.String()
3737
successfulLoad := false
3838

3939
var foundPath string
40-
rules := config.NewOpenShiftClientConfigLoadingRules()
40+
rules := kclientcmd.NewDefaultClientConfigLoadingRules()
4141
paths := append([]string{confFlagValue}, rules.Precedence...)
4242
for index, path := range paths {
4343
errmsg := ""
@@ -47,8 +47,8 @@ func (o DiagnosticsOptions) detectClientConfig() (expected bool, detected bool)
4747
case len(paths) - 1: // config in ~/.kube
4848
// no error message indicated if it is not there... user didn't say it would be
4949
default: // can be multiple paths from the env var in theory; all cases should go here
50-
if len(os.Getenv(config.OpenShiftConfigPathEnvVar)) != 0 {
51-
errmsg = fmt.Sprintf("Env var %s specified that client config could be at %s\n", config.OpenShiftConfigPathEnvVar, path)
50+
if len(os.Getenv(kclientcmd.RecommendedConfigPathEnvVar)) != 0 {
51+
errmsg = fmt.Sprintf("Env var %s specified that client config could be at %s\n", kclientcmd.RecommendedConfigPathEnvVar, path)
5252
}
5353
}
5454

@@ -88,7 +88,7 @@ location for use by the client and diagnostics.
8888
for _, path := range util.AdminKubeConfigPaths {
8989
msg := fmt.Sprintf("Looking for a possible client config at %s\n", path)
9090
if o.canOpenConfigFile(path, msg) {
91-
o.Logger().Warn("DCli1003", fmt.Sprintf(adminWarningF, config.OpenShiftConfigPathEnvVar, path, config.RecommendedHomeFile))
91+
o.Logger().Warn("DCli1003", fmt.Sprintf(adminWarningF, kclientcmd.RecommendedConfigPathEnvVar, path, kclientcmd.RecommendedHomeFile))
9292
break
9393
}
9494
}

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/openshift/helper.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
"github.com/golang/glog"
1111

1212
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+
kclientcmd "k8s.io/client-go/tools/clientcmd"
1314
"k8s.io/client-go/util/homedir"
1415
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
1516

16-
clientconfig "github.com/openshift/origin/pkg/client/config"
1717
configapi "github.com/openshift/origin/pkg/cmd/server/apis/config"
1818
_ "github.com/openshift/origin/pkg/cmd/server/apis/config/install"
1919
configapilatest "github.com/openshift/origin/pkg/cmd/server/apis/config/latest"
@@ -38,7 +38,7 @@ var (
3838
BasePorts = []int{4001, 7001, 8443, 10250, DefaultDNSPort}
3939
RouterPorts = []int{80, 443}
4040
AllPorts = append(RouterPorts, BasePorts...)
41-
SocatPidFile = filepath.Join(homedir.HomeDir(), clientconfig.OpenShiftConfigHomeDir, "socat-8443.pid")
41+
SocatPidFile = filepath.Join(homedir.HomeDir(), kclientcmd.RecommendedHomeDir, "socat-8443.pid")
4242
)
4343

4444
// Helper contains methods and utilities to help with OpenShift startup

pkg/oc/bootstrap/docker/openshift/project.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func setCurrentProject(f *clientcmd.Factory, name string, out io.Writer) error {
5353
}
5454

5555
func LoggedInUserFactory() (*clientcmd.Factory, error) {
56-
cfg, err := config.NewOpenShiftClientConfigLoadingRules().Load()
56+
cfg, err := kclientcmd.NewDefaultClientConfigLoadingRules().Load()
5757
if err != nil {
5858
return nil, err
5959
}

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/process.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,10 @@ func RunProcess(f *clientcmd.Factory, in io.Reader, out, errout io.Writer, cmd *
162162
}
163163
}
164164

165+
// the namespace
165166
namespace, explicit, err := f.DefaultNamespace()
166-
if err != nil {
167+
// we only need to fail on namespace acquisition if we're actually taking action. Otherwise the namespace can be enforced later
168+
if err != nil && !local {
167169
return err
168170
}
169171

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"flag"
66
"fmt"
77
"io"
8+
"path"
89
"strings"
910

1011
"github.com/spf13/cobra"
@@ -19,7 +20,6 @@ import (
1920
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
2021
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
2122

22-
cmdconfig "github.com/openshift/origin/pkg/client/config"
2323
cmdutil "github.com/openshift/origin/pkg/cmd/util"
2424
"github.com/openshift/origin/pkg/oc/cli/cmd/create"
2525
"github.com/openshift/origin/pkg/oc/cli/describe"
@@ -735,13 +735,13 @@ var (
735735
// NewCmdConfig is a wrapper for the Kubernetes cli config command
736736
func NewCmdConfig(parentName, name string, f *clientcmd.Factory, out, errOut io.Writer) *cobra.Command {
737737
pathOptions := &kclientcmd.PathOptions{
738-
GlobalFile: cmdconfig.RecommendedHomeFile,
739-
EnvVar: cmdconfig.OpenShiftConfigPathEnvVar,
740-
ExplicitFileFlag: cmdconfig.OpenShiftConfigFlagName,
738+
GlobalFile: kclientcmd.RecommendedHomeFile,
739+
EnvVar: kclientcmd.RecommendedConfigPathEnvVar,
740+
ExplicitFileFlag: kclientcmd.OpenShiftKubeConfigFlagName,
741741

742-
GlobalFileSubpath: cmdconfig.OpenShiftConfigHomeDirFileName,
742+
GlobalFileSubpath: path.Join(kclientcmd.RecommendedHomeDir, kclientcmd.RecommendedFileName),
743743

744-
LoadingRules: cmdconfig.NewOpenShiftClientConfigLoadingRules(),
744+
LoadingRules: kclientcmd.NewDefaultClientConfigLoadingRules(),
745745
}
746746
pathOptions.LoadingRules.DoNotResolvePaths = true
747747

pkg/oc/cli/config/loader.go

+4-29
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,22 @@
11
package config
22

33
import (
4-
"os"
5-
"path/filepath"
6-
74
"github.com/spf13/cobra"
85

9-
"k8s.io/client-go/tools/clientcmd"
106
kclientcmd "k8s.io/client-go/tools/clientcmd"
117
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
12-
13-
"github.com/openshift/origin/pkg/client/config"
148
)
159

16-
// NewOpenShiftClientConfigLoadingRules returns file priority loading rules for OpenShift.
17-
// 1. --config value
18-
// 2. if KUBECONFIG env var has a value, use it. Otherwise, ~/.kube/config file
19-
func NewOpenShiftClientConfigLoadingRules() *clientcmd.ClientConfigLoadingRules {
20-
chain := []string{}
21-
22-
envVarFile := os.Getenv(config.OpenShiftConfigPathEnvVar)
23-
if len(envVarFile) != 0 {
24-
chain = append(chain, filepath.SplitList(envVarFile)...)
25-
} else {
26-
chain = append(chain, config.RecommendedHomeFile)
27-
}
28-
29-
return &clientcmd.ClientConfigLoadingRules{
30-
Precedence: chain,
31-
MigrationRules: config.CurrentMigrationRules(),
32-
}
33-
}
34-
3510
func NewPathOptions(cmd *cobra.Command) *kclientcmd.PathOptions {
36-
return NewPathOptionsWithConfig(kcmdutil.GetFlagString(cmd, config.OpenShiftConfigFlagName))
11+
return NewPathOptionsWithConfig(kcmdutil.GetFlagString(cmd, kclientcmd.OpenShiftKubeConfigFlagName))
3712
}
3813

3914
func NewPathOptionsWithConfig(configPath string) *kclientcmd.PathOptions {
4015
return &kclientcmd.PathOptions{
41-
GlobalFile: config.RecommendedHomeFile,
16+
GlobalFile: kclientcmd.RecommendedHomeFile,
4217

43-
EnvVar: config.OpenShiftConfigPathEnvVar,
44-
ExplicitFileFlag: config.OpenShiftConfigFlagName,
18+
EnvVar: kclientcmd.RecommendedConfigPathEnvVar,
19+
ExplicitFileFlag: kclientcmd.OpenShiftKubeConfigFlagName,
4520

4621
LoadingRules: &kclientcmd.ClientConfigLoadingRules{
4722
ExplicitPath: configPath,

0 commit comments

Comments
 (0)