Skip to content

Commit

Permalink
Global replace Redis with NATS
Browse files Browse the repository at this point in the history
Signed-off-by: hossainemruz <emruz@appscode.com>
  • Loading branch information
hossainemruz committed Aug 12, 2021
1 parent 6d880e0 commit ff46ec0
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 67 deletions.
9 changes: 1 addition & 8 deletions Dockerfile.dbg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"]
10 changes: 1 addition & 9 deletions Dockerfile.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 4 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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_$*) > $@
Expand Down Expand Up @@ -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."
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion cmd/stash-redis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module stash.appscode.dev/redis
module stash.appscode.dev/nats

go 1.15

Expand Down
18 changes: 9 additions & 9 deletions pkg/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ func NewCmdBackup() *cobra.Command {
var (
masterURL string
kubeconfigPath string
opt = redisOptions{
opt = natsOptions{
waitTimeout: 300,
setupOptions: restic.SetupOptions{
ScratchDir: restic.DefaultScratchDir,
EnableCache: false,
},
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")
Expand Down Expand Up @@ -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{
Expand All @@ -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)")
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}

Expand Down
18 changes: 9 additions & 9 deletions pkg/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@ func NewCmdRestore() *cobra.Command {
var (
masterURL string
kubeconfigPath string
opt = redisOptions{
opt = natsOptions{
setupOptions: restic.SetupOptions{
ScratchDir: restic.DefaultScratchDir,
EnableCache: false,
},
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")
Expand Down Expand Up @@ -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{
Expand All @@ -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)")
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 14 additions & 16 deletions pkg/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ 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

namespace string
backupSessionName string
appBindingName string
redisArgs string
natsArgs string
waitTimeout int32
outputDir string

Expand All @@ -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{}{
Expand All @@ -96,15 +96,15 @@ 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
}
return true, nil
})
}

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
Expand All @@ -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
}

0 comments on commit ff46ec0

Please sign in to comment.