Upgrade k8s package verison (#5358)

* upgrade k8s package version

Signed-off-by: hongzhouzi <hongzhouzi@kubesphere.io>

* Script upgrade and code formatting.

Signed-off-by: hongzhouzi <hongzhouzi@kubesphere.io>

Signed-off-by: hongzhouzi <hongzhouzi@kubesphere.io>
This commit is contained in:
hongzhouzi
2022-11-15 14:56:38 +08:00
committed by GitHub
parent 5f91c1663a
commit 44167aa47a
3106 changed files with 321340 additions and 172080 deletions

View File

@@ -33,6 +33,7 @@ import (
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
utilpointer "k8s.io/utils/pointer"
)
const (
@@ -48,6 +49,7 @@ const (
flagCAFile = "certificate-authority"
flagBearerToken = "token"
flagImpersonate = "as"
flagImpersonateUID = "as-uid"
flagImpersonateGroup = "as-group"
flagUsername = "username"
flagPassword = "password"
@@ -55,10 +57,6 @@ const (
flagCacheDir = "cache-dir"
)
var (
defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "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
// as per the golang type overlapping.
@@ -94,6 +92,7 @@ type ConfigFlags struct {
CAFile *string
BearerToken *string
Impersonate *string
ImpersonateUID *string
ImpersonateGroup *[]string
Username *string
Password *string
@@ -102,15 +101,25 @@ type ConfigFlags struct {
// before it is returned in ToRESTConfig function.
WrapConfigFn func(*rest.Config) *rest.Config
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
clientConfig clientcmd.ClientConfig
clientConfigLock sync.Mutex
restMapper meta.RESTMapper
restMapperLock sync.Mutex
discoveryClient discovery.CachedDiscoveryInterface
discoveryClientLock sync.Mutex
// If set to true, will use persistent client config, rest mapper, discovery client, and
// propagate them to the places that need them, rather than
// instantiating them multiple times.
usePersistentConfig bool
// Allows increasing burst used for discovery, this is useful
// in clusters with many registered resources
discoveryBurst int
// Allows increasing qps used for discovery, this is useful
// in clusters with many registered resources
discoveryQPS float32
}
// ToRESTConfig implements RESTClientGetter.
@@ -164,6 +173,9 @@ func (f *ConfigFlags) toRawKubeConfigLoader() clientcmd.ClientConfig {
if f.Impersonate != nil {
overrides.AuthInfo.Impersonate = *f.Impersonate
}
if f.ImpersonateUID != nil {
overrides.AuthInfo.ImpersonateUID = *f.ImpersonateUID
}
if f.ImpersonateGroup != nil {
overrides.AuthInfo.ImpersonateGroups = *f.ImpersonateGroup
}
@@ -216,8 +228,8 @@ func (f *ConfigFlags) toRawKubeConfigLoader() clientcmd.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()
f.clientConfigLock.Lock()
defer f.clientConfigLock.Unlock()
if f.clientConfig == nil {
f.clientConfig = f.toRawKubeConfigLoader()
@@ -230,31 +242,85 @@ func (f *ConfigFlags) toRawKubePersistentConfigLoader() clientcmd.ClientConfig {
// Expects the AddFlags method to have been called.
// Returns a CachedDiscoveryInterface using a computed RESTConfig.
func (f *ConfigFlags) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
if f.usePersistentConfig {
return f.toPersistentDiscoveryClient()
}
return f.toDiscoveryClient()
}
func (f *ConfigFlags) toPersistentDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
f.discoveryClientLock.Lock()
defer f.discoveryClientLock.Unlock()
if f.discoveryClient == nil {
discoveryClient, err := f.toDiscoveryClient()
if err != nil {
return nil, err
}
f.discoveryClient = discoveryClient
}
return f.discoveryClient, nil
}
func (f *ConfigFlags) toDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
config, err := f.ToRESTConfig()
if err != nil {
return nil, err
}
// The more groups you have, the more discovery requests you need to make.
// given 25 groups (our groups + a few custom resources) with one-ish version each, discovery needs to make 50 requests
// double it just so we don't end up here again for a while. This config is only used for discovery.
config.Burst = f.discoveryBurst
config.QPS = f.discoveryQPS
cacheDir := defaultCacheDir
cacheDir := getDefaultCacheDir()
// retrieve a user-provided value for the "cache-dir"
// override httpCacheDir and discoveryCacheDir if user-value is given.
if f.CacheDir != nil {
// user-provided value has higher precedence than default
// and KUBECACHEDIR environment variable.
if f.CacheDir != nil && *f.CacheDir != "" && *f.CacheDir != getDefaultCacheDir() {
cacheDir = *f.CacheDir
}
httpCacheDir := filepath.Join(cacheDir, "http")
discoveryCacheDir := computeDiscoverCacheDir(filepath.Join(cacheDir, "discovery"), config.Host)
return diskcached.NewCachedDiscoveryClientForConfig(config, discoveryCacheDir, httpCacheDir, time.Duration(10*time.Minute))
return diskcached.NewCachedDiscoveryClientForConfig(config, discoveryCacheDir, httpCacheDir, time.Duration(6*time.Hour))
}
// getDefaultCacheDir returns default caching directory path.
// it first looks at KUBECACHEDIR env var if it is set, otherwise
// it returns standard kube cache dir.
func getDefaultCacheDir() string {
if kcd := os.Getenv("KUBECACHEDIR"); kcd != "" {
return kcd
}
return filepath.Join(homedir.HomeDir(), ".kube", "cache")
}
// ToRESTMapper returns a mapper.
func (f *ConfigFlags) ToRESTMapper() (meta.RESTMapper, error) {
if f.usePersistentConfig {
return f.toPersistentRESTMapper()
}
return f.toRESTMapper()
}
func (f *ConfigFlags) toPersistentRESTMapper() (meta.RESTMapper, error) {
f.restMapperLock.Lock()
defer f.restMapperLock.Unlock()
if f.restMapper == nil {
restMapper, err := f.toRESTMapper()
if err != nil {
return nil, err
}
f.restMapper = restMapper
}
return f.restMapper, nil
}
func (f *ConfigFlags) toRESTMapper() (meta.RESTMapper, error) {
discoveryClient, err := f.ToDiscoveryClient()
if err != nil {
return nil, err
@@ -285,7 +351,10 @@ func (f *ConfigFlags) AddFlags(flags *pflag.FlagSet) {
flags.StringVar(f.BearerToken, flagBearerToken, *f.BearerToken, "Bearer token for authentication to the API server")
}
if f.Impersonate != nil {
flags.StringVar(f.Impersonate, flagImpersonate, *f.Impersonate, "Username to impersonate for the operation")
flags.StringVar(f.Impersonate, flagImpersonate, *f.Impersonate, "Username to impersonate for the operation. User could be a regular user or a service account in a namespace.")
}
if f.ImpersonateUID != nil {
flags.StringVar(f.ImpersonateUID, flagImpersonateUID, *f.ImpersonateUID, "UID to impersonate for the operation.")
}
if f.ImpersonateGroup != nil {
flags.StringArrayVar(f.ImpersonateGroup, flagImpersonateGroup, *f.ImpersonateGroup, "Group to impersonate for the operation, this flag can be repeated to specify multiple groups.")
@@ -324,13 +393,12 @@ func (f *ConfigFlags) AddFlags(flags *pflag.FlagSet) {
if f.Timeout != nil {
flags.StringVar(f.Timeout, flagTimeout, *f.Timeout, "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.")
}
}
// WithDeprecatedPasswordFlag enables the username and password config flags
func (f *ConfigFlags) WithDeprecatedPasswordFlag() *ConfigFlags {
f.Username = stringptr("")
f.Password = stringptr("")
f.Username = utilpointer.String("")
f.Password = utilpointer.String("")
return f
}
@@ -340,6 +408,18 @@ func (f *ConfigFlags) WithDiscoveryBurst(discoveryBurst int) *ConfigFlags {
return f
}
// WithDiscoveryQPS sets the RESTClient QPS for discovery.
func (f *ConfigFlags) WithDiscoveryQPS(discoveryQPS float32) *ConfigFlags {
f.discoveryQPS = discoveryQPS
return f
}
// WithWrapConfigFn allows providing a wrapper function for the client Config.
func (f *ConfigFlags) WithWrapConfigFn(wrapConfigFn func(*rest.Config) *rest.Config) *ConfigFlags {
f.WrapConfigFn = wrapConfigFn
return f
}
// NewConfigFlags returns ConfigFlags with default values set
func NewConfigFlags(usePersistentConfig bool) *ConfigFlags {
impersonateGroup := []string{}
@@ -347,37 +427,34 @@ func NewConfigFlags(usePersistentConfig bool) *ConfigFlags {
return &ConfigFlags{
Insecure: &insecure,
Timeout: stringptr("0"),
KubeConfig: stringptr(""),
Timeout: utilpointer.String("0"),
KubeConfig: utilpointer.String(""),
CacheDir: stringptr(defaultCacheDir),
ClusterName: stringptr(""),
AuthInfoName: stringptr(""),
Context: stringptr(""),
Namespace: stringptr(""),
APIServer: stringptr(""),
TLSServerName: stringptr(""),
CertFile: stringptr(""),
KeyFile: stringptr(""),
CAFile: stringptr(""),
BearerToken: stringptr(""),
Impersonate: stringptr(""),
CacheDir: utilpointer.String(getDefaultCacheDir()),
ClusterName: utilpointer.String(""),
AuthInfoName: utilpointer.String(""),
Context: utilpointer.String(""),
Namespace: utilpointer.String(""),
APIServer: utilpointer.String(""),
TLSServerName: utilpointer.String(""),
CertFile: utilpointer.String(""),
KeyFile: utilpointer.String(""),
CAFile: utilpointer.String(""),
BearerToken: utilpointer.String(""),
Impersonate: utilpointer.String(""),
ImpersonateUID: utilpointer.String(""),
ImpersonateGroup: &impersonateGroup,
usePersistentConfig: usePersistentConfig,
// The more groups you have, the more discovery requests you need to make.
// given 25 groups (our groups + a few custom resources) with one-ish version each, discovery needs to make 50 requests
// double it just so we don't end up here again for a while. This config is only used for discovery.
discoveryBurst: 100,
// with a burst of 300, we will not be rate-limiting for most clusters but
// the safeguard will still be here. This config is only used for discovery.
discoveryBurst: 300,
}
}
func stringptr(val string) *string {
return &val
}
// overlyCautiousIllegalFileCharacters matches characters that *might* not be supported. Windows is really restrictive, so this is really restrictive
var overlyCautiousIllegalFileCharacters = regexp.MustCompile(`[^(\w/\.)]`)
var overlyCautiousIllegalFileCharacters = regexp.MustCompile(`[^(\w/.)]`)
// computeDiscoverCacheDir takes the parentDir and the host and comes up with a "usually non-colliding" name.
func computeDiscoverCacheDir(parentDir, host string) string {