From ff46ec020a90de366a06078dd8b6efce9ad99fd5 Mon Sep 17 00:00:00 2001 From: hossainemruz Date: Thu, 12 Aug 2021 12:44:43 +0600 Subject: [PATCH] Global replace Redis with NATS Signed-off-by: hossainemruz --- Dockerfile.dbg | 9 +-------- Dockerfile.in | 10 +--------- Makefile | 10 ++++------ README.md | 10 +++++----- cmd/stash-redis/main.go | 2 +- go.mod | 2 +- pkg/backup.go | 18 +++++++++--------- pkg/restore.go | 18 +++++++++--------- pkg/root.go | 6 +++--- pkg/util.go | 30 ++++++++++++++---------------- 10 files changed, 48 insertions(+), 67 deletions(-) diff --git a/Dockerfile.dbg b/Dockerfile.dbg index 5fa058cc..3ac579b3 100644 --- a/Dockerfile.dbg +++ b/Dockerfile.dbg @@ -26,16 +26,10 @@ RUN set -x && bzip2 -d restic.bz2 \ && chmod 755 restic -RUN set -x \ - && curl -fsSL -o redis-dump-go.tar.gz https://github.com/yannh/redis-dump-go/releases/download/v{REDIS_DUMP_VER}/redis-dump-go_{REDIS_DUMP_VER}_{ARG_OS}_{ARG_ARCH}.tar.gz \ - && tar -xzf redis-dump-go.tar.gz \ - && chmod 755 redis-dump-go - - FROM {ARG_FROM} -LABEL org.opencontainers.image.source https://github.com/stashed/redis +LABEL org.opencontainers.image.source https://github.com/stashed/nats ENV DEBIAN_FRONTEND noninteractive ENV DEBCONF_NONINTERACTIVE_SEEN true @@ -46,7 +40,6 @@ RUN set -x \ && rm -rf /var/lib/apt/lists/* /usr/share/doc /usr/share/man /tmp/* COPY --from=0 restic /bin/restic -COPY --from=0 redis-dump-go /bin/redis-dump-go COPY bin/{ARG_OS}_{ARG_ARCH}/{ARG_BIN} /{ARG_BIN} ENTRYPOINT ["/{ARG_BIN}"] diff --git a/Dockerfile.in b/Dockerfile.in index 85f8c885..b91a0cd8 100644 --- a/Dockerfile.in +++ b/Dockerfile.in @@ -26,16 +26,9 @@ RUN set -x && bzip2 -d restic.bz2 \ && chmod 755 restic -RUN set -x \ - && curl -fsSL -o redis-dump-go.tar.gz https://github.com/yannh/redis-dump-go/releases/download/v{REDIS_DUMP_VER}/redis-dump-go_{REDIS_DUMP_VER}_{ARG_OS}_{ARG_ARCH}.tar.gz \ - && tar -xzf redis-dump-go.tar.gz \ - && chmod 755 redis-dump-go - - - FROM {ARG_FROM} -LABEL org.opencontainers.image.source https://github.com/stashed/redis +LABEL org.opencontainers.image.source https://github.com/stashed/nats ENV DEBIAN_FRONTEND noninteractive ENV DEBCONF_NONINTERACTIVE_SEEN true @@ -46,7 +39,6 @@ RUN set -x \ && rm -rf /var/lib/apt/lists/* /usr/share/doc /usr/share/man /tmp/* COPY --from=0 /restic /bin/restic -COPY --from=0 redis-dump-go /bin/redis-dump-go COPY bin/{ARG_OS}_{ARG_ARCH}/{ARG_BIN} /{ARG_BIN} USER nobody diff --git a/Makefile b/Makefile index e980d5e6..2d395cd0 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ SHELL=/bin/bash -o pipefail GO_PKG := stash.appscode.dev REPO := $(notdir $(shell pwd)) -BIN := stash-redis +BIN := stash-nats COMPRESS ?= no # Where to push the docker image. @@ -43,7 +43,6 @@ else endif RESTIC_VER := 0.12.0-ac.20210727 -REDIS_DUMP_VER := 0.5.1 ### ### These variables should not need tweaking. @@ -59,8 +58,8 @@ BIN_PLATFORMS := $(DOCKER_PLATFORMS) OS := $(if $(GOOS),$(GOOS),$(shell go env GOOS)) ARCH := $(if $(GOARCH),$(GOARCH),$(shell go env GOARCH)) -BASEIMAGE_PROD ?= redis:6.2.5 -BASEIMAGE_DBG ?= redis:6.2.5 +BASEIMAGE_PROD ?= natsio/natsbox:0.6.0 +BASEIMAGE_DBG ?= natsio/natsbox:0.6.0 IMAGE := $(REGISTRY)/$(BIN) VERSION_PROD := $(VERSION) @@ -223,7 +222,6 @@ bin/.container-$(DOTFILE_IMAGE)-%: bin/$(OS)_$(ARCH)/$(BIN) $(DOCKERFILE_%) -e 's|{ARG_OS}|$(OS)|g' \ -e 's|{ARG_FROM}|$(BASEIMAGE_$*)|g' \ -e 's|{RESTIC_VER}|$(RESTIC_VER)|g' \ - -e 's|{REDIS_DUMP_VER}|$(REDIS_DUMP_VER)|g' \ $(DOCKERFILE_$*) > bin/.dockerfile-$*-$(OS)_$(ARCH) @DOCKER_CLI_EXPERIMENTAL=enabled docker buildx build --platform $(OS)/$(ARCH) --load --pull -t $(IMAGE):$(TAG_$*) -f bin/.dockerfile-$*-$(OS)_$(ARCH) . @docker images -q $(IMAGE):$(TAG_$*) > $@ @@ -369,5 +367,5 @@ clean: .PHONY: push-to-kind push-to-kind: container @echo "Loading docker image into kind cluster...." - @kind load docker-image $(REGISTRY)/stash-redis:$(TAG) + @kind load docker-image $(REGISTRY)/stash-nats:$(TAG) @echo "Image has been pushed successfully into kind cluster." diff --git a/README.md b/README.md index 9dbb512a..7d7d9c82 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ -[![Go Report Card](https://goreportcard.com/badge/stash.appscode.dev/redis)](https://goreportcard.com/report/stash.appscode.dev/redis) -![CI](https://github.com/stashed/redis/workflows/CI/badge.svg) -[![Docker Pulls](https://img.shields.io/docker/pulls/stashed/stash-redis.svg)](https://hub.docker.com/r/stashed/stash-redis/) +[![Go Report Card](https://goreportcard.com/badge/stash.appscode.dev/nats)](https://goreportcard.com/report/stash.appscode.dev/nats) +![CI](https://github.com/stashed/nats/workflows/CI/badge.svg) +[![Docker Pulls](https://img.shields.io/docker/pulls/stashed/stash-nats.svg)](https://hub.docker.com/r/stashed/stash-nats/) [![Slack](https://shields.io/badge/Join_Slack-salck?color=4A154B&logo=slack)](https://slack.appscode.com) [![Twitter](https://img.shields.io/twitter/follow/kubestash.svg?style=social&logo=twitter&label=Follow)](https://twitter.com/intent/follow?screen_name=KubeStash) -# Redis +# NATS -Redis backup and restore plugin for [Stash by AppsCode](https://stash.run). +NATS backup and restore plugin for [Stash by AppsCode](https://stash.run). ## Support diff --git a/cmd/stash-redis/main.go b/cmd/stash-redis/main.go index e705a3cd..f98a68b5 100644 --- a/cmd/stash-redis/main.go +++ b/cmd/stash-redis/main.go @@ -21,7 +21,7 @@ import ( "runtime" _ "stash.appscode.dev/apimachinery/client/clientset/versioned/fake" - "stash.appscode.dev/redis/pkg" + "stash.appscode.dev/nats/pkg" "gomodules.xyz/logs" _ "k8s.io/client-go/kubernetes/fake" diff --git a/go.mod b/go.mod index 61efcab5..1f6e642b 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module stash.appscode.dev/redis +module stash.appscode.dev/nats go 1.15 diff --git a/pkg/backup.go b/pkg/backup.go index e8ddafc7..505662e8 100644 --- a/pkg/backup.go +++ b/pkg/backup.go @@ -42,7 +42,7 @@ func NewCmdBackup() *cobra.Command { var ( masterURL string kubeconfigPath string - opt = redisOptions{ + opt = natsOptions{ waitTimeout: 300, setupOptions: restic.SetupOptions{ ScratchDir: restic.DefaultScratchDir, @@ -50,14 +50,14 @@ func NewCmdBackup() *cobra.Command { }, backupOptions: restic.BackupOptions{ Host: restic.DefaultHost, - StdinFileName: RedisDumpFile, + StdinFileName: NATSDumpFile, }, } ) cmd := &cobra.Command{ - Use: "backup-redis", - Short: "Takes a backup of Redis DB", + Use: "backup-nats", + Short: "Takes a backup of NATS DB", DisableAutoGenTag: true, RunE: func(cmd *cobra.Command, args []string) error { flags.EnsureRequiredFlags(cmd, "appbinding", "provider", "secret-dir") @@ -89,7 +89,7 @@ func NewCmdBackup() *cobra.Command { Name: opt.appBindingName, } var backupOutput *restic.BackupOutput - backupOutput, err = opt.backupRedis(targetRef) + backupOutput, err = opt.backupNATS(targetRef) if err != nil { backupOutput = &restic.BackupOutput{ BackupTargetStatus: api_v1beta1.BackupTargetStatus{ @@ -114,7 +114,7 @@ func NewCmdBackup() *cobra.Command { }, } - cmd.Flags().StringVar(&opt.redisArgs, "redis-args", opt.redisArgs, "Additional arguments") + cmd.Flags().StringVar(&opt.natsArgs, "nats-args", opt.natsArgs, "Additional arguments") cmd.Flags().Int32Var(&opt.waitTimeout, "wait-timeout", opt.waitTimeout, "Time limit to wait for the database to be ready") cmd.Flags().StringVar(&masterURL, "master", masterURL, "The address of the Kubernetes API server (overrides any value in kubeconfig)") @@ -150,7 +150,7 @@ func NewCmdBackup() *cobra.Command { return cmd } -func (opt *redisOptions) backupRedis(targetRef api_v1beta1.TargetRef) (*restic.BackupOutput, error) { +func (opt *natsOptions) backupNATS(targetRef api_v1beta1.TargetRef) (*restic.BackupOutput, error) { // if any pre-backup actions has been assigned to it, execute them actionOptions := api_util.ActionOptions{ StashClient: opt.stashClient, @@ -198,12 +198,12 @@ func (opt *redisOptions) backupRedis(targetRef api_v1beta1.TargetRef) (*restic.B // setup pipe command backupCmd := restic.Command{ - Name: RedisDumpCMD, + Name: NATSBackupCMD, Args: []interface{}{ "-host", appBinding.Spec.ClientConfig.Service.Name, }, } - for _, arg := range strings.Fields(opt.redisArgs) { + for _, arg := range strings.Fields(opt.natsArgs) { backupCmd.Args = append(backupCmd.Args, arg) } diff --git a/pkg/restore.go b/pkg/restore.go index ce0f272b..317108fb 100644 --- a/pkg/restore.go +++ b/pkg/restore.go @@ -40,7 +40,7 @@ func NewCmdRestore() *cobra.Command { var ( masterURL string kubeconfigPath string - opt = redisOptions{ + opt = natsOptions{ setupOptions: restic.SetupOptions{ ScratchDir: restic.DefaultScratchDir, EnableCache: false, @@ -48,14 +48,14 @@ func NewCmdRestore() *cobra.Command { waitTimeout: 300, dumpOptions: restic.DumpOptions{ Host: restic.DefaultHost, - FileName: RedisDumpFile, + FileName: NATSDumpFile, }, } ) cmd := &cobra.Command{ - Use: "restore-redis", - Short: "Restores Redis DB Backup", + Use: "restore-nats", + Short: "Restores NATS DB Backup", DisableAutoGenTag: true, RunE: func(cmd *cobra.Command, args []string) error { flags.EnsureRequiredFlags(cmd, "appbinding", "provider", "secret-dir") @@ -85,7 +85,7 @@ func NewCmdRestore() *cobra.Command { } var restoreOutput *restic.RestoreOutput - restoreOutput, err = opt.restoreRedis(targetRef) + restoreOutput, err = opt.restoreNATS(targetRef) if err != nil { restoreOutput = &restic.RestoreOutput{ RestoreTargetStatus: api_v1beta1.RestoreMemberStatus{ @@ -109,7 +109,7 @@ func NewCmdRestore() *cobra.Command { }, } - cmd.Flags().StringVar(&opt.redisArgs, "redis-args", opt.redisArgs, "Additional arguments") + cmd.Flags().StringVar(&opt.natsArgs, "nats-args", opt.natsArgs, "Additional arguments") cmd.Flags().Int32Var(&opt.waitTimeout, "wait-timeout", opt.waitTimeout, "Time limit to wait for the database to be ready") cmd.Flags().StringVar(&masterURL, "master", masterURL, "The address of the Kubernetes API server (overrides any value in kubeconfig)") @@ -137,7 +137,7 @@ func NewCmdRestore() *cobra.Command { return cmd } -func (opt *redisOptions) restoreRedis(targetRef api_v1beta1.TargetRef) (*restic.RestoreOutput, error) { +func (opt *natsOptions) restoreNATS(targetRef api_v1beta1.TargetRef) (*restic.RestoreOutput, error) { // apply nice, ionice settings from env var err error opt.setupOptions.Nice, err = v1.NiceSettingsFromEnv() @@ -169,13 +169,13 @@ func (opt *redisOptions) restoreRedis(targetRef api_v1beta1.TargetRef) (*restic. // setup pipe command restoreCmd := restic.Command{ - Name: RedisRestoreCMD, + Name: NATSRestoreCMD, Args: []interface{}{ "--pipe", "-h", appBinding.Spec.ClientConfig.Service.Name, }, } - for _, arg := range strings.Fields(opt.redisArgs) { + for _, arg := range strings.Fields(opt.natsArgs) { restoreCmd.Args = append(restoreCmd.Args, arg) } // if port is specified, append port in the arguments diff --git a/pkg/root.go b/pkg/root.go index 4df21475..cf0b494f 100644 --- a/pkg/root.go +++ b/pkg/root.go @@ -31,9 +31,9 @@ var licenseApiService string func NewRootCmd() *cobra.Command { var rootCmd = &cobra.Command{ - Use: "stash-redis", - Short: `Redis backup & restore plugin for Stash by AppsCode`, - Long: `Redis backup & restore plugin for Stash by AppsCode. For more information, visit here: https://stash.run`, + Use: "stash-nats", + Short: `NATS backup & restore plugin for Stash by AppsCode`, + Long: `NATS backup & restore plugin for Stash by AppsCode. For more information, visit here: https://stash.run`, DisableAutoGenTag: true, PersistentPreRunE: func(c *cobra.Command, args []string) error { cli.SendAnalytics(c, v.Version.Version) diff --git a/pkg/util.go b/pkg/util.go index 0f979018..b9a660df 100644 --- a/pkg/util.go +++ b/pkg/util.go @@ -33,16 +33,16 @@ import ( ) const ( - RedisUser = "username" - RedisPassword = "password" - RedisDumpFile = "dumpfile.resp" - RedisDumpCMD = "redis-dump-go" - RedisRestoreCMD = "redis-cli" - EnvRedisCLIAuth = "REDISCLI_AUTH" - EnvRedisDumpGoAuth = "REDISDUMPGO_AUTH" + NATSUser = "username" + NATSPassword = "password" + NATSDumpFile = "dumpfile.resp" + NATSBackupCMD = "nats" + NATSRestoreCMD = "nats" + EnvNATSUseer = "NATS_USER" + EnvNATSPassword = "NATS_PASSWORD" ) -type redisOptions struct { +type natsOptions struct { kubeClient kubernetes.Interface stashClient stash.Interface catalogClient appcatalog_cs.Interface @@ -50,7 +50,7 @@ type redisOptions struct { namespace string backupSessionName string appBindingName string - redisArgs string + natsArgs string waitTimeout int32 outputDir string @@ -76,7 +76,7 @@ func (wrapper *SessionWrapper) SetEnv(key, value string) { wrapper.Session.SetEnv(key, value) } -func (opt *redisOptions) waitForDBReady(appBinding *appcatalog.AppBinding) error { +func (opt *natsOptions) waitForDBReady(appBinding *appcatalog.AppBinding) error { klog.Infoln("Waiting for the database to be ready.....") sh := NewSessionWrapper() args := []interface{}{ @@ -96,7 +96,7 @@ func (opt *redisOptions) waitForDBReady(appBinding *appcatalog.AppBinding) error } return wait.PollImmediate(time.Second*5, time.Minute*5, func() (bool, error) { - err := sh.Command("redis-cli", args...).Run() + err := sh.Command("nats", args...).Run() if err != nil { return false, nil } @@ -104,7 +104,7 @@ func (opt *redisOptions) waitForDBReady(appBinding *appcatalog.AppBinding) error }) } -func (opt *redisOptions) setCredentials(sh Shell, appBinding *appcatalog.AppBinding) error { +func (opt *natsOptions) setCredentials(sh Shell, appBinding *appcatalog.AppBinding) error { // if credential secret is not provided in AppBinding, then nothing to do. if appBinding.Spec.Secret == nil { return nil @@ -122,10 +122,8 @@ func (opt *redisOptions) setCredentials(sh Shell, appBinding *appcatalog.AppBind return err } - // set auth env for redis-cli - sh.SetEnv(EnvRedisCLIAuth, string(secret.Data[RedisPassword])) + // set auth env for nats + sh.SetEnv(EnvNATSUseer, string(secret.Data[NATSPassword])) - // set auth env for redis-dump-go - sh.SetEnv(EnvRedisDumpGoAuth, string(secret.Data[RedisPassword])) return nil }