Skip to content

Commit fa6c2e4

Browse files
committed
K8SPG-712: Allow overriding default configurations
1 parent 6a80c22 commit fa6c2e4

File tree

9 files changed

+54
-31
lines changed

9 files changed

+54
-31
lines changed

internal/controller/postgrescluster/controller.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@ import (
2727
"sigs.k8s.io/controller-runtime/pkg/builder"
2828
"sigs.k8s.io/controller-runtime/pkg/client"
2929
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
30+
"sigs.k8s.io/controller-runtime/pkg/event"
3031
"sigs.k8s.io/controller-runtime/pkg/manager"
32+
"sigs.k8s.io/controller-runtime/pkg/predicate"
3133
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3234

3335
"github.com/percona/percona-postgresql-operator/internal/config"
3436
"github.com/percona/percona-postgresql-operator/internal/controller/runtime"
3537
"github.com/percona/percona-postgresql-operator/internal/initialize"
3638
"github.com/percona/percona-postgresql-operator/internal/logging"
39+
"github.com/percona/percona-postgresql-operator/internal/naming"
3740
"github.com/percona/percona-postgresql-operator/internal/pgaudit"
3841
"github.com/percona/percona-postgresql-operator/internal/pgbackrest"
3942
"github.com/percona/percona-postgresql-operator/internal/pgbouncer"
@@ -504,9 +507,22 @@ func (r *Reconciler) SetupWithManager(mgr manager.Manager) error {
504507
}
505508
}
506509

510+
// Create a predicate that filters ConfigMaps with a specific annotation
511+
configMapPredicate := builder.WithPredicates(predicate.Funcs{
512+
UpdateFunc: func(e event.UpdateEvent) bool {
513+
configMap, ok := e.ObjectNew.(*corev1.ConfigMap)
514+
if !ok {
515+
return true
516+
}
517+
// Skip reconciliation if the ConfigMap has the specified annotation
518+
_, hasAnnotation := configMap.Annotations[naming.OverrideConfigAnnotation]
519+
return !hasAnnotation
520+
},
521+
})
522+
507523
return builder.ControllerManagedBy(mgr).
508524
For(&v1beta1.PostgresCluster{}).
509-
Owns(&corev1.ConfigMap{}).
525+
Owns(&corev1.ConfigMap{}, configMapPredicate).
510526
Owns(&corev1.Endpoints{}).
511527
Owns(&corev1.PersistentVolumeClaim{}).
512528
Owns(&corev1.Secret{}).

