Skip to content

Commit

Permalink
Add global timeout flag
Browse files Browse the repository at this point in the history
This patch adds a global timeout flag (viewable with `kubectl -h`) with
a default value of `0s` (meaning no timeout).

The timeout value is added to the default http client, so that zero
values and default behavior are enforced by the client.

**Example**
```
$ kubectl get pods # no timeout flag set - default to 0s (which means no
timeout)
NAME                      READY     STATUS    RESTARTS   AGE
docker-registry-1-h7etw   1/1       Running   1          2h
router-1-uv0f9            1/1       Running   1          2h

$ kubectl get pods --timeout=0 # zero means no timeout no timeout flag set
NAME                      READY     STATUS    RESTARTS   AGE
docker-registry-1-h7etw   1/1       Running   1          2h
router-1-uv0f9            1/1       Running   1          2h

$kubectl get pods --timeout=1ms
Unable to connect to the server: net/http: request canceled while
waiting for connection (Client.Timeout exceeded while awaiting headers)
```
  • Loading branch information
juanvallejo committed Oct 6, 2016
1 parent 3933ddb commit 778e57e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
26 changes: 26 additions & 0 deletions hack/make-rules/test-cmd.sh
Expand Up @@ -1033,6 +1033,32 @@ __EOF__
# Clean up
kubectl delete deployment nginx "${kube_flags[@]}"

##################
# Global timeout #
##################

### Test global request timeout option
# Pre-condition: no POD exists
create_and_use_new_namespace
kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
# Command
kubectl create "${kube_flags[@]}" -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml
# Post-condition: valid-pod POD is created
kubectl get "${kube_flags[@]}" pods -o json
kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'valid-pod:'

## check --request-timeout on 'get pod'
output_message=$(kubectl get pod valid-pod --request-timeout=1s)
kube::test::if_has_string "${output_message}" 'valid-pod'

## check --request-timeout on 'get pod' with --watch
output_message=$(! kubectl get pod valid-pod --request-timeout=1s --watch 2>&1)
kube::test::if_has_string "${output_message}" 'Timeout exceeded while reading body'

## check argument validation for --request-timeout flag
output_message=$(! kubectl get pod valid-pod --request-timeout=1)
kube::test::if_has_string "${output_message}" 'invalid argument "1"'

###############
# Kubectl get #
###############
Expand Down
10 changes: 10 additions & 0 deletions pkg/client/restclient/config.go
Expand Up @@ -25,6 +25,7 @@ import (
"path"
gruntime "runtime"
"strings"
"time"

"github.com/golang/glog"

Expand Down Expand Up @@ -109,6 +110,9 @@ type Config struct {
// Rate limiter for limiting connections to the master from this client. If present overwrites QPS/Burst
RateLimiter flowcontrol.RateLimiter

// The maximum length of time to wait before giving up on a server request. A value of zero means no timeout.
Timeout time.Duration

// Version forces a specific version to be used (if registered)
// Do we need this?
// Version string
Expand Down Expand Up @@ -185,6 +189,9 @@ func RESTClientFor(config *Config) (*RESTClient, error) {
var httpClient *http.Client
if transport != http.DefaultTransport {
httpClient = &http.Client{Transport: transport}
if config.Timeout > 0 {
httpClient.Timeout = config.Timeout
}
}

return NewRESTClient(baseURL, versionedAPIPath, config.ContentConfig, qps, burst, config.RateLimiter, httpClient)
Expand All @@ -210,6 +217,9 @@ func UnversionedRESTClientFor(config *Config) (*RESTClient, error) {
var httpClient *http.Client
if transport != http.DefaultTransport {
httpClient = &http.Client{Transport: transport}
if config.Timeout > 0 {
httpClient.Timeout = config.Timeout
}
}

versionConfig := config.ContentConfig
Expand Down
1 change: 1 addition & 0 deletions pkg/client/unversioned/clientcmd/client_config.go
Expand Up @@ -108,6 +108,7 @@ func (config *DirectClientConfig) ClientConfig() (*restclient.Config, error) {

clientConfig := &restclient.Config{}
clientConfig.Host = configClusterInfo.Server
clientConfig.Timeout = config.overrides.Timeout
if u, err := url.ParseRequestURI(clientConfig.Host); err == nil && u.Opaque == "" && len(u.Path) > 1 {
u.RawQuery = ""
u.Fragment = ""
Expand Down
23 changes: 22 additions & 1 deletion pkg/client/unversioned/clientcmd/overrides.go
Expand Up @@ -18,6 +18,7 @@ package clientcmd

import (
"strconv"
"time"

"github.com/spf13/pflag"

Expand All @@ -33,6 +34,7 @@ type ConfigOverrides struct {
ClusterInfo clientcmdapi.Cluster
Context clientcmdapi.Context
CurrentContext string
Timeout time.Duration
}

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

// AuthOverrideFlags holds the flag names to be used for binding command line flags for AuthInfo objects
Expand Down Expand Up @@ -105,6 +108,20 @@ func (f FlagInfo) BindBoolFlag(flags *pflag.FlagSet, target *bool) {
}
}

// BindDurationFlag binds the flag based on the provided info. If LongName == "", nothing is registered
func (f FlagInfo) BindDurationFlag(flags *pflag.FlagSet, target *time.Duration) {
// you can't register a flag without a long name
if len(f.LongName) > 0 {
// try to parse Default as a Duration. If it fails, default to 0s
durationVal, err := time.ParseDuration(f.Default)
if err != nil {
durationVal = 0
}

flags.DurationVarP(target, f.LongName, f.ShortName, durationVal, f.Description)
}
}

const (
FlagClusterName = "cluster"
FlagAuthInfoName = "user"
Expand All @@ -121,6 +138,7 @@ const (
FlagImpersonate = "as"
FlagUsername = "username"
FlagPassword = "password"
FlagTimeout = "request-timeout"
)

// RecommendedAuthOverrideFlags is a convenience method to return recommended flag names prefixed with a string of your choosing
Expand Down Expand Up @@ -151,7 +169,9 @@ func RecommendedConfigOverrideFlags(prefix string) ConfigOverrideFlags {
AuthOverrideFlags: RecommendedAuthOverrideFlags(prefix),
ClusterOverrideFlags: RecommendedClusterOverrideFlags(prefix),
ContextOverrideFlags: RecommendedContextOverrideFlags(prefix),
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, "", "", "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."},
}
}

Expand Down Expand Up @@ -190,6 +210,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.Timeout.BindDurationFlag(flags, &overrides.Timeout)
}

// BindFlags is a convenience method to bind the specified flags to their associated variables
Expand Down

0 comments on commit 778e57e

Please sign in to comment.