From 054a2ace00cc2dbd5e37769f2db60aa9c15b7dde Mon Sep 17 00:00:00 2001 From: SK Ali Arman Date: Tue, 7 May 2024 18:54:25 +0600 Subject: [PATCH] Refactor Signed-off-by: SK Ali Arman --- pkg/cmds/pause.go | 1 + pkg/pauser/archiver.go | 173 ++++++++++++++++++++++++++++++++++++++++ pkg/pauser/mariadb.go | 40 +++++++--- pkg/pauser/mongodb.go | 32 +++++--- pkg/pauser/mysql.go | 32 +++++--- pkg/pauser/pauser.go | 2 +- pkg/pauser/postgres.go | 32 +++++--- pkg/pauser/util.go | 135 ------------------------------- pkg/resumer/mariadb.go | 42 +++++++--- pkg/resumer/mongodb.go | 32 +++++--- pkg/resumer/mysql.go | 32 +++++--- pkg/resumer/postgres.go | 32 +++++--- pkg/resumer/resumer.go | 2 +- 13 files changed, 362 insertions(+), 225 deletions(-) create mode 100644 pkg/pauser/archiver.go diff --git a/pkg/cmds/pause.go b/pkg/cmds/pause.go index 4fa48e448..f3b0cf829 100644 --- a/pkg/cmds/pause.go +++ b/pkg/cmds/pause.go @@ -183,6 +183,7 @@ func (o *PauseOptions) Run() error { } allErrs = append(allErrs, err) errs.Insert(err.Error()) + continue } pauseAll := !(o.onlyBackup || o.onlyDb || o.onlyArchiver) diff --git a/pkg/pauser/archiver.go b/pkg/pauser/archiver.go new file mode 100644 index 000000000..964b9f6e9 --- /dev/null +++ b/pkg/pauser/archiver.go @@ -0,0 +1,173 @@ +/* +Copyright AppsCode Inc. and Contributors + +Licensed under the AppsCode Community License 1.0.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package pauser + +import ( + "context" + + coreapi "kubedb.dev/apimachinery/apis/archiver/v1alpha1" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + kmapi "kmodules.xyz/client-go/api/v1" + kmc "kmodules.xyz/client-go/client" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +func PauseOrResumeMySQLArchiver(klient client.Client, value bool, reference kmapi.ObjectReference) error { + name := reference.Name + namespace := reference.Namespace + archiver, err := getMysqlArchiver(klient, kmapi.ObjectReference{ + Name: name, + Namespace: namespace, + }) + if err != nil { + return err + } + _, err = kmc.CreateOrPatch( + context.Background(), + klient, + archiver, + func(obj client.Object, createOp bool) client.Object { + in := obj.(*coreapi.MySQLArchiver) + in.Spec.Pause = value + return in + }, + ) + return err +} + +func getMysqlArchiver(klient client.Client, ref kmapi.ObjectReference) (*coreapi.MySQLArchiver, error) { + archiver := &coreapi.MySQLArchiver{ + ObjectMeta: metav1.ObjectMeta{ + Name: ref.Name, + Namespace: ref.Namespace, + }, + } + if err := klient.Get(context.Background(), client.ObjectKeyFromObject(archiver), archiver); err != nil { + return nil, err + } + return archiver, nil +} + +func PauseOrResumeMariaDBArchiver(klient client.Client, value bool, reference kmapi.ObjectReference) error { + name := reference.Name + namespace := reference.Namespace + archiver, err := getMariaDBArchiver(klient, kmapi.ObjectReference{ + Name: name, + Namespace: namespace, + }) + if err != nil { + return err + } + _, err = kmc.CreateOrPatch( + context.Background(), + klient, + archiver, + func(obj client.Object, createOp bool) client.Object { + in := obj.(*coreapi.MariaDBArchiver) + in.Spec.Pause = value + return in + }, + ) + return err +} + +func getMariaDBArchiver(klient client.Client, ref kmapi.ObjectReference) (*coreapi.MariaDBArchiver, error) { + archiver := &coreapi.MariaDBArchiver{ + ObjectMeta: metav1.ObjectMeta{ + Name: ref.Name, + Namespace: ref.Namespace, + }, + } + if err := klient.Get(context.Background(), client.ObjectKeyFromObject(archiver), archiver); err != nil { + return nil, err + } + + return archiver, nil +} + +func PauseOrResumePostgresArchiver(klient client.Client, value bool, reference kmapi.ObjectReference) error { + name := reference.Name + namespace := reference.Namespace + archiver, err := getPostgresArchiver(klient, kmapi.ObjectReference{ + Name: name, + Namespace: namespace, + }) + if err != nil { + return err + } + _, err = kmc.CreateOrPatch( + context.Background(), + klient, + archiver, + func(obj client.Object, createOp bool) client.Object { + in := obj.(*coreapi.PostgresArchiver) + in.Spec.Pause = value + return in + }, + ) + return err +} + +func getPostgresArchiver(klient client.Client, ref kmapi.ObjectReference) (*coreapi.PostgresArchiver, error) { + archiver := &coreapi.PostgresArchiver{ + ObjectMeta: metav1.ObjectMeta{ + Name: ref.Name, + Namespace: ref.Namespace, + }, + } + if err := klient.Get(context.Background(), client.ObjectKeyFromObject(archiver), archiver); err != nil { + return nil, err + } + return archiver, nil +} + +func PauseOrResumeMongoDBArchiver(klient client.Client, value bool, reference kmapi.ObjectReference) error { + name := reference.Name + namespace := reference.Namespace + archiver, err := getMongoDBArchiver(klient, kmapi.ObjectReference{ + Name: name, + Namespace: namespace, + }) + if err != nil { + return err + } + _, err = kmc.CreateOrPatch( + context.Background(), + klient, + archiver, + func(obj client.Object, createOp bool) client.Object { + in := obj.(*coreapi.MongoDBArchiver) + in.Spec.Pause = value + return in + }, + ) + return err +} + +func getMongoDBArchiver(klient client.Client, ref kmapi.ObjectReference) (*coreapi.MongoDBArchiver, error) { + archiver := &coreapi.MongoDBArchiver{ + ObjectMeta: metav1.ObjectMeta{ + Name: ref.Name, + Namespace: ref.Namespace, + }, + } + if err := klient.Get(context.Background(), client.ObjectKeyFromObject(archiver), archiver); err != nil { + return nil, err + } + return archiver, nil +} diff --git a/pkg/pauser/mariadb.go b/pkg/pauser/mariadb.go index cf2f94531..0324d8714 100644 --- a/pkg/pauser/mariadb.go +++ b/pkg/pauser/mariadb.go @@ -19,6 +19,7 @@ package pauser import ( "context" + coreapi "kubedb.dev/apimachinery/apis/archiver/v1alpha1" api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2" cs "kubedb.dev/apimachinery/client/clientset/versioned/typed/kubedb/v1alpha2" dbutil "kubedb.dev/apimachinery/client/clientset/versioned/typed/kubedb/v1alpha2/util" @@ -26,18 +27,22 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/rest" + kmc "kmodules.xyz/client-go/client" condutil "kmodules.xyz/client-go/conditions" + "sigs.k8s.io/controller-runtime/pkg/client" scs "stash.appscode.dev/apimachinery/client/clientset/versioned/typed/stash/v1beta1" ) type MariaDBPauser struct { - dbClient cs.KubedbV1alpha2Interface - stashClient scs.StashV1beta1Interface - onlyDb bool - onlyBackup bool + dbClient cs.KubedbV1alpha2Interface + stashClient scs.StashV1beta1Interface + uncachedClient client.Client + onlyDb bool + onlyBackup bool + onlyArchiver bool } -func NewMariaDBPauser(clientConfig *rest.Config, onlyDb, onlyBackup bool) (*MariaDBPauser, error) { +func NewMariaDBPauser(clientConfig *rest.Config, onlyDb, onlyBackup, onlyArchiver bool) (*MariaDBPauser, error) { dbClient, err := cs.NewForConfig(clientConfig) if err != nil { return nil, err @@ -48,11 +53,18 @@ func NewMariaDBPauser(clientConfig *rest.Config, onlyDb, onlyBackup bool) (*Mari return nil, err } + uncachedClient, err := kmc.NewUncachedClient(clientConfig, coreapi.AddToScheme) + if err != nil { + return nil, err + } + return &MariaDBPauser{ - dbClient: dbClient, - stashClient: stashClient, - onlyDb: onlyDb, - onlyBackup: onlyBackup, + dbClient: dbClient, + stashClient: stashClient, + uncachedClient: uncachedClient, + onlyDb: onlyDb, + onlyBackup: onlyBackup, + onlyArchiver: onlyArchiver, }, nil } @@ -62,7 +74,15 @@ func (e *MariaDBPauser) Pause(name, namespace string) (bool, error) { return false, nil } - pauseAll := !(e.onlyBackup || e.onlyDb) + pauseAll := !(e.onlyBackup || e.onlyDb || e.onlyArchiver) + if e.onlyArchiver || pauseAll { + if err := PauseOrResumeMariaDBArchiver(e.uncachedClient, true, db.Spec.Archiver.Ref); err != nil { + return false, err + } + if e.onlyArchiver { + return false, nil + } + } if e.onlyDb || pauseAll { _, err = dbutil.UpdateMariaDBStatus(context.TODO(), e.dbClient, db.ObjectMeta, func(status *api.MariaDBStatus) (types.UID, *api.MariaDBStatus) { diff --git a/pkg/pauser/mongodb.go b/pkg/pauser/mongodb.go index c7a7a3705..09298a529 100644 --- a/pkg/pauser/mongodb.go +++ b/pkg/pauser/mongodb.go @@ -19,6 +19,7 @@ package pauser import ( "context" + coreapi "kubedb.dev/apimachinery/apis/archiver/v1alpha1" api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2" cs "kubedb.dev/apimachinery/client/clientset/versioned/typed/kubedb/v1alpha2" dbutil "kubedb.dev/apimachinery/client/clientset/versioned/typed/kubedb/v1alpha2/util" @@ -26,16 +27,19 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/rest" + kmc "kmodules.xyz/client-go/client" condutil "kmodules.xyz/client-go/conditions" + "sigs.k8s.io/controller-runtime/pkg/client" scs "stash.appscode.dev/apimachinery/client/clientset/versioned/typed/stash/v1beta1" ) type MongoDBPauser struct { - dbClient cs.KubedbV1alpha2Interface - stashClient scs.StashV1beta1Interface - onlyDb bool - onlyBackup bool - onlyArchiver bool + dbClient cs.KubedbV1alpha2Interface + stashClient scs.StashV1beta1Interface + uncachedClient client.Client + onlyDb bool + onlyBackup bool + onlyArchiver bool } func NewMongoDBPauser(clientConfig *rest.Config, onlyDb, onlyBackup, onlyArchiver bool) (*MongoDBPauser, error) { @@ -49,12 +53,18 @@ func NewMongoDBPauser(clientConfig *rest.Config, onlyDb, onlyBackup, onlyArchive return nil, err } + uncachedClient, err := kmc.NewUncachedClient(clientConfig, coreapi.AddToScheme) + if err != nil { + return nil, err + } + return &MongoDBPauser{ - dbClient: dbClient, - stashClient: stashClient, - onlyDb: onlyDb, - onlyBackup: onlyBackup, - onlyArchiver: onlyArchiver, + dbClient: dbClient, + stashClient: stashClient, + uncachedClient: uncachedClient, + onlyDb: onlyDb, + onlyBackup: onlyBackup, + onlyArchiver: onlyArchiver, }, nil } @@ -66,7 +76,7 @@ func (e *MongoDBPauser) Pause(name, namespace string) (bool, error) { pauseAll := !(e.onlyBackup || e.onlyDb || e.onlyArchiver) if e.onlyArchiver || pauseAll { - if err := PauseMongoDBArchiver(true, db.Spec.Archiver.Ref.Name, db.Spec.Archiver.Ref.Namespace); err != nil { + if err := PauseOrResumeMongoDBArchiver(e.uncachedClient, true, db.Spec.Archiver.Ref); err != nil { return false, err } if e.onlyArchiver { diff --git a/pkg/pauser/mysql.go b/pkg/pauser/mysql.go index 83a32951d..d1e555c10 100644 --- a/pkg/pauser/mysql.go +++ b/pkg/pauser/mysql.go @@ -19,6 +19,7 @@ package pauser import ( "context" + coreapi "kubedb.dev/apimachinery/apis/archiver/v1alpha1" api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2" cs "kubedb.dev/apimachinery/client/clientset/versioned/typed/kubedb/v1alpha2" dbutil "kubedb.dev/apimachinery/client/clientset/versioned/typed/kubedb/v1alpha2/util" @@ -26,16 +27,19 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/rest" + kmc "kmodules.xyz/client-go/client" condutil "kmodules.xyz/client-go/conditions" + "sigs.k8s.io/controller-runtime/pkg/client" scs "stash.appscode.dev/apimachinery/client/clientset/versioned/typed/stash/v1beta1" ) type MySQLPauser struct { - dbClient cs.KubedbV1alpha2Interface - stashClient scs.StashV1beta1Interface - onlyDb bool - onlyBackup bool - onlyArchiver bool + dbClient cs.KubedbV1alpha2Interface + stashClient scs.StashV1beta1Interface + uncachedClient client.Client + onlyDb bool + onlyBackup bool + onlyArchiver bool } func NewMySQLPauser(clientConfig *rest.Config, onlyDb, onlyBackup, onlyArchiver bool) (*MySQLPauser, error) { @@ -49,12 +53,18 @@ func NewMySQLPauser(clientConfig *rest.Config, onlyDb, onlyBackup, onlyArchiver return nil, err } + uncachedClient, err := kmc.NewUncachedClient(clientConfig, coreapi.AddToScheme) + if err != nil { + return nil, err + } + return &MySQLPauser{ - dbClient: dbClient, - stashClient: stashClient, - onlyDb: onlyDb, - onlyBackup: onlyBackup, - onlyArchiver: onlyArchiver, + dbClient: dbClient, + stashClient: stashClient, + uncachedClient: uncachedClient, + onlyDb: onlyDb, + onlyBackup: onlyBackup, + onlyArchiver: onlyArchiver, }, nil } @@ -65,7 +75,7 @@ func (e *MySQLPauser) Pause(name string, namespace string) (bool, error) { } pauseAll := !(e.onlyBackup || e.onlyDb || e.onlyArchiver) if e.onlyArchiver || pauseAll { - if err := PauseMySQLArchiver(true, db.Spec.Archiver.Ref.Name, db.Spec.Archiver.Ref.Namespace); err != nil { + if err := PauseOrResumeMySQLArchiver(e.uncachedClient, true, db.Spec.Archiver.Ref); err != nil { return false, err } if e.onlyArchiver { diff --git a/pkg/pauser/pauser.go b/pkg/pauser/pauser.go index bfff4217a..ec97722e7 100644 --- a/pkg/pauser/pauser.go +++ b/pkg/pauser/pauser.go @@ -46,7 +46,7 @@ func NewPauser(restClientGetter genericclioptions.RESTClientGetter, mapping *met case api.ResourceKindMySQL: return NewMySQLPauser(clientConfig, onlyDb, onlyBackup, onlyArchiver) case api.ResourceKindMariaDB: - return NewMariaDBPauser(clientConfig, onlyDb, onlyBackup) + return NewMariaDBPauser(clientConfig, onlyDb, onlyBackup, onlyArchiver) case api.ResourceKindPostgres: return NewPostgresPauser(clientConfig, onlyDb, onlyBackup, onlyArchiver) case api.ResourceKindRedis: diff --git a/pkg/pauser/postgres.go b/pkg/pauser/postgres.go index 0393fe246..0b0de25da 100644 --- a/pkg/pauser/postgres.go +++ b/pkg/pauser/postgres.go @@ -19,6 +19,7 @@ package pauser import ( "context" + coreapi "kubedb.dev/apimachinery/apis/archiver/v1alpha1" api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2" cs "kubedb.dev/apimachinery/client/clientset/versioned/typed/kubedb/v1alpha2" dbutil "kubedb.dev/apimachinery/client/clientset/versioned/typed/kubedb/v1alpha2/util" @@ -26,16 +27,19 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/rest" + kmc "kmodules.xyz/client-go/client" condutil "kmodules.xyz/client-go/conditions" + "sigs.k8s.io/controller-runtime/pkg/client" scs "stash.appscode.dev/apimachinery/client/clientset/versioned/typed/stash/v1beta1" ) type PostgresPauser struct { - dbClient cs.KubedbV1alpha2Interface - stashClient scs.StashV1beta1Interface - onlyDb bool - onlyBackup bool - onlyArchiver bool + dbClient cs.KubedbV1alpha2Interface + stashClient scs.StashV1beta1Interface + uncachedClient client.Client + onlyDb bool + onlyBackup bool + onlyArchiver bool } func NewPostgresPauser(clientConfig *rest.Config, onlyDb, onlyBackup, onlyArchiver bool) (*PostgresPauser, error) { @@ -49,12 +53,18 @@ func NewPostgresPauser(clientConfig *rest.Config, onlyDb, onlyBackup, onlyArchiv return nil, err } + uncachedClient, err := kmc.NewUncachedClient(clientConfig, coreapi.AddToScheme) + if err != nil { + return nil, err + } + return &PostgresPauser{ - dbClient: dbClient, - stashClient: stashClient, - onlyDb: onlyDb, - onlyBackup: onlyBackup, - onlyArchiver: onlyArchiver, + dbClient: dbClient, + stashClient: stashClient, + uncachedClient: uncachedClient, + onlyDb: onlyDb, + onlyBackup: onlyBackup, + onlyArchiver: onlyArchiver, }, nil } @@ -67,7 +77,7 @@ func (e *PostgresPauser) Pause(name, namespace string) (bool, error) { pauseAll := !(e.onlyBackup || e.onlyDb || e.onlyArchiver) if e.onlyArchiver || pauseAll { - if err := PausePostgresArchiver(true, db.Spec.Archiver.Ref.Name, db.Spec.Archiver.Ref.Namespace); err != nil { + if err := PauseOrResumePostgresArchiver(e.uncachedClient, true, db.Spec.Archiver.Ref); err != nil { return false, err } if e.onlyArchiver { diff --git a/pkg/pauser/util.go b/pkg/pauser/util.go index a084f7d4b..bc9757a51 100644 --- a/pkg/pauser/util.go +++ b/pkg/pauser/util.go @@ -18,33 +18,15 @@ package pauser import ( "context" - "fmt" - - coreapi "kubedb.dev/apimachinery/apis/archiver/v1alpha1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - kmapi "kmodules.xyz/client-go/api/v1" - kmc "kmodules.xyz/client-go/client" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" "stash.appscode.dev/apimachinery/apis" stash "stash.appscode.dev/apimachinery/apis/stash/v1beta1" scs "stash.appscode.dev/apimachinery/client/clientset/versioned/typed/stash/v1beta1" scsutil "stash.appscode.dev/apimachinery/client/clientset/versioned/typed/stash/v1beta1/util" ) -func NewUncachedClient() (client.Client, error) { - cfg, err := ctrl.GetConfig() - if err != nil { - return nil, fmt.Errorf("failed to get Kubernetes config. Reason: %w", err) - } - return kmc.NewUncachedClient( - cfg, - coreapi.AddToScheme, - ) -} - func PauseBackupConfiguration(stashClient scs.StashV1beta1Interface, dbMeta metav1.ObjectMeta) (bool, error) { configs, err := stashClient.BackupConfigurations(dbMeta.Namespace).List(context.TODO(), metav1.ListOptions{}) if err != nil { @@ -73,120 +55,3 @@ func PauseBackupConfiguration(stashClient scs.StashV1beta1Interface, dbMeta meta } return dbBackupConfig != nil, nil } - -func PauseMySQLArchiver(value bool, name string, namespace string) error { - var klient client.Client - klient, err := NewUncachedClient() - if err != nil { - return err - } - archiver, err := getMysqlArchiver(klient, kmapi.ObjectReference{ - Name: name, - Namespace: namespace, - }) - if err != nil { - return err - } - _, err = kmc.CreateOrPatch( - context.Background(), - klient, - archiver, - func(obj client.Object, createOp bool) client.Object { - in := obj.(*coreapi.MySQLArchiver) - in.Spec.Pause = value - return in - }, - ) - return err -} - -func getMysqlArchiver(klient client.Client, ref kmapi.ObjectReference) (*coreapi.MySQLArchiver, error) { - archiver := &coreapi.MySQLArchiver{ - ObjectMeta: metav1.ObjectMeta{ - Name: ref.Name, - Namespace: ref.Namespace, - }, - } - if err := klient.Get(context.Background(), client.ObjectKeyFromObject(archiver), archiver); err != nil { - return nil, err - } - return archiver, nil -} - -func PausePostgresArchiver(value bool, name string, namespace string) error { - var klient client.Client - klient, err := NewUncachedClient() - if err != nil { - return err - } - archiver, err := getPostgresArchiver(klient, kmapi.ObjectReference{ - Name: name, - Namespace: namespace, - }) - if err != nil { - return err - } - _, err = kmc.CreateOrPatch( - context.Background(), - klient, - archiver, - func(obj client.Object, createOp bool) client.Object { - in := obj.(*coreapi.PostgresArchiver) - in.Spec.Pause = value - return in - }, - ) - return err -} - -func getPostgresArchiver(klient client.Client, ref kmapi.ObjectReference) (*coreapi.PostgresArchiver, error) { - archiver := &coreapi.PostgresArchiver{ - ObjectMeta: metav1.ObjectMeta{ - Name: ref.Name, - Namespace: ref.Namespace, - }, - } - if err := klient.Get(context.Background(), client.ObjectKeyFromObject(archiver), archiver); err != nil { - return nil, err - } - return archiver, nil -} - -func PauseMongoDBArchiver(value bool, name string, namespace string) error { - var klient client.Client - klient, err := NewUncachedClient() - if err != nil { - return err - } - archiver, err := getMongoDBArchiver(klient, kmapi.ObjectReference{ - Name: name, - Namespace: namespace, - }) - if err != nil { - return err - } - _, err = kmc.CreateOrPatch( - context.Background(), - klient, - archiver, - func(obj client.Object, createOp bool) client.Object { - in := obj.(*coreapi.MongoDBArchiver) - in.Spec.Pause = value - return in - }, - ) - return err -} - -func getMongoDBArchiver(klient client.Client, ref kmapi.ObjectReference) (*coreapi.MongoDBArchiver, error) { - archiver := &coreapi.MongoDBArchiver{ - ObjectMeta: metav1.ObjectMeta{ - Name: ref.Name, - Namespace: ref.Namespace, - }, - } - if err := klient.Get(context.Background(), client.ObjectKeyFromObject(archiver), archiver); err != nil { - return nil, err - } - return archiver, nil -} diff --git a/pkg/resumer/mariadb.go b/pkg/resumer/mariadb.go index b6baac212..5c237c4f2 100644 --- a/pkg/resumer/mariadb.go +++ b/pkg/resumer/mariadb.go @@ -19,26 +19,32 @@ package resumer import ( "context" + coreapi "kubedb.dev/apimachinery/apis/archiver/v1alpha1" api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2" cs "kubedb.dev/apimachinery/client/clientset/versioned/typed/kubedb/v1alpha2" dbutil "kubedb.dev/apimachinery/client/clientset/versioned/typed/kubedb/v1alpha2/util" + pautil "kubedb.dev/cli/pkg/pauser" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/rest" + kmc "kmodules.xyz/client-go/client" condutil "kmodules.xyz/client-go/conditions" + "sigs.k8s.io/controller-runtime/pkg/client" scs "stash.appscode.dev/apimachinery/client/clientset/versioned/typed/stash/v1beta1" ) type MariaDBResumer struct { - dbClient cs.KubedbV1alpha2Interface - stashClient scs.StashV1beta1Interface - onlyDb bool - onlyBackup bool + dbClient cs.KubedbV1alpha2Interface + stashClient scs.StashV1beta1Interface + uncachedClient client.Client + onlyDb bool + onlyBackup bool + onlyArchiver bool } -func NewMariaDBResumer(clientConfig *rest.Config, onlyDb, onlyBackup bool) (*MariaDBResumer, error) { +func NewMariaDBResumer(clientConfig *rest.Config, onlyDb, onlyBackup, onlyArchiver bool) (*MariaDBResumer, error) { dbClient, err := cs.NewForConfig(clientConfig) if err != nil { return nil, err @@ -47,12 +53,17 @@ func NewMariaDBResumer(clientConfig *rest.Config, onlyDb, onlyBackup bool) (*Mar if err != nil { return nil, err } - + uncachedClient, err := kmc.NewUncachedClient(clientConfig, coreapi.AddToScheme) + if err != nil { + return nil, err + } return &MariaDBResumer{ - dbClient: dbClient, - stashClient: stashClient, - onlyDb: onlyDb, - onlyBackup: onlyBackup, + dbClient: dbClient, + stashClient: stashClient, + uncachedClient: uncachedClient, + onlyDb: onlyDb, + onlyBackup: onlyBackup, + onlyArchiver: onlyArchiver, }, nil } @@ -62,7 +73,16 @@ func (e *MariaDBResumer) Resume(name, namespace string) (bool, error) { return false, err } - resumeAll := !(e.onlyBackup || e.onlyDb) + resumeAll := !(e.onlyBackup || e.onlyDb || e.onlyArchiver) + + if e.onlyArchiver || resumeAll { + if err := pautil.PauseOrResumeMariaDBArchiver(e.uncachedClient, false, db.Spec.Archiver.Ref); err != nil { + return false, err + } + if e.onlyArchiver { + return false, nil + } + } if e.onlyDb || resumeAll { _, err = dbutil.UpdateMariaDBStatus(context.TODO(), e.dbClient, db.ObjectMeta, func(status *api.MariaDBStatus) (types.UID, *api.MariaDBStatus) { diff --git a/pkg/resumer/mongodb.go b/pkg/resumer/mongodb.go index d4dca2ec0..795d20f03 100644 --- a/pkg/resumer/mongodb.go +++ b/pkg/resumer/mongodb.go @@ -19,6 +19,7 @@ package resumer import ( "context" + coreapi "kubedb.dev/apimachinery/apis/archiver/v1alpha1" api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2" cs "kubedb.dev/apimachinery/client/clientset/versioned/typed/kubedb/v1alpha2" dbutil "kubedb.dev/apimachinery/client/clientset/versioned/typed/kubedb/v1alpha2/util" @@ -28,16 +29,19 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/rest" + kmc "kmodules.xyz/client-go/client" condutil "kmodules.xyz/client-go/conditions" + "sigs.k8s.io/controller-runtime/pkg/client" scs "stash.appscode.dev/apimachinery/client/clientset/versioned/typed/stash/v1beta1" ) type MongoDBResumer struct { - dbClient cs.KubedbV1alpha2Interface - stashClient scs.StashV1beta1Interface - onlyDb bool - onlyBackup bool - onlyArchiver bool + dbClient cs.KubedbV1alpha2Interface + stashClient scs.StashV1beta1Interface + uncachedClient client.Client + onlyDb bool + onlyBackup bool + onlyArchiver bool } func NewMongoDBResumer(clientConfig *rest.Config, onlyDb, onlyBackup, onlyArchiver bool) (*MongoDBResumer, error) { @@ -50,12 +54,18 @@ func NewMongoDBResumer(clientConfig *rest.Config, onlyDb, onlyBackup, onlyArchiv return nil, err } + uncachedClient, err := kmc.NewUncachedClient(clientConfig, coreapi.AddToScheme) + if err != nil { + return nil, err + } + return &MongoDBResumer{ - dbClient: dbClient, - stashClient: stashClient, - onlyDb: onlyDb, - onlyBackup: onlyBackup, - onlyArchiver: onlyArchiver, + dbClient: dbClient, + stashClient: stashClient, + uncachedClient: uncachedClient, + onlyDb: onlyDb, + onlyBackup: onlyBackup, + onlyArchiver: onlyArchiver, }, nil } @@ -67,7 +77,7 @@ func (e *MongoDBResumer) Resume(name, namespace string) (bool, error) { resumeAll := !(e.onlyBackup || e.onlyDb || e.onlyArchiver) if e.onlyArchiver || resumeAll { - if err := pautil.PauseMongoDBArchiver(false, db.Spec.Archiver.Ref.Name, db.Spec.Archiver.Ref.Namespace); err != nil { + if err := pautil.PauseOrResumeMongoDBArchiver(e.uncachedClient, false, db.Spec.Archiver.Ref); err != nil { return false, err } if e.onlyArchiver { diff --git a/pkg/resumer/mysql.go b/pkg/resumer/mysql.go index 190cc20c5..3f6ebf697 100644 --- a/pkg/resumer/mysql.go +++ b/pkg/resumer/mysql.go @@ -19,6 +19,7 @@ package resumer import ( "context" + coreapi "kubedb.dev/apimachinery/apis/archiver/v1alpha1" api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2" cs "kubedb.dev/apimachinery/client/clientset/versioned/typed/kubedb/v1alpha2" dbutil "kubedb.dev/apimachinery/client/clientset/versioned/typed/kubedb/v1alpha2/util" @@ -28,16 +29,19 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/rest" + kmc "kmodules.xyz/client-go/client" condutil "kmodules.xyz/client-go/conditions" + "sigs.k8s.io/controller-runtime/pkg/client" scs "stash.appscode.dev/apimachinery/client/clientset/versioned/typed/stash/v1beta1" ) type MySQLResumer struct { - dbClient cs.KubedbV1alpha2Interface - stashClient scs.StashV1beta1Interface - onlyDb bool - onlyBackup bool - onlyArchiver bool + dbClient cs.KubedbV1alpha2Interface + stashClient scs.StashV1beta1Interface + uncachedClient client.Client + onlyDb bool + onlyBackup bool + onlyArchiver bool } func NewMySQLResumer(clientConfig *rest.Config, onlyDb, onlyBackup, onlyArchiver bool) (*MySQLResumer, error) { @@ -49,13 +53,17 @@ func NewMySQLResumer(clientConfig *rest.Config, onlyDb, onlyBackup, onlyArchiver if err != nil { return nil, err } - + uncachedClient, err := kmc.NewUncachedClient(clientConfig, coreapi.AddToScheme) + if err != nil { + return nil, err + } return &MySQLResumer{ - dbClient: dbClient, - stashClient: stashClient, - onlyDb: onlyDb, - onlyBackup: onlyBackup, - onlyArchiver: onlyArchiver, + dbClient: dbClient, + stashClient: stashClient, + uncachedClient: uncachedClient, + onlyDb: onlyDb, + onlyBackup: onlyBackup, + onlyArchiver: onlyArchiver, }, nil } @@ -68,7 +76,7 @@ func (e *MySQLResumer) Resume(name, namespace string) (bool, error) { resumeAll := !(e.onlyBackup || e.onlyDb || e.onlyArchiver) if e.onlyArchiver || resumeAll { - if err := pautil.PauseMySQLArchiver(false, db.Spec.Archiver.Ref.Name, db.Spec.Archiver.Ref.Namespace); err != nil { + if err := pautil.PauseOrResumeMySQLArchiver(e.uncachedClient, false, db.Spec.Archiver.Ref); err != nil { return false, err } if e.onlyArchiver { diff --git a/pkg/resumer/postgres.go b/pkg/resumer/postgres.go index 535bb9e50..21f9f9d5b 100644 --- a/pkg/resumer/postgres.go +++ b/pkg/resumer/postgres.go @@ -19,6 +19,7 @@ package resumer import ( "context" + coreapi "kubedb.dev/apimachinery/apis/archiver/v1alpha1" api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2" cs "kubedb.dev/apimachinery/client/clientset/versioned/typed/kubedb/v1alpha2" dbutil "kubedb.dev/apimachinery/client/clientset/versioned/typed/kubedb/v1alpha2/util" @@ -28,16 +29,19 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/rest" + kmc "kmodules.xyz/client-go/client" condutil "kmodules.xyz/client-go/conditions" + "sigs.k8s.io/controller-runtime/pkg/client" scs "stash.appscode.dev/apimachinery/client/clientset/versioned/typed/stash/v1beta1" ) type PostgresResumer struct { - dbClient cs.KubedbV1alpha2Interface - stashClient scs.StashV1beta1Interface - onlyDb bool - onlyBackup bool - onlyArchiver bool + dbClient cs.KubedbV1alpha2Interface + stashClient scs.StashV1beta1Interface + uncachedClient client.Client + onlyDb bool + onlyBackup bool + onlyArchiver bool } func NewPostgresResumer(clientConfig *rest.Config, onlyDb, onlyBackup, onlyArchiver bool) (*PostgresResumer, error) { @@ -50,12 +54,18 @@ func NewPostgresResumer(clientConfig *rest.Config, onlyDb, onlyBackup, onlyArchi return nil, err } + uncachedClient, err := kmc.NewUncachedClient(clientConfig, coreapi.AddToScheme) + if err != nil { + return nil, err + } + return &PostgresResumer{ - dbClient: dbClient, - stashClient: stashClient, - onlyDb: onlyDb, - onlyBackup: onlyBackup, - onlyArchiver: onlyArchiver, + dbClient: dbClient, + stashClient: stashClient, + uncachedClient: uncachedClient, + onlyDb: onlyDb, + onlyBackup: onlyBackup, + onlyArchiver: onlyArchiver, }, nil } @@ -67,7 +77,7 @@ func (e *PostgresResumer) Resume(name, namespace string) (bool, error) { resumeAll := !(e.onlyBackup || e.onlyDb || e.onlyArchiver) if e.onlyArchiver || resumeAll { - if err := pautil.PausePostgresArchiver(false, db.Spec.Archiver.Ref.Name, db.Spec.Archiver.Ref.Namespace); err != nil { + if err := pautil.PauseOrResumePostgresArchiver(e.uncachedClient, false, db.Spec.Archiver.Ref); err != nil { return false, err } if e.onlyArchiver { diff --git a/pkg/resumer/resumer.go b/pkg/resumer/resumer.go index 894e8c015..6292e6d18 100644 --- a/pkg/resumer/resumer.go +++ b/pkg/resumer/resumer.go @@ -46,7 +46,7 @@ func NewResumer(restClientGetter genericclioptions.RESTClientGetter, mapping *me case api.ResourceKindMySQL: return NewMySQLResumer(clientConfig, onlyDb, onlyBackup, onlyArchiver) case api.ResourceKindMariaDB: - return NewMariaDBResumer(clientConfig, onlyDb, onlyBackup) + return NewMariaDBResumer(clientConfig, onlyDb, onlyBackup, onlyArchiver) case api.ResourceKindPostgres: return NewPostgresResumer(clientConfig, onlyDb, onlyBackup, onlyArchiver) case api.ResourceKindRedis: