Skip to content

Commit 3a6426c

Browse files
committed
Pass Postgres settings to RestoreCommand using ParameterSet
1 parent d480b4a commit 3a6426c

File tree

6 files changed

+35
-42
lines changed

6 files changed

+35
-42
lines changed

internal/controller/postgrescluster/pgbackrest.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,17 +1262,15 @@ func (r *Reconciler) reconcileRestoreJob(ctx context.Context,
12621262
}
12631263
}
12641264

1265-
// Check to see if huge pages have been requested in the spec. If they have, include 'huge_pages = try'
1266-
// in the restore command. If they haven't, include 'huge_pages = off'.
1267-
hugePagesSetting := "off"
1268-
if postgres.HugePagesRequested(cluster) {
1269-
hugePagesSetting = "try"
1265+
params := postgres.NewParameterSet()
1266+
postgres.SetHugePages(cluster, params)
1267+
if fetchKeyCommand := config.FetchKeyCommand(&cluster.Spec); fetchKeyCommand != "" {
1268+
params.Add("encryption_key_command", fetchKeyCommand)
12701269
}
12711270

12721271
// NOTE (andrewlecuyer): Forcing users to put each argument separately might prevent the need
12731272
// to do any escaping or use eval.
1274-
cmd := pgbackrest.RestoreCommand(pgdata, hugePagesSetting, config.FetchKeyCommand(&cluster.Spec),
1275-
pgtablespaceVolumes, strings.Join(opts, " "))
1273+
cmd := pgbackrest.RestoreCommand(pgdata, params, strings.Join(opts, " "))
12761274

12771275
// create the volume resources required for the postgres data directory
12781276
dataVolumeMount := postgres.DataVolumeMount()

internal/controller/postgrescluster/postgres.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (r *Reconciler) generatePostgresParameters(
134134
pgaudit.PostgreSQLParameters(&builtin)
135135
pgbackrest.PostgreSQLParameters(cluster, &builtin, backupsSpecFound)
136136
pgmonitor.PostgreSQLParameters(ctx, cluster, &builtin)
137-
postgres.SetHugePages(cluster, &builtin)
137+
postgres.SetHugePages(cluster, builtin.Default)
138138

139139
// Last write wins, so start with the recommended defaults.
140140
result := cmp.Or(builtin.Default.DeepCopy(), postgres.NewParameterSet())

internal/pgbackrest/config.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,9 @@ func MakePGBackrestLogDir(template *corev1.PodTemplateSpec,
212212
// - Renames the data directory as needed to bootstrap the cluster using the restored database.
213213
// This ensures compatibility with the "existing" bootstrap method that is included in the
214214
// Patroni config when bootstrapping a cluster using an existing data directory.
215-
func RestoreCommand(pgdata, hugePagesSetting, fetchKeyCommand string, _ []*corev1.PersistentVolumeClaim, args ...string) []string {
216-
ps := postgres.NewParameterSet()
215+
func RestoreCommand(pgdata string, params *postgres.ParameterSet, args ...string) []string {
216+
ps := params.DeepCopy()
217217
ps.Add("data_directory", pgdata)
218-
ps.Add("huge_pages", hugePagesSetting)
219218

220219
// Keep history and WAL files until the cluster starts with its normal
221220
// archiving enabled.
@@ -226,10 +225,6 @@ func RestoreCommand(pgdata, hugePagesSetting, fetchKeyCommand string, _ []*corev
226225
// progress during recovery.
227226
ps.Add("hot_standby", "on")
228227

229-
if fetchKeyCommand != "" {
230-
ps.Add("encryption_key_command", fetchKeyCommand)
231-
}
232-
233228
configure := strings.Join([]string{
234229
// With "hot_standby" on, some parameters cannot be smaller than they were
235230
// when Postgres was backed up. Configure these to match values reported by

internal/pgbackrest/config_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/crunchydata/postgres-operator/internal/initialize"
2020
"github.com/crunchydata/postgres-operator/internal/naming"
21+
"github.com/crunchydata/postgres-operator/internal/postgres"
2122
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
2223
"github.com/crunchydata/postgres-operator/internal/testing/require"
2324
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
@@ -789,11 +790,7 @@ func TestReloadCommandPrettyYAML(t *testing.T) {
789790
func TestRestoreCommand(t *testing.T) {
790791
shellcheck := require.ShellCheck(t)
791792

792-
pgdata := "/pgdata/pg13"
793-
opts := []string{
794-
"--stanza=" + DefaultStanzaName, "--pg1-path=" + pgdata,
795-
"--repo=1"}
796-
command := RestoreCommand(pgdata, "try", "", nil, strings.Join(opts, " "))
793+
command := RestoreCommand("/pgdata/pg13", postgres.NewParameterSet(), "--repo=1")
797794

798795
assert.DeepEqual(t, command[:3], []string{"bash", "-ceu", "--"})
799796
assert.Assert(t, len(command) > 3)
@@ -810,17 +807,20 @@ func TestRestoreCommand(t *testing.T) {
810807
func TestRestoreCommandPrettyYAML(t *testing.T) {
811808
assert.Assert(t,
812809
cmp.MarshalContains(
813-
RestoreCommand("/dir", "try", "", nil, "--options"),
810+
RestoreCommand("/dir", postgres.NewParameterSet(), "--options"),
814811
"\n- |",
815812
),
816813
"expected literal block scalar")
817814
}
818815

819816
func TestRestoreCommandTDE(t *testing.T) {
817+
params := postgres.NewParameterSet()
818+
params.Add("encryption_key_command", "whatever")
819+
820820
assert.Assert(t,
821821
cmp.MarshalContains(
822-
RestoreCommand("/dir", "try", "echo testValue", nil, "--options"),
823-
"encryption_key_command = 'echo testValue'",
822+
RestoreCommand("/dir", params, "--options"),
823+
"encryption_key_command = 'whatever'",
824824
),
825825
"expected encryption_key_command setting")
826826
}

internal/postgres/huge_pages.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import (
1616
// This function looks for a valid huge_pages resource request. If it finds one,
1717
// it sets the PostgreSQL parameter "huge_pages" to "try". If it doesn't find
1818
// one, it sets "huge_pages" to "off".
19-
func SetHugePages(cluster *v1beta1.PostgresCluster, pgParameters *Parameters) {
19+
func SetHugePages(cluster *v1beta1.PostgresCluster, params *ParameterSet) {
2020
if HugePagesRequested(cluster) {
21-
pgParameters.Default.Add("huge_pages", "try")
21+
params.Add("huge_pages", "try")
2222
} else {
23-
pgParameters.Default.Add("huge_pages", "off")
23+
params.Add("huge_pages", "off")
2424
}
2525
}
2626

internal/postgres/huge_pages_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ func TestSetHugePages(t *testing.T) {
2727
},
2828
}}
2929

30-
pgParameters := NewParameters()
31-
SetHugePages(cluster, &pgParameters)
30+
params := NewParameterSet()
31+
SetHugePages(cluster, params)
3232

33-
assert.Equal(t, pgParameters.Default.Has("huge_pages"), true)
34-
assert.Equal(t, pgParameters.Default.Value("huge_pages"), "off")
33+
assert.Equal(t, params.Has("huge_pages"), true)
34+
assert.Equal(t, params.Value("huge_pages"), "off")
3535
})
3636

3737
t.Run("hugepages quantity not set", func(t *testing.T) {
@@ -48,11 +48,11 @@ func TestSetHugePages(t *testing.T) {
4848
},
4949
}}
5050

51-
pgParameters := NewParameters()
52-
SetHugePages(cluster, &pgParameters)
51+
params := NewParameterSet()
52+
SetHugePages(cluster, params)
5353

54-
assert.Equal(t, pgParameters.Default.Has("huge_pages"), true)
55-
assert.Equal(t, pgParameters.Default.Value("huge_pages"), "off")
54+
assert.Equal(t, params.Has("huge_pages"), true)
55+
assert.Equal(t, params.Value("huge_pages"), "off")
5656
})
5757

5858
t.Run("hugepages set to zero", func(t *testing.T) {
@@ -68,11 +68,11 @@ func TestSetHugePages(t *testing.T) {
6868
},
6969
}}
7070

71-
pgParameters := NewParameters()
72-
SetHugePages(cluster, &pgParameters)
71+
params := NewParameterSet()
72+
SetHugePages(cluster, params)
7373

74-
assert.Equal(t, pgParameters.Default.Has("huge_pages"), true)
75-
assert.Equal(t, pgParameters.Default.Value("huge_pages"), "off")
74+
assert.Equal(t, params.Has("huge_pages"), true)
75+
assert.Equal(t, params.Value("huge_pages"), "off")
7676
})
7777

7878
t.Run("hugepages set correctly", func(t *testing.T) {
@@ -88,11 +88,11 @@ func TestSetHugePages(t *testing.T) {
8888
},
8989
}}
9090

91-
pgParameters := NewParameters()
92-
SetHugePages(cluster, &pgParameters)
91+
params := NewParameterSet()
92+
SetHugePages(cluster, params)
9393

94-
assert.Equal(t, pgParameters.Default.Has("huge_pages"), true)
95-
assert.Equal(t, pgParameters.Default.Value("huge_pages"), "try")
94+
assert.Equal(t, params.Has("huge_pages"), true)
95+
assert.Equal(t, params.Value("huge_pages"), "try")
9696
})
9797

9898
}

0 commit comments

Comments
 (0)