mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-30 23:15:14 +00:00
c-go cache: Only cache discovery requests
Only cache discovery requests, and only for kubectl.
This commit is contained in:
parent
30325cd580
commit
a97a1ba14e
@ -63,6 +63,10 @@ const (
|
|||||||
FlagMatchBinaryVersion = "match-server-version"
|
FlagMatchBinaryVersion = "match-server-version"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
FlagHTTPCacheDir = "cache-dir"
|
||||||
|
)
|
||||||
|
|
||||||
// Factory provides abstractions that allow the Kubectl command to be extended across multiple types
|
// Factory provides abstractions that allow the Kubectl command to be extended across multiple types
|
||||||
// of resources and different API sets.
|
// of resources and different API sets.
|
||||||
// The rings are here for a reason. In order for composers to be able to provide alternative factory implementations
|
// The rings are here for a reason. In order for composers to be able to provide alternative factory implementations
|
||||||
@ -82,13 +86,17 @@ type Factory interface {
|
|||||||
type DiscoveryClientFactory interface {
|
type DiscoveryClientFactory interface {
|
||||||
// Returns a discovery client
|
// Returns a discovery client
|
||||||
DiscoveryClient() (discovery.CachedDiscoveryInterface, error)
|
DiscoveryClient() (discovery.CachedDiscoveryInterface, error)
|
||||||
|
|
||||||
|
// BindFlags adds any discovery flags that are common to all kubectl sub commands.
|
||||||
|
BindFlags(flags *pflag.FlagSet)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClientAccessFactory holds the first level of factory methods.
|
// ClientAccessFactory holds the first level of factory methods.
|
||||||
// Generally provides discovery, negotiation, and no-dep calls.
|
// Generally provides discovery, negotiation, and no-dep calls.
|
||||||
// TODO The polymorphic calls probably deserve their own interface.
|
// TODO The polymorphic calls probably deserve their own interface.
|
||||||
type ClientAccessFactory interface {
|
type ClientAccessFactory interface {
|
||||||
DiscoveryClientFactory
|
// Returns a discovery client
|
||||||
|
DiscoveryClient() (discovery.CachedDiscoveryInterface, error)
|
||||||
|
|
||||||
// ClientSet gives you back an internal, generated clientset
|
// ClientSet gives you back an internal, generated clientset
|
||||||
ClientSet() (internalclientset.Interface, error)
|
ClientSet() (internalclientset.Interface, error)
|
||||||
|
@ -90,6 +90,7 @@ func NewClientAccessFactoryFromDiscovery(flags *pflag.FlagSet, clientConfig clie
|
|||||||
|
|
||||||
type discoveryFactory struct {
|
type discoveryFactory struct {
|
||||||
clientConfig clientcmd.ClientConfig
|
clientConfig clientcmd.ClientConfig
|
||||||
|
cacheDir string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *discoveryFactory) DiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
|
func (f *discoveryFactory) DiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
|
||||||
@ -97,6 +98,9 @@ func (f *discoveryFactory) DiscoveryClient() (discovery.CachedDiscoveryInterface
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cfg.CacheDir = f.cacheDir
|
||||||
|
|
||||||
discoveryClient, err := discovery.NewDiscoveryClientForConfig(cfg)
|
discoveryClient, err := discovery.NewDiscoveryClientForConfig(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -105,6 +109,11 @@ func (f *discoveryFactory) DiscoveryClient() (discovery.CachedDiscoveryInterface
|
|||||||
return NewCachedDiscoveryClient(discoveryClient, cacheDir, time.Duration(10*time.Minute)), nil
|
return NewCachedDiscoveryClient(discoveryClient, cacheDir, time.Duration(10*time.Minute)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *discoveryFactory) BindFlags(flags *pflag.FlagSet) {
|
||||||
|
defaultCacheDir := filepath.Join(homedir.HomeDir(), ".kube", "http-cache")
|
||||||
|
flags.StringVar(&f.cacheDir, FlagHTTPCacheDir, defaultCacheDir, "Default HTTP cache directory")
|
||||||
|
}
|
||||||
|
|
||||||
// DefaultClientConfig creates a clientcmd.ClientConfig with the following hierarchy:
|
// DefaultClientConfig creates a clientcmd.ClientConfig with the following hierarchy:
|
||||||
// 1. Use the kubeconfig builder. The number of merges and overrides here gets a little crazy. Stay with me.
|
// 1. Use the kubeconfig builder. The number of merges and overrides here gets a little crazy. Stay with me.
|
||||||
// 1. Merge the kubeconfig itself. This is done with the following hierarchy rules:
|
// 1. Merge the kubeconfig itself. This is done with the following hierarchy rules:
|
||||||
@ -377,6 +386,8 @@ func (f *ring0Factory) BindFlags(flags *pflag.FlagSet) {
|
|||||||
// to do that automatically for every subcommand.
|
// to do that automatically for every subcommand.
|
||||||
flags.BoolVar(&f.clientCache.matchVersion, FlagMatchBinaryVersion, false, "Require server version to match client version")
|
flags.BoolVar(&f.clientCache.matchVersion, FlagMatchBinaryVersion, false, "Require server version to match client version")
|
||||||
|
|
||||||
|
f.discoveryFactory.BindFlags(flags)
|
||||||
|
|
||||||
// Normalize all flags that are coming from other packages or pre-configurations
|
// Normalize all flags that are coming from other packages or pre-configurations
|
||||||
// a.k.a. change all "_" to "-". e.g. glog package
|
// a.k.a. change all "_" to "-". e.g. glog package
|
||||||
flags.SetNormalizeFunc(utilflag.WordSepNormalizeFunc)
|
flags.SetNormalizeFunc(utilflag.WordSepNormalizeFunc)
|
||||||
|
@ -22,7 +22,6 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
@ -32,19 +31,16 @@ import (
|
|||||||
restclient "k8s.io/client-go/rest"
|
restclient "k8s.io/client-go/rest"
|
||||||
clientauth "k8s.io/client-go/tools/auth"
|
clientauth "k8s.io/client-go/tools/auth"
|
||||||
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
||||||
"k8s.io/client-go/util/homedir"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// ClusterDefaults has the same behavior as the old EnvVar and DefaultCluster fields
|
// ClusterDefaults has the same behavior as the old EnvVar and DefaultCluster fields
|
||||||
// DEPRECATED will be replaced
|
// DEPRECATED will be replaced
|
||||||
ClusterDefaults = clientcmdapi.Cluster{Server: getDefaultServer()}
|
ClusterDefaults = clientcmdapi.Cluster{Server: getDefaultServer()}
|
||||||
cacheDirDefault = filepath.Join(homedir.HomeDir(), ".kube", "http-cache")
|
|
||||||
// DefaultClientConfig represents the legacy behavior of this package for defaulting
|
// DefaultClientConfig represents the legacy behavior of this package for defaulting
|
||||||
// DEPRECATED will be replace
|
// DEPRECATED will be replace
|
||||||
DefaultClientConfig = DirectClientConfig{*clientcmdapi.NewConfig(), "", &ConfigOverrides{
|
DefaultClientConfig = DirectClientConfig{*clientcmdapi.NewConfig(), "", &ConfigOverrides{
|
||||||
ClusterDefaults: ClusterDefaults,
|
ClusterDefaults: ClusterDefaults,
|
||||||
CacheDir: cacheDirDefault,
|
|
||||||
}, nil, NewDefaultClientConfigLoadingRules(), promptedCredentials{}}
|
}, nil, NewDefaultClientConfigLoadingRules(), promptedCredentials{}}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -135,7 +131,6 @@ func (config *DirectClientConfig) ClientConfig() (*restclient.Config, error) {
|
|||||||
|
|
||||||
clientConfig := &restclient.Config{}
|
clientConfig := &restclient.Config{}
|
||||||
clientConfig.Host = configClusterInfo.Server
|
clientConfig.Host = configClusterInfo.Server
|
||||||
clientConfig.CacheDir = config.overrides.CacheDir
|
|
||||||
|
|
||||||
if len(config.overrides.Timeout) > 0 {
|
if len(config.overrides.Timeout) > 0 {
|
||||||
timeout, err := ParseTimeout(config.overrides.Timeout)
|
timeout, err := ParseTimeout(config.overrides.Timeout)
|
||||||
|
@ -17,13 +17,11 @@ limitations under the License.
|
|||||||
package clientcmd
|
package clientcmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
|
|
||||||
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
||||||
"k8s.io/client-go/util/homedir"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConfigOverrides holds values that should override whatever information is pulled from the actual Config object. You can't
|
// ConfigOverrides holds values that should override whatever information is pulled from the actual Config object. You can't
|
||||||
@ -36,7 +34,6 @@ type ConfigOverrides struct {
|
|||||||
Context clientcmdapi.Context
|
Context clientcmdapi.Context
|
||||||
CurrentContext string
|
CurrentContext string
|
||||||
Timeout string
|
Timeout string
|
||||||
CacheDir string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConfigOverrideFlags holds the flag names to be used for binding command line flags. Notice that this structure tightly
|
// ConfigOverrideFlags holds the flag names to be used for binding command line flags. Notice that this structure tightly
|
||||||
@ -47,7 +44,6 @@ type ConfigOverrideFlags struct {
|
|||||||
ContextOverrideFlags ContextOverrideFlags
|
ContextOverrideFlags ContextOverrideFlags
|
||||||
CurrentContext FlagInfo
|
CurrentContext FlagInfo
|
||||||
Timeout FlagInfo
|
Timeout FlagInfo
|
||||||
CacheDir FlagInfo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AuthOverrideFlags holds the flag names to be used for binding command line flags for AuthInfo objects
|
// AuthOverrideFlags holds the flag names to be used for binding command line flags for AuthInfo objects
|
||||||
@ -150,12 +146,10 @@ const (
|
|||||||
FlagUsername = "username"
|
FlagUsername = "username"
|
||||||
FlagPassword = "password"
|
FlagPassword = "password"
|
||||||
FlagTimeout = "request-timeout"
|
FlagTimeout = "request-timeout"
|
||||||
FlagCacheDir = "cachedir"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// RecommendedConfigOverrideFlags is a convenience method to return recommended flag names prefixed with a string of your choosing
|
// RecommendedConfigOverrideFlags is a convenience method to return recommended flag names prefixed with a string of your choosing
|
||||||
func RecommendedConfigOverrideFlags(prefix string) ConfigOverrideFlags {
|
func RecommendedConfigOverrideFlags(prefix string) ConfigOverrideFlags {
|
||||||
defaultCacheDir := filepath.Join(homedir.HomeDir(), ".kube", "http-cache")
|
|
||||||
return ConfigOverrideFlags{
|
return ConfigOverrideFlags{
|
||||||
AuthOverrideFlags: RecommendedAuthOverrideFlags(prefix),
|
AuthOverrideFlags: RecommendedAuthOverrideFlags(prefix),
|
||||||
ClusterOverrideFlags: RecommendedClusterOverrideFlags(prefix),
|
ClusterOverrideFlags: RecommendedClusterOverrideFlags(prefix),
|
||||||
@ -163,7 +157,6 @@ func RecommendedConfigOverrideFlags(prefix string) ConfigOverrideFlags {
|
|||||||
|
|
||||||
CurrentContext: FlagInfo{prefix + FlagContext, "", "", "The name of the kubeconfig context to use"},
|
CurrentContext: FlagInfo{prefix + FlagContext, "", "", "The name of the kubeconfig context to use"},
|
||||||
Timeout: FlagInfo{prefix + FlagTimeout, "", "0", "The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests."},
|
Timeout: FlagInfo{prefix + FlagTimeout, "", "0", "The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests."},
|
||||||
CacheDir: FlagInfo{prefix + FlagCacheDir, "", defaultCacheDir, "Path to http-cache directory"},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,7 +198,6 @@ func BindOverrideFlags(overrides *ConfigOverrides, flags *pflag.FlagSet, flagNam
|
|||||||
BindContextFlags(&overrides.Context, flags, flagNames.ContextOverrideFlags)
|
BindContextFlags(&overrides.Context, flags, flagNames.ContextOverrideFlags)
|
||||||
flagNames.CurrentContext.BindStringFlag(flags, &overrides.CurrentContext)
|
flagNames.CurrentContext.BindStringFlag(flags, &overrides.CurrentContext)
|
||||||
flagNames.Timeout.BindStringFlag(flags, &overrides.Timeout)
|
flagNames.Timeout.BindStringFlag(flags, &overrides.Timeout)
|
||||||
flagNames.CacheDir.BindStringFlag(flags, &overrides.CacheDir)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// BindAuthInfoFlags is a convenience method to bind the specified flags to their associated variables
|
// BindAuthInfoFlags is a convenience method to bind the specified flags to their associated variables
|
||||||
|
Loading…
Reference in New Issue
Block a user