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

Add support for timeout for restic backup and restore #130

Merged
merged 5 commits into from
Sep 18, 2024
Merged
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
1 change: 1 addition & 0 deletions apis/addons/v1alpha1/zz_generated.deepcopy.go

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

1 change: 1 addition & 0 deletions apis/config/v1alpha1/zz_generated.deepcopy.go

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

7 changes: 6 additions & 1 deletion apis/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ const (
KubeStashAppRefKind = "kubestash.com/app-ref-kind"
KubeStashAppRefNamespace = "kubestash.com/app-ref-namespace"
KubeStashAppRefName = "kubestash.com/app-ref-name"
KubeDBAppVersion = "kubedb.com/db-version"
)

// Keys for structure logging
Expand Down Expand Up @@ -153,3 +152,9 @@ const (
SnapshotVersionV1 = "v1"
DirRepository = "repository"
)

// Annotations
const (
AnnKubeDBAppVersion = "kubedb.com/db-version"
AnnRestoreSessionBeneficiary = "restoresession.kubestash.com/beneficiary"
)
6 changes: 3 additions & 3 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
// if backup does not complete within this time limit. By default, KubeStash don't set any timeout for backup.
// BackupTimeout specifies the maximum duration of backup. Backup will be considered Failed
// if backup tasks do 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
11 changes: 11 additions & 0 deletions apis/core/v1alpha1/backupsession_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,14 @@ func (b *BackupSession) checkFailureInRetentionPolicy() (bool, string) {
}
return false, ""
}

