mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 02:41:25 +00:00
Merge pull request #71117 from dixudx/read_kubeconfig_once
loads kubeconfig only once
This commit is contained in:
commit
9d58c8fbcf
@ -423,7 +423,7 @@ func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command {
|
|||||||
|
|
||||||
addProfilingFlags(flags)
|
addProfilingFlags(flags)
|
||||||
|
|
||||||
kubeConfigFlags := genericclioptions.NewConfigFlags()
|
kubeConfigFlags := genericclioptions.NewConfigFlags(true)
|
||||||
kubeConfigFlags.AddFlags(flags)
|
kubeConfigFlags.AddFlags(flags)
|
||||||
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags)
|
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags)
|
||||||
matchVersionKubeConfigFlags.AddFlags(cmds.PersistentFlags())
|
matchVersionKubeConfigFlags.AddFlags(cmds.PersistentFlags())
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
@ -92,6 +93,13 @@ type ConfigFlags struct {
|
|||||||
Username *string
|
Username *string
|
||||||
Password *string
|
Password *string
|
||||||
Timeout *string
|
Timeout *string
|
||||||
|
|
||||||
|
clientConfig clientcmd.ClientConfig
|
||||||
|
lock sync.Mutex
|
||||||
|
// If set to true, will use persistent client config and
|
||||||
|
// propagate the config to the places that need it, rather than
|
||||||
|
// loading the config multiple times
|
||||||
|
usePersistentConfig bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToRESTConfig implements RESTClientGetter.
|
// ToRESTConfig implements RESTClientGetter.
|
||||||
@ -106,6 +114,13 @@ func (f *ConfigFlags) ToRESTConfig() (*rest.Config, error) {
|
|||||||
// Returns an interactive clientConfig if the password flag is enabled,
|
// Returns an interactive clientConfig if the password flag is enabled,
|
||||||
// or a non-interactive clientConfig otherwise.
|
// or a non-interactive clientConfig otherwise.
|
||||||
func (f *ConfigFlags) ToRawKubeConfigLoader() clientcmd.ClientConfig {
|
func (f *ConfigFlags) ToRawKubeConfigLoader() clientcmd.ClientConfig {
|
||||||
|
if f.usePersistentConfig {
|
||||||
|
return f.toRawKubePersistentConfigLoader()
|
||||||
|
}
|
||||||
|
return f.toRawKubeConfigLoader()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *ConfigFlags) toRawKubeConfigLoader() clientcmd.ClientConfig {
|
||||||
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
|
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
|
||||||
// use the standard defaults for this client command
|
// use the standard defaults for this client command
|
||||||
// DEPRECATED: remove and replace with something more accurate
|
// DEPRECATED: remove and replace with something more accurate
|
||||||
@ -181,6 +196,19 @@ func (f *ConfigFlags) ToRawKubeConfigLoader() clientcmd.ClientConfig {
|
|||||||
return clientConfig
|
return clientConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// toRawKubePersistentConfigLoader binds config flag values to config overrides
|
||||||
|
// Returns a persistent clientConfig for propagation.
|
||||||
|
func (f *ConfigFlags) toRawKubePersistentConfigLoader() clientcmd.ClientConfig {
|
||||||
|
f.lock.Lock()
|
||||||
|
defer f.lock.Unlock()
|
||||||
|
|
||||||
|
if f.clientConfig == nil {
|
||||||
|
f.clientConfig = f.toRawKubeConfigLoader()
|
||||||
|
}
|
||||||
|
|
||||||
|
return f.clientConfig
|
||||||
|
}
|
||||||
|
|
||||||
// ToDiscoveryClient implements RESTClientGetter.
|
// ToDiscoveryClient implements RESTClientGetter.
|
||||||
// Expects the AddFlags method to have been called.
|
// Expects the AddFlags method to have been called.
|
||||||
// Returns a CachedDiscoveryInterface using a computed RESTConfig.
|
// Returns a CachedDiscoveryInterface using a computed RESTConfig.
|
||||||
@ -285,7 +313,7 @@ func (f *ConfigFlags) WithDeprecatedPasswordFlag() *ConfigFlags {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewConfigFlags returns ConfigFlags with default values set
|
// NewConfigFlags returns ConfigFlags with default values set
|
||||||
func NewConfigFlags() *ConfigFlags {
|
func NewConfigFlags(usePersistentConfig bool) *ConfigFlags {
|
||||||
impersonateGroup := []string{}
|
impersonateGroup := []string{}
|
||||||
insecure := false
|
insecure := false
|
||||||
|
|
||||||
@ -306,6 +334,8 @@ func NewConfigFlags() *ConfigFlags {
|
|||||||
BearerToken: stringptr(""),
|
BearerToken: stringptr(""),
|
||||||
Impersonate: stringptr(""),
|
Impersonate: stringptr(""),
|
||||||
ImpersonateGroup: &impersonateGroup,
|
ImpersonateGroup: &impersonateGroup,
|
||||||
|
|
||||||
|
usePersistentConfig: usePersistentConfig,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ type NamespaceOptions struct {
|
|||||||
// NewNamespaceOptions provides an instance of NamespaceOptions with default values
|
// NewNamespaceOptions provides an instance of NamespaceOptions with default values
|
||||||
func NewNamespaceOptions(streams genericclioptions.IOStreams) *NamespaceOptions {
|
func NewNamespaceOptions(streams genericclioptions.IOStreams) *NamespaceOptions {
|
||||||
return &NamespaceOptions{
|
return &NamespaceOptions{
|
||||||
configFlags: genericclioptions.NewConfigFlags(),
|
configFlags: genericclioptions.NewConfigFlags(true),
|
||||||
|
|
||||||
IOStreams: streams,
|
IOStreams: streams,
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user