mirror of
https://github.com/helm/helm.git
synced 2026-03-28 21:35:17 -04:00
feat(getter): add timeout option (#7950)
To allow finer grain control over the request when utilizing `getter` as a package. Signed-off-by: Hidde Beydals <hello@hidde.co>
This commit is contained in:
parent
374c721240
commit
ae738d7d87
3 changed files with 17 additions and 0 deletions
|
|
@ -18,6 +18,7 @@ package getter
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
|
|
@ -36,6 +37,7 @@ type options struct {
|
|||
username string
|
||||
password string
|
||||
userAgent string
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
// Option allows specifying various settings configurable by the user for overriding the defaults
|
||||
|
|
@ -81,6 +83,13 @@ func WithTLSClientConfig(certFile, keyFile, caFile string) Option {
|
|||
}
|
||||
}
|
||||
|
||||
// WithTimeout sets the timeout for requests
|
||||
func WithTimeout(timeout time.Duration) Option {
|
||||
return func(opts *options) {
|
||||
opts.timeout = timeout
|
||||
}
|
||||
}
|
||||
|
||||
// Getter is an interface to support GET to the specified URL.
|
||||
type Getter interface {
|
||||
// Get file content by url string
|
||||
|
|
|
|||
|
|
@ -119,6 +119,7 @@ func (g *HTTPGetter) httpClient() (*http.Client, error) {
|
|||
|
||||
client := &http.Client{
|
||||
Transport: transport,
|
||||
Timeout: g.opts.timeout,
|
||||
}
|
||||
|
||||
return client, nil
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
|
|
@ -48,6 +49,7 @@ func TestHTTPGetter(t *testing.T) {
|
|||
join := filepath.Join
|
||||
ca, pub, priv := join(cd, "rootca.crt"), join(cd, "crt.pem"), join(cd, "key.pem")
|
||||
insecure := false
|
||||
timeout := time.Second * 5
|
||||
|
||||
// Test with options
|
||||
g, err = NewHTTPGetter(
|
||||
|
|
@ -55,6 +57,7 @@ func TestHTTPGetter(t *testing.T) {
|
|||
WithUserAgent("Groot"),
|
||||
WithTLSClientConfig(pub, priv, ca),
|
||||
WithInsecureSkipVerifyTLS(insecure),
|
||||
WithTimeout(timeout),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
@ -93,6 +96,10 @@ func TestHTTPGetter(t *testing.T) {
|
|||
t.Errorf("Expected NewHTTPGetter to contain %t as InsecureSkipVerifyTLs flag, got %t", false, hg.opts.insecureSkipVerifyTLS)
|
||||
}
|
||||
|
||||
if hg.opts.timeout != timeout {
|
||||
t.Errorf("Expected NewHTTPGetter to contain %s as Timeout flag, got %s", timeout, hg.opts.timeout)
|
||||
}
|
||||
|
||||
// Test if setting insecureSkipVerifyTLS is being passed to the ops
|
||||
insecure = true
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue