Skip to content

Commit

Permalink
Add timeout method
Browse files Browse the repository at this point in the history
Signed-off-by: Md. Ishtiaq Islam <[email protected]>
  • Loading branch information
ishtiaqhimel committed Sep 12, 2024
1 parent b528254 commit f2681d3
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 40 deletions.
5 changes: 4 additions & 1 deletion apis/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ const (
KubeStashAppRefKind = "kubestash.com/app-ref-kind"
KubeStashAppRefNamespace = "kubestash.com/app-ref-namespace"
KubeStashAppRefName = "kubestash.com/app-ref-name"
KubeDBAppVersion = "kubedb.com/db-version"
)

const (
AnnKubeDBAppVersion = "kubedb.com/db-version"
)

// Keys for structure logging
Expand Down
4 changes: 2 additions & 2 deletions apis/core/v1alpha1/backupconfiguration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ type SessionConfig struct {
// +optional
RetryConfig *RetryConfig `json:"retryConfig,omitempty"`

// Timeout specifies the maximum duration of backup. BackupSession will be considered Failed
// BackupTimeout specifies the maximum duration of backup. Backup will be considered Failed
// if backup does not complete within this time limit. By default, KubeStash don't set any timeout for backup.
// +optional
Timeout *metav1.Duration `json:"timeout,omitempty"`
BackupTimeout *metav1.Duration `json:"backupTimeout,omitempty"`

// SessionHistoryLimit specifies how many backup Jobs and associate resources KubeStash should keep for debugging purpose.
// The default value is 1.
Expand Down
4 changes: 2 additions & 2 deletions apis/core/v1alpha1/backupsession_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ type BackupSessionSpec struct {
// +optional
RetryLeft int32 `json:"retryLeft,omitempty"`

// Timeout specifies the maximum duration of backup. Backup will be considered Failed
// BackupTimeout specifies the maximum duration of backup. Backup will be considered Failed
// if backup does not complete within this time limit. By default, KubeStash don't set any timeout for backup.
// +optional
Timeout *metav1.Duration `json:"timeout,omitempty"`
BackupTimeout *metav1.Duration `json:"timeout,omitempty"`
}

// BackupSessionStatus defines the observed state of BackupSession
Expand Down
4 changes: 2 additions & 2 deletions apis/core/v1alpha1/restoresession_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ type RestoreSessionSpec struct {
// +optional
Hooks *RestoreHooks `json:"hooks,omitempty"`

// Timeout specifies a duration that KubeStash should wait for the restore to be completed.
// RestoreTimeout specifies a duration that KubeStash should wait for the restore to be completed.
// If the restore does not finish within this time period, KubeStash will consider this restore as a failure.
// +optional
Timeout *metav1.Duration `json:"timeout,omitempty"`
RestoreTimeout *metav1.Duration `json:"restoreTimeout,omitempty"`

// ManifestOptions provide options to select particular manifest object to restore
// +optional
Expand Down
12 changes: 6 additions & 6 deletions apis/core/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions apis/storage/v1alpha1/snapshot_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1alpha1

import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"kmodules.xyz/client-go/apiextensions"
cutil "kmodules.xyz/client-go/conditions"
"kmodules.xyz/client-go/meta"
Expand Down Expand Up @@ -187,3 +188,15 @@ func (s *Snapshot) OffshootLabels() map[string]string {
func (s *Snapshot) GetComponentPath(componentName string) string {
return filepath.Join(apis.DirRepository, s.Spec.Version, s.Spec.Session, componentName)
}

func (s *Snapshot) GetRemainingTimeoutDuration(timeout *metav1.Duration) (*metav1.Duration, error) {
if timeout == nil {
return nil, nil
}

diff := metav1.Now().Sub(s.GetCreationTimestamp().Time)
if diff < timeout.Duration {
return &metav1.Duration{Duration: timeout.Duration - diff}, nil
}
return nil, fmt.Errorf("deadline exceeded")
}
12 changes: 6 additions & 6 deletions crds/core.kubestash.com_backupbatches.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ spec:
description: BatchSession specifies the session configuration for
the targets.
properties:
backupTimeout:
description: BackupTimeout specifies the maximum duration of
backup. Backup will be considered Failed if backup does not
complete within this time limit. By default, KubeStash don't
set any timeout for backup.
type: string
hooks:
description: Hooks specifies the backup hooks that should be
executed before and/or after the backup.
Expand Down Expand Up @@ -36194,12 +36200,6 @@ spec:
type: array
type: object
type: array
timeout:
description: Timeout specifies the maximum duration of backup.
BackupSession will be considered Failed if backup does not
complete within this time limit. By default, KubeStash don't
set any timeout for backup.
type: string
type: object
type: array
targets:
Expand Down
12 changes: 6 additions & 6 deletions crds/core.kubestash.com_backupblueprints.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15716,6 +15716,12 @@ spec:
type: object
type: array
type: object
backupTimeout:
description: BackupTimeout specifies the maximum duration
of backup. Backup will be considered Failed if backup
does not complete within this time limit. By default,
KubeStash don't set any timeout for backup.
type: string
hooks:
description: Hooks specifies the backup hooks that should
be executed before and/or after the backup.
Expand Down Expand Up @@ -37239,12 +37245,6 @@ spec:
debugging purpose. The default value is 1.
format: int32
type: integer
timeout:
description: Timeout specifies the maximum duration of backup.
BackupSession will be considered Failed if backup does
not complete within this time limit. By default, KubeStash
don't set any timeout for backup.
type: string
type: object
type: array
type: object
Expand Down
12 changes: 6 additions & 6 deletions crds/core.kubestash.com_backupconfigurations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14554,6 +14554,12 @@ spec:
type: object
type: array
type: object
backupTimeout:
description: BackupTimeout specifies the maximum duration of
backup. Backup will be considered Failed if backup does not
complete within this time limit. By default, KubeStash don't
set any timeout for backup.
type: string
hooks:
description: Hooks specifies the backup hooks that should be
executed before and/or after the backup.
Expand Down Expand Up @@ -34373,12 +34379,6 @@ spec:
purpose. The default value is 1.
format: int32
type: integer
timeout:
description: Timeout specifies the maximum duration of backup.
BackupSession will be considered Failed if backup does not
complete within this time limit. By default, KubeStash don't
set any timeout for backup.
type: string
type: object
type: array
target:
Expand Down
7 changes: 4 additions & 3 deletions crds/core.kubestash.com_backupsessions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ spec:
this backup
type: string
timeout:
description: Timeout specifies the maximum duration of backup. Backup
will be considered Failed if backup does not complete within this
time limit. By default, KubeStash don't set any timeout for backup.
description: BackupTimeout specifies the maximum duration of backup.
Backup will be considered Failed if backup does not complete within
this time limit. By default, KubeStash don't set any timeout for
backup.
type: string
type: object
status:
Expand Down
12 changes: 6 additions & 6 deletions crds/core.kubestash.com_restoresessions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24312,6 +24312,12 @@ spec:
restored files will be applied
type: string
type: object
restoreTimeout:
description: RestoreTimeout specifies a duration that KubeStash should
wait for the restore to be completed. If the restore does not finish
within this time period, KubeStash will consider this restore as
a failure.
type: string
target:
description: Target indicates the target application where the data
will be restored. The target must be in the same namespace as the
Expand All @@ -24330,12 +24336,6 @@ spec:
required:
- name
type: object
timeout:
description: Timeout specifies a duration that KubeStash should wait
for the restore to be completed. If the restore does not finish
within this time period, KubeStash will consider this restore as
a failure.
type: string
type: object
status:
description: RestoreSessionStatus defines the observed state of RestoreSession
Expand Down
33 changes: 33 additions & 0 deletions pkg/restic/restic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"testing"
"time"
)

var (
Expand Down Expand Up @@ -462,6 +463,38 @@ func TestBackupRestoreWithArgs(t *testing.T) {
}
}

func TestBackupWithTimeout(t *testing.T) {
tempDir, err := os.MkdirTemp("", "stash-unit-test-")
if err != nil {
t.Error(err)
return
}

w, err := setupTest(tempDir)
if err != nil {
t.Error(err)
return
}
defer cleanup(tempDir)

// Initialize Repository
err = w.InitializeRepository()
if err != nil {
t.Error(err)
return
}

duration := metav1.Duration{Duration: 10 * time.Millisecond}
w.config.Timeout = &duration

backupOpt := BackupOptions{
StdinPipeCommands: []Command{stdinPipeCommand},
StdinFileName: fileName,
}
_, err = w.RunBackup(backupOpt)
assert.Error(t, err, "Timeout error")
}

func TestVerifyRepositoryIntegrity(t *testing.T) {
tempDir, err := os.MkdirTemp("", "stash-unit-test-")
if err != nil {
Expand Down

0 comments on commit f2681d3

Please sign in to comment.