Skip to content

Commit 7d4bfee

Browse files
author
Emruz Hossain
authored
Make Redis addon ready to use (#1)
Signed-off-by: hossainemruz <emruz@appscode.com>
1 parent f5cf21d commit 7d4bfee

File tree

6 files changed

+103
-32
lines changed

6 files changed

+103
-32
lines changed

Dockerfile.dbg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ RUN set -x \
2222
&& apt-get install -y --no-install-recommends apt-transport-https ca-certificates curl bzip2 tar
2323

2424
RUN set -x \
25-
&& curl -fsSL -o restic.bz2 https://github.com/restic/restic/releases/download/v{RESTIC_VER}/restic_{RESTIC_VER}_{ARG_OS}_{ARG_ARCH}.bz2 \
25+
&& curl -fsSL -o restic.bz2 https://github.com/stashed/restic/releases/download/v{RESTIC_VER}/restic_{RESTIC_VER}_{ARG_OS}_{ARG_ARCH}.bz2 \
2626
&& bzip2 -d restic.bz2 \
2727
&& chmod 755 restic
2828

2929
RUN set -x \
30-
&& curl -fsSL -o redis-dump-go.tar.gz https://github.com/yannh/redis-dump-go/releases/download/v{REDIS_DUMP_VER}/redis-dump-go-{ARG_OS}-{ARG_ARCH}.tar.gz \
30+
&& 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 \
3131
&& tar -xzf redis-dump-go.tar.gz \
3232
&& chmod 755 redis-dump-go
3333

Dockerfile.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ RUN set -x \
2222
&& apt-get install -y --no-install-recommends apt-transport-https ca-certificates curl bzip2 tar
2323

2424
RUN set -x \
25-
&& curl -fsSL -o restic.bz2 https://github.com/restic/restic/releases/download/v{RESTIC_VER}/restic_{RESTIC_VER}_{ARG_OS}_{ARG_ARCH}.bz2 \
25+
&& curl -fsSL -o restic.bz2 https://github.com/stashed/restic/releases/download/v{RESTIC_VER}/restic_{RESTIC_VER}_{ARG_OS}_{ARG_ARCH}.bz2 \
2626
&& bzip2 -d restic.bz2 \
2727
&& chmod 755 restic
2828

2929
RUN set -x \
30-
&& curl -fsSL -o redis-dump-go.tar.gz https://github.com/yannh/redis-dump-go/releases/download/v{REDIS_DUMP_VER}/redis-dump-go-{ARG_OS}-{ARG_ARCH}.tar.gz \
30+
&& 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 \
3131
&& tar -xzf redis-dump-go.tar.gz \
3232
&& chmod 755 redis-dump-go
3333

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ BIN_PLATFORMS := $(DOCKER_PLATFORMS)
5959
OS := $(if $(GOOS),$(GOOS),$(shell go env GOOS))
6060
ARCH := $(if $(GOARCH),$(GOARCH),$(shell go env GOARCH))
6161

62-
BASEIMAGE_PROD ?= redis:5.0.3
63-
BASEIMAGE_DBG ?= redis:5.0.3
62+
BASEIMAGE_PROD ?= redis:6.2.5
63+
BASEIMAGE_DBG ?= redis:6.2.5
6464

6565
IMAGE := $(REGISTRY)/$(BIN)
6666
VERSION_PROD := $(VERSION)

pkg/backup.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ func NewCmdBackup() *cobra.Command {
4343
masterURL string
4444
kubeconfigPath string
4545
opt = redisOptions{
46-
47-
myArgs: "--all-databases",
4846
waitTimeout: 300,
4947
setupOptions: restic.SetupOptions{
5048
ScratchDir: restic.DefaultScratchDir,
@@ -116,7 +114,7 @@ func NewCmdBackup() *cobra.Command {
116114
},
117115
}
118116

119-
cmd.Flags().StringVar(&opt.myArgs, "redis-args", opt.myArgs, "Additional arguments")
117+
cmd.Flags().StringVar(&opt.redisArgs, "redis-args", opt.redisArgs, "Additional arguments")
120118
cmd.Flags().Int32Var(&opt.waitTimeout, "wait-timeout", opt.waitTimeout, "Time limit to wait for the database to be ready")
121119

122120
cmd.Flags().StringVar(&masterURL, "master", masterURL, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
@@ -192,27 +190,37 @@ func (opt *redisOptions) backupRedis(targetRef api_v1beta1.TargetRef) (*restic.B
192190
return nil, err
193191
}
194192

193+
// set access credentials
194+
err = opt.setCredentials(resticWrapper, appBinding)
195+
if err != nil {
196+
return nil, err
197+
}
198+
195199
// setup pipe command
196-
opt.backupOptions.StdinPipeCommand = restic.Command{
200+
backupCmd := restic.Command{
197201
Name: RedisDumpCMD,
198202
Args: []interface{}{
199203
"-host", appBinding.Spec.ClientConfig.Service.Name,
200204
},
201205
}
202-
for _, arg := range strings.Fields(opt.myArgs) {
203-
opt.backupOptions.StdinPipeCommand.Args = append(opt.backupOptions.StdinPipeCommand.Args, arg)
206+
for _, arg := range strings.Fields(opt.redisArgs) {
207+
backupCmd.Args = append(backupCmd.Args, arg)
204208
}
209+
205210
// if port is specified, append port in the arguments
206211
if appBinding.Spec.ClientConfig.Service.Port != 0 {
207-
opt.backupOptions.StdinPipeCommand.Args = append(opt.backupOptions.StdinPipeCommand.Args, "-port", strconv.Itoa(int(appBinding.Spec.ClientConfig.Service.Port)))
212+
backupCmd.Args = append(backupCmd.Args, "-port", strconv.Itoa(int(appBinding.Spec.ClientConfig.Service.Port)))
208213
}
209214

210215
// wait for DB ready
211-
err = waitForDBReady(appBinding)
216+
err = opt.waitForDBReady(appBinding)
212217
if err != nil {
213218
return nil, err
214219
}
215220

221+
// add backup command in the pipeline
222+
opt.backupOptions.StdinPipeCommands = append(opt.backupOptions.StdinPipeCommands, backupCmd)
223+
216224
// Run backup
217225
return resticWrapper.RunBackup(opt.backupOptions, targetRef)
218226
}

pkg/restore.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func NewCmdRestore() *cobra.Command {
109109
},
110110
}
111111

112-
cmd.Flags().StringVar(&opt.myArgs, "redis-args", opt.myArgs, "Additional arguments")
112+
cmd.Flags().StringVar(&opt.redisArgs, "redis-args", opt.redisArgs, "Additional arguments")
113113
cmd.Flags().Int32Var(&opt.waitTimeout, "wait-timeout", opt.waitTimeout, "Time limit to wait for the database to be ready")
114114

115115
cmd.Flags().StringVar(&masterURL, "master", masterURL, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
@@ -161,28 +161,37 @@ func (opt *redisOptions) restoreRedis(targetRef api_v1beta1.TargetRef) (*restic.
161161
return nil, err
162162
}
163163

164+
// set access credentials
165+
err = opt.setCredentials(resticWrapper, appBinding)
166+
if err != nil {
167+
return nil, err
168+
}
169+
164170
// setup pipe command
165-
opt.dumpOptions.StdoutPipeCommand = restic.Command{
171+
restoreCmd := restic.Command{
166172
Name: RedisRestoreCMD,
167173
Args: []interface{}{
168174
"--pipe",
169175
"-h", appBinding.Spec.ClientConfig.Service.Name,
170176
},
171177
}
172-
for _, arg := range strings.Fields(opt.myArgs) {
173-
opt.dumpOptions.StdoutPipeCommand.Args = append(opt.dumpOptions.StdoutPipeCommand.Args, arg)
178+
for _, arg := range strings.Fields(opt.redisArgs) {
179+
restoreCmd.Args = append(restoreCmd.Args, arg)
174180
}
175181
// if port is specified, append port in the arguments
176182
if appBinding.Spec.ClientConfig.Service.Port != 0 {
177-
opt.backupOptions.StdinPipeCommand.Args = append(opt.backupOptions.StdinPipeCommand.Args, "-p", strconv.Itoa(int(appBinding.Spec.ClientConfig.Service.Port)))
183+
restoreCmd.Args = append(restoreCmd.Args, "-p", strconv.Itoa(int(appBinding.Spec.ClientConfig.Service.Port)))
178184
}
179185

180186
// wait for DB ready
181-
err = waitForDBReady(appBinding)
187+
err = opt.waitForDBReady(appBinding)
182188
if err != nil {
183189
return nil, err
184190
}
185191

192+
// append the restore command to the pipeline
193+
opt.dumpOptions.StdoutPipeCommands = append(opt.dumpOptions.StdoutPipeCommands, restoreCmd)
194+
186195
// Run dump
187196
return resticWrapper.Dump(opt.dumpOptions, targetRef)
188197
}

pkg/util.go

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,29 @@ limitations under the License.
1717
package pkg
1818

1919
import (
20+
"context"
2021
"time"
2122

2223
stash "stash.appscode.dev/apimachinery/client/clientset/versioned"
2324
"stash.appscode.dev/apimachinery/pkg/restic"
2425

25-
"gomodules.xyz/go-sh"
26+
shell "gomodules.xyz/go-sh"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2628
"k8s.io/apimachinery/pkg/util/wait"
2729
"k8s.io/client-go/kubernetes"
2830
"k8s.io/klog/v2"
29-
"kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1"
31+
appcatalog "kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1"
3032
appcatalog_cs "kmodules.xyz/custom-resources/client/clientset/versioned"
3133
)
3234

3335
const (
34-
RedisUser = "username"
35-
RedisPassword = "password"
36-
RedisDumpFile = "dumpfile.resp"
37-
RedisDumpCMD = "redis-dump-go"
38-
RedisRestoreCMD = "redis-cli"
39-
EnvRedisPassword = "REDIS_PWD"
36+
RedisUser = "username"
37+
RedisPassword = "password"
38+
RedisDumpFile = "dumpfile.resp"
39+
RedisDumpCMD = "redis-dump-go"
40+
RedisRestoreCMD = "redis-cli"
41+
EnvRedisCLIAuth = "REDISCLI_AUTH"
42+
EnvRedisDumpGoAuth = "REDISDUMPGO_AUTH"
4043
)
4144

4245
type redisOptions struct {
@@ -47,7 +50,7 @@ type redisOptions struct {
4750
namespace string
4851
backupSessionName string
4952
appBindingName string
50-
myArgs string
53+
redisArgs string
5154
waitTimeout int32
5255
outputDir string
5356

@@ -56,22 +59,73 @@ type redisOptions struct {
5659
dumpOptions restic.DumpOptions
5760
}
5861

59-
func waitForDBReady(appBinding *v1alpha1.AppBinding) error {
62+
type Shell interface {
63+
SetEnv(key, value string)
64+
}
65+
66+
type SessionWrapper struct {
67+
*shell.Session
68+
}
69+
70+
func NewSessionWrapper() *SessionWrapper {
71+
return &SessionWrapper{
72+
shell.NewSession(),
73+
}
74+
}
75+
func (wrapper *SessionWrapper) SetEnv(key, value string) {
76+
wrapper.Session.SetEnv(key, value)
77+
}
78+
79+
func (opt *redisOptions) waitForDBReady(appBinding *appcatalog.AppBinding) error {
6080
klog.Infoln("Waiting for the database to be ready.....")
61-
shell := sh.NewSession()
81+
sh := NewSessionWrapper()
6282
args := []interface{}{
6383
"-h", appBinding.Spec.ClientConfig.Service.Name,
6484
"ping",
6585
}
86+
6687
//if port is specified, append port in the arguments
6788
if appBinding.Spec.ClientConfig.Service.Port != 0 {
6889
args = append(args, "-p", appBinding.Spec.ClientConfig.Service.Port)
6990
}
91+
92+
// set access credentials
93+
err := opt.setCredentials(sh, appBinding)
94+
if err != nil {
95+
return err
96+
}
97+
7098
return wait.PollImmediate(time.Second*5, time.Minute*5, func() (bool, error) {
71-
err := shell.Command("redis-cli", args...).Run()
99+
err := sh.Command("redis-cli", args...).Run()
72100
if err != nil {
73101
return false, nil
74102
}
75103
return true, nil
76104
})
77105
}
106+
107+
func (opt *redisOptions) setCredentials(sh Shell, appBinding *appcatalog.AppBinding) error {
108+
// if credential secret is not provided in AppBinding, then nothing to do.
109+
if appBinding.Spec.Secret == nil {
110+
return nil
111+
}
112+
113+
// get the Secret
114+
secret, err := opt.kubeClient.CoreV1().Secrets(opt.namespace).Get(context.TODO(), appBinding.Spec.Secret.Name, metav1.GetOptions{})
115+
if err != nil {
116+
return err
117+
}
118+
119+
// perform necessary transform if secretTransforms section is provided in the AppBinding
120+
err = appBinding.TransformSecret(opt.kubeClient, secret.Data)
121+
if err != nil {
122+
return err
123+
}
124+
125+
// set auth env for redis-cli
126+
sh.SetEnv(EnvRedisCLIAuth, string(secret.Data[RedisPassword]))
127+
128+
// set auth env for redis-dump-go
129+
sh.SetEnv(EnvRedisDumpGoAuth, string(secret.Data[RedisPassword]))
130+
return nil
131+
}

0 commit comments

Comments
 (0)