Skip to content

Commit 81be6c8

Browse files
committed
fix physical restore
1 parent 4d2e269 commit 81be6c8

File tree

3 files changed

+56
-57
lines changed

3 files changed

+56
-57
lines changed

build/pbm-entry.sh

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ function urlencode {
55
echo -n "$uri" | jq -s -R -r @uri
66
}
77

8-
PBM_MONGODB_URI="mongodb://$(urlencode "$PBM_AGENT_MONGODB_USERNAME"):$(urlencode "$PBM_AGENT_MONGODB_PASSWORD")@localhost:${PBM_MONGODB_PORT}/?replicaSet=${PBM_MONGODB_REPLSET}"
9-
10-
if [[ -z ${PBM_AGENT_TLS_ENABLED} ]] || [[ ${PBM_AGENT_TLS_ENABLED} == "true" ]]; then
11-
MONGO_SSL_DIR=/etc/mongodb-ssl
12-
if [[ -f "${MONGO_SSL_DIR}/tls.crt" ]] && [[ -f "${MONGO_SSL_DIR}/tls.key" ]]; then
13-
PBM_MONGODB_URI="${PBM_MONGODB_URI}&tls=true&tlsCertificateKeyFile=%2Ftmp%2Ftls.pem&tlsCAFile=${MONGO_SSL_DIR}%2Fca.crt&tlsInsecure=true"
14-
cat "${MONGO_SSL_DIR}/tls.key" "${MONGO_SSL_DIR}/tls.crt" >/tmp/tls.pem
8+
if [[ -z $POD_NAME ]]; then
9+
PBM_MONGODB_URI="mongodb://$(urlencode "$PBM_AGENT_MONGODB_USERNAME"):$(urlencode "$PBM_AGENT_MONGODB_PASSWORD")@localhost:${PBM_MONGODB_PORT}/?replicaSet=${PBM_MONGODB_REPLSET}"
10+
if [[ -z ${PBM_AGENT_TLS_ENABLED} ]] || [[ ${PBM_AGENT_TLS_ENABLED} == "true" ]]; then
11+
MONGO_SSL_DIR=/etc/mongodb-ssl
12+
if [[ -f "${MONGO_SSL_DIR}/tls.crt" ]] && [[ -f "${MONGO_SSL_DIR}/tls.key" ]]; then
13+
PBM_MONGODB_URI="${PBM_MONGODB_URI}&tls=true&tlsCertificateKeyFile=%2Ftmp%2Ftls.pem&tlsCAFile=${MONGO_SSL_DIR}%2Fca.crt&tlsInsecure=true"
14+
cat "${MONGO_SSL_DIR}/tls.key" "${MONGO_SSL_DIR}/tls.crt" >/tmp/tls.pem
15+
fi
1516
fi
17+
else
18+
PBM_MONGODB_URI="mongodb://$(urlencode "$PBM_AGENT_MONGODB_USERNAME"):$(urlencode "$PBM_AGENT_MONGODB_PASSWORD")@$POD_NAME"
1619
fi
1720

1821
export PBM_MONGODB_URI

