Skip to content

Commit e0d47d8

Browse files
K8SPS-69 | async cluster readiness check should account for running backup (#1292)
* check backup status in async readiness check Signed-off-by: Mayank Shah <mayank.shah@percona.com> * check that backups are enabled first Signed-off-by: Mayank Shah <mayank.shah@percona.com> * fix nil pointer Signed-off-by: Mayank Shah <mayank.shah@percona.com> * fix e2e tests Signed-off-by: Mayank Shah <mayank.shah@percona.com> * use env constant Signed-off-by: Mayank Shah <mayank.shah@percona.com> * update unit tests Signed-off-by: Mayank Shah <mayank.shah@percona.com> * check backup running only when replication is stopped Signed-off-by: Mayank Shah <mayank.shah@percona.com> --------- Signed-off-by: Mayank Shah <mayank.shah@percona.com>
1 parent 125e51b commit e0d47d8

7 files changed

Lines changed: 62 additions & 1 deletion

File tree

cmd/healthcheck/main.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
mysqldb "github.com/percona/percona-server-mysql-operator/pkg/db"
1919
"github.com/percona/percona-server-mysql-operator/pkg/k8s"
2020
"github.com/percona/percona-server-mysql-operator/pkg/naming"
21+
"github.com/percona/percona-server-mysql-operator/pkg/xtrabackup"
2122
)
2223

2324
const (
@@ -130,7 +131,12 @@ func checkReadinessAsync(ctx context.Context) error {
130131
case replStatus == mysqldb.ReplicationStatusActive && !readOnly:
131132
return errors.New("replica is not read only")
132133
case replStatus == mysqldb.ReplicationStatusStopped:
133-
return errors.New("replication is stopped")
134+
// If replication is stopped, check if it is because of a running backup
135+
if running, err := isBackupRunning(ctx); err != nil {
136+
return errors.Wrap(err, "check backup running")
137+
} else if !running {
138+
return errors.New("replication is stopped")
139+
}
134140
}
135141
return nil
136142
}
@@ -327,3 +333,16 @@ func fileExists(name string) (bool, error) {
327333
}
328334
return true, nil
329335
}
336+
337+
func isBackupRunning(ctx context.Context) (bool, error) {
338+
backupsEnabled := os.Getenv(naming.EnvBackupsEnabled)
339+
if backupsEnabled != "true" {
340+
return false, nil
341+
}
342+
sc := xtrabackup.NewSidecarClient("localhost")
343+
bcp, err := sc.GetRunningBackupConfig(ctx)
344+
if err != nil {
345+
return false, errors.Wrap(err, "get running backup config")
346+
}
347+
return bcp != nil, nil
348+
}

e2e-tests/tests/limits/01-assert.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ spec:
7474
value: /var/lib/mysql/mysql.state
7575
- name: KEYRING_VAULT_PATH
7676
value: /etc/mysql/vault-keyring-secret/keyring_vault.cnf
77+
- name: BACKUPS_ENABLED
78+
value: "true"
7779
imagePullPolicy: Always
7880
livenessProbe:
7981
exec:

e2e-tests/tests/limits/03-assert.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ spec:
7474
value: /var/lib/mysql/mysql.state
7575
- name: KEYRING_VAULT_PATH
7676
value: /etc/mysql/vault-keyring-secret/keyring_vault.cnf
77+
- name: BACKUPS_ENABLED
78+
value: "true"
7779
imagePullPolicy: Always
7880
livenessProbe:
7981
exec:

e2e-tests/tests/limits/05-assert.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ spec:
7474
value: /var/lib/mysql/mysql.state
7575
- name: KEYRING_VAULT_PATH
7676
value: /etc/mysql/vault-keyring-secret/keyring_vault.cnf
77+
- name: BACKUPS_ENABLED
78+
value: "true"
7779
imagePullPolicy: Always
7880
livenessProbe:
7981
exec:

pkg/mysql/mysql.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,17 @@ func mysqldContainer(cr *apiv1.PerconaServerMySQL) corev1.Container {
679679
})
680680
}
681681

682+
if cr.CompareVersion("1.1.0") >= 0 {
683+
backupsEnabled := false
684+
if cr.Spec.Backup != nil {
685+
backupsEnabled = cr.Spec.Backup.Enabled
686+
}
687+
env = append(env, corev1.EnvVar{
688+
Name: naming.EnvBackupsEnabled,
689+
Value: strconv.FormatBool(backupsEnabled),
690+
})
691+
}
692+
682693
container := corev1.Container{
683694
Name: AppName,
684695
Image: spec.Image,

pkg/mysql/mysql_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,29 @@ func TestStatefulSet(t *testing.T) {
160160
assert.Equal(t, "mysql-annotation", sts.Spec.Template.Annotations["mysql-annotation"])
161161

162162
})
163+
164+
t.Run("env variables", func(t *testing.T) {
165+
cluster := cr.DeepCopy()
166+
cluster.Spec.Backup = &apiv1.BackupSpec{
167+
Enabled: true,
168+
}
169+
sts := StatefulSet(cluster, initImage, configHash, tlsHash, secret)
170+
171+
envs := sts.Spec.Template.Spec.Containers[0].Env
172+
assert.Contains(t, envs, corev1.EnvVar{
173+
Name: naming.EnvBackupsEnabled,
174+
Value: "true",
175+
})
176+
177+
// backups disabled
178+
cluster.Spec.Backup.Enabled = false
179+
sts = StatefulSet(cluster, initImage, configHash, tlsHash, secret)
180+
envs = sts.Spec.Template.Spec.Containers[0].Env
181+
assert.Contains(t, envs, corev1.EnvVar{
182+
Name: naming.EnvBackupsEnabled,
183+
Value: "false",
184+
})
185+
})
163186
}
164187

165188
func TestStatefulsetVolumes(t *testing.T) {

pkg/naming/env.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ const (
1515
EnvAsyncSourceRetryCount = "ASYNC_SOURCE_RETRY_COUNT"
1616

1717
EnvAsyncSourceConnectRetry = "ASYNC_SOURCE_CONNECT_RETRY"
18+
19+
EnvBackupsEnabled = "BACKUPS_ENABLED"
1820
)

0 commit comments

Comments
 (0)