Skip to content

Commit 230e572

Browse files
authored
[cherry-pick] Fix component status updating for shard backup and restore (#1806) (#1828)
Signed-off-by: hmsayem <hmsayem@appscode.com>
1 parent b9ca59c commit 230e572

File tree

239 files changed

+21153
-11762
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

239 files changed

+21153
-11762
lines changed

go.mod

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,33 @@ go 1.18
44

55
require (
66
github.com/pkg/errors v0.9.1
7-
github.com/spf13/cobra v1.6.0
7+
github.com/spf13/cobra v1.7.0
88
go.bytebuilders.dev/license-verifier/kubernetes v0.12.0
99
gomodules.xyz/flags v0.1.3
1010
gomodules.xyz/go-sh v0.1.0
1111
gomodules.xyz/logs v0.0.6
1212
gomodules.xyz/pointer v0.1.0
13-
gomodules.xyz/x v0.0.14
14-
k8s.io/api v0.25.1
13+
gomodules.xyz/x v0.0.15
14+
k8s.io/api v0.25.3
1515
k8s.io/apimachinery v0.25.3
16-
k8s.io/client-go v0.25.1
16+
k8s.io/client-go v0.25.3
1717
k8s.io/klog/v2 v2.80.1
18-
kmodules.xyz/client-go v0.25.19
19-
kmodules.xyz/custom-resources v0.25.0
20-
kmodules.xyz/offshoot-api v0.25.0
21-
kubedb.dev/apimachinery v0.28.4-0.20220918021210-a0b96812228b
22-
stash.appscode.dev/apimachinery v0.29.0
18+
kmodules.xyz/client-go v0.25.23
19+
kmodules.xyz/custom-resources v0.25.1
20+
kmodules.xyz/offshoot-api v0.25.3
21+
kubedb.dev/apimachinery v0.33.1
22+
stash.appscode.dev/apimachinery v0.29.1-0.20230529131221-1e979c48da10
2323
)
2424

2525
require (
2626
cloud.google.com/go v0.99.0 // indirect
2727
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
28-
github.com/Azure/go-autorest/autorest v0.11.27 // indirect
29-
github.com/Azure/go-autorest/autorest/adal v0.9.20 // indirect
28+
github.com/Azure/go-autorest/autorest v0.11.28 // indirect
29+
github.com/Azure/go-autorest/autorest/adal v0.9.21 // indirect
3030
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
3131
github.com/Azure/go-autorest/logger v0.2.1 // indirect
3232
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
33-
github.com/Masterminds/semver/v3 v3.1.1 // indirect
33+
github.com/Masterminds/semver/v3 v3.2.1 // indirect
3434
github.com/armon/circbuf v0.0.0-20190214190532-5111143e8da2 // indirect
3535
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 // indirect
3636
github.com/davecgh/go-spew v1.1.1 // indirect
@@ -49,7 +49,7 @@ require (
4949
github.com/google/go-cmp v0.5.9 // indirect
5050
github.com/google/gofuzz v1.2.0 // indirect
5151
github.com/imdario/mergo v0.3.13 // indirect
52-
github.com/inconshreveable/mousetrap v1.0.1 // indirect
52+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
5353
github.com/josharian/intern v1.0.0 // indirect
5454
github.com/json-iterator/go v1.1.12 // indirect
5555
github.com/mailru/easyjson v0.7.7 // indirect
@@ -62,16 +62,16 @@ require (
6262
github.com/yudai/gojsondiff v1.0.0 // indirect
6363
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
6464
go.bytebuilders.dev/license-proxyserver v0.0.3 // indirect
65-
go.bytebuilders.dev/license-verifier v0.12.1-0.20221113063237-6eb88040dd50 // indirect
66-
golang.org/x/crypto v0.6.0 // indirect
67-
golang.org/x/net v0.7.0 // indirect
65+
go.bytebuilders.dev/license-verifier v0.12.1 // indirect
66+
golang.org/x/crypto v0.9.0 // indirect
67+
golang.org/x/net v0.10.0 // indirect
6868
golang.org/x/oauth2 v0.5.0 // indirect
69-
golang.org/x/sys v0.5.0 // indirect
70-
golang.org/x/term v0.5.0 // indirect
71-
golang.org/x/text v0.7.0 // indirect
72-
golang.org/x/time v0.1.0 // indirect
69+
golang.org/x/sys v0.8.0 // indirect
70+
golang.org/x/term v0.8.0 // indirect
71+
golang.org/x/text v0.9.0 // indirect
72+
golang.org/x/time v0.3.0 // indirect
7373
gomodules.xyz/clock v0.0.0-20200817085942-06523dba733f // indirect
74-
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
74+
gomodules.xyz/jsonpatch/v2 v2.3.0 // indirect
7575
gomodules.xyz/mergo v0.3.13 // indirect
7676
gomodules.xyz/sets v0.2.1 // indirect
7777
gomodules.xyz/wait v0.2.0 // indirect
@@ -80,11 +80,11 @@ require (
8080
gopkg.in/inf.v0 v0.9.1 // indirect
8181
gopkg.in/yaml.v2 v2.4.0 // indirect
8282
gopkg.in/yaml.v3 v3.0.1 // indirect
83-
k8s.io/apiextensions-apiserver v0.25.1 // indirect
84-
k8s.io/apiserver v0.25.1 // indirect
83+
k8s.io/apiextensions-apiserver v0.25.3 // indirect
84+
k8s.io/apiserver v0.25.3 // indirect
8585
k8s.io/kube-aggregator v0.25.1 // indirect
86-
k8s.io/kube-openapi v0.0.0-20220803164354-a70c9af30aea // indirect
87-
k8s.io/utils v0.0.0-20220823124924-e9cbc92d1a73 // indirect
86+
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
87+
k8s.io/utils v0.0.0-20221012122500-cfd413dd9e85 // indirect
8888
kmodules.xyz/apiversion v0.2.0 // indirect
8989
kmodules.xyz/objectstore-api v0.25.1-0.20221104003322-f0289b5b6ca2 // indirect
9090
kmodules.xyz/prober v0.25.0 // indirect

go.sum

Lines changed: 54 additions & 58 deletions
Large diffs are not rendered by default.

pkg/backup.go

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"os/exec"
2525
"os/signal"
2626
"path/filepath"
27+
"strconv"
2728
"strings"
2829
"syscall"
2930
"time"
@@ -143,14 +144,8 @@ func NewCmdBackup() *cobra.Command {
143144
if err != nil {
144145
backupOutput = &restic.BackupOutput{
145146
BackupTargetStatus: api_v1beta1.BackupTargetStatus{
146-
Ref: targetRef,
147-
Stats: []api_v1beta1.HostBackupStats{
148-
{
149-
Hostname: opt.defaultBackupOptions.Host,
150-
Phase: api_v1beta1.HostBackupFailed,
151-
Error: err.Error(),
152-
},
153-
},
147+
Ref: targetRef,
148+
Stats: opt.getHostBackupStats(err),
154149
},
155150
}
156151
}
@@ -295,8 +290,10 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
295290
// So, for stand-alone MongoDB and MongoDB ReplicaSet, we don't have to do anything.
296291
// We only need to update totalHosts field for sharded MongoDB
297292

293+
opt.totalHosts = 1
298294
// For sharded MongoDB, parameter.ConfigServer will not be empty
299295
if parameters.ConfigServer != "" {
296+
opt.totalHosts = len(parameters.ReplicaSets) + 1 // for each shard there will be one key in parameters.ReplicaSet
300297
backupSession, err := opt.stashClient.StashV1beta1().BackupSessions(opt.namespace).Get(context.TODO(), opt.backupSessionName, metav1.GetOptions{})
301298
if err != nil {
302299
return nil, err
@@ -308,7 +305,7 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
308305
opt.stashClient.StashV1beta1(),
309306
backupSession.ObjectMeta,
310307
func(status *api_v1beta1.BackupSessionStatus) (types.UID, *api_v1beta1.BackupSessionStatus) {
311-
status.Targets[i].TotalHosts = pointer.Int32P(int32(len(parameters.ReplicaSets) + 1)) // for each shard there will be one key in parameters.ReplicaSet
308+
status.Targets[i].TotalHosts = pointer.Int32P(int32(opt.totalHosts))
312309
return backupSession.UID, status
313310
},
314311
metav1.UpdateOptions{},
@@ -524,7 +521,12 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
524521
// hide password, don't print cmd
525522
resticWrapper.HideCMD()
526523

527-
return resticWrapper.RunParallelBackup(opt.backupOptions, targetRef, opt.maxConcurrency)
524+
out, err := resticWrapper.RunParallelBackup(opt.backupOptions, targetRef, opt.maxConcurrency)
525+
if err != nil {
526+
klog.Warningln("backup failed!", err.Error())
527+
}
528+
// error not returned, error is encoded into output
529+
return out, nil
528530
}
529531

530532
// cleanup usually unlocks the locked servers
@@ -536,6 +538,32 @@ func cleanup() {
536538
}
537539
}
538540

541+
func (opt *mongoOptions) getHostBackupStats(err error) []api_v1beta1.HostBackupStats {
542+
var backupStats []api_v1beta1.HostBackupStats
543+
544+
errMsg := fmt.Sprintf("failed to start backup: %s", err.Error())
545+
for _, backupOpt := range opt.backupOptions {
546+
backupStats = append(backupStats, api_v1beta1.HostBackupStats{
547+
Hostname: backupOpt.Host,
548+
Phase: api_v1beta1.HostBackupFailed,
549+
Error: errMsg,
550+
})
551+
}
552+
553+
if opt.totalHosts > len(backupStats) {
554+
rem := opt.totalHosts - len(backupStats)
555+
for i := 0; i < rem; i++ {
556+
backupStats = append(backupStats, api_v1beta1.HostBackupStats{
557+
Hostname: fmt.Sprintf("unknown-%s", strconv.Itoa(i)),
558+
Phase: api_v1beta1.HostBackupFailed,
559+
Error: errMsg,
560+
})
561+
}
562+
}
563+
564+
return backupStats
565+
}
566+
539567
func getSSLUser(path string) (string, error) {
540568
data, err := sh.Command(OpenSSLCMD, "x509", "-in", path, "-inform", "PEM", "-subject", "-nameopt", "RFC2253", "-noout").Output()
541569
if err != nil {

pkg/restore.go

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"os"
2424
"path/filepath"
25+
"strconv"
2526
"strings"
2627

2728
api_v1beta1 "stash.appscode.dev/apimachinery/apis/stash/v1beta1"
@@ -105,14 +106,8 @@ func NewCmdRestore() *cobra.Command {
105106
if err != nil {
106107
restoreOutput = &restic.RestoreOutput{
107108
RestoreTargetStatus: api_v1beta1.RestoreMemberStatus{
108-
Ref: targetRef,
109-
Stats: []api_v1beta1.HostRestoreStats{
110-
{
111-
Hostname: opt.defaultDumpOptions.Host,
112-
Phase: api_v1beta1.HostRestoreFailed,
113-
Error: err.Error(),
114-
},
115-
},
109+
Ref: targetRef,
110+
Stats: opt.getHostRestoreStats(err),
116111
},
117112
}
118113
}
@@ -231,8 +226,10 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti
231226
// So, for stand-alone MongoDB and MongoDB ReplicaSet, we don't have to do anything.
232227
// We only need to update totalHosts field for sharded MongoDB
233228

229+
opt.totalHosts = 1
234230
// For sharded MongoDB, parameter.ConfigServer will not be empty
235231
if parameters.ConfigServer != "" {
232+
opt.totalHosts = len(parameters.ReplicaSets) + 1 // for each shard there will be one key in parameters.ReplicaSet
236233
restoreSession, err := opt.stashClient.StashV1beta1().RestoreSessions(opt.namespace).Get(context.TODO(), opt.restoreSessionName, metav1.GetOptions{})
237234
if err != nil {
238235
return nil, err
@@ -242,7 +239,7 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti
242239
opt.stashClient.StashV1beta1(),
243240
restoreSession.ObjectMeta,
244241
func(status *api_v1beta1.RestoreSessionStatus) (types.UID, *api_v1beta1.RestoreSessionStatus) {
245-
status.TotalHosts = pointer.Int32P(int32(len(parameters.ReplicaSets) + 1)) // for each shard there will be one key in parameters.ReplicaSet
242+
status.TotalHosts = pointer.Int32P(int32(opt.totalHosts))
246243
return restoreSession.UID, status
247244
},
248245
metav1.UpdateOptions{},
@@ -396,5 +393,36 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti
396393
resticWrapper.HideCMD()
397394

398395
// Run dump
399-
return resticWrapper.ParallelDump(opt.dumpOptions, targetRef, opt.maxConcurrency)
396+
out, err := resticWrapper.ParallelDump(opt.dumpOptions, targetRef, opt.maxConcurrency)
397+
if err != nil {
398+
klog.Warningln("restore failed!", err.Error())
399+
}
400+
// error not returned, error is encoded into output
401+
return out, nil
402+
}
403+
404+
func (opt *mongoOptions) getHostRestoreStats(err error) []api_v1beta1.HostRestoreStats {
405+
var restoreStats []api_v1beta1.HostRestoreStats
406+
407+
errMsg := fmt.Sprintf("failed to start data restoration: %s", err.Error())
408+
for _, dumpOpt := range opt.dumpOptions {
409+
restoreStats = append(restoreStats, api_v1beta1.HostRestoreStats{
410+
Hostname: dumpOpt.Host,
411+
Phase: api_v1beta1.HostRestoreFailed,
412+
Error: errMsg,
413+
})
414+
}
415+
416+
if opt.totalHosts > len(restoreStats) {
417+
rem := opt.totalHosts - len(restoreStats)
418+
for i := 0; i < rem; i++ {
419+
restoreStats = append(restoreStats, api_v1beta1.HostRestoreStats{
420+
Hostname: fmt.Sprintf("unknown-%s", strconv.Itoa(i)),
421+
Phase: api_v1beta1.HostRestoreFailed,
422+
Error: errMsg,
423+
})
424+
}
425+
}
426+
427+
return restoreStats
400428
}

pkg/utils.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type mongoOptions struct {
6868
dumpOptions []restic.DumpOptions
6969
defaultDumpOptions restic.DumpOptions
7070
config *restclient.Config
71+
totalHosts int
7172
}
7273

7374
func waitForDBReady(host string, port, waitTimeout int32) {

vendor/github.com/Masterminds/semver/v3/.golangci.yml

Lines changed: 12 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/Masterminds/semver/v3/CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/Masterminds/semver/v3/Makefile

Lines changed: 5 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)