Skip to content

Commit

Permalink
Fix argument passing (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
hossainemruz authored and tamalsaha committed Oct 17, 2019
1 parent ccdf81f commit 3097a67
Show file tree
Hide file tree
Showing 20 changed files with 1,338 additions and 25 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ module stash.appscode.dev/mongodb
go 1.12

require (
github.com/appscode/go v0.0.0-20190808133642-1d4ef1f1c1e0
github.com/appscode/go v0.0.0-20191016085057-e186b6c94a3b
github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27
github.com/pkg/errors v0.8.1
github.com/spf13/cobra v0.0.5
k8s.io/apimachinery v0.0.0-20190508063446-a3da69d3723c
k8s.io/client-go v11.0.0+incompatible
kmodules.xyz/client-go v0.0.0-20190808141354-bbb9e14f60ab
k8s.io/klog v0.3.1 // indirect
kmodules.xyz/client-go v0.0.0-20191016085444-3044854ca625
kmodules.xyz/custom-resources v0.0.0-20190808144301-114abf10dfe2
kubedb.dev/apimachinery v0.13.0-rc.0.0.20190830121921-d28870c9d73a
stash.appscode.dev/stash v0.9.0-rc.0.0.20190910104640-5f49f5d214aa
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb
github.com/appscode/docker-registry-client v0.0.0-20180426150142-1bb02bb202b0/go.mod h1:W9bsWfSbgJXUXzBZ+tSBV2g+zzT7ANPHQFsSXRHYKsA=
github.com/appscode/go v0.0.0-20190808133642-1d4ef1f1c1e0 h1:EZp2qE4LUJUG0wMHyUxOg982hVbOFh7qqcM1XIMVGpk=
github.com/appscode/go v0.0.0-20190808133642-1d4ef1f1c1e0/go.mod h1:iy07dV61Z7QQdCKJCIvUoDL21u6AIceRhZzyleh2ymc=
github.com/appscode/go v0.0.0-20191016085057-e186b6c94a3b h1:UskbETXW8u0DRCPU1pyBIgA55BFUnjuYjmt3qOBMdAY=
github.com/appscode/go v0.0.0-20191016085057-e186b6c94a3b/go.mod h1:hUW7Fq0KY2/ntGnYAzemyUpIhLL6bXrTljN6SRY/+Lc=
github.com/appscode/osm v0.12.0/go.mod h1:+ZXU2HilSSow2o0gsOolJfOTkhqQ2FcGROOum4wEh4w=
github.com/armon/circbuf v0.0.0-20190214190532-5111143e8da2 h1:7Ip0wMmLHLRJdrloDxZfhMm0xrLXZS8+COSu2bXmEQs=
github.com/armon/circbuf v0.0.0-20190214190532-5111143e8da2/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
Expand Down Expand Up @@ -509,6 +511,8 @@ gomodules.xyz/jsonpatch/v2 v2.0.0 h1:OyHbl+7IOECpPKfVK42oFr6N7+Y2dR+Jsb/IiDV3hOo
gomodules.xyz/jsonpatch/v2 v2.0.0/go.mod h1:IhYNNY4jnS53ZnfE4PAmpKtDpTCj1JFXc+3mwe7XcUU=
gomodules.xyz/stow v0.2.0/go.mod h1:YDDPM4nmJgQhxq4I5aGtLM/xrvJzDSVM/fbc7NGizpY=
gomodules.xyz/version v0.0.0-20190507203204-7cec7ee542d3/go.mod h1:Y8xuV02mL/45psyPKG3NCVOwvAOy6T5Kx0l3rCjKSjU=
gomodules.xyz/version v0.1.0 h1:inGItCg/egI0jPMeIE0SQkiDIJaodOMoCrxYqasQLR0=
gomodules.xyz/version v0.1.0/go.mod h1:Y8xuV02mL/45psyPKG3NCVOwvAOy6T5Kx0l3rCjKSjU=
google.golang.org/api v0.0.0-20180910000450-7ca32eb868bf/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
google.golang.org/api v0.0.0-20181220000619-583d854617af/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
google.golang.org/api v0.1.0/go.mod h1:UGEZY7KEX120AnNLIHFMKIo4obdJhkp2tPbaPlQx13Y=
Expand Down Expand Up @@ -584,11 +588,14 @@ k8s.io/utils v0.0.0-20190221042446-c2654d5206da h1:ElyM7RPonbKnQqOcw7dG2IK5uvQQn
k8s.io/utils v0.0.0-20190221042446-c2654d5206da/go.mod h1:8k8uAuAQ0rXslZKaEWd0c3oVhZz7sSzSiPnVZayjIX0=
kmodules.xyz/client-go v0.0.0-20190808141354-bbb9e14f60ab h1:rnKcFaDLtqpdooase2BqPfHV9Mu1WvzbTUmucnJjLio=
kmodules.xyz/client-go v0.0.0-20190808141354-bbb9e14f60ab/go.mod h1:1GI6h5D31op/2D+Hrn9DhXW6iUd5wsHBQRoLeY7fndM=
kmodules.xyz/client-go v0.0.0-20191016085444-3044854ca625 h1:A/ceKV+HXdheKSpaY2CF7Q70E/iDKoysxiWtAuYMXyE=
kmodules.xyz/client-go v0.0.0-20191016085444-3044854ca625/go.mod h1:wFUBDofZzgkLW1sFJT5SSt6/G3ph2P8zUi5v81q5BKQ=
kmodules.xyz/custom-resources v0.0.0-20190808144301-114abf10dfe2 h1:u0zEFCvAttk09O64UzY1DVXNke9uAANmf4vllnVquS8=
kmodules.xyz/custom-resources v0.0.0-20190808144301-114abf10dfe2/go.mod h1:vlKyFcCXC+2Kfn3Fa5Z7RnBWyp4t46FSeEutNqpqMm8=
kmodules.xyz/monitoring-agent-api v0.0.0-20190808150221-601a4005b7f7/go.mod h1:9v6LM+dmaAP3jjDiPOPOLPfsvD7jNjBJsi37JNCiFho=
kmodules.xyz/objectstore-api v0.0.0-20190808153322-733e8798e8de h1:Z/z3SSS+mvRGzLT5HXLazogjnOYIfbYTYWrKBsiLtp8=
kmodules.xyz/objectstore-api v0.0.0-20190808153322-733e8798e8de/go.mod h1:Wx+Ffc7weI6zv7NQvq0GWXMGGQFn6LqVb0KcXQFwRFs=
kmodules.xyz/objectstore-api v0.0.0-20190824212210-196174aa0fc0 h1:ELq3ybK3noQP9SyZx34QsnZgU2As9r8hPAGEnuIy7Co=
kmodules.xyz/objectstore-api v0.0.0-20190824212210-196174aa0fc0/go.mod h1:vFvmrMvRlkcUANfU4Ko3dZR3HUhWMUQc1sEHcwufims=
kmodules.xyz/offshoot-api v0.0.0-20190808152534-e3dc715f844b h1:VZUtg4L9Qm2C7PBwa4sp/SvHckKidZSpkA33GoB24SA=
kmodules.xyz/offshoot-api v0.0.0-20190808152534-e3dc715f844b/go.mod h1:AzvHBjJvhFwUEiWrEKfdsgoqt5Ulio44Beo1vPoq7ww=
Expand All @@ -607,4 +614,5 @@ sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
stash.appscode.dev/stash v0.9.0-rc.0 h1:GhzBnqHYBgq+2bcuwsDK3nhq2PPEhGkBH0EFAXp2OmY=
stash.appscode.dev/stash v0.9.0-rc.0/go.mod h1:/YjAPx/9E4ScxWea1mbJC6qx9TomhqS83AUy2LufVVk=
stash.appscode.dev/stash v0.9.0-rc.0.0.20190910104640-5f49f5d214aa h1:/cDpVS+SY8jr0P9AmPiIkj6EaPrVJzsmGDX2RgUYtMU=
stash.appscode.dev/stash v0.9.0-rc.0.0.20190910104640-5f49f5d214aa/go.mod h1:6WrRpjkI1av7305B60BLRx+PZzwMAoiGmOd5ewYAAyw=
21 changes: 18 additions & 3 deletions pkg/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
appcatalog_cs "kmodules.xyz/custom-resources/client/clientset/versioned"
Expand Down Expand Up @@ -236,16 +237,30 @@ func (opt *mongoOptions) backupMongoDB() (*restic.BackupOutput, error) {
"--archive",
}, adminCreds...),
}
userArgs := strings.Fields(opt.mongoArgs)

