Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add deadline instead of timeout #599

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions e2e/sidecars/suite_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
42 changes: 25 additions & 17 deletions pkg/knuu/knuu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}
Expand All @@ -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)
}

Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pkg/knuu/knuu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Revisit the usage of assert.Equal here.

The current invocation, assert.Equal(t, defaultTimeout, defaultTimeout, deadlineHandlerName), compares defaultTimeout with itself and uses deadlineHandlerName as the message. This has no real assertion effect. Consider adjusting it if your goal was to test that the defaultTimeout is applied or to ensure that the correct handler name is used.

- assert.Equal(t, defaultTimeout, defaultTimeout, deadlineHandlerName)
+ // Example fix if validating actual usage of defaultTimeout and handler name
+ assert.Equal(t, defaultTimeout, someComputedTimeout, "defaultTimeout should match the expected fallback")
+ assert.Equal(t, "deadline-handler", deadlineHandlerName, "Invalid or missing handler name")

Committable suggestion skipped: line range outside the PR's diff.

},
},
{
Expand Down
Loading