diff --git a/e2e-tests/tests/init-deploy/05-assert.yaml b/e2e-tests/tests/init-deploy/05-assert.yaml index 86b1aa02c3..8eb8c841f6 100644 --- a/e2e-tests/tests/init-deploy/05-assert.yaml +++ b/e2e-tests/tests/init-deploy/05-assert.yaml @@ -180,6 +180,24 @@ metadata: kind: PostgresCluster name: init-deploy --- +apiVersion: v1 +kind: Endpoints +metadata: + labels: + postgres-operator.crunchydata.com/cluster: init-deploy + postgres-operator.crunchydata.com/patroni: init-deploy-ha + test-label: test + name: init-deploy-ha +--- +apiVersion: v1 +kind: Endpoints +metadata: + labels: + postgres-operator.crunchydata.com/cluster: init-deploy + postgres-operator.crunchydata.com/patroni: init-deploy-ha + test-label: test + name: init-deploy-ha-config +--- apiVersion: apps/v1 kind: StatefulSet metadata: diff --git a/internal/patroni/config.go b/internal/patroni/config.go index 79d879dc4b..659c6d2fc5 100644 --- a/internal/patroni/config.go +++ b/internal/patroni/config.go @@ -46,6 +46,12 @@ func clusterYAML( cluster *v1beta1.PostgresCluster, pgHBAs postgres.HBAs, pgParameters postgres.Parameters, ) (string, error) { + + labels := map[string]string{naming.LabelCluster: cluster.Name} + if cluster.CompareVersion("2.9.0") >= 0 { + labels = naming.Merge(cluster.Spec.Metadata.GetLabelsOrNil(), labels) + } + root := map[string]any{ // The cluster identifier. This value cannot change during the cluster's // lifetime. @@ -65,9 +71,7 @@ func clusterYAML( // In addition to "scope_label" above, Patroni will add the following to // every object it creates. It will also use these as filters when doing // any lookups. - "labels": map[string]string{ - naming.LabelCluster: cluster.Name, - }, + "labels": labels, }, "postgresql": map[string]any{ diff --git a/internal/patroni/config_test.go b/internal/patroni/config_test.go index 710704424f..c886b6ce9c 100644 --- a/internal/patroni/config_test.go +++ b/internal/patroni/config_test.go @@ -18,10 +18,12 @@ import ( "sigs.k8s.io/yaml" "github.com/percona/percona-postgresql-operator/v2/internal/initialize" + "github.com/percona/percona-postgresql-operator/v2/internal/naming" "github.com/percona/percona-postgresql-operator/v2/internal/postgres" "github.com/percona/percona-postgresql-operator/v2/internal/testing/cmp" "github.com/percona/percona-postgresql-operator/v2/internal/testing/require" pNaming "github.com/percona/percona-postgresql-operator/v2/percona/naming" + "github.com/percona/percona-postgresql-operator/v2/percona/version" "github.com/percona/percona-postgresql-operator/v2/pkg/apis/postgres-operator.crunchydata.com/v1beta1" ) @@ -86,6 +88,38 @@ watchdog: `)+"\n") }) + t.Run("metadata labels propagated to labels", func(t *testing.T) { + cluster := new(v1beta1.PostgresCluster) + err := cluster.Default(context.Background(), nil) + assert.NilError(t, err) + cluster.Namespace = "some-namespace" + cluster.Name = "cluster-name" + cluster.Labels = map[string]string{ + naming.LabelVersion: version.Version(), + } + cluster.Spec.Metadata = &v1beta1.Metadata{ + Labels: map[string]string{ + "example.com/env": "production", + "example.com/owner": "team-a", + }, + } + + data, err := clusterYAML(cluster, postgres.HBAs{}, postgres.Parameters{}) + assert.NilError(t, err) + + var parsed map[string]any + assert.NilError(t, yaml.Unmarshal([]byte(data), &parsed)) + + k8sSection, ok := parsed["kubernetes"].(map[string]any) + assert.Assert(t, ok, "expected kubernetes section") + labels, ok := k8sSection["labels"].(map[string]any) + assert.Assert(t, ok, "expected kubernetes.labels section") + + assert.Equal(t, labels["example.com/env"], "production") + assert.Equal(t, labels["example.com/owner"], "team-a") + assert.Equal(t, labels["postgres-operator.crunchydata.com/cluster"], "cluster-name") + }) + t.Run(">PG10", func(t *testing.T) { cluster := new(v1beta1.PostgresCluster) err := cluster.Default(context.Background(), nil) diff --git a/percona/controller/pgbackup/controller.go b/percona/controller/pgbackup/controller.go index e3ba6f93db..fd2b402c69 100644 --- a/percona/controller/pgbackup/controller.go +++ b/percona/controller/pgbackup/controller.go @@ -2,7 +2,6 @@ package pgbackup import ( "context" - "fmt" "path" "slices" "time"