Skip to content

Commit

Permalink
br: add new methods about manifest fetcher
Browse files Browse the repository at this point in the history
Signed-off-by: WangLe1321 <[email protected]>
  • Loading branch information
WangLe1321 committed Jul 28, 2023
1 parent 6159eb3 commit e38f413
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 23 deletions.
36 changes: 20 additions & 16 deletions pkg/backup/backup/backup_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ type backupManager struct {
func NewBackupManager(deps *controller.Dependencies) backup.BackupManager {
statusUpdater := controller.NewRealBackupConditionUpdater(deps.Clientset, deps.BackupLister, deps.Recorder)
manifestFetchers := []ManifestFetcher{
&TiDBClusterAutoScalerFetcher{},
&TiDBDashboardFetcher{},
&TiDBInitializerFetcher{},
&TiDBMonitorFetcher{},
&TiDBNgMonitoringFetcher{},
NewTiDBClusterAutoScalerFetcher(deps.TiDBClusterAutoScalerLister),
NewTiDBDashboardFetcher(deps.TiDBDashboardLister),
NewTiDBInitializerFetcher(deps.TiDBInitializerLister),
NewTiDBMonitorFetcher(deps.TiDBMonitorLister),
NewTiDBNgMonitoringFetcher(deps.TiDBNGMonitoringLister),
}
return &backupManager{
deps: deps,
Expand Down Expand Up @@ -798,19 +798,22 @@ func (bm *backupManager) saveClusterMetaToExternalStorage(b *v1alpha1.Backup, cs
}

func (bm *backupManager) volumeSnapshotBackup(b *v1alpha1.Backup, tc *v1alpha1.TidbCluster) (string, error) {
if s, reason, err := snapshotter.NewSnapshotterForBackup(b.Spec.Mode, bm.deps); err != nil {
if err := bm.backupManifests(b, tc); err != nil {
return "BackupManifestsFailed", err
}

s, reason, err := snapshotter.NewSnapshotterForBackup(b.Spec.Mode, bm.deps)
if err != nil {
return reason, err
} else if s != nil {
csb, reason, err := s.GenerateBackupMetadata(b, tc)
if err != nil {
return reason, err
}
}

return bm.saveClusterMetaToExternalStorage(b, csb)
csb, reason, err := s.GenerateBackupMetadata(b, tc)
if err != nil {
return reason, err
}

if err := bm.backupManifests(b, tc); err != nil {
return "BackupManifestsFailed", err
if reason, err = bm.saveClusterMetaToExternalStorage(b, csb); err != nil {
return reason, err
}
return "", nil
}
Expand All @@ -833,14 +836,14 @@ func (bm *backupManager) backupManifests(b *v1alpha1.Backup, tc *v1alpha1.TidbCl
}

for _, manifest := range manifests {
if err := bm.saveManifest(manifest, externalStorage); err != nil {
if err := bm.saveManifest(b, manifest, externalStorage); err != nil {
return err
}
}
return nil
}

func (bm *backupManager) saveManifest(manifest runtime.Object, externalStorage *backuputil.StorageBackend) error {
func (bm *backupManager) saveManifest(b *v1alpha1.Backup, manifest runtime.Object, externalStorage *backuputil.StorageBackend) error {
metadataAccessor := meta.NewAccessor()
namespace, err := metadataAccessor.Namespace(manifest)
if err != nil {
Expand Down Expand Up @@ -875,6 +878,7 @@ func (bm *backupManager) saveManifest(manifest runtime.Object, externalStorage *
if err := externalStorage.WriteAll(ctx, filePath, buf.Bytes(), nil); err != nil {
return err
}
klog.Infof("%s/%s upload manifest %s successfully", b.Namespace, b.Name, filePath)
return nil
}

Expand Down
55 changes: 48 additions & 7 deletions pkg/backup/backup/backup_manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,28 @@
package backup

import (
pingcapv1alpha1 "github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
listers "github.com/pingcap/tidb-operator/pkg/client/listers/pingcap/v1alpha1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
)

type ManifestFetcher interface {
ListByTC(tc *pingcapv1alpha1.TidbCluster) (objects []runtime.Object, err error)
ListByTC(tc *v1alpha1.TidbCluster) (objects []runtime.Object, err error)
}

type TiDBDashboardFetcher struct {
lister listers.TidbDashboardLister
}

func (f *TiDBDashboardFetcher) ListByTC(tc *pingcapv1alpha1.TidbCluster) (objects []runtime.Object, err error) {
func NewTiDBDashboardFetcher(lister listers.TidbDashboardLister) *TiDBDashboardFetcher {
return &TiDBDashboardFetcher{
lister: lister,
}
}

func (f *TiDBDashboardFetcher) ListByTC(tc *v1alpha1.TidbCluster) (objects []runtime.Object, err error) {
emptySelector := labels.NewSelector()
dashboards, err := f.lister.List(emptySelector)
if err != nil {
Expand All @@ -43,6 +50,8 @@ func (f *TiDBDashboardFetcher) ListByTC(tc *pingcapv1alpha1.TidbCluster) (object
namespace = dashboard.Namespace
}
if name == tc.Name && namespace == tc.Namespace {
klog.Infof("TiDBDashboard %s/%s matches tc %s/%s",
dashboard.Namespace, dashboard.Name, tc.Namespace, tc.Name)
objects = append(objects, dashboard)
break
}
Expand All @@ -55,7 +64,13 @@ type TiDBMonitorFetcher struct {
lister listers.TidbMonitorLister
}

func (f *TiDBMonitorFetcher) ListByTC(tc *pingcapv1alpha1.TidbCluster) (objects []runtime.Object, err error) {
func NewTiDBMonitorFetcher(lister listers.TidbMonitorLister) *TiDBMonitorFetcher {
return &TiDBMonitorFetcher{
lister: lister,
}
}

func (f *TiDBMonitorFetcher) ListByTC(tc *v1alpha1.TidbCluster) (objects []runtime.Object, err error) {
emptySelector := labels.NewSelector()
monitors, err := f.lister.List(emptySelector)
if err != nil {
Expand All @@ -70,6 +85,8 @@ func (f *TiDBMonitorFetcher) ListByTC(tc *pingcapv1alpha1.TidbCluster) (objects
namespace = monitor.Namespace
}
if name == tc.Name && namespace == tc.Namespace {
klog.Infof("TidbMonitor %s/%s matches tc %s/%s",
monitor.Namespace, monitor.Name, tc.Namespace, tc.Name)
objects = append(objects, monitor)
break
}
Expand All @@ -82,7 +99,13 @@ type TiDBClusterAutoScalerFetcher struct {
lister listers.TidbClusterAutoScalerLister
}

func (f *TiDBClusterAutoScalerFetcher) ListByTC(tc *pingcapv1alpha1.TidbCluster) (objects []runtime.Object, err error) {
func NewTiDBClusterAutoScalerFetcher(lister listers.TidbClusterAutoScalerLister) *TiDBClusterAutoScalerFetcher {
return &TiDBClusterAutoScalerFetcher{
lister: lister,
}
}

func (f *TiDBClusterAutoScalerFetcher) ListByTC(tc *v1alpha1.TidbCluster) (objects []runtime.Object, err error) {
emptySelector := labels.NewSelector()
autoScalers, err := f.lister.List(emptySelector)
if err != nil {
Expand All @@ -96,6 +119,8 @@ func (f *TiDBClusterAutoScalerFetcher) ListByTC(tc *pingcapv1alpha1.TidbCluster)
namespace = autoScaler.Namespace
}
if name == tc.Name && namespace == tc.Namespace {
klog.Infof("TiDBClusterAutoScaler %s/%s matches tc %s/%s",
autoScaler.Namespace, autoScaler.Name, tc.Namespace, tc.Name)
objects = append(objects, autoScaler)
}
}
Expand All @@ -106,7 +131,13 @@ type TiDBInitializerFetcher struct {
lister listers.TidbInitializerLister
}

func (f *TiDBInitializerFetcher) ListByTC(tc *pingcapv1alpha1.TidbCluster) (objects []runtime.Object, err error) {
func NewTiDBInitializerFetcher(lister listers.TidbInitializerLister) *TiDBInitializerFetcher {
return &TiDBInitializerFetcher{
lister: lister,
}
}

func (f *TiDBInitializerFetcher) ListByTC(tc *v1alpha1.TidbCluster) (objects []runtime.Object, err error) {
emptySelector := labels.NewSelector()
initializers, err := f.lister.List(emptySelector)
if err != nil {
Expand All @@ -120,6 +151,8 @@ func (f *TiDBInitializerFetcher) ListByTC(tc *pingcapv1alpha1.TidbCluster) (obje
namespace = initializer.Namespace
}
if name == tc.Name && namespace == tc.Namespace {
klog.Infof("TiDBInitializer %s/%s matches tc %s/%s",
initializer.Namespace, initializer.Name, tc.Namespace, tc.Name)
objects = append(objects, initializer)
}
}
Expand All @@ -130,7 +163,13 @@ type TiDBNgMonitoringFetcher struct {
lister listers.TidbNGMonitoringLister
}

func (f *TiDBNgMonitoringFetcher) ListByTC(tc *pingcapv1alpha1.TidbCluster) (objects []runtime.Object, err error) {
func NewTiDBNgMonitoringFetcher(lister listers.TidbNGMonitoringLister) *TiDBNgMonitoringFetcher {
return &TiDBNgMonitoringFetcher{
lister: lister,
}
}

func (f *TiDBNgMonitoringFetcher) ListByTC(tc *v1alpha1.TidbCluster) (objects []runtime.Object, err error) {
emptySelector := labels.NewSelector()
monitorings, err := f.lister.List(emptySelector)
if err != nil {
Expand All @@ -145,6 +184,8 @@ func (f *TiDBNgMonitoringFetcher) ListByTC(tc *pingcapv1alpha1.TidbCluster) (obj
namespace = monitoring.Namespace
}
if name == tc.Name && namespace == tc.Namespace {
klog.Infof("TidbNGMonitoring %s/%s matches tc %s/%s",
monitoring.Namespace, monitoring.Name, tc.Namespace, tc.Name)
objects = append(objects, monitoring)
break
}
Expand Down

0 comments on commit e38f413

Please sign in to comment.