Merge pull request #90845 from soltysh/config_flags

Provide more verbose empty config error for all config load actions
This commit is contained in:
Kubernetes Prow Robot 2020-05-08 05:24:18 -07:00 committed by GitHub
commit c179d51979
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 23 deletions

View File

@ -5,6 +5,7 @@ go_library(
srcs = [
"builder_flags.go",
"builder_flags_fake.go",
"client_config.go",
"config_flags.go",
"config_flags_fake.go",
"doc.go",

View File

@ -0,0 +1,71 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package genericclioptions
import (
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
var (
ErrEmptyConfig = clientcmd.NewEmptyConfigError(`Missing or incomplete configuration info. Please point to an existing, complete config file:
1. Via the command-line flag --kubeconfig
2. Via the KUBECONFIG environment variable
3. In your home directory as ~/.kube/config
To view or setup config directly use the 'config' command.`)
)
var _ = clientcmd.ClientConfig(&clientConfig{})
type clientConfig struct {
defaultClientConfig clientcmd.ClientConfig
}
func (c *clientConfig) RawConfig() (clientcmdapi.Config, error) {
config, err := c.defaultClientConfig.RawConfig()
// replace client-go's ErrEmptyConfig error with our custom, more verbose version
if clientcmd.IsEmptyConfig(err) {
return config, ErrEmptyConfig
}
return config, err
}
func (c *clientConfig) ClientConfig() (*restclient.Config, error) {
config, err := c.defaultClientConfig.ClientConfig()
// replace client-go's ErrEmptyConfig error with our custom, more verbose version
if clientcmd.IsEmptyConfig(err) {
return config, ErrEmptyConfig
}
return config, err
}
func (c *clientConfig) Namespace() (string, bool, error) {
namespace, ok, err := c.defaultClientConfig.Namespace()
// replace client-go's ErrEmptyConfig error with our custom, more verbose version
if clientcmd.IsEmptyConfig(err) {
return namespace, ok, ErrEmptyConfig
}
return namespace, ok, err
}
func (c *clientConfig) ConfigAccess() clientcmd.ConfigAccess {
return c.defaultClientConfig.ConfigAccess()
}

View File

@ -55,16 +55,7 @@ const (
flagHTTPCacheDir = "cache-dir"
)
var (
defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "http-cache")
ErrEmptyConfig = clientcmd.NewEmptyConfigError(`Missing or incomplete configuration info. Please point to an existing, complete config file:
1. Via the command-line flag --kubeconfig
2. Via the KUBECONFIG environment variable
3. In your home directory as ~/.kube/config
To view or setup config directly use the 'config' command.`)
)
var defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "http-cache")
// RESTClientGetter is an interface that the ConfigFlags describe to provide an easier way to mock for commands
// and eliminate the direct coupling to a struct type. Users may wish to duplicate this type in their own packages
@ -119,12 +110,7 @@ type ConfigFlags struct {
// to a .kubeconfig file, loading rules, and config flag overrides.
// Expects the AddFlags method to have been called.
func (f *ConfigFlags) ToRESTConfig() (*rest.Config, error) {
config, err := f.ToRawKubeConfigLoader().ClientConfig()
// replace client-go's ErrEmptyConfig error with our custom, more verbose version
if clientcmd.IsEmptyConfig(err) {
return nil, ErrEmptyConfig
}
return config, err
return f.ToRawKubeConfigLoader().ClientConfig()
}
// ToRawKubeConfigLoader binds config flag values to config overrides
@ -204,16 +190,11 @@ func (f *ConfigFlags) toRawKubeConfigLoader() clientcmd.ClientConfig {
overrides.Timeout = *f.Timeout
}
var clientConfig clientcmd.ClientConfig
// we only have an interactive prompt when a password is allowed
if f.Password == nil {
clientConfig = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, overrides)
} else {
clientConfig = clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, overrides, os.Stdin)
return &clientConfig{clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, overrides)}
}
return clientConfig
return &clientConfig{clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, overrides, os.Stdin)}
}
// toRawKubePersistentConfigLoader binds config flag values to config overrides