Skip to content

Commit 3097a67

Browse files
hossainemruztamalsaha
authored andcommitted
Fix argument passing (#5)
1 parent ccdf81f commit 3097a67

File tree

20 files changed

+1338
-25
lines changed

20 files changed

+1338
-25
lines changed

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ module stash.appscode.dev/mongodb
33
go 1.12
44

55
require (
6-
github.com/appscode/go v0.0.0-20190808133642-1d4ef1f1c1e0
6+
github.com/appscode/go v0.0.0-20191016085057-e186b6c94a3b
77
github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27
88
github.com/pkg/errors v0.8.1
99
github.com/spf13/cobra v0.0.5
1010
k8s.io/apimachinery v0.0.0-20190508063446-a3da69d3723c
1111
k8s.io/client-go v11.0.0+incompatible
12-
kmodules.xyz/client-go v0.0.0-20190808141354-bbb9e14f60ab
12+
k8s.io/klog v0.3.1 // indirect
13+
kmodules.xyz/client-go v0.0.0-20191016085444-3044854ca625
1314
kmodules.xyz/custom-resources v0.0.0-20190808144301-114abf10dfe2
1415
kubedb.dev/apimachinery v0.13.0-rc.0.0.20190830121921-d28870c9d73a
1516
stash.appscode.dev/stash v0.9.0-rc.0.0.20190910104640-5f49f5d214aa

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb
4343
github.com/appscode/docker-registry-client v0.0.0-20180426150142-1bb02bb202b0/go.mod h1:W9bsWfSbgJXUXzBZ+tSBV2g+zzT7ANPHQFsSXRHYKsA=
4444
github.com/appscode/go v0.0.0-20190808133642-1d4ef1f1c1e0 h1:EZp2qE4LUJUG0wMHyUxOg982hVbOFh7qqcM1XIMVGpk=
4545
github.com/appscode/go v0.0.0-20190808133642-1d4ef1f1c1e0/go.mod h1:iy07dV61Z7QQdCKJCIvUoDL21u6AIceRhZzyleh2ymc=
46+
github.com/appscode/go v0.0.0-20191016085057-e186b6c94a3b h1:UskbETXW8u0DRCPU1pyBIgA55BFUnjuYjmt3qOBMdAY=
47+
github.com/appscode/go v0.0.0-20191016085057-e186b6c94a3b/go.mod h1:hUW7Fq0KY2/ntGnYAzemyUpIhLL6bXrTljN6SRY/+Lc=
4648
github.com/appscode/osm v0.12.0/go.mod h1:+ZXU2HilSSow2o0gsOolJfOTkhqQ2FcGROOum4wEh4w=
4749
github.com/armon/circbuf v0.0.0-20190214190532-5111143e8da2 h1:7Ip0wMmLHLRJdrloDxZfhMm0xrLXZS8+COSu2bXmEQs=
4850
github.com/armon/circbuf v0.0.0-20190214190532-5111143e8da2/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
@@ -509,6 +511,8 @@ gomodules.xyz/jsonpatch/v2 v2.0.0 h1:OyHbl+7IOECpPKfVK42oFr6N7+Y2dR+Jsb/IiDV3hOo
509511
gomodules.xyz/jsonpatch/v2 v2.0.0/go.mod h1:IhYNNY4jnS53ZnfE4PAmpKtDpTCj1JFXc+3mwe7XcUU=
510512
gomodules.xyz/stow v0.2.0/go.mod h1:YDDPM4nmJgQhxq4I5aGtLM/xrvJzDSVM/fbc7NGizpY=
511513
gomodules.xyz/version v0.0.0-20190507203204-7cec7ee542d3/go.mod h1:Y8xuV02mL/45psyPKG3NCVOwvAOy6T5Kx0l3rCjKSjU=
514+
gomodules.xyz/version v0.1.0 h1:inGItCg/egI0jPMeIE0SQkiDIJaodOMoCrxYqasQLR0=
515+
gomodules.xyz/version v0.1.0/go.mod h1:Y8xuV02mL/45psyPKG3NCVOwvAOy6T5Kx0l3rCjKSjU=
512516
google.golang.org/api v0.0.0-20180910000450-7ca32eb868bf/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
513517
google.golang.org/api v0.0.0-20181220000619-583d854617af/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
514518
google.golang.org/api v0.1.0/go.mod h1:UGEZY7KEX120AnNLIHFMKIo4obdJhkp2tPbaPlQx13Y=
@@ -584,11 +588,14 @@ k8s.io/utils v0.0.0-20190221042446-c2654d5206da h1:ElyM7RPonbKnQqOcw7dG2IK5uvQQn
584588
k8s.io/utils v0.0.0-20190221042446-c2654d5206da/go.mod h1:8k8uAuAQ0rXslZKaEWd0c3oVhZz7sSzSiPnVZayjIX0=
585589
kmodules.xyz/client-go v0.0.0-20190808141354-bbb9e14f60ab h1:rnKcFaDLtqpdooase2BqPfHV9Mu1WvzbTUmucnJjLio=
586590
kmodules.xyz/client-go v0.0.0-20190808141354-bbb9e14f60ab/go.mod h1:1GI6h5D31op/2D+Hrn9DhXW6iUd5wsHBQRoLeY7fndM=
591+
kmodules.xyz/client-go v0.0.0-20191016085444-3044854ca625 h1:A/ceKV+HXdheKSpaY2CF7Q70E/iDKoysxiWtAuYMXyE=
592+
kmodules.xyz/client-go v0.0.0-20191016085444-3044854ca625/go.mod h1:wFUBDofZzgkLW1sFJT5SSt6/G3ph2P8zUi5v81q5BKQ=
587593
kmodules.xyz/custom-resources v0.0.0-20190808144301-114abf10dfe2 h1:u0zEFCvAttk09O64UzY1DVXNke9uAANmf4vllnVquS8=
588594
kmodules.xyz/custom-resources v0.0.0-20190808144301-114abf10dfe2/go.mod h1:vlKyFcCXC+2Kfn3Fa5Z7RnBWyp4t46FSeEutNqpqMm8=
589595
kmodules.xyz/monitoring-agent-api v0.0.0-20190808150221-601a4005b7f7/go.mod h1:9v6LM+dmaAP3jjDiPOPOLPfsvD7jNjBJsi37JNCiFho=
590596
kmodules.xyz/objectstore-api v0.0.0-20190808153322-733e8798e8de h1:Z/z3SSS+mvRGzLT5HXLazogjnOYIfbYTYWrKBsiLtp8=
591597
kmodules.xyz/objectstore-api v0.0.0-20190808153322-733e8798e8de/go.mod h1:Wx+Ffc7weI6zv7NQvq0GWXMGGQFn6LqVb0KcXQFwRFs=
598+
kmodules.xyz/objectstore-api v0.0.0-20190824212210-196174aa0fc0 h1:ELq3ybK3noQP9SyZx34QsnZgU2As9r8hPAGEnuIy7Co=
592599
kmodules.xyz/objectstore-api v0.0.0-20190824212210-196174aa0fc0/go.mod h1:vFvmrMvRlkcUANfU4Ko3dZR3HUhWMUQc1sEHcwufims=
593600
kmodules.xyz/offshoot-api v0.0.0-20190808152534-e3dc715f844b h1:VZUtg4L9Qm2C7PBwa4sp/SvHckKidZSpkA33GoB24SA=
594601
kmodules.xyz/offshoot-api v0.0.0-20190808152534-e3dc715f844b/go.mod h1:AzvHBjJvhFwUEiWrEKfdsgoqt5Ulio44Beo1vPoq7ww=
@@ -607,4 +614,5 @@ sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
607614
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
608615
stash.appscode.dev/stash v0.9.0-rc.0 h1:GhzBnqHYBgq+2bcuwsDK3nhq2PPEhGkBH0EFAXp2OmY=
609616
stash.appscode.dev/stash v0.9.0-rc.0/go.mod h1:/YjAPx/9E4ScxWea1mbJC6qx9TomhqS83AUy2LufVVk=
617+
stash.appscode.dev/stash v0.9.0-rc.0.0.20190910104640-5f49f5d214aa h1:/cDpVS+SY8jr0P9AmPiIkj6EaPrVJzsmGDX2RgUYtMU=
610618
stash.appscode.dev/stash v0.9.0-rc.0.0.20190910104640-5f49f5d214aa/go.mod h1:6WrRpjkI1av7305B60BLRx+PZzwMAoiGmOd5ewYAAyw=

pkg/backup.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/pkg/errors"
1919
"github.com/spf13/cobra"
2020
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
"k8s.io/apimachinery/pkg/util/sets"
2122
"k8s.io/client-go/kubernetes"
2223
"k8s.io/client-go/tools/clientcmd"
2324
appcatalog_cs "kmodules.xyz/custom-resources/client/clientset/versioned"
@@ -236,16 +237,30 @@ func (opt *mongoOptions) backupMongoDB() (*restic.BackupOutput, error) {
236237
"--archive",
237238
}, adminCreds...),
238239
}
240+
userArgs := strings.Fields(opt.mongoArgs)
241+
239242
if isStandalone {
240243
backupOpt.StdinPipeCommand.Args = append(backupOpt.StdinPipeCommand.Args, "--port="+fmt.Sprint(appBinding.Spec.ClientConfig.Service.Port))
241244
} else {
242245
// - port is already added in mongoDSN with replicasetName/host:port format.
243246
// - oplog is enabled automatically for replicasets.
244-
backupOpt.StdinPipeCommand.Args = append(backupOpt.StdinPipeCommand.Args, "--oplog")
247+
// Don't use --oplog if user specify any of these arguments through opt.mongoArgs
248+
// 1. --db
249+
// 2. --collection
250+
// xref: https://docs.mongodb.com/manual/reference/program/mongodump/#cmdoption-mongodump-oplog
251+
forbiddenArgs := sets.NewString(
252+
"-d", "--db",
253+
"-c", "--collection",
254+
)
255+
if !containsArg(userArgs, forbiddenArgs) {
256+
backupOpt.StdinPipeCommand.Args = append(backupOpt.StdinPipeCommand.Args, "--oplog")
257+
}
245258
}
246-
if opt.mongoArgs != "" {
247-
backupOpt.StdinPipeCommand.Args = append(backupOpt.StdinPipeCommand.Args, opt.mongoArgs)
259+
260+
for _, arg := range userArgs {
261+
backupOpt.StdinPipeCommand.Args = append(backupOpt.StdinPipeCommand.Args, arg)
248262
}
263+
249264
return backupOpt
250265
}
251266

