Skip to content

Commit 43b98f4

Browse files
Create stanza after repohost is added (#3965)
Given a cloud host is already in place, the user should be able to add a repohost.
1 parent 17bd5bf commit 43b98f4

File tree

4 files changed

+86
-11
lines changed

4 files changed

+86
-11
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.DS_Store
22
/vendor/
33
/testing/kuttl/e2e-generated*/
4+
gke_gcloud_auth_plugin_cache

internal/controller/postgrescluster/pgbackrest.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2595,7 +2595,7 @@ func (r *Reconciler) reconcileStanzaCreate(ctx context.Context,
25952595

25962596
// Always attempt to create pgBackRest stanza first
25972597
configHashMismatch, err := pgbackrest.Executor(exec).StanzaCreateOrUpgrade(ctx, configHash,
2598-
false)
2598+
false, postgresCluster)
25992599
if err != nil {
26002600
// record and log any errors resulting from running the stanza-create command
26012601
r.Recorder.Event(postgresCluster, corev1.EventTypeWarning, EventUnableToCreateStanzas,

internal/pgbackrest/pgbackrest.go

+42-6
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ import (
2323
"strings"
2424

2525
"github.com/pkg/errors"
26+
27+
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
2628
)
2729

2830
const (
2931
// errMsgConfigHashMismatch is the error message displayed when a configuration hash mismatch
3032
// is detected while attempting stanza creation
3133
errMsgConfigHashMismatch = "postgres operator error: pgBackRest config hash mismatch"
3234

35+
// errMsgStaleReposWithVolumesConfig is the error message displayed when a volume-backed repo has been
36+
// configured, but the configuration has not yet propagated into the container.
37+
errMsgStaleReposWithVolumesConfig = "postgres operator error: pgBackRest stale volume-backed repo configuration"
38+
3339
// errMsgBackupDbMismatch is the error message returned from pgBackRest when PG versions
3440
// or PG system identifiers do not match between the PG instance and the existing stanza
3541
errMsgBackupDbMismatch = "backup and archive info files exist but do not match the database"
@@ -51,7 +57,7 @@ type Executor func(
5157
// from running (with a config mismatch indicating that the pgBackRest configuration as stored in
5258
// the cluster's pgBackRest ConfigMap has not yet propagated to the Pod).
5359
func (exec Executor) StanzaCreateOrUpgrade(ctx context.Context, configHash string,
54-
upgrade bool) (bool, error) {
60+
upgrade bool, postgresCluster *v1beta1.PostgresCluster) (bool, error) {
5561

5662
var stdout, stderr bytes.Buffer
5763

@@ -60,22 +66,46 @@ func (exec Executor) StanzaCreateOrUpgrade(ctx context.Context, configHash strin
6066
stanzaCmd = "upgrade"
6167
}
6268

69+
var reposWithVolumes []v1beta1.PGBackRestRepo
70+
for _, repo := range postgresCluster.Spec.Backups.PGBackRest.Repos {
71+
if repo.Volume != nil {
72+
reposWithVolumes = append(reposWithVolumes, repo)
73+
}
74+
}
75+
76+
grep := "grep %s-path /etc/pgbackrest/conf.d/pgbackrest_instance.conf"
77+
78+
var checkRepoCmd string
79+
if len(reposWithVolumes) > 0 {
80+
repo := reposWithVolumes[0]
81+
checkRepoCmd = checkRepoCmd + fmt.Sprintf(grep, repo.Name)
82+
83+
reposWithVolumes = reposWithVolumes[1:]
84+
for _, repo := range reposWithVolumes {
85+
checkRepoCmd = checkRepoCmd + fmt.Sprintf(" && "+grep, repo.Name)
86+
}
87+
}
88+
6389
// this is the script that is run to create a stanza. First it checks the
6490
// "config-hash" file to ensure all configuration changes (e.g. from ConfigMaps) have
6591
// propagated to the container, and if not, it prints an error and returns with exit code 1).
92+
// Next, it checks that any volume-backed repo added to the config has propagated into
93+
// the container, and if not, prints an error and exits with code 1.
6694
// Otherwise, it runs the pgbackrest command, which will either be "stanza-create" or
6795
// "stanza-upgrade", depending on the value of the boolean "upgrade" parameter.
6896
const script = `
69-
declare -r hash="$1" stanza="$2" message="$3" cmd="$4"
97+
declare -r hash="$1" stanza="$2" hash_msg="$3" vol_msg="$4" cmd="$5" check_repo_cmd="$6"
7098
if [[ "$(< /etc/pgbackrest/conf.d/config-hash)" != "${hash}" ]]; then
71-
printf >&2 "%s" "${message}"; exit 1;
99+
printf >&2 "%s" "${hash_msg}"; exit 1;
100+
elif ! bash -c "${check_repo_cmd}"; then
101+
printf >&2 "%s" "${vol_msg}"; exit 1;
72102
else
73103
pgbackrest "${cmd}" --stanza="${stanza}"
74104
fi
75105
`
76106
if err := exec(ctx, nil, &stdout, &stderr, "bash", "-ceu", "--",
77-
script, "-", configHash, DefaultStanzaName, errMsgConfigHashMismatch,
78-
fmt.Sprintf("stanza-%s", stanzaCmd)); err != nil {
107+
script, "-", configHash, DefaultStanzaName, errMsgConfigHashMismatch, errMsgStaleReposWithVolumesConfig,
108+
fmt.Sprintf("stanza-%s", stanzaCmd), checkRepoCmd); err != nil {
79109

80110
errReturn := stderr.String()
81111

@@ -86,10 +116,16 @@ fi
86116
return true, nil
87117
}
88118

119+
// if the configuration for volume-backed repositories is stale, return true and don't return an error since this
120+
// is expected while waiting for config changes in ConfigMaps to make it to the container
121+
if errReturn == errMsgStaleReposWithVolumesConfig {
122+
return true, nil
123+
}
124+
89125
// if the err returned from pgbackrest command is about a version mismatch
90126
// then we should run upgrade rather than create
91127
if strings.Contains(errReturn, errMsgBackupDbMismatch) {
92-
return exec.StanzaCreateOrUpgrade(ctx, configHash, true)
128+
return exec.StanzaCreateOrUpgrade(ctx, configHash, true, postgresCluster)
93129
}
94130

95131
// if none of the above errors, return the err

internal/pgbackrest/pgbackrest_test.go

+42-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ import (
2424
"testing"
2525

2626
"gotest.tools/v3/assert"
27+
"k8s.io/apimachinery/pkg/api/resource"
28+
29+
corev1 "k8s.io/api/core/v1"
2730

2831
"github.com/crunchydata/postgres-operator/internal/testing/require"
32+
33+
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
2934
)
3035

3136
func TestStanzaCreateOrUpgrade(t *testing.T) {
@@ -34,15 +39,20 @@ func TestStanzaCreateOrUpgrade(t *testing.T) {
3439
ctx := context.Background()
3540
configHash := "7f5d4d5bdc"
3641
expectedCommand := []string{"bash", "-ceu", "--", `
37-
declare -r hash="$1" stanza="$2" message="$3" cmd="$4"
42+
declare -r hash="$1" stanza="$2" hash_msg="$3" vol_msg="$4" cmd="$5" check_repo_cmd="$6"
3843
if [[ "$(< /etc/pgbackrest/conf.d/config-hash)" != "${hash}" ]]; then
39-
printf >&2 "%s" "${message}"; exit 1;
44+
printf >&2 "%s" "${hash_msg}"; exit 1;
45+
elif ! bash -c "${check_repo_cmd}"; then
46+
printf >&2 "%s" "${vol_msg}"; exit 1;
4047
else
4148
pgbackrest "${cmd}" --stanza="${stanza}"
4249
fi
4350
`,
4451
"-", "7f5d4d5bdc", "db", "postgres operator error: pgBackRest config hash mismatch",
45-
"stanza-create"}
52+
"postgres operator error: pgBackRest stale volume-backed repo configuration",
53+
"stanza-create",
54+
"grep repo1-path /etc/pgbackrest/conf.d/pgbackrest_instance.conf",
55+
}
4656

4757
var shellCheckScript string
4858
stanzaExec := func(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer,
@@ -56,8 +66,36 @@ fi
5666

5767
return nil
5868
}
69+
postgresCluster := &v1beta1.PostgresCluster{
70+
Spec: v1beta1.PostgresClusterSpec{
71+
Backups: v1beta1.Backups{
72+
PGBackRest: v1beta1.PGBackRestArchive{
73+
Repos: []v1beta1.PGBackRestRepo{{
74+
Name: "repo1",
75+
Volume: &v1beta1.RepoPVC{
76+
VolumeClaimSpec: corev1.PersistentVolumeClaimSpec{
77+
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteMany},
78+
Resources: corev1.VolumeResourceRequirements{
79+
Requests: map[corev1.ResourceName]resource.Quantity{
80+
corev1.ResourceStorage: resource.MustParse("1Gi"),
81+
},
82+
},
83+
},
84+
},
85+
}, {
86+
Name: "repo2",
87+
S3: &v1beta1.RepoS3{
88+
Bucket: "bucket",
89+
Endpoint: "endpoint",
90+
Region: "region",
91+
},
92+
}},
93+
},
94+
},
95+
},
96+
}
5997

60-
configHashMismatch, err := Executor(stanzaExec).StanzaCreateOrUpgrade(ctx, configHash, false)
98+
configHashMismatch, err := Executor(stanzaExec).StanzaCreateOrUpgrade(ctx, configHash, false, postgresCluster)
6199
assert.NilError(t, err)
62100
assert.Assert(t, !configHashMismatch)
63101

0 commit comments

Comments
 (0)