Skip to content

Commit 9d8b028

Browse files
egeguneshors
andauthored
K8SPG-712: Allow overriding default configurations (#1108)
* K8SPG-712: Allow overriding default configurations * add debug log for replacing patroni config * enable debug logs with make deploy * add test * fix unit test --------- Co-authored-by: Viacheslav Sarzhan <[email protected]>
1 parent 677d96a commit 9d8b028

File tree

12 files changed

+85
-33
lines changed

12 files changed

+85
-33
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ uninstall: ## Delete the postgrescluster CRD
120120
deploy: ## Deploy the PostgreSQL Operator (enables the postgrescluster controller)
121121
yq eval '(.spec.template.spec.containers[] | select(.name=="operator")).image = "$(IMAGE)"' ./deploy/operator.yaml \
122122
| yq eval '(.spec.template.spec.containers[] | select(.name=="operator") | .env[] | select(.name=="DISABLE_TELEMETRY") | .value) = "true"' - \
123+
| yq eval '(.spec.template.spec.containers[] | select(.name=="operator") | .env[] | select(.name=="LOG_LEVEL") | .value) = "DEBUG"' - \
123124
| kubectl apply -f -
124125

125126
.PHONY: undeploy

internal/controller/postgrescluster/controller.go

Lines changed: 17 additions & 1 deletion
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+
// K8SPG-712: Allow overriding default configurations
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). // K8SPG-712
510526
Owns(&corev1.Endpoints{}).
511527
Owns(&corev1.PersistentVolumeClaim{}).
512528
Owns(&corev1.Secret{}).

internal/controller/postgrescluster/controller_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,31 @@ spec:
512512
Expect(icm.Data["patroni.yaml"]).ToNot(BeZero())
513513
})
514514

515+
It("doesn't reconcile ConfigMap with override annotation", func() {
516+
icm := &corev1.ConfigMap{}
517+
Expect(suite.Client.Get(context.Background(), client.ObjectKey{
518+
Namespace: test.Namespace.Name, Name: instance.Name + "-config",
519+
}, icm)).To(Succeed())
520+
521+
Expect(icm.Labels[naming.LabelCluster]).To(Equal("carlos"))
522+
Expect(icm.Labels[naming.LabelInstance]).To(Equal(instance.Name))
523+
Expect(icm.Data["patroni.yaml"]).ToNot(BeZero())
524+
525+
icm.SetAnnotations(map[string]string{
526+
naming.OverrideConfigAnnotation: "true",
527+
})
528+
icm.Data["patroni.yaml"] = ""
529+
530+
Expect(suite.Client.Update(context.Background(), icm)).To(Succeed())
531+
532+
Expect(suite.Client.Get(context.Background(), client.ObjectKey{
533+
Namespace: test.Namespace.Name, Name: instance.Name + "-config",
534+
}, icm)).To(Succeed())
535+
536+
Expect(icm.Annotations[naming.OverrideConfigAnnotation]).To(Equal("true"))
537+
Expect(icm.Data["patroni.yaml"]).To(BeZero())
538+
})
539+
515540
Specify("Instance StatefulSet", func() {
516541
Expect(instance.Labels[naming.LabelCluster]).To(Equal("carlos"))
517542
Expect(instance.Labels[naming.LabelInstanceSet]).To(Equal("samba"))

internal/controller/postgrescluster/patroni.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ func (r *Reconciler) reconcilePatroniDynamicConfiguration(
222222
}
223223
configuration = patroni.DynamicConfiguration(cluster, configuration, pgHBAs, pgParameters)
224224

225+
logging.FromContext(ctx).V(1).Info("Replacing patroni dynamic configuration")
226+
225227
return errors.WithStack(
226228
patroni.Executor(exec).ReplaceConfiguration(ctx, configuration))
227229
}

internal/controller/postgrescluster/pgbackrest_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,7 +2060,7 @@ func TestReconcileCloudBasedDataSource(t *testing.T) {
20602060
result: testResult{
20612061
configCount: 1, jobCount: 1, pvcCount: 1,
20622062
expectedClusterCondition: nil,
2063-
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",
2063+
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",
20642064
},
20652065
}, {
20662066
desc: "global/configuration set",
@@ -2077,7 +2077,7 @@ func TestReconcileCloudBasedDataSource(t *testing.T) {
20772077
result: testResult{
20782078
configCount: 1, jobCount: 1, pvcCount: 1,
20792079
expectedClusterCondition: nil,
2080-
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",
2080+
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",
20812081
},
20822082
}, {
20832083
desc: "invalid option: stanza",
@@ -2092,7 +2092,7 @@ func TestReconcileCloudBasedDataSource(t *testing.T) {
20922092
result: testResult{
20932093
configCount: 1, jobCount: 0, pvcCount: 1,
20942094
expectedClusterCondition: nil,
2095-
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",
2095+
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",
20962096
},
20972097
}, {
20982098
desc: "cluster bootstrapped init condition missing",
@@ -2111,7 +2111,7 @@ func TestReconcileCloudBasedDataSource(t *testing.T) {
21112111
Reason: "ClusterAlreadyBootstrapped",
21122112
Message: "The cluster is already bootstrapped",
21132113
},
2114-
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",
2114+
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",
21152115
},
21162116
}}
21172117

internal/naming/annotations.go

Lines changed: 9 additions & 1 deletion
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,11 @@ 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+
// K8SPG-712
74+
// OverrideConfigAnnotation is an annotation used to prevent the controller from reconciling
75+
// ConfigMaps when a user wants to manually override their contents. When this annotation
76+
// is present, the controller will not update the ConfigMap, allowing users to make custom
77+
// modifications that won't be overwritten during reconciliation.
78+
OverrideConfigAnnotation = perconaAnnotationPrefix + "override-config"
7179
)

internal/patroni/config.go

Lines changed: 3 additions & 3 deletions
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

Lines changed: 12 additions & 12 deletions
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
@@ -983,8 +983,8 @@ tags: {}
983983
dataWithCustomMethods, err := instanceYAML(cluster, instance, nil)
984984
assert.NilError(t, err)
985985
assert.Equal(t, dataWithCustomMethods, strings.Trim(`
986-
# Generated by postgres-operator. DO NOT EDIT.
987-
# Your changes will not be saved.
986+
# Generated by postgres-operator. DO NOT EDIT UNLESS YOU KNOW WHAT YOU'RE DOING.
987+
# If you want to override the config, annotate this ConfigMap with pgv2.percona.com/override-config=true
988988
bootstrap:
989989
initdb:
990990
- data-checksums

internal/pgbackrest/config.go

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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

0 commit comments

Comments
 (0)