Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
55 changes: 55 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,60 @@ 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 {
if b == nil {
return nil
}
return util.MergeEnvLists(b.Env, b.Args.Env())
}

func (b *BackupContainerOptions) GetEnvVar(cluster *PerconaServerMySQL, storage *BackupStorageSpec) []corev1.EnvVar {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cluster option here is not used in the function, maybe we can remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if b != nil {
return b.GetEnv()
}

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

return storage.ContainerOptions.GetEnvVar(nil, nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to understand the logic here. Essentially, the cluster argument is not needed. For nil storage, we call the same function, and we return essentially nil. Why not just

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

Essentially I'm missing why storage is passed as an argument. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

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
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
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it be better to set these env vars for specific commands? like:

xtrabackup := exec.CommandContext(gCtx, "xtrabackup", xtrabackupArgs(string(backupUser), backupPass, &backupConf)...)
env := os.Environ()
for _, e := range backupConf.ContainerOptions.Env {
    env = append(env, fmt.Sprintf("%s=%s", e.Name, e.Value)
}
xtrabackup.Env = env

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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
Loading
Loading