diff --git a/apis/constant.go b/apis/constant.go index f47f5a05..5d59e8b7 100644 --- a/apis/constant.go +++ b/apis/constant.go @@ -16,7 +16,9 @@ limitations under the License. package apis -import "time" +import ( + "time" +) const ( KubeStashKey = "kubestash.com" @@ -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" +) diff --git a/apis/core/v1alpha1/restoresession_helpers.go b/apis/core/v1alpha1/restoresession_helpers.go index 213d70fa..a3579a3b 100644 --- a/apis/core/v1alpha1/restoresession_helpers.go +++ b/apis/core/v1alpha1/restoresession_helpers.go @@ -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" @@ -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] -}