Skip to content

Commit

Permalink
Add Redis Script
Browse files Browse the repository at this point in the history
Signed-off-by: Shaad7 <[email protected]>
  • Loading branch information
AbdullahAlShaad committed Aug 14, 2023
1 parent 34a2da8 commit 201c656
Show file tree
Hide file tree
Showing 59 changed files with 19,454 additions and 92 deletions.
9 changes: 3 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,10 @@ require (
kmodules.xyz/custom-resources v0.25.2
kmodules.xyz/monitoring-agent-api v0.25.1
kubedb.dev/apimachinery v0.34.0
kubedb.dev/db-client-go v0.0.7
stash.appscode.dev/apimachinery v0.30.0
)

require (
github.com/onsi/ginkgo/v2 v2.4.0 // indirect
github.com/onsi/gomega v1.24.0 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
)

require (
cloud.google.com/go v0.99.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
Expand All @@ -50,6 +45,7 @@ require (
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
Expand Down Expand Up @@ -94,6 +90,7 @@ require (
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/redis/go-redis/v9 v9.0.5 // indirect
github.com/russross/blackfriday v1.5.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
Expand Down
16 changes: 10 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJm
github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/bsm/ginkgo/v2 v2.7.0 h1:ItPMPH90RbmZJt5GtkcNvIRuGEdwlBItdNVoyzaNQao=
github.com/bsm/gomega v1.26.0 h1:LhQm+AFcgV2M0WyKroMASzAzCAJVpAxQXv4SaI9a69Y=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cert-manager/cert-manager v1.9.1 h1:bNIsQyfWdIMSEwxgO4sVUEyAn6xuSgNwdt9m92OBACc=
Expand Down Expand Up @@ -161,6 +163,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
Expand Down Expand Up @@ -508,8 +512,7 @@ github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042
github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/ginkgo/v2 v2.1.4/go.mod h1:um6tUpWM/cxCK3/FK8BXqEiUMUwRgSM4JXG47RKZmLU=
github.com/onsi/ginkgo/v2 v2.1.6/go.mod h1:MEH45j8TBi6u9BMogfbp0stKC5cdGjumZj5Y7AG4VIk=
github.com/onsi/ginkgo/v2 v2.4.0 h1:+Ig9nvqgS5OBSACXNk15PLdp0U9XPYROt9CFzVdFGIs=
github.com/onsi/ginkgo/v2 v2.4.0/go.mod h1:iHkDK1fKGcBoEHT5W7YBq4RFWaQulw+caOMkAt4OrFo=
github.com/onsi/ginkgo/v2 v2.6.1 h1:1xQPCjcqYw/J5LchOcp4/2q/jzJFjiAOc25chhnDw+Q=
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
Expand All @@ -518,15 +521,13 @@ github.com/onsi/gomega v1.14.0/go.mod h1:cIuvLEne0aoVhAgh/O6ac0Op8WWw9H6eYCriF+t
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/onsi/gomega v1.20.1/go.mod h1:DtrZpjmvpn2mPm4YWQa0/ALMDj9v4YxLgojwPeREyVo=
github.com/onsi/gomega v1.24.0 h1:+0glovB9Jd6z3VR+ScSwQqXVTIfJcGA9UBM8yzQxhqg=
github.com/onsi/gomega v1.24.0/go.mod h1:Z/NWtiqwBrwUt4/2loMmHL63EDLnYHmVbuBpDr2vQAg=
github.com/onsi/gomega v1.24.2 h1:J/tulyYK6JwBldPViHJReihxxZ+22FHs0piGjQAvoUE=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down Expand Up @@ -571,11 +572,12 @@ github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1
github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5mo=
github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/redis/go-redis/v9 v9.0.5 h1:CuQcn5HIEeK7BgElubPP8CGtE0KakrnbBSTLjathl5o=
github.com/redis/go-redis/v9 v9.0.5/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down Expand Up @@ -1312,6 +1314,8 @@ kmodules.xyz/prober v0.25.0 h1:R5uRLHJEvEtEoogj+vaTAob0Btph6+PX5IlS6hPh8PA=
kmodules.xyz/prober v0.25.0/go.mod h1:z4RTnjaajNQa/vPltsiOnO3xI716I/ziD2ac2Exm+1M=
kubedb.dev/apimachinery v0.34.0 h1:ve906AZanI47PD42xxqBulvA/sd+zSR4lu2Ff1P4+u8=
kubedb.dev/apimachinery v0.34.0/go.mod h1:TEtf7k2WIKjka6IpXibsfFPONqAqOLELCta40Ox7a+c=
kubedb.dev/db-client-go v0.0.7 h1:IQeN4OsCaZ5ScT28gtuMRITBNoBnfbJ+gytY3KHsZKA=
kubedb.dev/db-client-go v0.0.7/go.mod h1:Em2g9xhNCboWlN2aBsrjFfHYajkMO3RyAipJvobbdds=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmds/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewCmdData(f cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "data",
Short: i18n.T("Insert of verify data in a database"),
Long: execLong,
Long: dataLong,
Example: dataExample,
Run: func(cmd *cobra.Command, args []string) {},
DisableFlagsInUseLine: true,
Expand Down
108 changes: 35 additions & 73 deletions pkg/data/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@ import (
"fmt"
"log"
"os"
"strings"

api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2"
cs "kubedb.dev/apimachinery/client/clientset/versioned"
"kubedb.dev/cli/pkg/lib"
_ "kubedb.dev/db-client-go/redis"

"github.com/spf13/cobra"
shell "gomodules.xyz/go-sh"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
)
Expand All @@ -44,7 +41,7 @@ func InsertRedisDataCMD(f cmdutil.Factory) *cobra.Command {
rows int
)

rdConnectCmd := &cobra.Command{
rdInsertCmd := &cobra.Command{
Use: "redis",
Aliases: []string{
"rd",
Expand All @@ -71,11 +68,11 @@ func InsertRedisDataCMD(f cmdutil.Factory) *cobra.Command {
if err != nil {
log.Fatal(err)
}

},
}
rdInsertCmd.Flags().IntVarP(&rows, "rows", "r", 10, "rows in ")

return rdConnectCmd
return rdInsertCmd
}

func VerifyRedisDataCMD(f cmdutil.Factory) *cobra.Command {
Expand Down Expand Up @@ -117,12 +114,11 @@ Examples:
if err != nil {
log.Fatalln(err)
}
tunnel, err := lib.TunnelToDBService(opts.config, dbName, namespace, api.RedisDatabasePort)

err = opts.verifyRedisKeys()
if err != nil {
log.Fatal("couldn't creat tunnel, error: ", err)
log.Fatalln(err)
}

tunnel.Close()
},
}

Expand All @@ -133,14 +129,9 @@ Examples:

type redisOpts struct {
db *api.Redis
config *rest.Config
client *kubernetes.Clientset
dbClient *cs.Clientset

errWriter *bytes.Buffer

keys []string
args []string
}

func newRedisOpts(f cmdutil.Factory, dbName, namespace string) (*redisOpts, error) {
Expand All @@ -149,11 +140,6 @@ func newRedisOpts(f cmdutil.Factory, dbName, namespace string) (*redisOpts, erro
return nil, err
}

client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}

dbClient, err := cs.NewForConfig(config)
if err != nil {
return nil, err
Expand All @@ -170,14 +156,38 @@ func newRedisOpts(f cmdutil.Factory, dbName, namespace string) (*redisOpts, erro

return &redisOpts{
db: db,
config: config,
client: client,
dbClient: dbClient,
errWriter: &bytes.Buffer{},
}, nil
}

var script = `
for i = 1, ARGV[1], 1 do
redis.call("SET", "key"..i, tostring({}):sub(10))
end
return "Ok!"
`

func (opts *redisOpts) insertDataToDatabase(rows int) error {
redisExtraFlags := []interface{}{
"eval", script, "0", fmt.Sprintf("%d", rows),
}
shSession := opts.getShellCommand(nil, redisExtraFlags)
out, err := shSession.Output()
if err != nil {
return fmt.Errorf("failed to execute command, error: %s, output: %s\n", err, out)
}
output := ""
if string(out) != "" {
output = ", output:\n\n" + string(out)
}

errOutput := opts.errWriter.String()
if errOutput != "" {
return fmt.Errorf("failed to execute command, stderr: %s%s", errOutput, output)
}
fmt.Printf("command applied successfully%s", output)
return nil
}

Expand All @@ -197,7 +207,7 @@ func (opts *redisOpts) getShellCommand(kubectlFlags, redisExtraFlags []interface
kubectlCommand = append(kubectlCommand, kubectlFlags...)

redisCommand := []interface{}{
"--", "redis-cli", "-n", "0", "-c", "redis",
"--", "redis-cli",
}

if db.Spec.TLS != nil {
Expand All @@ -209,61 +219,13 @@ func (opts *redisOpts) getShellCommand(kubectlFlags, redisExtraFlags []interface
)
}

kubectlCommand = append(kubectlCommand, "redis")
finalCommand := append(kubectlCommand, redisCommand...)
if redisExtraFlags != nil {
finalCommand = append(finalCommand, redisExtraFlags...)
}
return sh.Command("kubectl", finalCommand...).SetStdin(os.Stdin)
}

func (opts *redisOpts) connect() error {
kubectlFlag := []interface{}{
"-it",
}
shSession := opts.getShellCommand(kubectlFlag, nil)

err := shSession.Run()
if err != nil {
return err
}

func (opts *redisOpts) verifyRedisKeys() error {
return nil
}

func (opts *redisOpts) executeCommand(command string) error {
if len(opts.keys) != 0 || len(opts.args) != 0 {
return fmt.Errorf("argv and keys flags are only allowed with lua files, please provide lua file with --file")
}

commands := strings.Split(command, " ")
redisExtraFlags := convertToInterfaceArray(commands)

shSession := opts.getShellCommand(nil, redisExtraFlags)

out, err := shSession.Output()
if err != nil {
return fmt.Errorf("failed to execute command, error: %s, output: %s\n", err, out)
}
output := ""
if string(out) != "" {
output = ", output:\n\n" + string(out)
}

errOutput := opts.errWriter.String()
if errOutput != "" {
return fmt.Errorf("failed to execute command, stderr: %s%s", errOutput, output)
}
fmt.Printf("command applied successfully%s", output)

return nil
}

func convertToInterfaceArray(strs []string) []interface{} {
interfaceArray := make([]interface{}, len(strs))
for i := range strs {
interfaceArray[i] = strs[i]
}

return interfaceArray
}
21 changes: 21 additions & 0 deletions vendor/github.com/dgryski/go-rendezvous/LICENSE

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

Loading

0 comments on commit 201c656

Please sign in to comment.