Skip to content

Commit

Permalink
Create stanza after repohost is added
Browse files Browse the repository at this point in the history
Given a cloud host is already in place,
the user should still be able to add a
repohost.
  • Loading branch information
Anthony Landreth committed Aug 2, 2024
1 parent 9aa988c commit a14de1e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
/vendor/
/testing/kuttl/e2e-generated*/
gke_gcloud_auth_plugin_cache
2 changes: 1 addition & 1 deletion internal/controller/postgrescluster/pgbackrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2640,7 +2640,7 @@ func (r *Reconciler) reconcileStanzaCreate(ctx context.Context,

// Always attempt to create pgBackRest stanza first
configHashMismatch, err := pgbackrest.Executor(exec).StanzaCreateOrUpgrade(ctx, configHash,
false)
false, postgresCluster)
if err != nil {
// record and log any errors resulting from running the stanza-create command
r.Recorder.Event(postgresCluster, corev1.EventTypeWarning, EventUnableToCreateStanzas,
Expand Down
31 changes: 27 additions & 4 deletions internal/pgbackrest/pgbackrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io"
"strings"

"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -51,7 +52,7 @@ type Executor func(
// from running (with a config mismatch indicating that the pgBackRest configuration as stored in
// the cluster's pgBackRest ConfigMap has not yet propagated to the Pod).
func (exec Executor) StanzaCreateOrUpgrade(ctx context.Context, configHash string,
upgrade bool) (bool, error) {
upgrade bool, postgresCluster *v1beta1.PostgresCluster) (bool, error) {

var stdout, stderr bytes.Buffer

Expand All @@ -60,22 +61,44 @@ func (exec Executor) StanzaCreateOrUpgrade(ctx context.Context, configHash strin
stanzaCmd = "upgrade"
}

var reposWithVolumes []v1beta1.PGBackRestRepo
for _, repo := range postgresCluster.Spec.Backups.PGBackRest.Repos {
if repo.Volume != nil {
reposWithVolumes = append(reposWithVolumes, repo)
}
}

grep := "grep %s-path /etc/pgbackrest/conf.d/pgbackrest_instance.conf"

var checkRepoCmd string
if len(reposWithVolumes) > 0 {
repo := reposWithVolumes[0]
checkRepoCmd = checkRepoCmd + fmt.Sprintf(grep, repo.Name)

reposWithVolumes = reposWithVolumes[1:]
for _, repo := range reposWithVolumes {
checkRepoCmd = checkRepoCmd + fmt.Sprintf(" && "+grep, repo.Name)
}
}

// this is the script that is run to create a stanza. First it checks the
// "config-hash" file to ensure all configuration changes (e.g. from ConfigMaps) have
// propagated to the container, and if not, it prints an error and returns with exit code 1).
// Otherwise, it runs the pgbackrest command, which will either be "stanza-create" or
// "stanza-upgrade", depending on the value of the boolean "upgrade" parameter.
const script = `
declare -r hash="$1" stanza="$2" message="$3" cmd="$4"
declare -r hash="$1" stanza="$2" message="$3" cmd="$4" check_repo_cmd="$5"
if [[ "$(< /etc/pgbackrest/conf.d/config-hash)" != "${hash}" ]]; then
printf >&2 "%s" "${message}"; exit 1;
elif ! bash -c "${check_repo_cmd}"; then
printf >&2 "%s" "${message}"; exit 1;
else
pgbackrest "${cmd}" --stanza="${stanza}"
fi
`
if err := exec(ctx, nil, &stdout, &stderr, "bash", "-ceu", "--",
script, "-", configHash, DefaultStanzaName, errMsgConfigHashMismatch,
fmt.Sprintf("stanza-%s", stanzaCmd)); err != nil {
fmt.Sprintf("stanza-%s", stanzaCmd), checkRepoCmd); err != nil {

errReturn := stderr.String()

Expand All @@ -89,7 +112,7 @@ fi
// if the err returned from pgbackrest command is about a version mismatch
// then we should run upgrade rather than create
if strings.Contains(errReturn, errMsgBackupDbMismatch) {
return exec.StanzaCreateOrUpgrade(ctx, configHash, true)
return exec.StanzaCreateOrUpgrade(ctx, configHash, true, postgresCluster)
}

// if none of the above errors, return the err
Expand Down
41 changes: 38 additions & 3 deletions internal/pgbackrest/pgbackrest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ import (
"testing"

"gotest.tools/v3/assert"
"k8s.io/apimachinery/pkg/api/resource"

"github.com/crunchydata/postgres-operator/internal/testing/require"
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
corev1 "k8s.io/api/core/v1"
)

func TestStanzaCreateOrUpgrade(t *testing.T) {
Expand All @@ -34,15 +37,19 @@ func TestStanzaCreateOrUpgrade(t *testing.T) {
ctx := context.Background()
configHash := "7f5d4d5bdc"
expectedCommand := []string{"bash", "-ceu", "--", `
declare -r hash="$1" stanza="$2" message="$3" cmd="$4"
declare -r hash="$1" stanza="$2" message="$3" cmd="$4" check_repo_cmd="$5"
if [[ "$(< /etc/pgbackrest/conf.d/config-hash)" != "${hash}" ]]; then
printf >&2 "%s" "${message}"; exit 1;
elif ! bash -c "${check_repo_cmd}"; then
printf >&2 "%s" "${message}"; exit 1;
else
pgbackrest "${cmd}" --stanza="${stanza}"
fi
`,
"-", "7f5d4d5bdc", "db", "postgres operator error: pgBackRest config hash mismatch",
"stanza-create"}
"stanza-create",
"grep repo1-path /etc/pgbackrest/conf.d/pgbackrest_instance.conf",
}

var shellCheckScript string
stanzaExec := func(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer,
Expand All @@ -56,8 +63,36 @@ fi

return nil
}
postgresCluster := &v1beta1.PostgresCluster{
Spec: v1beta1.PostgresClusterSpec{
Backups: v1beta1.Backups{
PGBackRest: v1beta1.PGBackRestArchive{
Repos: []v1beta1.PGBackRestRepo{{
Name: "repo1",
Volume: &v1beta1.RepoPVC{
VolumeClaimSpec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteMany},
Resources: corev1.VolumeResourceRequirements{
Requests: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceStorage: resource.MustParse("1Gi"),
},
},
},
},
}, {
Name: "repo2",
S3: &v1beta1.RepoS3{
Bucket: "bucket",
Endpoint: "endpoint",
Region: "region",
},
}},
},
},
},
}

configHashMismatch, err := Executor(stanzaExec).StanzaCreateOrUpgrade(ctx, configHash, false)
configHashMismatch, err := Executor(stanzaExec).StanzaCreateOrUpgrade(ctx, configHash, false, postgresCluster)
assert.NilError(t, err)
assert.Assert(t, !configHashMismatch)

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a14de1e

Please sign in to comment.