internal/controller/postgrescluster/pgbackrest_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -2059,7 +2059,7 @@ func TestReconcileCloudBasedDataSource(t *testing.T) {
20592059
result: testResult{
20602060
configCount: 1, jobCount: 1, pvcCount: 1,
20612061
expectedClusterCondition: nil,
2062-
conf: "|\n # Generated by postgres-operator. DO NOT EDIT.\n # Your changes will not be saved.\n\n [global]\n archive-async = y\n log-path = /pgdata/pgbackrest/log\n repo1-path = /pgbackrest/repo1\n spool-path = /pgdata/pgbackrest-spool\n\n [db]\n pg1-path = /pgdata/pg13\n pg1-port = 5432\n pg1-socket-path = /tmp/postgres\n",
2062+
conf: "|\n # Generated by postgres-operator. DO NOT EDIT UNLESS YOU KNOW WHAT YOU'RE DOING.\n # If you want to override the config, annotate this ConfigMap with pgv2.percona.com/override-config=true\n\n [global]\n archive-async = y\n log-path = /pgdata/pgbackrest/log\n repo1-path = /pgbackrest/repo1\n spool-path = /pgdata/pgbackrest-spool\n\n [db]\n pg1-path = /pgdata/pg13\n pg1-port = 5432\n pg1-socket-path = /tmp/postgres\n",
20632063
},
20642064
}, {
20652065
desc: "global/configuration set",
@@ -2076,7 +2076,7 @@ func TestReconcileCloudBasedDataSource(t *testing.T) {
20762076
result: testResult{
20772077
configCount: 1, jobCount: 1, pvcCount: 1,
20782078
expectedClusterCondition: nil,
2079-
conf: "|\n # Generated by postgres-operator. DO NOT EDIT.\n # Your changes will not be saved.\n\n [global]\n archive-async = y\n log-path = /pgdata/pgbackrest/log\n repo1-path = elephant\n spool-path = /pgdata/pgbackrest-spool\n\n [db]\n pg1-path = /pgdata/pg13\n pg1-port = 5432\n pg1-socket-path = /tmp/postgres\n",
2079+
conf: "|\n # Generated by postgres-operator. DO NOT EDIT UNLESS YOU KNOW WHAT YOU'RE DOING.\n # If you want to override the config, annotate this ConfigMap with pgv2.percona.com/override-config=true\n\n [global]\n archive-async = y\n log-path = /pgdata/pgbackrest/log\n repo1-path = elephant\n spool-path = /pgdata/pgbackrest-spool\n\n [db]\n pg1-path = /pgdata/pg13\n pg1-port = 5432\n pg1-socket-path = /tmp/postgres\n",
20802080
},
20812081
}, {
20822082
desc: "invalid option: stanza",
@@ -2091,7 +2091,7 @@ func TestReconcileCloudBasedDataSource(t *testing.T) {
20912091
result: testResult{
20922092
configCount: 1, jobCount: 0, pvcCount: 1,
20932093
expectedClusterCondition: nil,
2094-
conf: "|\n # Generated by postgres-operator. DO NOT EDIT.\n # Your changes will not be saved.\n\n [global]\n archive-async = y\n log-path = /pgdata/pgbackrest/log\n repo1-path = /pgbackrest/repo1\n spool-path = /pgdata/pgbackrest-spool\n\n [db]\n pg1-path = /pgdata/pg13\n pg1-port = 5432\n pg1-socket-path = /tmp/postgres\n",
2094+
conf: "|\n # Generated by postgres-operator. DO NOT EDIT UNLESS YOU KNOW WHAT YOU'RE DOING.\n # If you want to override the config, annotate this ConfigMap with pgv2.percona.com/override-config=true\n\n [global]\n archive-async = y\n log-path = /pgdata/pgbackrest/log\n repo1-path = /pgbackrest/repo1\n spool-path = /pgdata/pgbackrest-spool\n\n [db]\n pg1-path = /pgdata/pg13\n pg1-port = 5432\n pg1-socket-path = /tmp/postgres\n",
20952095
},
20962096
}, {
20972097
desc: "cluster bootstrapped init condition missing",
@@ -2110,7 +2110,7 @@ func TestReconcileCloudBasedDataSource(t *testing.T) {
21102110
Reason: "ClusterAlreadyBootstrapped",
21112111
Message: "The cluster is already bootstrapped",
21122112
},
2113-
conf: "|\n # Generated by postgres-operator. DO NOT EDIT.\n # Your changes will not be saved.\n\n [global]\n archive-async = y\n log-path = /pgdata/pgbackrest/log\n repo1-path = /pgbackrest/repo1\n spool-path = /pgdata/pgbackrest-spool\n\n [db]\n pg1-path = /pgdata/pg13\n pg1-port = 5432\n pg1-socket-path = /tmp/postgres\n",
2113+
conf: "|\n # Generated by postgres-operator. DO NOT EDIT UNLESS YOU KNOW WHAT YOU'RE DOING.\n # If you want to override the config, annotate this ConfigMap with pgv2.percona.com/override-config=true\n\n [global]\n archive-async = y\n log-path = /pgdata/pgbackrest/log\n repo1-path = /pgbackrest/repo1\n spool-path = /pgdata/pgbackrest-spool\n\n [db]\n pg1-path = /pgdata/pg13\n pg1-port = 5432\n pg1-socket-path = /tmp/postgres\n",
21142114
},
21152115
}}
21162116

internal/naming/annotations.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
package naming
66

