Skip to content

Commit

Permalink
Add kubedbDB manged DB kind as constant
Browse files Browse the repository at this point in the history
Signed-off-by: Anisur Rahman <[email protected]>
  • Loading branch information
anisurrahman75 committed Sep 23, 2024
1 parent a64c73a commit 0e390f8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 35 deletions.
18 changes: 17 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 @@ -172,3 +174,17 @@ const (

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"
)
83 changes: 49 additions & 34 deletions apis/core/v1alpha1/restoresession_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1alpha1
import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
kmapi "kmodules.xyz/client-go/api/v1"
"kmodules.xyz/client-go/apiextensions"
cutil "kmodules.xyz/client-go/conditions"
Expand Down Expand Up @@ -201,68 +202,82 @@ func (rs *RestoreSession) GetTargetObjectRef(snap *v1alpha1.Snapshot) *kmapi.Obj
}
}

var ref kmapi.ObjectReference
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 opt.MySQL != nil:
ref = kmapi.ObjectReference{
Namespace: opt.MySQL.RestoreNamespace,
case targetKind == apis.KindMySQL && opt.MySQL != nil:
ref = types.NamespacedName{
Name: opt.MySQL.DBName,
Namespace: opt.MySQL.RestoreNamespace,
}
case opt.Postgres != nil:
ref = kmapi.ObjectReference{
case targetKind == apis.KindPostgres && opt.Postgres != nil:
ref = types.NamespacedName{
Namespace: opt.Postgres.RestoreNamespace,
Name: opt.Postgres.DBName,
}
case opt.MongoDB != nil:
ref = kmapi.ObjectReference{
case targetKind == apis.KindMongoDB && opt.MongoDB != nil:
ref = types.NamespacedName{
Namespace: opt.MongoDB.RestoreNamespace,
Name: opt.MongoDB.DBName,
}
case opt.MariaDB != nil:
ref = kmapi.ObjectReference{
case targetKind == apis.KindMariaDB && opt.MariaDB != nil:
ref = types.NamespacedName{
Namespace: opt.MariaDB.RestoreNamespace,
Name: opt.MariaDB.DBName,
}
case opt.Redis != nil:
ref = kmapi.ObjectReference{
case targetKind == apis.KindRedis && opt.Redis != nil:
ref = types.NamespacedName{
Namespace: opt.Redis.RestoreNamespace,
Name: opt.Redis.DBName,
}
case opt.MSSQLServer != nil:
ref = kmapi.ObjectReference{
case targetKind == apis.KindMSSQLServer && opt.MSSQLServer != nil:
ref = types.NamespacedName{
Namespace: opt.MSSQLServer.RestoreNamespace,
Name: opt.MSSQLServer.DBName,
}
case opt.Druid != nil:
ref = kmapi.ObjectReference{
case targetKind == apis.KindDruid && opt.Druid != nil:
ref = types.NamespacedName{
Namespace: opt.Druid.RestoreNamespace,
Name: opt.Druid.DBName,
}
case opt.ZooKeeper != nil:
ref = kmapi.ObjectReference{
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,
}
}
}

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

0 comments on commit 0e390f8

Please sign in to comment.