diff --git a/pkg/backup.go b/pkg/backup.go index 56d6a21b4..28dbdd4fc 100644 --- a/pkg/backup.go +++ b/pkg/backup.go @@ -84,6 +84,7 @@ func NewCmdBackup() *cobra.Command { masterURL string kubeconfigPath string opt = mongoOptions{ + totalHosts: 1, waitTimeout: 300, setupOptions: restic.SetupOptions{ ScratchDir: restic.DefaultScratchDir, @@ -149,14 +150,8 @@ func NewCmdBackup() *cobra.Command { if err != nil { backupOutput = &restic.BackupOutput{ BackupTargetStatus: api_v1beta1.BackupTargetStatus{ - Ref: targetRef, - Stats: []api_v1beta1.HostBackupStats{ - { - Hostname: opt.defaultBackupOptions.Host, - Phase: api_v1beta1.HostBackupFailed, - Error: err.Error(), - }, - }, + Ref: targetRef, + Stats: opt.getHostBackupStats(err), }, } } @@ -300,9 +295,9 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic // 3. For sharded MongoDB, totalHosts=(number of shard + 1) // extra 1 for config server // So, for stand-alone MongoDB and MongoDB ReplicaSet, we don't have to do anything. // We only need to update totalHosts field for sharded MongoDB - // For sharded MongoDB, parameter.ConfigServer will not be empty if parameters.ConfigServer != "" { + opt.totalHosts = len(parameters.ReplicaSets) + 1 backupSession, err := opt.stashClient.StashV1beta1().BackupSessions(opt.namespace).Get(context.TODO(), opt.backupSessionName, metav1.GetOptions{}) if err != nil { return nil, err @@ -314,7 +309,7 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic opt.stashClient.StashV1beta1(), backupSession.ObjectMeta, func(status *api_v1beta1.BackupSessionStatus) (types.UID, *api_v1beta1.BackupSessionStatus) { - status.Targets[i].TotalHosts = pointer.Int32P(int32(len(parameters.ReplicaSets) + 1)) // for each shard there will be one key in parameters.ReplicaSet + status.Targets[i].TotalHosts = pointer.Int32P(int32(opt.totalHosts)) // for each shard there will be one key in parameters.ReplicaSet return backupSession.UID, status }, metav1.UpdateOptions{}, @@ -601,6 +596,33 @@ func cleanup() { } } +func (opt *mongoOptions) getHostBackupStats(err error) []api_v1beta1.HostBackupStats { + var backupStats []api_v1beta1.HostBackupStats + + for _, backupOpt := range opt.backupOptions { + backupStats = append(backupStats, api_v1beta1.HostBackupStats{ + Hostname: backupOpt.Host, + Phase: api_v1beta1.HostBackupFailed, + Error: "backup couldn't start", + }) + } + + if opt.totalHosts > len(backupStats) { + rem := opt.totalHosts - len(backupStats) + for i := 0; i < rem; i++ { + backupStats = append(backupStats, api_v1beta1.HostBackupStats{ + Hostname: fmt.Sprintf("unknown-%s", strconv.Itoa(i)), + Phase: api_v1beta1.HostBackupFailed, + Error: "backup couldn't start", + }) + } + } + + // set the actual error message + backupStats[0].Error = err.Error() + return backupStats +} + func getSSLUser(path string) (string, error) { data, err := sh.Command(OpenSSLCMD, "x509", "-in", path, "-inform", "PEM", "-subject", "-nameopt", "RFC2253", "-noout").Output() if err != nil { diff --git a/pkg/utils.go b/pkg/utils.go index d2a9fd237..c28de5cf1 100644 --- a/pkg/utils.go +++ b/pkg/utils.go @@ -68,6 +68,7 @@ type mongoOptions struct { dumpOptions []restic.DumpOptions defaultDumpOptions restic.DumpOptions config *restclient.Config + totalHosts int } func waitForDBReady(host string, port, waitTimeout int32) {