Skip to content

Commit 04440fd

Browse files
committed
fix: avoid premature up timeout
1 parent aeba4b3 commit 04440fd

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

internal/cli/up.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func newUpCmd(opts *Options) *cobra.Command {
9898
if warnErr := warnIfConfigNewerThanSession(opts, k, ns, sn, pod, ui.warnWriter()); warnErr != nil {
9999
slog.Debug("skip config drift warning", "error", warnErr)
100100
}
101-
ctx, cancel := defaultContext()
101+
ctx, cancel := upCommandContext(waitTimeout)
102102
defer cancel()
103103
if createMissingPVC {
104104
createdPVCs, err := reconcileMissingPVCs(ctx, k, ns, volumes, missingPVCSize, missingPVCStorageClass, labels, annotations)
@@ -277,6 +277,15 @@ func newUpCmd(opts *Options) *cobra.Command {
277277
return cmd
278278
}
279279

280+
func upCommandContext(waitTimeout time.Duration) (context.Context, context.CancelFunc) {
281+
timeout := 5 * time.Minute
282+
needed := (2 * waitTimeout) + (2 * time.Minute)
283+
if needed > timeout {
284+
timeout = needed
285+
}
286+
return context.WithTimeout(context.Background(), timeout)
287+
}
288+
280289
func reconcileMissingPVCs(ctx context.Context, k interface {
281290
PersistentVolumeClaimExists(context.Context, string, string) (bool, error)
282291
Apply(context.Context, string, []byte) error

internal/cli/up_context_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cli
2+
3+
import (
4+
"context"
5+
"testing"
6+
"time"
7+
)
8+
9+
func TestUpCommandContextUsesDefaultFloor(t *testing.T) {
10+
ctx, cancel := upCommandContext(30 * time.Second)
11+
defer cancel()
12+
13+
deadline, ok := ctx.Deadline()
14+
if !ok {
15+
t.Fatal("expected context deadline")
16+
}
17+
remaining := time.Until(deadline)
18+
if remaining < 4*time.Minute || remaining > 5*time.Minute+5*time.Second {
19+
t.Fatalf("expected default timeout near 5m, got %s", remaining)
20+
}
21+
}
22+
23+
func TestUpCommandContextExpandsForLargeWaitTimeout(t *testing.T) {
24+
ctx, cancel := upCommandContext(10 * time.Minute)
25+
defer cancel()
26+
27+
deadline, ok := ctx.Deadline()
28+
if !ok {
29+
t.Fatal("expected context deadline")
30+
}
31+
remaining := time.Until(deadline)
32+
expected := (2 * 10 * time.Minute) + (2 * time.Minute)
33+
if remaining < expected-5*time.Second || remaining > expected+5*time.Second {
34+
t.Fatalf("expected timeout near %s, got %s", expected, remaining)
35+
}
36+
}
37+
38+
func TestUpCommandContextIsCancellable(t *testing.T) {
39+
ctx, cancel := upCommandContext(time.Minute)
40+
cancel()
41+
42+
select {
43+
case <-ctx.Done():
44+
default:
45+
t.Fatal("expected cancelled context to be done")
46+
}
47+
if ctx.Err() != context.Canceled {
48+
t.Fatalf("expected context canceled, got %v", ctx.Err())
49+
}
50+
}

0 commit comments

Comments
 (0)