Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions api/v1alpha1/perconaservermysql_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (

"github.com/percona/percona-server-mysql-operator/pkg/naming"
"github.com/percona/percona-server-mysql-operator/pkg/platform"
"github.com/percona/percona-server-mysql-operator/pkg/util"
"github.com/percona/percona-server-mysql-operator/pkg/version"
)

Expand Down Expand Up @@ -326,6 +327,62 @@ type BackupStorageSpec struct {
ContainerSecurityContext *corev1.SecurityContext `json:"containerSecurityContext,omitempty"`
RuntimeClassName *string `json:"runtimeClassName,omitempty"`
VerifyTLS *bool `json:"verifyTLS,omitempty"`
ContainerOptions *BackupContainerOptions `json:"containerOptions,omitempty"`
}

type BackupContainerOptions struct {
// +optional
Env []corev1.EnvVar `json:"env,omitempty"`
// +optional
Args BackupContainerArgs `json:"args"`
}

func (b *BackupContainerOptions) GetEnv() []corev1.EnvVar {
if b == nil {
return nil
}
return util.MergeEnvLists(b.Env, b.Args.Env())
}

func (b *BackupContainerOptions) GetEnvVar(storage *BackupStorageSpec) []corev1.EnvVar {
if b != nil {
return b.GetEnv()
}

if storage == nil || storage.ContainerOptions == nil {
return nil
}

return storage.ContainerOptions.GetEnv()
}

type BackupContainerArgs struct {
Xtrabackup []string `json:"xtrabackup,omitempty"`
Xbcloud []string `json:"xbcloud,omitempty"`
Xbstream []string `json:"xbstream,omitempty"`
}

func (b *BackupContainerArgs) Env() []corev1.EnvVar {
envs := []corev1.EnvVar{}
if len(b.Xtrabackup) > 0 {
envs = append(envs, corev1.EnvVar{
Name: "XB_EXTRA_ARGS",
Value: strings.Join(b.Xtrabackup, " "),
})
}
if len(b.Xbcloud) > 0 {
envs = append(envs, corev1.EnvVar{
Name: "XBCLOUD_EXTRA_ARGS",
Value: strings.Join(b.Xbcloud, " "),
})
}
if len(b.Xbstream) > 0 {
envs = append(envs, corev1.EnvVar{
Name: "XBSTREAM_EXTRA_ARGS",
Value: strings.Join(b.Xbstream, " "),
})
}
return envs
}