pkg/restore.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import (
66
"io/ioutil"
77
"os"
88
"path/filepath"
9+
"strings"
910

1011
"github.com/appscode/go/flags"
1112
"github.com/appscode/go/log"
1213
"github.com/pkg/errors"
1314
"github.com/spf13/cobra"
1415
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16+
"k8s.io/apimachinery/pkg/util/sets"
1517
"k8s.io/client-go/kubernetes"
1618
"k8s.io/client-go/tools/clientcmd"
1719
appcatalog_cs "kmodules.xyz/custom-resources/client/clientset/versioned"
@@ -191,16 +193,33 @@ func (opt *mongoOptions) restoreMongoDB() (*restic.RestoreOutput, error) {
191193
}, adminCreds...),
192194
}
193195

196+
userArgs := strings.Fields(opt.mongoArgs)
194197
if isStandalone {
195198
dumpOpt.StdoutPipeCommand.Args = append(dumpOpt.StdoutPipeCommand.Args, "--port="+fmt.Sprint(appBinding.Spec.ClientConfig.Service.Port))
196199
} else {
197200
// - port is already added in mongoDSN with replicasetName/host:port format.
198201
// - oplog is enabled automatically for replicasets.
199-
dumpOpt.StdoutPipeCommand.Args = append(dumpOpt.StdoutPipeCommand.Args, "--oplogReplay")
202+
// Don't use --oplogReplay if user specify any of these arguments through opt.mongoArgs
203+
// 1. --db
204+
// 2. --collection
205+
// 3. --nsInclude
206+
// 4. --nsExclude
207+
// xref: https://docs.mongodb.com/manual/reference/program/mongorestore/#cmdoption-mongorestore-oplogreplay
208+
forbiddenArgs := sets.NewString(
209+
"-d", "--db",
210+
"-c", "--collection",
211+
"--nsInclude",
212+
"--nsExclude",
213+
)
214+
if !containsArg(userArgs, forbiddenArgs) {
215+
dumpOpt.StdoutPipeCommand.Args = append(dumpOpt.StdoutPipeCommand.Args, "--oplogReplay")
216+
}
200217
}
201-
if opt.mongoArgs != "" {
202-
dumpOpt.StdoutPipeCommand.Args = append(dumpOpt.StdoutPipeCommand.Args, opt.mongoArgs)
218+
219+
for _, arg := range userArgs {
220+
dumpOpt.StdoutPipeCommand.Args = append(dumpOpt.StdoutPipeCommand.Args, arg)
203221
}
222+
204223
return dumpOpt
205224
}
206225

