Skip to content

Commit

Permalink
Add supportive function for kubedb restoring phase (#133)
Browse files Browse the repository at this point in the history
* Add supportive function for kubedb `restoring` phase

Signed-off-by: Anisur Rahman <[email protected]>

* Add tasks related constants

Signed-off-by: Anisur Rahman <[email protected]>

* update deps

Signed-off-by: Anisur Rahman <[email protected]>

* Add Singlestore manifest option

Signed-off-by: Anisur Rahman <[email protected]>

* Add kubedbDB manged DB kind as constant

Signed-off-by: Anisur Rahman <[email protected]>

---------

Signed-off-by: Anisur Rahman <[email protected]>
  • Loading branch information
anisurrahman75 authored Sep 25, 2024
1 parent 6f40843 commit 86c6124
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 4 deletions.
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 {
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,
}
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,
}
}
}

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

0 comments on commit 86c6124

Please sign in to comment.