77
const (
8-
annotationPrefix = labelPrefix
8+
annotationPrefix = labelPrefix
9+
perconaAnnotationPrefix = perconaLabelPrefix
910

1011
// Finalizer marks an object to be garbage collected by this module.
1112
Finalizer = annotationPrefix + "finalizer"
@@ -68,4 +69,10 @@ const (
6869
// to a cluster without backups. As usual with the operator, we do not
6970
// touch cloud-based backups.
7071
AuthorizeBackupRemovalAnnotation = annotationPrefix + "authorizeBackupRemoval"
72+
73+
// OverrideConfigAnnotation is an annotation used to prevent the controller from reconciling
74+
// ConfigMaps when a user wants to manually override their contents. When this annotation
75+
// is present, the controller will not update the ConfigMap, allowing users to make custom
76+
// modifications that won't be overwritten during reconciliation.
77+
OverrideConfigAnnotation = perconaAnnotationPrefix + "override-config"
7178
)

internal/patroni/config.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ const (
3030

3131
const (
3232
yamlGeneratedWarning = "" +
33-
"# Generated by postgres-operator. DO NOT EDIT.\n" +
34-
"# Your changes will not be saved.\n"
33+
"# Generated by postgres-operator. DO NOT EDIT UNLESS YOU KNOW WHAT YOU'RE DOING.\n" +
34+
"# If you want to override the config, annotate this ConfigMap with " + naming.OverrideConfigAnnotation + "=true\n"
3535
)
3636

3737
// quoteShellWord ensures that s is interpreted by a shell as single word.
@@ -535,7 +535,7 @@ func instanceYAML(
535535
// NOTE(cbandy): The "--waldir" option was introduced in PostgreSQL v10.
536536
"waldir=" + postgres.WALDirectory(cluster, instance),
537537
}
538-
methods := []string{"basebackup"}
538+
methods := []string{basebackupCreateReplicaMethod}
539539

540540
// Prefer a pgBackRest method when it is available, and fallback to other
541541
// methods when it fails.

internal/patroni/config_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ func TestClusterYAML(t *testing.T) {
3838
data, err := clusterYAML(cluster, postgres.HBAs{}, postgres.Parameters{})
3939
assert.NilError(t, err)
4040
assert.Equal(t, data, strings.TrimSpace(`
41-
# Generated by postgres-operator. DO NOT EDIT.
42-
# Your changes will not be saved.
41+
# Generated by postgres-operator. DO NOT EDIT UNLESS YOU KNOW WHAT YOU'RE DOING.
42+
# If you want to override the config, annotate this ConfigMap with pgv2.percona.com/override-config=true
4343
bootstrap:
4444
dcs:
4545
loop_wait: 10
@@ -97,8 +97,8 @@ watchdog:
9797
data, err := clusterYAML(cluster, postgres.HBAs{}, postgres.Parameters{})
9898
assert.NilError(t, err)
9999
assert.Equal(t, data, strings.TrimSpace(`
100-
# Generated by postgres-operator. DO NOT EDIT.
101-
# Your changes will not be saved.
100+
# Generated by postgres-operator. DO NOT EDIT UNLESS YOU KNOW WHAT YOU'RE DOING.
101+
# If you want to override the config, annotate this ConfigMap with pgv2.percona.com/override-config=true
102102
bootstrap:
103103
dcs:
104104
loop_wait: 10
@@ -895,8 +895,8 @@ func TestInstanceYAML(t *testing.T) {
895895
data, err := instanceYAML(cluster, instance, nil)
896896
assert.NilError(t, err)
897897
assert.Equal(t, data, strings.Trim(`
898-
# Generated by postgres-operator. DO NOT EDIT.
899-
# Your changes will not be saved.
898+
# Generated by postgres-operator. DO NOT EDIT UNLESS YOU KNOW WHAT YOU'RE DOING.
899+
# If you want to override the config, annotate this ConfigMap with pgv2.percona.com/override-config=true
900900
bootstrap:
901901
initdb:
902902
- data-checksums
@@ -918,8 +918,8 @@ tags: {}
918918
dataWithReplicaCreate, err := instanceYAML(cluster, instance, []string{"some", "backrest", "cmd"})
919919
assert.NilError(t, err)
920920
assert.Equal(t, dataWithReplicaCreate, strings.Trim(`
921-
# Generated by postgres-operator. DO NOT EDIT.
922-
# Your changes will not be saved.
921+
# Generated by postgres-operator. DO NOT EDIT UNLESS YOU KNOW WHAT YOU'RE DOING.
922+
# If you want to override the config, annotate this ConfigMap with pgv2.percona.com/override-config=true
923923
bootstrap:
924924
initdb:
925925
- data-checksums
@@ -958,8 +958,8 @@ tags: {}
958958
datawithTDE, err := instanceYAML(cluster, instance, nil)
959959
assert.NilError(t, err)
960960
assert.Equal(t, datawithTDE, strings.Trim(`
961-
# Generated by postgres-operator. DO NOT EDIT.
962-
# Your changes will not be saved.
961+
# Generated by postgres-operator. DO NOT EDIT UNLESS YOU KNOW WHAT YOU'RE DOING.
962+
# If you want to override the config, annotate this ConfigMap with pgv2.percona.com/override-config=true
963963
bootstrap:
964964
initdb:
965965
- data-checksums

internal/pgbackrest/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ const (
5656

5757
const (
5858
iniGeneratedWarning = "" +
59-
"# Generated by postgres-operator. DO NOT EDIT.\n" +
60-
"# Your changes will not be saved.\n"
59+
"# Generated by postgres-operator. DO NOT EDIT UNLESS YOU KNOW WHAT YOU'RE DOING.\n" +
60+
"# If you want to override the config, annotate this ConfigMap with " + naming.OverrideConfigAnnotation + "=true\n"
6161
)
6262

6363
// CreatePGBackRestConfigMapIntent creates a configmap struct with pgBackRest pgbackrest.conf settings in the data field.

internal/pgbackrest/config_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ func TestCreatePGBackRestConfigMapIntent(t *testing.T) {
8989

9090
assert.Equal(t, configmap.Data["config-hash"], "abcde12345")
9191
assert.Equal(t, configmap.Data["pgbackrest_repo.conf"], strings.Trim(`
92-
# Generated by postgres-operator. DO NOT EDIT.
93-
# Your changes will not be saved.
92+
# Generated by postgres-operator. DO NOT EDIT UNLESS YOU KNOW WHAT YOU'RE DOING.
93+
# If you want to override the config, annotate this ConfigMap with pgv2.percona.com/override-config=true
9494
9595
[global]
9696
log-path = /pgbackrest/repo1/log
@@ -120,8 +120,8 @@ pg1-socket-path = /tmp/postgres
120120
`, "\t\n")+"\n")
121121

122122
assert.Equal(t, configmap.Data["pgbackrest_instance.conf"], strings.Trim(`
123-
# Generated by postgres-operator. DO NOT EDIT.
124-
# Your changes will not be saved.
123+
# Generated by postgres-operator. DO NOT EDIT UNLESS YOU KNOW WHAT YOU'RE DOING.
124+
# If you want to override the config, annotate this ConfigMap with pgv2.percona.com/override-config=true
125125
126126
[global]
127127
archive-async = y

internal/pgbouncer/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const (
3535

3636
const (
3737
iniGeneratedWarning = "" +
38-
"# Generated by postgres-operator. DO NOT EDIT.\n" +
39-
"# Your changes will not be saved.\n"
38+
"# Generated by postgres-operator. DO NOT EDIT UNLESS YOU KNOW WHAT YOU'RE DOING.\n" +
39+
"# If you want to override the config, annotate this ConfigMap with " + naming.OverrideConfigAnnotation + "=true\n"
4040
)
4141

4242
type iniValueSet map[string]string

internal/pgbouncer/config_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ func TestClusterINI(t *testing.T) {
5757

5858
t.Run("Default", func(t *testing.T) {
5959
assert.Equal(t, clusterINI(cluster), strings.Trim(`
60-
# Generated by postgres-operator. DO NOT EDIT.
61-
# Your changes will not be saved.
60+
# Generated by postgres-operator. DO NOT EDIT UNLESS YOU KNOW WHAT YOU'RE DOING.
61+
# If you want to override the config, annotate this ConfigMap with pgv2.percona.com/override-config=true
6262
6363
[pgbouncer]
6464
%include /etc/pgbouncer/pgbouncer.ini
@@ -97,8 +97,8 @@ unix_socket_dir =
9797
}
9898

9999
assert.Equal(t, clusterINI(cluster), strings.Trim(`
100-
# Generated by postgres-operator. DO NOT EDIT.
101-
# Your changes will not be saved.
100+
# Generated by postgres-operator. DO NOT EDIT UNLESS YOU KNOW WHAT YOU'RE DOING.
101+
# If you want to override the config, annotate this ConfigMap with pgv2.percona.com/override-config=true
102102
103103
[pgbouncer]
104104
%include /etc/pgbouncer/pgbouncer.ini

0 commit comments

Comments
 (0)