type BackupStorageS3Spec struct {
Expand Down
5 changes: 3 additions & 2 deletions api/v1alpha1/perconaservermysqlbackup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import (

// PerconaServerMySQLBackupSpec defines the desired state of PerconaServerMySQLBackup
type PerconaServerMySQLBackupSpec struct {
ClusterName string `json:"clusterName"`
StorageName string `json:"storageName"`
ClusterName string `json:"clusterName"`
StorageName string `json:"storageName"`
ContainerOptions *BackupContainerOptions `json:"containerOptions,omitempty"`
}

type BackupState string
Expand Down
7 changes: 4 additions & 3 deletions api/v1alpha1/perconaservermysqlrestore_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import (

// PerconaServerMySQLRestoreSpec defines the desired state of PerconaServerMySQLRestore
type PerconaServerMySQLRestoreSpec struct {
ClusterName string `json:"clusterName"`
BackupName string `json:"backupName,omitempty"`
BackupSource *PerconaServerMySQLBackupStatus `json:"backupSource,omitempty"`
ClusterName string `json:"clusterName"`
BackupName string `json:"backupName,omitempty"`
BackupSource *PerconaServerMySQLBackupStatus `json:"backupSource,omitempty"`
ContainerOptions *BackupContainerOptions `json:"containerOptions,omitempty"`
}

type RestoreState string
Expand Down
70 changes: 69 additions & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

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

3 changes: 3 additions & 0 deletions build/run-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
{
"destination": "$(json_escape "${BACKUP_DEST}")",
"type": "$(json_escape "${STORAGE_TYPE}")",
"containerOptions": ${CONTAINER_OPTIONS},
"verifyTLS": $(json_escape "${VERIFY_TLS}"),
"s3": {
"bucket": "$(json_escape "${S3_BUCKET}")",
Expand All @@ -29,6 +30,7 @@
"destination": "$(json_escape "${BACKUP_DEST}")",
"verifyTLS": $(json_escape "${VERIFY_TLS}"),
"type": "$(json_escape "${STORAGE_TYPE}")",
"containerOptions": ${CONTAINER_OPTIONS},
"gcs": {
"bucket": "$(json_escape "${GCS_BUCKET}")",
"endpointUrl": "$(json_escape "${GCS_ENDPOINT}")",
Expand All @@ -45,6 +47,7 @@
"destination": "$(json_escape "${BACKUP_DEST}")",
"verifyTLS": $(json_escape "${VERIFY_TLS}"),
"type": "$(json_escape "${STORAGE_TYPE}")",
"containerOptions": ${CONTAINER_OPTIONS},
"azure": {
"containerName": "$(json_escape "${AZURE_CONTAINER_NAME}")",
"storageAccount": "$(json_escape "${AZURE_STORAGE_ACCOUNT}")",
Expand All @@ -60,7 +63,7 @@

# json_escape takes a string and replaces `\` to `\\` and `"` to `\"` to make it safe to insert provided argument into a json string
json_escape() {
escaped_backslash=${1//'\'/'\\'}

Check notice on line 66 in build/run-backup.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/run-backup.sh#L66 <ShellCheck.SC1003>

Want to escape a single quote? echo 'This is how it'\''s done'.
Raw output
./build/run-backup.sh:66:26: info: Want to escape a single quote? echo 'This is how it'\''s done'. (ShellCheck.SC1003)

Check notice on line 66 in build/run-backup.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/run-backup.sh#L66 <ShellCheck.SC1003>

Want to escape a single quote? echo 'This is how it'\''s done'.
Raw output
./build/run-backup.sh:66:31: info: Want to escape a single quote? echo 'This is how it'\''s done'. (ShellCheck.SC1003)
escaped_quotes=${escaped_backslash//'"'/'\"'}
echo -n "$escaped_quotes"
}
Expand Down
17 changes: 10 additions & 7 deletions build/run-restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@

DATADIR=${DATADIR:-/var/lib/mysql}
PARALLEL=$(grep -c processor /proc/cpuinfo)
XBCLOUD_ARGS="--curl-retriable-errors=7 --parallel=${PARALLEL}"
XBCLOUD_ARGS="--curl-retriable-errors=7 --parallel=${PARALLEL} ${XBCLOUD_EXTRA_ARGS}"
if [ -n "$VERIFY_TLS" ] && [[ $VERIFY_TLS == "false" ]]; then
XBCLOUD_ARGS="${XBCLOUD_ARGS} --insecure"
fi

run_s3() {
xbcloud get "${XBCLOUD_ARGS}" "${BACKUP_DEST}" --storage=s3 --s3-bucket="${S3_BUCKET}"
xbcloud get ${XBCLOUD_ARGS} "${BACKUP_DEST}" --storage=s3 --s3-bucket="${S3_BUCKET}"

Check notice on line 14 in build/run-restore.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/run-restore.sh#L14 <ShellCheck.SC2086>

Double quote to prevent globbing and word splitting.
Raw output
./build/run-restore.sh:14:14: info: Double quote to prevent globbing and word splitting. (ShellCheck.SC2086)
}

run_gcs() {
xbcloud get "${XBCLOUD_ARGS}" "${BACKUP_DEST}" --storage=google --google-bucket="${GCS_BUCKET}"
xbcloud get ${XBCLOUD_ARGS} "${BACKUP_DEST}" --storage=google --google-bucket="${GCS_BUCKET}"

Check notice on line 18 in build/run-restore.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/run-restore.sh#L18 <ShellCheck.SC2086>

Double quote to prevent globbing and word splitting.
Raw output
./build/run-restore.sh:18:14: info: Double quote to prevent globbing and word splitting. (ShellCheck.SC2086)
}

run_azure() {
xbcloud get "${XBCLOUD_ARGS}" "${BACKUP_DEST}" --storage=azure
xbcloud get ${XBCLOUD_ARGS} "${BACKUP_DEST}" --storage=azure

Check notice on line 22 in build/run-restore.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/run-restore.sh#L22 <ShellCheck.SC2086>

Double quote to prevent globbing and word splitting.
Raw output
./build/run-restore.sh:22:14: info: Double quote to prevent globbing and word splitting. (ShellCheck.SC2086)
}

extract() {
local targetdir=$1

xbstream -xv -C "${targetdir}" --parallel="${PARALLEL}"
# shellcheck disable=SC2086
xbstream -xv -C "${targetdir}" --parallel="${PARALLEL}" ${XBSTREAM_EXTRA_ARGS}
}

main() {
Expand All @@ -47,8 +48,10 @@
keyring="--keyring-vault-config=${KEYRING_VAULT_PATH}"
fi

xtrabackup --prepare --rollback-prepared-trx --target-dir="${tmpdir}" ${keyring}
xtrabackup --datadir="${DATADIR}" --move-back --force-non-empty-directories --target-dir="${tmpdir}"
# shellcheck disable=SC2086
xtrabackup --prepare --rollback-prepared-trx --target-dir="${tmpdir}" ${XB_EXTRA_ARGS} ${keyring}
# shellcheck disable=SC2086
xtrabackup --datadir="${DATADIR}" --move-back --force-non-empty-directories --target-dir="${tmpdir}" ${XB_EXTRA_ARGS}

rm -rf "${tmpdir}"

Expand Down
26 changes: 21 additions & 5 deletions cmd/sidecar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ import (
"syscall"
"time"

"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

"github.com/pkg/errors"

apiv1alpha1 "github.com/percona/percona-server-mysql-operator/api/v1alpha1"
"github.com/percona/percona-server-mysql-operator/pkg/mysql"
xb "github.com/percona/percona-server-mysql-operator/pkg/xtrabackup"
Expand Down Expand Up @@ -148,8 +147,8 @@ func getNamespace() (string, error) {
return string(ns), nil
}

func xtrabackupArgs(user, pass string) []string {
return []string{
func xtrabackupArgs(user, pass string, conf *xb.BackupConfig) []string {
args := []string{
"--backup",
"--stream=xbstream",
"--safe-slave-backup",
Expand All @@ -158,6 +157,10 @@ func xtrabackupArgs(user, pass string) []string {
fmt.Sprintf("--user=%s", user),
fmt.Sprintf("--password=%s", pass),
}
if conf != nil && conf.ContainerOptions != nil {
args = append(args, conf.ContainerOptions.Args.Xtrabackup...)
}
return args
}

func backupHandler(w http.ResponseWriter, req *http.Request) {
Expand Down Expand Up @@ -247,6 +250,7 @@ func deleteBackup(ctx context.Context, cfg *xb.BackupConfig, backupName string)
logWriter = io.MultiWriter(backupLog, os.Stderr)
}
xbcloud := exec.CommandContext(ctx, "xbcloud", xb.XBCloudArgs(xb.XBCloudActionDelete, cfg)...)
xbcloud.Env = envs(*cfg)
xbcloudErr, err := xbcloud.StderrPipe()
if err != nil {
return errors.Wrap(err, "xbcloud stderr pipe failed")
Expand Down Expand Up @@ -393,7 +397,8 @@ func createBackupHandler(w http.ResponseWriter, req *http.Request) {
}
g, gCtx := errgroup.WithContext(req.Context())

xtrabackup := exec.CommandContext(gCtx, "xtrabackup", xtrabackupArgs(string(backupUser), backupPass)...)
xtrabackup := exec.CommandContext(gCtx, "xtrabackup", xtrabackupArgs(string(backupUser), backupPass, &backupConf)...)
xtrabackup.Env = envs(backupConf)

xbOut, err := xtrabackup.StdoutPipe()
if err != nil {
Expand Down Expand Up @@ -421,6 +426,7 @@ func createBackupHandler(w http.ResponseWriter, req *http.Request) {
logWriter := io.MultiWriter(backupLog, os.Stderr)

xbcloud := exec.CommandContext(gCtx, "xbcloud", xb.XBCloudArgs(xb.XBCloudActionPut, &backupConf)...)
xbcloud.Env = envs(backupConf)
xbcloud.Stdin = xbOut

xbcloudErr, err := xbcloud.StderrPipe()
Expand Down Expand Up @@ -514,3 +520,13 @@ func logHandler(w http.ResponseWriter, req *http.Request) {
return
}
}

func envs(cfg xb.BackupConfig) []string {
envs := os.Environ()
if cfg.ContainerOptions != nil {
for _, env := range cfg.ContainerOptions.Env {
envs = append(envs, fmt.Sprintf("%s=%s", env.Name, env.Value))
}
}
return envs
}
Loading
Loading