if isStandalone {
backupOpt.StdinPipeCommand.Args = append(backupOpt.StdinPipeCommand.Args, "--port="+fmt.Sprint(appBinding.Spec.ClientConfig.Service.Port))
} else {
// - port is already added in mongoDSN with replicasetName/host:port format.
// - oplog is enabled automatically for replicasets.
backupOpt.StdinPipeCommand.Args = append(backupOpt.StdinPipeCommand.Args, "--oplog")
// Don't use --oplog if user specify any of these arguments through opt.mongoArgs
// 1. --db
// 2. --collection
// xref: https://docs.mongodb.com/manual/reference/program/mongodump/#cmdoption-mongodump-oplog
forbiddenArgs := sets.NewString(
"-d", "--db",
"-c", "--collection",
)
if !containsArg(userArgs, forbiddenArgs) {
backupOpt.StdinPipeCommand.Args = append(backupOpt.StdinPipeCommand.Args, "--oplog")
}
}
if opt.mongoArgs != "" {
backupOpt.StdinPipeCommand.Args = append(backupOpt.StdinPipeCommand.Args, opt.mongoArgs)

for _, arg := range userArgs {
backupOpt.StdinPipeCommand.Args = append(backupOpt.StdinPipeCommand.Args, arg)
}

return backupOpt
}

