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 supportive function for kubedb restoring phase #133

Merged
merged 5 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 14 additions & 0 deletions apis/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,17 @@ const (
AnnKubeDBAppVersion = "kubedb.com/db-version"
AnnRestoreSessionBeneficiary = "restoresession.kubestash.com/beneficiary"
)

// Tasks name related constants
const (
LogicalBackup = "logical-backup"
LogicalBackupRestore = "logical-backup-restore"

ManifestBackup = "manifest-backup"
ManifestRestore = "manifest-restore"

VolumeSnapshot = "volume-snapshot"
VolumeSnapshotRestore = "volume-snapshot-restore"

VolumeClone = "volume-clone"
)
80 changes: 77 additions & 3 deletions apis/core/v1alpha1/restoresession_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kmapi "kmodules.xyz/client-go/api/v1"
"kubestash.dev/apimachinery/apis"
"kubestash.dev/apimachinery/crds"

"kmodules.xyz/client-go/apiextensions"
cutil "kmodules.xyz/client-go/conditions"
meta_util "kmodules.xyz/client-go/meta"
"kubestash.dev/apimachinery/apis"
"kubestash.dev/apimachinery/apis/storage/v1alpha1"
"kubestash.dev/apimachinery/crds"
)

func (_ RestoreSession) CustomResourceDefinition() *apiextensions.CustomResourceDefinition {
Expand Down Expand Up @@ -192,3 +192,77 @@ func (rs *RestoreSession) GetRemainingTimeoutDuration() (*metav1.Duration, error
}
return &metav1.Duration{Duration: rs.Status.RestoreDeadline.Sub(currentTime.Time)}, nil
}

func (rs *RestoreSession) GetTargetObjectRef(snap *v1alpha1.Snapshot) *kmapi.ObjectReference {
anisurrahman75 marked this conversation as resolved.
Show resolved Hide resolved
if rs.Spec.Target != nil {
return &kmapi.ObjectReference{
Namespace: rs.Spec.Target.Namespace,
Name: rs.Spec.Target.Name,
}
}

var ref kmapi.ObjectReference
if rs.Spec.ManifestOptions != nil {
opt := rs.Spec.ManifestOptions
switch {
case opt.MySQL != nil:
ref = kmapi.ObjectReference{
Namespace: opt.MySQL.RestoreNamespace,
Name: opt.MySQL.DBName,
}
case opt.Postgres != nil:
ref = kmapi.ObjectReference{
Namespace: opt.Postgres.RestoreNamespace,
Name: opt.Postgres.DBName,
}
case opt.MongoDB != nil:
ref = kmapi.ObjectReference{
Namespace: opt.MongoDB.RestoreNamespace,
Name: opt.MongoDB.DBName,
}
case opt.MariaDB != nil:
ref = kmapi.ObjectReference{
Namespace: opt.MariaDB.RestoreNamespace,
Name: opt.MariaDB.DBName,
}
case opt.Redis != nil:
ref = kmapi.ObjectReference{
Namespace: opt.Redis.RestoreNamespace,
Name: opt.Redis.DBName,
}
case opt.MSSQLServer != nil:
ref = kmapi.ObjectReference{
Namespace: opt.MSSQLServer.RestoreNamespace,
Name: opt.MSSQLServer.DBName,
}
case opt.Druid != nil:
ref = kmapi.ObjectReference{
Namespace: opt.Druid.RestoreNamespace,
Name: opt.Druid.DBName,
}
case opt.ZooKeeper != nil:
ref = kmapi.ObjectReference{
Namespace: opt.ZooKeeper.RestoreNamespace,
Name: opt.ZooKeeper.DBName,
}
}
}
anisurrahman75 marked this conversation as resolved.
Show resolved Hide resolved

if ref.Name == "" {
ref.Name = snap.Spec.AppRef.Name
}
if ref.Namespace == "" {
ref.Namespace = snap.Spec.AppRef.Namespace
}

return &ref
}

func (rs *RestoreSession) IsApplicationLevelRestore() bool {
tasks := map[string]bool{}
for _, task := range rs.Spec.Addon.Tasks {
tasks[task.Name] = true
}

return tasks[apis.ManifestRestore] && tasks[apis.LogicalBackupRestore]
}
Loading