Skip to content

K8SPS-400: custom options for xtrabackup, xbstream, xbcloud #986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
52 changes: 52 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 @@ -279,6 +280,57 @@ 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 {
Env []corev1.EnvVar `json:"env,omitempty"`
Args BackupContainerArgs `json:"args"`
}

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

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

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

return storage.ContainerOptions.GetEnvVar(nil, nil)
}

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
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
63 changes: 63 additions & 0 deletions 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
11 changes: 7 additions & 4 deletions build/run-restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -o xtrace

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
Expand All @@ -25,7 +25,8 @@ run_azure() {
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 @@ main() {
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
23 changes: 18 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 @@ -381,6 +384,16 @@ func createBackupHandler(w http.ResponseWriter, req *http.Request) {
}
}

if backupConf.ContainerOptions != nil {
for _, env := range backupConf.ContainerOptions.Env {
if err := os.Setenv(env.Name, env.Value); err != nil {
log.Error(err, "failed to set env")
http.Error(w, "failed to set env", http.StatusInternalServerError)
return
}
}
}

w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Connection", "keep-alive")

Expand All @@ -393,7 +406,7 @@ 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)...)

xbOut, err := xtrabackup.StdoutPipe()
if err != nil {
Expand Down
86 changes: 86 additions & 0 deletions config/crd/bases/ps.percona.com_perconaservermysqlbackups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,92 @@ spec:
- container
- credentialsSecret
type: object
containerOptions:
properties:
args:
properties:
xbcloud:
items:
type: string
type: array
xbstream:
items:
type: string
type: array
xtrabackup:
items:
type: string
type: array
type: object
env:
items:
properties:
name:
type: string
value:
type: string
valueFrom:
properties:
configMapKeyRef:
properties:
key:
type: string
name:
default: ""
type: string
optional:
type: boolean
required:
- key
type: object
x-kubernetes-map-type: atomic
fieldRef:
properties:
apiVersion:
type: string
fieldPath:
type: string
required:
- fieldPath
type: object
x-kubernetes-map-type: atomic
resourceFieldRef:
properties:
containerName:
type: string
divisor:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
resource:
type: string
required:
- resource
type: object
x-kubernetes-map-type: atomic
secretKeyRef:
properties:
key:
type: string
name:
default: ""
type: string
optional:
type: boolean
required:
- key
type: object
x-kubernetes-map-type: atomic
type: object
required:
- name
type: object
type: array
required:
- args
type: object
containerSecurityContext:
properties:
allowPrivilegeEscalation:
Expand Down
Loading
Loading