From b5dfb721752c4c5ae9406378ae88164313cf55e4 Mon Sep 17 00:00:00 2001 From: Mojtaba Date: Thu, 19 Dec 2024 16:38:30 +0100 Subject: [PATCH] feat: add deadline instead of timeout --- e2e/sidecars/suite_setup_test.go | 4 +-- pkg/knuu/knuu.go | 42 +++++++++++++++++++------------- pkg/knuu/knuu_test.go | 2 +- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/e2e/sidecars/suite_setup_test.go b/e2e/sidecars/suite_setup_test.go index 3d52ee9..2595638 100644 --- a/e2e/sidecars/suite_setup_test.go +++ b/e2e/sidecars/suite_setup_test.go @@ -34,8 +34,8 @@ func (s *Suite) SetupSuite() { ) s.Knuu, err = knuu.New(ctx, knuu.Options{ - Timeout: testTimeout, - Logger: logger, + Deadline: time.Now().Add(testTimeout), + Logger: logger, }) s.Require().NoError(err) diff --git a/pkg/knuu/knuu.go b/pkg/knuu/knuu.go index 4c4ac0c..5c250ce 100644 --- a/pkg/knuu/knuu.go +++ b/pkg/knuu/knuu.go @@ -25,14 +25,13 @@ import ( ) const ( - defaultTimeout = 60 * time.Minute - timeoutHandlerName = "timeout-handler" + defaultTimeout = 60 * time.Minute + deadlineHandlerName = "deadline-handler" // FIXME: use supported kubernetes version images (use of latest could break) (https://github.com/celestiaorg/knuu/issues/116) - timeoutHandlerImage = "docker.io/bitnami/kubectl:latest" + deadlineHandlerImage = "docker.io/bitnami/kubectl:latest" - timeoutHandlerNameStop = timeoutHandlerName + "-stop" - timeoutHandlerTimeout = 1 * time.Second - ExitCodeSIGINT = 130 + deadlineHandlerNameStop = deadlineHandlerName + "-stop" + ExitCodeSIGINT = 130 TimeFormat = "20060102T150405Z" ) @@ -49,7 +48,8 @@ type Options struct { ImageBuilder builder.Builder Scope string ProxyEnabled bool - Timeout time.Duration + Timeout time.Duration // deprecated, use Deadline instead + Deadline time.Time Logger *logrus.Logger ClusterDomain string // optional, if not set, "cluster.local" will be used } @@ -75,10 +75,17 @@ func New(ctx context.Context, opts Options) (*Knuu, error) { return nil, err } - if opts.Timeout == 0 { - opts.Timeout = defaultTimeout + if opts.Deadline.IsZero() { + if opts.Timeout != 0 { + k.Logger.Warn("Timeout is deprecated, use deadline instead") + opts.Deadline = time.Now().Add(opts.Timeout) + + } else { + opts.Deadline = time.Now().Add(defaultTimeout) + } } - if err := k.handleTimeout(ctx, opts.Timeout, timeoutHandlerName); err != nil { + + if err := k.handleDeadline(ctx, opts.Deadline, deadlineHandlerName); err != nil { return nil, ErrHandleTimeout.Wrap(err) } @@ -104,9 +111,9 @@ func (k *Knuu) HandleStopSignal(ctx context.Context) { // Lock the stop mutex to prevent multiple stop signals from being processed concurrently k.stopMu.Lock() defer k.stopMu.Unlock() - err := k.handleTimeout(ctx, timeoutHandlerTimeout, timeoutHandlerNameStop) + err := k.handleDeadline(ctx, time.Now(), deadlineHandlerNameStop) if err != nil { - k.Logger.Errorf("Error cleaning up resources with timeout handler: %v", err) + k.Logger.Errorf("Error cleaning up resources with deadline handler: %v", err) } k.K8sClient.Terminate() // Allow other signal handlers to run @@ -115,15 +122,15 @@ func (k *Knuu) HandleStopSignal(ctx context.Context) { }() } -// handleTimeout creates a timeout handler that will delete all resources with the scope after the timeout -func (k *Knuu) handleTimeout(ctx context.Context, timeout time.Duration, timeoutHandlerName string) error { - inst, err := k.NewInstance(timeoutHandlerName) +// handleDeadline creates a deadline handler that will delete all resources with the scope after the deadline +func (k *Knuu) handleDeadline(ctx context.Context, deadline time.Time, deadlineHandlerName string) error { + inst, err := k.NewInstance(deadlineHandlerName) if err != nil { return ErrCannotCreateInstance.Wrap(err) } inst.SetInstanceType(instance.TimeoutHandlerInstance) - if err := inst.Build().SetImage(ctx, timeoutHandlerImage); err != nil { + if err := inst.Build().SetImage(ctx, deadlineHandlerImage); err != nil { return ErrCannotSetImage.Wrap(err) } if err := inst.Build().Commit(ctx); err != nil { @@ -134,7 +141,8 @@ func (k *Knuu) handleTimeout(ctx context.Context, timeout time.Duration, timeout // Wait for a specific period before executing the next operation. // This is useful to ensure that any previous operation has time to complete. - commands = append(commands, fmt.Sprintf("sleep %d", int64(timeout.Seconds()))) + commands = append(commands, fmt.Sprintf(`t=$(( %d - $(date +%%s) )); if [ "$t" -gt 0 ]; then sleep "$t"; fi`, deadline.Unix())) + // Collects all resources (pods, services, etc.) within the specified namespace that match a specific label, excluding certain types, // and then deletes them. This is useful for cleaning up specific test resources before proceeding to delete the namespace. commands = append(commands, diff --git a/pkg/knuu/knuu_test.go b/pkg/knuu/knuu_test.go index b2c5dd5..55c10db 100644 --- a/pkg/knuu/knuu_test.go +++ b/pkg/knuu/knuu_test.go @@ -74,7 +74,7 @@ func TestNew(t *testing.T) { assert.NotNil(t, k.K8sClient) assert.NotNil(t, k.ImageBuilder) assert.NotEmpty(t, k.Scope) - assert.Equal(t, defaultTimeout, defaultTimeout, timeoutHandlerName) + assert.Equal(t, defaultTimeout, defaultTimeout, deadlineHandlerName) }, }, {