Skip to content

Commit a2a0935

Browse files
authored
Merge pull request helm#30900 from unguiculus/issue-12952
Add timeout flag to repo add and update commands
2 parents 8e69436 + d448cf1 commit a2a0935

4 files changed

Lines changed: 48 additions & 15 deletions

File tree

pkg/cmd/repo_add.go

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

5657
certFile string
5758
keyFile string
@@ -96,6 +97,7 @@ func newRepoAddCmd(out io.Writer) *cobra.Command {
9697
f.BoolVar(&o.insecureSkipTLSverify, "insecure-skip-tls-verify", false, "skip tls certificate checks for the repository")
9798
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")
9899
f.BoolVar(&o.passCredentialsAll, "pass-credentials", false, "pass credentials to all domains")
100+
f.DurationVar(&o.timeout, "timeout", getter.DefaultHTTPTimeout*time.Second, "time to wait for the index file download to complete")
99101

100102
return cmd
101103
}
@@ -199,7 +201,7 @@ func (o *repoAddOptions) run(out io.Writer) error {
199201
return nil
200202
}
201203

202-
r, err := repo.NewChartRepository(&c, getter.All(settings))
204+
r, err := repo.NewChartRepository(&c, getter.All(settings, getter.WithTimeout(o.timeout)))
203205
if err != nil {
204206
return err
205207
}

pkg/cmd/repo_update.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"io"
2323
"slices"
2424
"sync"
25+
"time"
2526

2627
"github.com/spf13/cobra"
2728

@@ -46,6 +47,7 @@ type repoUpdateOptions struct {
4647
repoFile string
4748
repoCache string
4849
names []string
50+
timeout time.Duration
4951
}
5052

5153
func newRepoUpdateCmd(out io.Writer) *cobra.Command {
@@ -68,6 +70,9 @@ func newRepoUpdateCmd(out io.Writer) *cobra.Command {
6870
},
6971
}
7072

73+
f := cmd.Flags()
74+
f.DurationVar(&o.timeout, "timeout", getter.DefaultHTTPTimeout*time.Second, "time to wait for the index file download to complete")
75+
7176
return cmd
7277
}
7378

@@ -94,7 +99,7 @@ func (o *repoUpdateOptions) run(out io.Writer) error {
9499

95100
for _, cfg := range f.Repositories {
96101
if updateAllRepos || isRepoRequested(cfg.Name, o.names) {
97-
r, err := repo.NewChartRepository(cfg, getter.All(settings))
102+
r, err := repo.NewChartRepository(cfg, getter.All(settings, getter.WithTimeout(o.timeout)))
98103
if err != nil {
99104
return err
100105
}

pkg/getter/getter.go

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

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

194-
var httpProvider = Provider{
195-
Schemes: []string{"http", "https"},
196-
New: func(options ...Option) (Getter, error) {
197-
options = append(options, defaultOptions...)
198-
return NewHTTPGetter(options...)
199-
},
200-
}
201-
202-
var ociProvider = Provider{
203-
Schemes: []string{registry.OCIScheme},
204-
New: NewOCIGetter,
194+
func Getters(extraOpts ...Option) Providers {
195+
return Providers{
196+
Provider{
197+
Schemes: []string{"http", "https"},
198+
New: func(options ...Option) (Getter, error) {
199+
options = append(options, defaultOptions...)
200+
options = append(options, extraOpts...)
201+
return NewHTTPGetter(options...)
202+
},
203+
},
204+
Provider{
205+
Schemes: []string{registry.OCIScheme},
206+
New: func(options ...Option) (Getter, error) {
207+
options = append(options, defaultOptions...)
208+
options = append(options, extraOpts...)
209+
return NewOCIGetter(options...)
210+
},
211+
},
212+
}
205213
}
206214

207215
// All finds all of the registered getters as a list of Provider instances.
208216
// Currently, the built-in getters and the discovered plugins with downloader
209217
// notations are collected.
210-
func All(settings *cli.EnvSettings) Providers {
211-
result := Providers{httpProvider, ociProvider}
218+
func All(settings *cli.EnvSettings, opts ...Option) Providers {
219+
result := Getters(opts...)
212220
pluginDownloaders, _ := collectPlugins(settings)
213221
result = append(result, pluginDownloaders...)
214222
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/v4/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)