pkg/utils.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package pkg
33
import (
44
"fmt"
55
"os/exec"
6+
"strings"
67
"time"
78

89
"github.com/appscode/go/log"
10+
"k8s.io/apimachinery/pkg/util/sets"
911
"k8s.io/client-go/kubernetes"
1012
appcatalog_cs "kmodules.xyz/custom-resources/client/clientset/versioned"
1113
"stash.appscode.dev/stash/pkg/restic"
@@ -51,3 +53,15 @@ func waitForDBReady(host string, port int32) {
5153
time.Sleep(5 * time.Second)
5254
}
5355
}
56+
57+
func containsArg(args []string, checklist sets.String) bool {
58+
for i := range args {
59+
a := strings.FieldsFunc(args[i], func(r rune) bool {
60+
return r == '='
61+
})
62+
if checklist.Has(a[0]) {
63+
return true
64+
}
65+
}
66+
return false
67+
}

vendor/github.com/appscode/go/encoding/json/types/int_hash.go

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

vendor/github.com/appscode/go/version/version.go

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: go
2+
3+
go:
4+
- 1.9
5+
- "1.10"
6+
- 1.11
7+
- 1.12
8+
9+
go_import_path: gomodules.xyz/version
10+
11+
script:
12+
- go test

0 commit comments

Comments
 (0)