Skip to content

Commit 79a9cc5

Browse files
committed
Add timeout flag to repo add and update flags
Backport of #30900 to v3. (cherry picked from commit d448cf1) Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>
1 parent 67d5366 commit 79a9cc5

4 files changed

Lines changed: 46 additions & 15 deletions

File tree

cmd/helm/repo_add.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type repoAddOptions struct {
5151
passCredentialsAll bool
5252
forceUpdate bool
5353
allowDeprecatedRepos bool
54+
timeout time.Duration
5455

5556
certFile string
5657
keyFile string
@@ -99,6 +100,7 @@ func newRepoAddCmd(out io.Writer) *cobra.Command {
99100
f.BoolVar(&o.insecureSkipTLSverify, "insecure-skip-tls-verify", false, "skip tls certificate checks for the repository")
100101
f.BoolVar(&o.allowDeprecatedRepos, "allow-deprecated-repos", false, "by default, this command will not allow adding official repos that have been permanently deleted. This disables that behavior")
101102
f.BoolVar(&o.passCredentialsAll, "pass-credentials", false, "pass credentials to all domains")
103+
f.DurationVar(&o.timeout, "timeout", getter.DefaultHTTPTimeout*time.Second, "time to wait for the index file download to complete")
102104

103105
return cmd
104106
}
@@ -203,7 +205,7 @@ func (o *repoAddOptions) run(out io.Writer) error {
203205
return nil
204206
}
205207

206-
r, err := repo.NewChartRepository(&c, getter.All(settings))
208+
r, err := repo.NewChartRepository(&c, getter.All(settings, getter.WithTimeout(o.timeout)))
207209
if err != nil {
208210
return err
209211
}

cmd/helm/repo_update.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"io"
2222
"sync"
23+
"time"
2324

2425
"github.com/pkg/errors"
2526
"github.com/spf13/cobra"
@@ -45,6 +46,7 @@ type repoUpdateOptions struct {
4546
repoFile string
4647
repoCache string
4748
names []string
49+
timeout time.Duration
4850
failOnRepoUpdateFail bool
4951
}
5052

@@ -69,6 +71,7 @@ func newRepoUpdateCmd(out io.Writer) *cobra.Command {
6971
}
7072

7173
f := cmd.Flags()
74+
f.DurationVar(&o.timeout, "timeout", getter.DefaultHTTPTimeout*time.Second, "time to wait for the index file download to complete")
7275

7376
// Adding this flag for Helm 3 as stop gap functionality for https://github.com/helm/helm/issues/10016.
7477
// This should be deprecated in Helm 4 by update to the behaviour of `helm repo update` command.
@@ -100,7 +103,7 @@ func (o *repoUpdateOptions) run(out io.Writer) error {
100103

101104
for _, cfg := range f.Repositories {
102105
if updateAllRepos || isRepoRequested(cfg.Name, o.names) {
103-
r, err := repo.NewChartRepository(cfg, getter.All(settings))
106+
r, err := repo.NewChartRepository(cfg, getter.All(settings, getter.WithTimeout(o.timeout)))
104107
if err != nil {
105108
return err
106109
}

pkg/getter/getter.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -196,24 +196,32 @@ const (
196196

197197
var defaultOptions = []Option{WithTimeout(time.Second * DefaultHTTPTimeout)}
198198

199-
var httpProvider = Provider{
200-
Schemes: []string{"http", "https"},
201-
New: func(options ...Option) (Getter, error) {
202-
options = append(options, defaultOptions...)
203-
return NewHTTPGetter(options...)
204-
},
205-
}
206-
207-
var ociProvider = Provider{
208-
Schemes: []string{registry.OCIScheme},
209-
New: NewOCIGetter,
199+
func Getters(extraOpts ...Option) Providers {
200+
return Providers{
201+
Provider{
202+
Schemes: []string{"http", "https"},
203+
New: func(options ...Option) (Getter, error) {
204+
options = append(options, defaultOptions...)
205+
options = append(options, extraOpts...)
206+
return NewHTTPGetter(options...)
207+
},
208+
},
209+
Provider{
210+
Schemes: []string{registry.OCIScheme},
211+
New: func(options ...Option) (Getter, error) {
212+
options = append(options, defaultOptions...)
213+
options = append(options, extraOpts...)
214+
return NewOCIGetter(options...)
215+
},
216+
},
217+
}
210218
}
211219

212220
// All finds all of the registered getters as a list of Provider instances.
213221
// Currently, the built-in getters and the discovered plugins with downloader
214222
// notations are collected.
215-
func All(settings *cli.EnvSettings) Providers {
216-
result := Providers{httpProvider, ociProvider}
223+
func All(settings *cli.EnvSettings, opts ...Option) Providers {
224+
result := Getters(opts...)
217225
pluginDownloaders, _ := collectPlugins(settings)
218226
result = append(result, pluginDownloaders...)
219227
return result

pkg/getter/getter_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package getter
1717

1818
import (
1919
"testing"
20+
"time"
2021

2122
"helm.sh/helm/v3/pkg/cli"
2223
)
@@ -52,6 +53,23 @@ func TestProviders(t *testing.T) {
5253
}
5354
}
5455

56+
func TestProvidersWithTimeout(t *testing.T) {
57+
want := time.Hour
58+
getters := Getters(WithTimeout(want))
59+
getter, err := getters.ByScheme("http")
60+
if err != nil {
61+
t.Error(err)
62+
}
63+
client, err := getter.(*HTTPGetter).httpClient()
64+
if err != nil {
65+
t.Error(err)
66+
}
67+
got := client.Timeout
68+
if got != want {
69+
t.Errorf("Expected %q, got %q", want, got)
70+
}
71+
}
72+
5573
func TestAll(t *testing.T) {
5674
env := cli.New()
5775
env.PluginsDirectory = pluginDir

0 commit comments

Comments
 (0)