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 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
32 changes: 31 additions & 1 deletion apis/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ limitations under the License.

package apis

import "time"
import (
"time"
)

const (
KubeStashKey = "kubestash.com"
Expand Down Expand Up @@ -158,3 +160,31 @@ 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"
)

// KubeDB managed databases Kind
const (
KindMySQL = "MySQL"
KindPostgres = "Postgres"
KindMongoDB = "MongoDB"
KindMariaDB = "MariaDB"
KindRedis = "Redis"
KindMSSQLServer = "MSSQLServer"
KindDruid = "Druid"
KindZooKeeper = "ZooKeeper"
KindSinglestore = "Singlestore"
KindElasticsearch = "Elasticsearch"
)
95 changes: 92 additions & 3 deletions apis/core/v1alpha1/restoresession_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ package v1alpha1
import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
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 +193,91 @@ 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,
}
}

objRef := kmapi.ObjectReference{
Name: snap.Spec.AppRef.Name,
Namespace: snap.Spec.AppRef.Namespace,
}
targetRef := rs.getRestoreNamespacedName(snap.Spec.AppRef.Kind)
if targetRef.Namespace != "" {
objRef.Namespace = targetRef.Namespace
}
if targetRef.Name != "" {
objRef.Name = targetRef.Name
}

return &objRef
}

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]
}

func (rs *RestoreSession) getRestoreNamespacedName(targetKind string) *types.NamespacedName {
var ref types.NamespacedName
if rs.Spec.ManifestOptions != nil {
opt := rs.Spec.ManifestOptions
switch {
case targetKind == apis.KindMySQL && opt.MySQL != nil:
ref = types.NamespacedName{
Name: opt.MySQL.DBName,
Namespace: opt.MySQL.RestoreNamespace,
anisurrahman75 marked this conversation as resolved.
Show resolved Hide resolved
}
case targetKind == apis.KindPostgres && opt.Postgres != nil:
ref = types.NamespacedName{
Namespace: opt.Postgres.RestoreNamespace,
Name: opt.Postgres.DBName,
}
case targetKind == apis.KindMongoDB && opt.MongoDB != nil:
ref = types.NamespacedName{
Namespace: opt.MongoDB.RestoreNamespace,
Name: opt.MongoDB.DBName,
}
case targetKind == apis.KindMariaDB && opt.MariaDB != nil:
ref = types.NamespacedName{
Namespace: opt.MariaDB.RestoreNamespace,
Name: opt.MariaDB.DBName,
}
case targetKind == apis.KindRedis && opt.Redis != nil:
ref = types.NamespacedName{
Namespace: opt.Redis.RestoreNamespace,
Name: opt.Redis.DBName,
}
case targetKind == apis.KindMSSQLServer && opt.MSSQLServer != nil:
ref = types.NamespacedName{
Namespace: opt.MSSQLServer.RestoreNamespace,
Name: opt.MSSQLServer.DBName,
}
case targetKind == apis.KindDruid && opt.Druid != nil:
ref = types.NamespacedName{
Namespace: opt.Druid.RestoreNamespace,
Name: opt.Druid.DBName,
}
case targetKind == apis.KindZooKeeper && opt.ZooKeeper != nil:
ref = types.NamespacedName{
Namespace: opt.ZooKeeper.RestoreNamespace,
Name: opt.ZooKeeper.DBName,
}
case targetKind == apis.KindSinglestore && opt.Singlestore != nil:
ref = types.NamespacedName{
Namespace: opt.Singlestore.RestoreNamespace,
Name: opt.Singlestore.DBName,
}
}
}
anisurrahman75 marked this conversation as resolved.
Show resolved Hide resolved

return &ref
}
4 changes: 4 additions & 0 deletions apis/core/v1alpha1/restoresession_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ type ManifestRestoreOptions struct {
// +optional
ZooKeeper *KubeDBManifestOptions `json:"zooKeeper,omitempty"`

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

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