pkg/controller/perconaservermongodbrestore/physical.go

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (r *ReconcilePerconaServerMongoDBRestore) reconcilePhysicalRestore(ctx cont
5858
if err := retry.OnError(anotherOpBackoff, func(err error) bool {
5959
return strings.Contains(err.Error(), "another operation")
6060
}, func() error {
61-
return r.disablePITR(ctx, &pod)
61+
return r.disablePITR(ctx, cluster, &pod)
6262
}); err != nil {
6363
return status, errors.Wrap(err, "disable pitr")
6464
}
@@ -69,7 +69,7 @@ func (r *ReconcilePerconaServerMongoDBRestore) reconcilePhysicalRestore(ctx cont
6969
case psmdbv1.PITRestoreTypeDate:
7070
ts = cr.Spec.PITR.Date.Format("2006-01-02T15:04:05")
7171
case psmdbv1.PITRestoreTypeLatest:
72-
ts, err = r.getLatestChunkTS(ctx, &pod)
72+
ts, err = r.getLatestChunkTS(ctx, cluster, &pod)
7373
if err != nil {
7474
return status, errors.Wrap(err, "get latest chunk timestamp")
7575
}
@@ -132,11 +132,10 @@ func (r *ReconcilePerconaServerMongoDBRestore) reconcilePhysicalRestore(ctx cont
132132
stdoutBuf.Reset()
133133
stderrBuf.Reset()
134134

135-
command := []string{"/opt/percona/pbm", "config", "--file", "/etc/pbm/pbm_config.yaml"}
135+
command := []string{"config", "--file", "/etc/pbm/pbm_config.yaml"}
136136
log.Info("Set PBM configuration", "command", command, "pod", pod.Name)
137137

138-
err := r.clientcmd.Exec(ctx, &pod, "mongod", command, nil, stdoutBuf, stderrBuf, false)
139-
if err != nil {
138+
if err := r.pbmExec(ctx, command, cluster, &pod, stdoutBuf, stderrBuf); err != nil {
140139
log.Error(err, "failed to set PBM configuration")
141140
}
142141

@@ -147,15 +146,15 @@ func (r *ReconcilePerconaServerMongoDBRestore) reconcilePhysicalRestore(ctx cont
147146
}
148147

149148
time.Sleep(5 * time.Second) // wait until pbm will start resync
150-
if err := r.waitForPBMOperationsToFinish(ctx, &pod); err != nil {
149+
if err := r.waitForPBMOperationsToFinish(ctx, cluster, &pod); err != nil {
151150
return status, err
152151
}
153152

154153
var restoreCommand []string
155154
if cr.Spec.PITR != nil {
156-
restoreCommand = []string{"/opt/percona/pbm", "restore", "--base-snapshot", bcp.Status.PBMname, "--time", cr.Status.PITRTarget, "--out", "json"}
155+
restoreCommand = []string{"restore", "--base-snapshot", bcp.Status.PBMname, "--time", cr.Status.PITRTarget, "--out", "json"}
157156
} else {
158-
restoreCommand = []string{"/opt/percona/pbm", "restore", bcp.Status.PBMname, "--out", "json"}
157+
restoreCommand = []string{"restore", bcp.Status.PBMname, "--out", "json"}
159158
}
160159

161160
err = retry.OnError(anotherOpBackoff, func(err error) bool {
@@ -166,8 +165,7 @@ func (r *ReconcilePerconaServerMongoDBRestore) reconcilePhysicalRestore(ctx cont
166165
stdoutBuf.Reset()
167166
stderrBuf.Reset()
168167

169-
err := r.clientcmd.Exec(ctx, &pod, "mongod", restoreCommand, nil, stdoutBuf, stderrBuf, false)
170-
if err != nil {
168+
if err := r.pbmExec(ctx, restoreCommand, cluster, &pod, stdoutBuf, stderrBuf); err != nil {
171169
log.Error(nil, "Restore failed to start", "pod", pod.Name, "stderr", stderrBuf.String(), "stdout", stdoutBuf.String())
172170
return errors.Wrapf(err, "start restore stderr: %s stdout: %s", stderrBuf.String(), stdoutBuf.String())
173171
}
@@ -203,7 +201,7 @@ func (r *ReconcilePerconaServerMongoDBRestore) reconcilePhysicalRestore(ctx cont
203201
stderrBuf.Reset()
204202

205203
command := []string{
206-
"/opt/percona/pbm", "describe-restore", cr.Status.PBMname,
204+
"describe-restore", cr.Status.PBMname,
207205
"--config", "/etc/pbm/pbm_config.yaml",
208206
"--out", "json",
209207
}
@@ -215,7 +213,7 @@ func (r *ReconcilePerconaServerMongoDBRestore) reconcilePhysicalRestore(ctx cont
215213

216214
log.V(1).Info("Check restore status", "command", command, "pod", pod.Name)
217215

218-
if err := r.clientcmd.Exec(ctx, &pod, "mongod", command, nil, stdoutBuf, stderrBuf, false); err != nil {
216+
if err := r.pbmExec(ctx, command, cluster, &pod, stdoutBuf, stderrBuf); err != nil {
219217
return errors.Wrapf(err, "describe restore stderr: %s stdout: %s", stderrBuf.String(), stdoutBuf.String())
220218
}
221219

@@ -428,11 +426,13 @@ func (r *ReconcilePerconaServerMongoDBRestore) updateStatefulSetForPhysicalResto
428426
},
429427
},
430428
},
431-
{
429+
}...)
430+
if cluster.CompareVersion("1.18.0") < 0 {
431+
sts.Spec.Template.Spec.Containers[0].Env = append(sts.Spec.Template.Spec.Containers[0].Env, corev1.EnvVar{
432432
Name: "PBM_MONGODB_URI",
433433
Value: "mongodb://$(PBM_AGENT_MONGODB_USERNAME):$(PBM_AGENT_MONGODB_PASSWORD)@$(POD_NAME)",
434-
},
435-
}...)
434+
})
435+
}
436436

437437
err = r.client.Update(ctx, &sts)
438438
if err != nil {
@@ -815,21 +815,11 @@ func (r *ReconcilePerconaServerMongoDBRestore) checkIfStatefulSetsAreReadyForPhy
815815
return true, nil
816816
}
817817

818-
func (r *ReconcilePerconaServerMongoDBRestore) getLatestChunkTS(ctx context.Context, pod *corev1.Pod) (string, error) {
818+
func (r *ReconcilePerconaServerMongoDBRestore) getLatestChunkTS(ctx context.Context, cluster *psmdbv1.PerconaServerMongoDB, pod *corev1.Pod) (string, error) {
819819
stdoutBuf := &bytes.Buffer{}
820820
stderrBuf := &bytes.Buffer{}
821821

822-
container := "mongod"
823-
pbmBinary := "/opt/percona/pbm"
824-
for _, c := range pod.Spec.Containers {
825-
if c.Name == "backup-agent" {
826-
container = c.Name
827-
pbmBinary = "pbm"
828-
}
829-
}
830-
831-
command := []string{pbmBinary, "status", "--out", "json"}
832-
if err := r.clientcmd.Exec(ctx, pod, container, command, nil, stdoutBuf, stderrBuf, false); err != nil {
822+
if err := r.pbmExec(ctx, []string{"status", "--out", "json"}, cluster, pod, stdoutBuf, stderrBuf); err != nil {
833823
return "", errors.Wrapf(err, "get PBM status stderr: %s stdout: %s", stderrBuf.String(), stdoutBuf.String())
834824
}
835825

@@ -856,21 +846,34 @@ func (r *ReconcilePerconaServerMongoDBRestore) getLatestChunkTS(ctx context.Cont
856846
return ts.Format("2006-01-02T15:04:05"), nil
857847
}
858848

859-
func (r *ReconcilePerconaServerMongoDBRestore) disablePITR(ctx context.Context, pod *corev1.Pod) error {
860-
stdoutBuf := &bytes.Buffer{}
861-
stderrBuf := &bytes.Buffer{}
862-
849+
func (r *ReconcilePerconaServerMongoDBRestore) pbmExec(ctx context.Context, command []string, cluster *psmdbv1.PerconaServerMongoDB, pod *corev1.Pod, stdoutBuf, stderrBuf *bytes.Buffer) error {
863850
container := "mongod"
864851
pbmBinary := "/opt/percona/pbm"
865852
for _, c := range pod.Spec.Containers {
866853
if c.Name == "backup-agent" {
867854
container = c.Name
868-
pbmBinary = "pbm"
855+
pbmBinary = "/bin/pbm"
869856
}
870857
}
871858

872-
command := []string{pbmBinary, "config", "--set", "pitr.enabled=false"}
859+
command = append([]string{pbmBinary}, command...)
860+
861+
if cluster.CompareVersion("1.18.0") >= 0 {
862+
command = append([]string{"/opt/percona/pbm-entry.sh"}, command...)
863+
command = []string{"bash", "-c", strings.Join(command, " ")}
864+
}
865+
873866
if err := r.clientcmd.Exec(ctx, pod, container, command, nil, stdoutBuf, stderrBuf, false); err != nil {
867+
return err
868+
}
869+
return nil
870+
}
871+
872+
func (r *ReconcilePerconaServerMongoDBRestore) disablePITR(ctx context.Context, cluster *psmdbv1.PerconaServerMongoDB, pod *corev1.Pod) error {
873+
stdoutBuf := &bytes.Buffer{}
874+
stderrBuf := &bytes.Buffer{}
875+
876+
if err := r.pbmExec(ctx, []string{"config", "--set", "pitr.enabled=false"}, cluster, pod, stdoutBuf, stderrBuf); err != nil {
874877
return errors.Wrapf(err, "disable PiTR stderr: %s stdout: %s", stderrBuf.String(), stdoutBuf.String())
875878
}
876879

@@ -884,21 +887,12 @@ func (r *ReconcilePerconaServerMongoDBRestore) pbmConfigName(cluster *psmdbv1.Pe
884887
return cluster.Name + "-pbm-config"
885888
}
886889

887-
func (r *ReconcilePerconaServerMongoDBRestore) waitForPBMOperationsToFinish(ctx context.Context, pod *corev1.Pod) error {
890+
func (r *ReconcilePerconaServerMongoDBRestore) waitForPBMOperationsToFinish(ctx context.Context, cluster *psmdbv1.PerconaServerMongoDB, pod *corev1.Pod) error {
888891
log := logf.FromContext(ctx)
889892

890893
stdoutBuf := &bytes.Buffer{}
891894
stderrBuf := &bytes.Buffer{}
892895

893-
container := "mongod"
894-
pbmBinary := "/opt/percona/pbm"
895-
for _, c := range pod.Spec.Containers {
896-
if c.Name == "backup-agent" {
897-
container = c.Name
898-
pbmBinary = "pbm"
899-
}
900-
}
901-
902896
waitErr := errors.New("waiting for PBM operation to finish")
903897
err := retry.OnError(wait.Backoff{
904898
Duration: 5 * time.Second,
@@ -910,9 +904,7 @@ func (r *ReconcilePerconaServerMongoDBRestore) waitForPBMOperationsToFinish(ctx
910904
stdoutBuf.Reset()
911905
stderrBuf.Reset()
912906

913-
command := []string{pbmBinary, "status", "--out", "json"}
914-
err := r.clientcmd.Exec(ctx, pod, container, command, nil, stdoutBuf, stderrBuf, false)
915-
if err != nil {
907+
if err := r.pbmExec(ctx, []string{"status", "--out", "json"}, cluster, pod, stdoutBuf, stderrBuf); err != nil {
916908
log.Error(err, "failed to get PBM status")
917909
return err
918910
}

pkg/psmdb/statefulset.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,15 @@ func backupAgentContainer(cr *api.PerconaServerMongoDB, replsetName string, tlsE
415415
},
416416
},
417417
},
418-
{
419-
Name: "PBM_MONGODB_URI",
420-
Value: "mongodb://$(PBM_AGENT_MONGODB_USERNAME):$(PBM_AGENT_MONGODB_PASSWORD)@$(POD_NAME)",
421-
},
422418
}...)
419+
if cr.CompareVersion("1.18.0") < 0 {
420+
c.Env = append(c.Env, []corev1.EnvVar{
421+
{
422+
Name: "PBM_MONGODB_URI",
423+
Value: "mongodb://$(PBM_AGENT_MONGODB_USERNAME):$(PBM_AGENT_MONGODB_PASSWORD)@$(POD_NAME)",
424+
},
425+
}...)
426+
}
423427

424428
c.VolumeMounts = append(c.VolumeMounts, []corev1.VolumeMount{
425429
{

0 commit comments

Comments
 (0)