Expand Down
25 changes: 22 additions & 3 deletions pkg/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/appscode/go/flags"
"github.com/appscode/go/log"
"github.com/pkg/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
appcatalog_cs "kmodules.xyz/custom-resources/client/clientset/versioned"
Expand Down Expand Up @@ -191,16 +193,33 @@ func (opt *mongoOptions) restoreMongoDB() (*restic.RestoreOutput, error) {
}, adminCreds...),
}

userArgs := strings.Fields(opt.mongoArgs)
if isStandalone {
dumpOpt.StdoutPipeCommand.Args = append(dumpOpt.StdoutPipeCommand.Args, "--port="+fmt.Sprint(appBinding.Spec.ClientConfig.Service.Port))
} else {
// - port is already added in mongoDSN with replicasetName/host:port format.
// - oplog is enabled automatically for replicasets.
dumpOpt.StdoutPipeCommand.Args = append(dumpOpt.StdoutPipeCommand.Args, "--oplogReplay")
// Don't use --oplogReplay if user specify any of these arguments through opt.mongoArgs
// 1. --db
// 2. --collection
// 3. --nsInclude
// 4. --nsExclude
// xref: https://docs.mongodb.com/manual/reference/program/mongorestore/#cmdoption-mongorestore-oplogreplay
forbiddenArgs := sets.NewString(
"-d", "--db",
"-c", "--collection",
"--nsInclude",
"--nsExclude",
)
if !containsArg(userArgs, forbiddenArgs) {
dumpOpt.StdoutPipeCommand.Args = append(dumpOpt.StdoutPipeCommand.Args, "--oplogReplay")
}
}
if opt.mongoArgs != "" {
dumpOpt.StdoutPipeCommand.Args = append(dumpOpt.StdoutPipeCommand.Args, opt.mongoArgs)

for _, arg := range userArgs {
dumpOpt.StdoutPipeCommand.Args = append(dumpOpt.StdoutPipeCommand.Args, arg)
}

return dumpOpt
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package pkg
import (
"fmt"
"os/exec"
"strings"
"time"

"github.com/appscode/go/log"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes"
appcatalog_cs "kmodules.xyz/custom-resources/client/clientset/versioned"
"stash.appscode.dev/stash/pkg/restic"
Expand Down Expand Up @@ -51,3 +53,15 @@ func waitForDBReady(host string, port int32) {
time.Sleep(5 * time.Second)
}
}

func containsArg(args []string, checklist sets.String) bool {
for i := range args {
a := strings.FieldsFunc(args[i], func(r rune) bool {
return r == '='
})
if checklist.Has(a[0]) {
return true
}
}
return false
}
13 changes: 13 additions & 0 deletions vendor/github.com/appscode/go/encoding/json/types/int_hash.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion vendor/github.com/appscode/go/version/version.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions vendor/gomodules.xyz/version/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: go

go:
- 1.9
- "1.10"
- 1.11
- 1.12

go_import_path: gomodules.xyz/version

script:
- go test
Loading

0 comments on commit 3097a67

Please sign in to comment.