Skip to content

Commit

Permalink
Merge pull request #286 from 3scale-ops/fix/backups
Browse files Browse the repository at this point in the history
Fix sudo in shardedredisbackups
  • Loading branch information
3scale-robot authored Jan 9, 2024
2 parents e30da3a + c909d17 commit 9411528
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# To re-generate a bundle for another specific version without changing the standard setup, you can:
# - use the VERSION as arg of the bundle target (e.g make bundle VERSION=0.0.2)
# - use environment variables to overwrite this value (e.g export VERSION=0.0.2)
VERSION ?= 0.21.0-alpha.19
VERSION ?= 0.21.0-alpha.20

# CHANNELS define the bundle channels used in the bundle.
# Add a new line here if you would like to change its default config. (E.g CHANNELS = "candidate,fast,stable")
Expand Down
8 changes: 4 additions & 4 deletions bundle/manifests/saas-operator.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -598,15 +598,15 @@ metadata:
capabilities: Basic Install
categories: Integration & Delivery
containerImage: quay.io/3scale/saas-operator
createdAt: "2024-01-08T17:25:42Z"
createdAt: "2024-01-09T13:50:20Z"
description: |-
The 3scale SaaS Operator creates and maintains a SaaS-ready deployment
of the Red Hat 3scale API Management on OpenShift.
operators.operatorframework.io/builder: operator-sdk-v1.27.0
operators.operatorframework.io/project_layout: go.kubebuilder.io/v3
repository: https://github.com/3scale-ops/saas-operator
support: Red Hat
name: saas-operator.v0.21.0-alpha.19
name: saas-operator.v0.21.0-alpha.20
namespace: placeholder
spec:
apiservicedefinitions: {}
Expand Down Expand Up @@ -4484,7 +4484,7 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.annotations['olm.targetNamespaces']
image: quay.io/3scale/saas-operator:v0.21.0-alpha.19
image: quay.io/3scale/saas-operator:v0.21.0-alpha.20
livenessProbe:
httpGet:
path: /healthz
Expand Down Expand Up @@ -5048,4 +5048,4 @@ spec:
provider:
name: Red Hat
url: https://www.3scale.net/
version: 0.21.0-alpha.19
version: 0.21.0-alpha.20
2 changes: 1 addition & 1 deletion config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ kind: Kustomization
images:
- name: controller
newName: quay.io/3scale/saas-operator
newTag: v0.21.0-alpha.19
newTag: v0.21.0-alpha.20
6 changes: 3 additions & 3 deletions pkg/redis/backup/s3upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ func (br *Runner) UploadBackup(ctx context.Context) error {
Logger: logger,
CmdTimeout: 0,
Commands: []ssh.Runnable{
ssh.NewCommand(fmt.Sprintf("mv %s %s/%s", br.RedisDBFile, path.Dir(br.RedisDBFile), br.BackupFile())),
ssh.NewCommand(fmt.Sprintf("gzip -1 %s/%s", path.Dir(br.RedisDBFile), br.BackupFile())),
ssh.NewCommand(fmt.Sprintf("mv %s %s/%s", br.RedisDBFile, path.Dir(br.RedisDBFile), br.BackupFile())).WithSudo(br.SSHSudo),
ssh.NewCommand(fmt.Sprintf("gzip -1 %s/%s", path.Dir(br.RedisDBFile), br.BackupFile())).WithSudo(br.SSHSudo),
ssh.NewScript(fmt.Sprintf("%s=%s %s=%s %s=%s python -",
operatorutils.AWSRegionEnvvar, br.AWSRegion,
operatorutils.AWSAccessKeyEnvvar, br.AWSAccessKeyID,
operatorutils.AWSSecretKeyEnvvar, br.AWSSecretAccessKey),
uploadScript,
br.AWSSecretAccessKey,
),
ssh.NewCommand(fmt.Sprintf("rm -f %s/%s*", path.Dir(br.RedisDBFile), br.BackupFileBaseName())),
ssh.NewCommand(fmt.Sprintf("rm -f %s/%s*", path.Dir(br.RedisDBFile), br.BackupFileBaseName())).WithSudo(br.SSHSudo),
},
}

Expand Down
35 changes: 31 additions & 4 deletions pkg/ssh/remote_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ func (re *RemoteExecutor) Run() error {
type Runnable interface {
Run(*ssh.Client) (string, error)
Info() string
WithSudo(bool) Runnable
}

type Command struct {
value string
sensitive []string
sudo bool
}

var _ Runnable = &Command{}
Expand All @@ -75,8 +77,20 @@ func NewCommand(value string, sensitive ...string) *Command {
return &Command{value: value, sensitive: sensitive}
}

func (c *Command) WithSudo(sudo bool) Runnable {
c.sudo = sudo
return c
}

func (c *Command) resolveValue() string {
if c.sudo {
return "sudo " + c.value
}
return c.value
}

func (c *Command) Info() string {
return fmt.Sprintf("run command: %s", hideSensitive(c.value, c.sensitive...))
return fmt.Sprintf("run command: %s", hideSensitive(c.resolveValue(), c.sensitive...))
}

func (c *Command) Run(client *ssh.Client) (string, error) {
Expand All @@ -87,7 +101,7 @@ func (c *Command) Run(client *ssh.Client) (string, error) {
}
defer session.Close()

output, err := session.CombinedOutput(c.value)
output, err := session.CombinedOutput(c.resolveValue())
if err != nil {
return string(output), err
}
Expand All @@ -99,6 +113,7 @@ type Script struct {
value []byte
interpreter string
sensitive []string
sudo bool
}

var _ Runnable = &Script{}
Expand All @@ -111,13 +126,25 @@ func NewScript(interpreter string, script string, sensitive ...string) *Script {
}
}

func (s *Script) WithSudo(sudo bool) Runnable {
s.sudo = sudo
return s
}

func (s *Script) Info() string {
return fmt.Sprintf("run script with: '%s' \n'%s'",
hideSensitive(s.interpreter, s.sensitive...),
hideSensitive(s.resolveInterpreter(), s.sensitive...),
hideSensitive(string(s.value), s.sensitive...),
)
}

func (s *Script) resolveInterpreter() string {
if s.sudo {
return "sudo " + s.interpreter
}
return s.interpreter
}

func (s *Script) Run(client *ssh.Client) (string, error) {
// Create a session. It is one session per command.
session, err := client.NewSession()
Expand All @@ -140,7 +167,7 @@ func (s *Script) Run(client *ssh.Client) (string, error) {
chRsp := make(chan response)

go func() {
output, err := session.CombinedOutput(s.interpreter)
output, err := session.CombinedOutput(s.resolveInterpreter())
if err != nil {
chRsp <- response{output: output, err: err}
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package version

const (
version string = "v0.21.0-alpha.19"
version string = "v0.21.0-alpha.20"
)

// Current returns the current marin3r operator version
Expand Down

0 comments on commit 9411528

Please sign in to comment.