Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate arguments to a Patroni function #4057

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions internal/controller/postgrescluster/patroni.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,9 @@ func (r *Reconciler) reconcilePatroniDynamicConfiguration(
return r.PodExec(ctx, pod.Namespace, pod.Name, naming.ContainerDatabase, stdin, stdout, stderr, command...)
}

var configuration map[string]any
if cluster.Spec.Patroni != nil {
configuration = cluster.Spec.Patroni.DynamicConfiguration
}
configuration = patroni.DynamicConfiguration(cluster, configuration, pgHBAs, pgParameters)

return errors.WithStack(
patroni.Executor(exec).ReplaceConfiguration(ctx, configuration))
patroni.Executor(exec).ReplaceConfiguration(ctx,
patroni.DynamicConfiguration(&cluster.Spec, pgHBAs, pgParameters)))
}

// generatePatroniLeaderLeaseService returns a v1.Service that exposes the
Expand Down
50 changes: 21 additions & 29 deletions internal/patroni/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,8 @@ func clusterYAML(
// Patroni has not yet bootstrapped. Populate the "bootstrap.dcs" field to
// facilitate it. When Patroni is already bootstrapped, this field is ignored.

var configuration map[string]any
if cluster.Spec.Patroni != nil {
configuration = cluster.Spec.Patroni.DynamicConfiguration
}

root["bootstrap"] = map[string]any{
"dcs": DynamicConfiguration(cluster, configuration, pgHBAs, pgParameters),
"dcs": DynamicConfiguration(&cluster.Spec, pgHBAs, pgParameters),

// Missing here is "users" which runs *after* "post_bootstrap". It is
// not possible to use roles created by the former in the latter.
Expand All @@ -200,33 +195,33 @@ func clusterYAML(
// DynamicConfiguration combines configuration with some PostgreSQL settings
// and returns a value that can be marshaled to JSON.
func DynamicConfiguration(
cluster *v1beta1.PostgresCluster,
configuration map[string]any,
spec *v1beta1.PostgresClusterSpec,
pgHBAs postgres.HBAs, pgParameters postgres.Parameters,
) map[string]any {
// Copy the entire configuration before making any changes.
root := make(map[string]any, len(configuration))
for k, v := range configuration {
root[k] = v
root := make(map[string]any)
if spec.Patroni != nil && spec.Patroni.DynamicConfiguration != nil {
root = spec.Patroni.DynamicConfiguration.DeepCopy()
}

root["ttl"] = *cluster.Spec.Patroni.LeaderLeaseDurationSeconds
root["loop_wait"] = *cluster.Spec.Patroni.SyncPeriodSeconds
// NOTE: These are always populated due to [v1beta1.PatroniSpec.Default]
root["ttl"] = *spec.Patroni.LeaderLeaseDurationSeconds
root["loop_wait"] = *spec.Patroni.SyncPeriodSeconds

// Copy the "postgresql" section before making any changes.
postgresql := map[string]any{
// TODO(cbandy): explain this. requires an archive, perhaps.
"use_slots": false,
}

// When TDE is configured, override the pg_rewind binary name to point
// to the wrapper script.
if config.FetchKeyCommand(&cluster.Spec) != "" {
if config.FetchKeyCommand(spec) != "" {
postgresql["bin_name"] = map[string]any{
"pg_rewind": "/tmp/pg_rewind_tde.sh",
}
}

// Copy the "postgresql" section over the above defaults.
if section, ok := root["postgresql"].(map[string]any); ok {
for k, v := range section {
postgresql[k] = v
Expand Down Expand Up @@ -300,32 +295,29 @@ func DynamicConfiguration(
// Recent versions of `pg_rewind` can run with limited permissions granted
// by Patroni to the user defined in "postgresql.authentication.rewind".
// PostgreSQL v10 and earlier require superuser access over the network.
postgresql["use_pg_rewind"] = cluster.Spec.PostgresVersion > 10

if cluster.Spec.Standby != nil && cluster.Spec.Standby.Enabled {
// Copy the "standby_cluster" section before making any changes.
standby := make(map[string]any)
if section, ok := root["standby_cluster"].(map[string]any); ok {
for k, v := range section {
standby[k] = v
}
postgresql["use_pg_rewind"] = spec.PostgresVersion > 10

if spec.Standby != nil && spec.Standby.Enabled {
standby, _ := root["standby_cluster"].(map[string]any)
if standby == nil {
standby = make(map[string]any)
}

// Unset any previous value for restore_command - we will set it later if needed
delete(standby, "restore_command")

// Populate replica creation methods based on options provided in the standby spec:
methods := []string{}
if cluster.Spec.Standby.Host != "" {
standby["host"] = cluster.Spec.Standby.Host
if cluster.Spec.Standby.Port != nil {
standby["port"] = *cluster.Spec.Standby.Port
if spec.Standby.Host != "" {
standby["host"] = spec.Standby.Host
if spec.Standby.Port != nil {
standby["port"] = *spec.Standby.Port
}

methods = append([]string{basebackupCreateReplicaMethod}, methods...)
}

if cluster.Spec.Standby.RepoName != "" {
if spec.Standby.RepoName != "" {
// Append pgbackrest as the first choice when creating the standby
methods = append([]string{pgBackRestCreateReplicaMethod}, methods...)

Expand Down
Loading