Skip to content

Commit c9d4dda

Browse files
authored
Custom Labels
This update allows, users to define custom labels for any/all resources created by the PostgreSQL Operator. Updating the PostgresCluster Spec with custom labels in the following fields will be added to resources when they are reconciled: - `spec.metadata.labels` (cluster) - `spec.archive.pgbackrest.metadata.labels` (pgbackrest) - `spec.proxy.pgbouncer.metadata.labels` (pgbouncer) - `spec.instances[i].metadata.labels` (instance) The labels that get added to a resource are a combination of cluster labels that are added globally, instance, pgbackrest, or pgbouncer labels that are added to their corresponding resources, and the non-user labels that the PostgreSQL Operator adds normally. When these labels are merged non-user labels will always win, then specific resource labels, then global labels. End-to-end style tests have been added to ensure that when a cluster is created and reconciled with custom labels, every resource that it creates is labeled. The tests use resource labels to determine what resources should be labeled. For example, PGBackRest tests list resources with the `postgres-operator.crunchydata.com/pgbackrest` label and make sure that it also has any label defined in `spec.archive.pgbackrest.metadata.labels`.
1 parent 1294aac commit c9d4dda

File tree

20 files changed

+968
-108
lines changed

20 files changed

+968
-108
lines changed

config/crd/bases/postgres-operator.crunchydata.com_postgresclusters.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,15 @@ spec:
284284
configuration generated by the PostgreSQL Operator, and
285285
then mounted under "/etc/pgbackrest/conf.d": https://pgbackrest.org/configuration.html'
286286
type: object
287+
metadata:
288+
description: Metadata contains metadata for PostgresCluster
289+
resources
290+
properties:
291+
labels:
292+
additionalProperties:
293+
type: string
294+
type: object
295+
type: object
287296
repoHost:
288297
description: Defines a pgBackRest repository host
289298
properties:
@@ -789,6 +798,15 @@ spec:
789798
instances:
790799
items:
791800
properties:
801+
metadata:
802+
description: Metadata contains metadata for PostgresCluster
803+
resources
804+
properties:
805+
labels:
806+
additionalProperties:
807+
type: string
808+
type: object
809+
type: object
792810
name:
793811
default: ""
794812
type: string
@@ -958,6 +976,14 @@ spec:
958976
x-kubernetes-list-map-keys:
959977
- name
960978
x-kubernetes-list-type: map
979+
metadata:
980+
description: Metadata contains metadata for PostgresCluster resources
981+
properties:
982+
labels:
983+
additionalProperties:
984+
type: string
985+
type: object
986+
type: object
961987
openshift:
962988
description: Whether or not the PostgreSQL cluster is being deployed
963989
to an OpenShift envioronment
@@ -1986,6 +2012,15 @@ spec:
19862012
1.15 or newer. Changing this value causes PgBouncer to restart.
19872013
More info: https://kubernetes.io/docs/concepts/containers/images'
19882014
type: string
2015+
metadata:
2016+
description: Metadata contains metadata for PostgresCluster
2017+
resources
2018+
properties:
2019+
labels:
2020+
additionalProperties:
2021+
type: string
2022+
type: object
2023+
type: object
19892024
port:
19902025
default: 5432
19912026
description: Port on which PgBouncer should listen for client

internal/controller/postgrescluster/cluster.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ func (r *Reconciler) reconcileClusterConfigMap(
4545

4646
err := errors.WithStack(r.setControllerReference(cluster, clusterConfigMap))
4747

48-
clusterConfigMap.Labels = map[string]string{
49-
naming.LabelCluster: cluster.Name,
50-
}
48+
clusterConfigMap.Labels = naming.Merge(cluster.Spec.Metadata.Labels,
49+
map[string]string{
50+
naming.LabelCluster: cluster.Name,
51+
})
5152

5253
if err == nil {
5354
err = patroni.ClusterConfigMap(ctx, cluster, pgHBAs, pgParameters, pgUser, clusterConfigMap)
@@ -71,9 +72,10 @@ func (r *Reconciler) reconcileClusterPodService(
7172

7273
err := errors.WithStack(r.setControllerReference(cluster, clusterPodService))
7374

74-
clusterPodService.Labels = map[string]string{
75-
naming.LabelCluster: cluster.Name,
76-
}
75+
clusterPodService.Labels = naming.Merge(cluster.Spec.Metadata.Labels,
76+
map[string]string{
77+
naming.LabelCluster: cluster.Name,
78+
})
7779

7880
// Allocate no IP address (headless) and match any Pod with the cluster
7981
// label, regardless of its readiness. Not particularly useful by itself, but
@@ -111,10 +113,11 @@ func (r *Reconciler) reconcileClusterPrimaryService(
111113

112114
err := errors.WithStack(r.setControllerReference(cluster, clusterPrimaryService))
113115

114-
clusterPrimaryService.Labels = map[string]string{
115-
naming.LabelCluster: cluster.Name,
116-
naming.LabelRole: naming.RolePrimary,
117-
}
116+
clusterPrimaryService.Labels = naming.Merge(cluster.Spec.Metadata.Labels,
117+
map[string]string{
118+
naming.LabelCluster: cluster.Name,
119+
naming.LabelRole: naming.RolePrimary,
120+
})
118121

119122
if err == nil && leader == nil {
120123
// TODO(cbandy): We need to build a different kind of Service here.
@@ -153,10 +156,11 @@ func (r *Reconciler) reconcileClusterPrimaryService(
153156
err = errors.WithStack(r.setControllerReference(cluster, endpoints))
154157
}
155158

156-
endpoints.Labels = map[string]string{
157-
naming.LabelCluster: cluster.Name,
158-
naming.LabelRole: naming.RolePrimary,
159-
}
159+
endpoints.Labels = naming.Merge(cluster.Spec.Metadata.Labels,
160+
map[string]string{
161+
naming.LabelCluster: cluster.Name,
162+
naming.LabelRole: naming.RolePrimary,
163+
})
160164

161165
// Resolve to the ClusterIP for which Patroni has configured the Endpoints.
162166
endpoints.Subsets = []v1.EndpointSubset{{
@@ -246,10 +250,11 @@ func (r *Reconciler) reconcilePGUserSecret(
246250
intent.Data["uri"] = []byte(connectionString)
247251

248252
// set postgrescluster label
249-
intent.Labels = map[string]string{
250-
naming.LabelCluster: cluster.Name,
251-
naming.LabelUserSecret: cluster.Name,
252-
}
253+
intent.Labels = naming.Merge(cluster.Spec.Metadata.Labels,
254+
map[string]string{
255+
naming.LabelCluster: cluster.Name,
256+
naming.LabelUserSecret: cluster.Name,
257+
})
253258

254259
err = errors.WithStack(r.setControllerReference(cluster, intent))
255260

0 commit comments

Comments
 (0)