Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable Requesting Compression by default on restclient #48477

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions staging/src/k8s.io/client-go/rest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ type Config struct {
// The maximum length of time to wait before giving up on a server request. A value of zero means no timeout.
Timeout time.Duration

// EnableCompression, if true the Transport will
// request compression with an "Accept-Encoding: gzip"
// request header when the Request contains no existing
// Accept-Encoding value.
EnableCompression bool

// Version forces a specific version to be used (if registered)
// Do we need this?
// Version string
Expand Down Expand Up @@ -406,12 +412,13 @@ func AnonymousClientConfig(config *Config) *Config {
CAFile: config.TLSClientConfig.CAFile,
CAData: config.TLSClientConfig.CAData,
},
RateLimiter: config.RateLimiter,
UserAgent: config.UserAgent,
Transport: config.Transport,
WrapTransport: config.WrapTransport,
QPS: config.QPS,
Burst: config.Burst,
Timeout: config.Timeout,
RateLimiter: config.RateLimiter,
UserAgent: config.UserAgent,
Transport: config.Transport,
WrapTransport: config.WrapTransport,
QPS: config.QPS,
Burst: config.Burst,
Timeout: config.Timeout,
EnableCompression: config.EnableCompression,
}
}
1 change: 1 addition & 0 deletions staging/src/k8s.io/client-go/rest/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,6 @@ func (c *Config) TransportConfig() (*transport.Config, error) {
Groups: c.Impersonate.Groups,
Extra: c.Impersonate.Extra,
},
EnableCompression: c.EnableCompression,
}, nil
}
2 changes: 2 additions & 0 deletions staging/src/k8s.io/client-go/tools/clientcmd/client_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ func (config *DirectClientConfig) ClientConfig() (*restclient.Config, error) {
clientConfig := &restclient.Config{}
clientConfig.Host = configClusterInfo.Server

clientConfig.EnableCompression = config.overrides.EnableCompression

if len(config.overrides.Timeout) > 0 {
timeout, err := ParseTimeout(config.overrides.Timeout)
if err != nil {
Expand Down
51 changes: 28 additions & 23 deletions staging/src/k8s.io/client-go/tools/clientcmd/overrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ import (
type ConfigOverrides struct {
AuthInfo clientcmdapi.AuthInfo
// ClusterDefaults are applied before the configured cluster info is loaded.
ClusterDefaults clientcmdapi.Cluster
ClusterInfo clientcmdapi.Cluster
Context clientcmdapi.Context
CurrentContext string
Timeout string
ClusterDefaults clientcmdapi.Cluster
ClusterInfo clientcmdapi.Cluster
Context clientcmdapi.Context
CurrentContext string
Timeout string
EnableCompression bool
}

// ConfigOverrideFlags holds the flag names to be used for binding command line flags. Notice that this structure tightly
Expand All @@ -43,6 +44,7 @@ type ConfigOverrideFlags struct {
ClusterOverrideFlags ClusterOverrideFlags
ContextOverrideFlags ContextOverrideFlags
CurrentContext FlagInfo
EnableCompression FlagInfo
Timeout FlagInfo
}

Expand Down Expand Up @@ -130,22 +132,23 @@ func (f FlagInfo) BindBoolFlag(flags *pflag.FlagSet, target *bool) FlagInfo {
}

const (
FlagClusterName = "cluster"
FlagAuthInfoName = "user"
FlagContext = "context"
FlagNamespace = "namespace"
FlagAPIServer = "server"
FlagInsecure = "insecure-skip-tls-verify"
FlagCertFile = "client-certificate"
FlagKeyFile = "client-key"
FlagCAFile = "certificate-authority"
FlagEmbedCerts = "embed-certs"
FlagBearerToken = "token"
FlagImpersonate = "as"
FlagImpersonateGroup = "as-group"
FlagUsername = "username"
FlagPassword = "password"
FlagTimeout = "request-timeout"
FlagClusterName = "cluster"
FlagAuthInfoName = "user"
FlagContext = "context"
FlagNamespace = "namespace"
FlagAPIServer = "server"
FlagInsecure = "insecure-skip-tls-verify"
FlagCertFile = "client-certificate"
FlagKeyFile = "client-key"
FlagCAFile = "certificate-authority"
FlagEmbedCerts = "embed-certs"
FlagBearerToken = "token"
FlagImpersonate = "as"
FlagImpersonateGroup = "as-group"
FlagUsername = "username"
FlagPassword = "password"
FlagTimeout = "request-timeout"
FlagEnableCompression = "enable-compression"
)

// RecommendedConfigOverrideFlags is a convenience method to return recommended flag names prefixed with a string of your choosing
Expand All @@ -155,8 +158,9 @@ func RecommendedConfigOverrideFlags(prefix string) ConfigOverrideFlags {
ClusterOverrideFlags: RecommendedClusterOverrideFlags(prefix),
ContextOverrideFlags: RecommendedContextOverrideFlags(prefix),

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."},
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."},
EnableCompression: FlagInfo{prefix + FlagEnableCompression, "", "false", "Enable HTTP client to request compressed responses from server"},
}
}

Expand Down Expand Up @@ -197,6 +201,7 @@ func BindOverrideFlags(overrides *ConfigOverrides, flags *pflag.FlagSet, flagNam
BindClusterFlags(&overrides.ClusterInfo, flags, flagNames.ClusterOverrideFlags)
BindContextFlags(&overrides.Context, flags, flagNames.ContextOverrideFlags)
flagNames.CurrentContext.BindStringFlag(flags, &overrides.CurrentContext)
flagNames.EnableCompression.BindBoolFlag(flags, &overrides.EnableCompression)
flagNames.Timeout.BindStringFlag(flags, &overrides.Timeout)
}

Expand Down
15 changes: 14 additions & 1 deletion staging/src/k8s.io/client-go/transport/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,19 @@ func (c *tlsTransportCache) get(config *Config) (http.RoundTripper, error) {
}
// The options didn't require a custom TLS config
if tlsConfig == nil {
return http.DefaultTransport, nil
tr := http.DefaultTransport
if defaultTransport, ok := tr.(*http.Transport); ok {
tr = &http.Transport{
Proxy: defaultTransport.Proxy,
DialContext: defaultTransport.DialContext,
MaxIdleConns: defaultTransport.MaxIdleConns,
IdleConnTimeout: defaultTransport.IdleConnTimeout,
TLSHandshakeTimeout: defaultTransport.TLSHandshakeTimeout,
ExpectContinueTimeout: defaultTransport.ExpectContinueTimeout,
DisableCompression: !config.EnableCompression,
}
}
return tr, nil
}

// Cache a single transport for these options
Expand All @@ -73,6 +85,7 @@ func (c *tlsTransportCache) get(config *Config) (http.RoundTripper, error) {
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
DisableCompression: !config.EnableCompression,
})
return c.transports[key], nil
}
Expand Down
6 changes: 6 additions & 0 deletions staging/src/k8s.io/client-go/transport/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ type Config struct {
// Bearer token for authentication
BearerToken string

// EnableCompression, if true the Transport will
// request compression with an "Accept-Encoding: gzip"
// request header when the Request contains no existing
// Accept-Encoding value.
EnableCompression bool

// Impersonate is the config that this Config will impersonate using
Impersonate ImpersonationConfig

Expand Down
19 changes: 17 additions & 2 deletions staging/src/k8s.io/client-go/transport/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package transport

import (
"net/http"
"reflect"
"testing"
)

Expand Down Expand Up @@ -181,8 +182,22 @@ func TestNew(t *testing.T) {
}

switch {
case testCase.Default && transport != http.DefaultTransport:
t.Errorf("%s: expected the default transport, got %#v", k, transport)
case testCase.Default:
defaulTransport := http.DefaultTransport.(*http.Transport)
httpTransport, ok := transport.(*http.Transport)
if !ok {
t.Errorf("%s: expected transport did not match the default transport, got %#v", k, transport)
}
match := reflect.ValueOf(httpTransport.Proxy).Pointer() == reflect.ValueOf(defaulTransport.Proxy).Pointer() &&
reflect.ValueOf(httpTransport.DialContext).Pointer() == reflect.ValueOf(defaulTransport.DialContext).Pointer() &&
httpTransport.MaxIdleConns == defaulTransport.MaxIdleConns &&
httpTransport.IdleConnTimeout == defaulTransport.IdleConnTimeout &&
httpTransport.TLSHandshakeTimeout == defaulTransport.TLSHandshakeTimeout &&
httpTransport.ExpectContinueTimeout == defaulTransport.ExpectContinueTimeout

if !match {
t.Errorf("%s: expected transport did not match the default transport, got %#v", k, transport)
}
continue
case !testCase.Default && transport == http.DefaultTransport:
t.Errorf("%s: expected non-default transport, got %#v", k, transport)
Expand Down