From 2c759f395ad77b342337575fa2e67d79f50ff8e4 Mon Sep 17 00:00:00 2001 From: Tiger Kaovilai Date: Mon, 12 Sep 2022 10:40:28 -0400 Subject: [PATCH] cancel downloadRequest checkFunc if timeout passed Signed-off-by: Tiger Kaovilai --- changelogs/unreleased/5329-kaovilai | 1 + pkg/cmd/util/downloadrequest/downloadrequest.go | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 changelogs/unreleased/5329-kaovilai diff --git a/changelogs/unreleased/5329-kaovilai b/changelogs/unreleased/5329-kaovilai new file mode 100644 index 000000000..81615413c --- /dev/null +++ b/changelogs/unreleased/5329-kaovilai @@ -0,0 +1 @@ +Cancel downloadRequest when timeout without downloadURL \ No newline at end of file diff --git a/pkg/cmd/util/downloadrequest/downloadrequest.go b/pkg/cmd/util/downloadrequest/downloadrequest.go index 22861245f..f131b32e0 100644 --- a/pkg/cmd/util/downloadrequest/downloadrequest.go +++ b/pkg/cmd/util/downloadrequest/downloadrequest.go @@ -40,6 +40,7 @@ import ( // ErrNotFound is exported for external packages to check for when a file is // not found var ErrNotFound = errors.New("file not found") +var ErrDownloadRequestDownloadURLTimeout = errors.New("download request download url timeout, check velero server logs for errors. backup storage location may not be available") func Stream(ctx context.Context, kbClient kbclient.Client, namespace, name string, kind velerov1api.DownloadTargetKind, w io.Writer, timeout time.Duration, insecureSkipTLSVerify bool, caCertFile string) error { uuid, err := uuid.NewRandom() @@ -58,7 +59,14 @@ func Stream(ctx context.Context, kbClient kbclient.Client, namespace, name strin defer cancel() key := kbclient.ObjectKey{Name: created.Name, Namespace: namespace} + timeStreamFirstCheck := time.Now() + downloadUrlTimeout := false checkFunc := func() { + // if timeout has been reached, cancel request + if time.Now().After(timeStreamFirstCheck.Add(timeout)) { + downloadUrlTimeout = true + cancel() + } updated := &velerov1api.DownloadRequest{} if err := kbClient.Get(ctx, key, updated); err != nil { return @@ -77,9 +85,8 @@ func Stream(ctx context.Context, kbClient kbclient.Client, namespace, name strin } wait.Until(checkFunc, 25*time.Millisecond, ctx.Done()) - - if created.Status.DownloadURL == "" { - return ErrNotFound + if downloadUrlTimeout { + return ErrDownloadRequestDownloadURLTimeout } var caPool *x509.CertPool