func (b *BackupSession) GetRemainingTimeoutDuration() (*metav1.Duration, error) {
if b.Spec.BackupTimeout == nil || b.Status.BackupDeadline == nil {
return nil, nil
}
currentTime := metav1.Now()
if b.Status.BackupDeadline.Before(&currentTime) {
return nil, fmt.Errorf("deadline exceeded")
}
return &metav1.Duration{Duration: b.Status.BackupDeadline.Sub(currentTime.Time)}, nil
}
11 changes: 8 additions & 3 deletions apis/core/v1alpha1/backupsession_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ type BackupSessionSpec struct {
// If this set to non-zero, KubeStash will create a new BackupSession if the current one fails.
// +optional
RetryLeft int32 `json:"retryLeft,omitempty"`

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

// BackupSessionStatus defines the observed state of BackupSession
Expand All @@ -75,10 +80,10 @@ type BackupSessionStatus struct {
// +optional
Duration string `json:"duration,omitempty"`

// Deadline specifies the deadline of backup. BackupSession will be
// considered Failed if backup does not complete within this deadline
// BackupDeadline specifies the deadline of backup. Backup will be
// considered Failed if it does not complete within this deadline
// +optional
Deadline *metav1.Time `json:"sessionDeadline,omitempty"`
BackupDeadline *metav1.Time `json:"backupDeadline,omitempty"`

// TotalSnapshots specifies the total number of snapshots created for this backupSession.
// +optional
Expand Down
15 changes: 13 additions & 2 deletions apis/core/v1alpha1/restoresession_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha1

import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kmapi "kmodules.xyz/client-go/api/v1"
"kubestash.dev/apimachinery/apis"
Expand All @@ -41,8 +42,7 @@ func (rs *RestoreSession) CalculatePhase() RestorePhase {
}

if cutil.IsConditionTrue(rs.Status.Conditions, TypeMetricsPushed) &&
(cutil.IsConditionTrue(rs.Status.Conditions, TypeDeadlineExceeded) ||
cutil.IsConditionFalse(rs.Status.Conditions, TypePreRestoreHooksExecutionSucceeded) ||
(cutil.IsConditionFalse(rs.Status.Conditions, TypePreRestoreHooksExecutionSucceeded) ||
cutil.IsConditionFalse(rs.Status.Conditions, TypePostRestoreHooksExecutionSucceeded) ||
cutil.IsConditionFalse(rs.Status.Conditions, TypeRestoreExecutorEnsured)) {
return RestoreFailed
Expand Down Expand Up @@ -181,3 +181,14 @@ func (rs *RestoreSession) GetDataSourceNamespace() string {
}
return rs.Spec.DataSource.Namespace
}

func (rs *RestoreSession) GetRemainingTimeoutDuration() (*metav1.Duration, error) {
if rs.Spec.RestoreTimeout == nil || rs.Status.RestoreDeadline == nil {
return nil, nil
}
currentTime := metav1.Now()
if rs.Status.RestoreDeadline.Before(&currentTime) {
return nil, fmt.Errorf("deadline exceeded")
}
return &metav1.Duration{Duration: rs.Status.RestoreDeadline.Sub(currentTime.Time)}, nil
}
19 changes: 0 additions & 19 deletions apis/core/v1alpha1/restoresession_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,25 +230,6 @@ func TestRestoreSessionPhaseIsFailedIfRestoreExecutorEnsuredConditionIsFalse(t *
assert.Equal(t, RestoreFailed, rs.CalculatePhase())
}

func TestRestoreSessionPhaseIsFailedIfDeadlineExceededConditionIsTrue(t *testing.T) {
rs := sampleRestoreSession(func(r *RestoreSession) {
r.Status.Conditions = append(r.Status.Conditions,
kmapi.Condition{
Type: TypeDeadlineExceeded,
Status: metav1.ConditionTrue,
Reason: ReasonFailedToCompleteWithinDeadline,
},
kmapi.Condition{
Type: TypeMetricsPushed,
Status: metav1.ConditionTrue,
Reason: ReasonSuccessfullyPushedMetrics,
},
)
})

assert.Equal(t, RestoreFailed, rs.CalculatePhase())
}

func TestRestoreSessionPhaseIsRunningIfPostRestoreHooksNotExecuted(test *testing.T) {
rs := sampleRestoreSession(func(r *RestoreSession) {
r.Status.Components = map[string]ComponentRestoreStatus{
Expand Down
16 changes: 10 additions & 6 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 session execution to be completed.
// If the session execution does not finish within this time period, KubeStash will consider this session as a failure.
// RestoreTimeout specifies a duration that KubeStash should wait for the restore to be completed.
// If the restore tasks do 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 Expand Up @@ -106,6 +106,10 @@ type ManifestRestoreOptions struct {
// ZooKeeper specifies the options for selecting particular ZooKeeper components to restore in manifest restore
// +optional
ZooKeeper *KubeDBManifestOptions `json:"zooKeeper,omitempty"`

// Redis specifies the options for selecting particular Redis components to restore in manifest restore
// +optional
Redis *KubeDBManifestOptions `json:"redis,omitempty"`
}

type MSSQLServerManifestOptions struct {
Expand Down Expand Up @@ -278,10 +282,10 @@ type RestoreSessionStatus struct {
// +optional
Duration string `json:"duration,omitempty"`

// Deadline specifies a timestamp till this session is valid. If the session does not complete within this deadline,
// it will be considered as failed.
// RestoreDeadline specifies the deadline of restore. Restore will be
// considered Failed if it does not complete within this deadline
// +optional
Deadline *metav1.Time `json:"deadline,omitempty"`
RestoreDeadline *metav1.Time `json:"restoreDeadline,omitempty"`

// TotalComponents represents the number of total components for this RestoreSession
// +optional
Expand Down
3 changes: 0 additions & 3 deletions apis/core/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,6 @@ type RetryConfig struct {
}

const (
TypeDeadlineExceeded = "DeadlineExceeded"
ReasonFailedToCompleteWithinDeadline = "FailedToCompleteWithinDeadline"

// TypeMetricsPushed indicates whether Metrics are pushed or not
TypeMetricsPushed = "MetricsPushed"
ReasonSuccessfullyPushedMetrics = "SuccessfullyPushedMetrics"
Expand Down
27 changes: 19 additions & 8 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.

1 change: 1 addition & 0 deletions apis/storage/v1alpha1/zz_generated.deepcopy.go

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

1 change: 1 addition & 0 deletions apis/zz_generated.deepcopy.go

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

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 tasks do
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
tasks do 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 tasks do
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
17 changes: 11 additions & 6 deletions crds/core.kubestash.com_backupsessions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ spec:
description: BackupSessionSpec specifies the information related to the
respective backup invoker and session.
properties:
backupTimeout:
description: BackupTimeout specifies the maximum duration of backup.
Backup will be considered Failed if backup tasks do not complete
within this time limit. By default, KubeStash don't set any timeout
for backup.
type: string
invoker:
description: Invoker points to the respective BackupConfiguration
or BackupBatch which is responsible for triggering this backup.
Expand Down Expand Up @@ -91,6 +97,11 @@ spec:
status:
description: BackupSessionStatus defines the observed state of BackupSession
properties:
backupDeadline:
description: BackupDeadline specifies the deadline of backup. Backup
will be considered Failed if it does not complete within this deadline
format: date-time
type: string
conditions:
description: Conditions represents list of conditions regarding this
BackupSession
Expand Down Expand Up @@ -249,12 +260,6 @@ spec:
not. This field will exist only if the `retryConfig` has been set
in the respective backup invoker.
type: boolean
sessionDeadline:
description: Deadline specifies the deadline of backup. BackupSession
will be considered Failed if backup does not complete within this
deadline
format: date-time
type: string
snapshots:
description: Snapshots specifies the Snapshots status
items:
Expand Down
Loading
Loading