mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 12:15:52 +00:00
Merge pull request #90845 from soltysh/config_flags
Provide more verbose empty config error for all config load actions
This commit is contained in:
commit
c179d51979
@ -5,6 +5,7 @@ go_library(
|
|||||||
srcs = [
|
srcs = [
|
||||||
"builder_flags.go",
|
"builder_flags.go",
|
||||||
"builder_flags_fake.go",
|
"builder_flags_fake.go",
|
||||||
|
"client_config.go",
|
||||||
"config_flags.go",
|
"config_flags.go",
|
||||||
"config_flags_fake.go",
|
"config_flags_fake.go",
|
||||||
"doc.go",
|
"doc.go",
|
||||||
|
@ -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()
|
||||||
|
}
|
@ -55,16 +55,7 @@ const (
|
|||||||
flagHTTPCacheDir = "cache-dir"
|
flagHTTPCacheDir = "cache-dir"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "http-cache")
|
||||||
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.`)
|
|
||||||
)
|
|
||||||
|
|
||||||
// RESTClientGetter is an interface that the ConfigFlags describe to provide an easier way to mock for commands
|
// 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
|
// 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.
|
// to a .kubeconfig file, loading rules, and config flag overrides.
|
||||||
// Expects the AddFlags method to have been called.
|
// Expects the AddFlags method to have been called.
|
||||||
func (f *ConfigFlags) ToRESTConfig() (*rest.Config, error) {
|
func (f *ConfigFlags) ToRESTConfig() (*rest.Config, error) {
|
||||||
config, err := f.ToRawKubeConfigLoader().ClientConfig()
|
return 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToRawKubeConfigLoader binds config flag values to config overrides
|
// ToRawKubeConfigLoader binds config flag values to config overrides
|
||||||
@ -204,16 +190,11 @@ func (f *ConfigFlags) toRawKubeConfigLoader() clientcmd.ClientConfig {
|
|||||||
overrides.Timeout = *f.Timeout
|
overrides.Timeout = *f.Timeout
|
||||||
}
|
}
|
||||||
|
|
||||||
var clientConfig clientcmd.ClientConfig
|
|
||||||
|
|
||||||
// we only have an interactive prompt when a password is allowed
|
// we only have an interactive prompt when a password is allowed
|
||||||
if f.Password == nil {
|
if f.Password == nil {
|
||||||
clientConfig = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, overrides)
|
return &clientConfig{clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, overrides)}
|
||||||
} else {
|
|
||||||
clientConfig = clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, overrides, os.Stdin)
|
|
||||||
}
|
}
|
||||||
|
return &clientConfig{clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, overrides, os.Stdin)}
|
||||||
return clientConfig
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// toRawKubePersistentConfigLoader binds config flag values to config overrides
|
// toRawKubePersistentConfigLoader binds config flag values to config overrides
|
||||||
|
Loading…
Reference in New Issue
Block a user