Skip to content

Commit

Permalink
Merge branch 'main' into dev/K8SPG-175
Browse files Browse the repository at this point in the history
  • Loading branch information
pooknull authored Nov 2, 2023
2 parents 03839e0 + 8f6e051 commit f9f8255
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 77 deletions.
16 changes: 8 additions & 8 deletions internal/controller/postgrescluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func (r *Reconciler) reconcileClusterConfigMap(

clusterConfigMap.Annotations = naming.Merge(cluster.Spec.Metadata.GetAnnotationsOrNil())
clusterConfigMap.Labels = naming.Merge(cluster.Spec.Metadata.GetLabelsOrNil(),
map[string]string{
naming.WithPerconaLabels(map[string]string{
naming.LabelCluster: cluster.Name,
})
}, cluster.Name, ""))

if err == nil {
err = patroni.ClusterConfigMap(ctx, cluster, pgHBAs, pgParameters,
Expand All @@ -76,9 +76,9 @@ func (r *Reconciler) reconcileClusterPodService(

clusterPodService.Annotations = naming.Merge(cluster.Spec.Metadata.GetAnnotationsOrNil())
clusterPodService.Labels = naming.Merge(cluster.Spec.Metadata.GetLabelsOrNil(),
map[string]string{
naming.WithPerconaLabels(map[string]string{
naming.LabelCluster: cluster.Name,
})
}, cluster.Name, "pg"))

// Allocate no IP address (headless) and match any Pod with the cluster
// label, regardless of its readiness. Not particularly useful by itself, but
Expand Down Expand Up @@ -118,10 +118,10 @@ func (r *Reconciler) generateClusterPrimaryService(
cluster.Spec.Metadata.GetAnnotationsOrNil())
service.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
map[string]string{
naming.WithPerconaLabels(map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelRole: naming.RolePrimary,
})
}, cluster.Name, "pg"))

err := errors.WithStack(r.setControllerReference(cluster, service))

Expand Down Expand Up @@ -203,10 +203,10 @@ func (r *Reconciler) generateClusterReplicaService(
cluster.Spec.Metadata.GetAnnotationsOrNil())
service.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
map[string]string{
naming.WithPerconaLabels(map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelRole: naming.RoleReplica,
})
}, cluster.Name, "pg"))

// Allocate an IP address and let Kubernetes manage the Endpoints by
// selecting Pods with the Patroni replica role.
Expand Down
20 changes: 20 additions & 0 deletions internal/controller/postgrescluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,11 @@ kind: Service
assert.Assert(t, marshalMatches(service.ObjectMeta, `
creationTimestamp: null
labels:
app.kubernetes.io/component: pg
app.kubernetes.io/instance: pg5
app.kubernetes.io/managed-by: percona-postgresql-operator
app.kubernetes.io/name: percona-postgresql
app.kubernetes.io/part-of: percona-postgresql
postgres-operator.crunchydata.com/cluster: pg5
postgres-operator.crunchydata.com/role: primary
name: pg5-primary
Expand Down Expand Up @@ -650,6 +655,11 @@ kind: Endpoints
metadata:
creationTimestamp: null
labels:
app.kubernetes.io/component: pg
app.kubernetes.io/instance: pg5
app.kubernetes.io/managed-by: percona-postgresql-operator
app.kubernetes.io/name: percona-postgresql
app.kubernetes.io/part-of: percona-postgresql
postgres-operator.crunchydata.com/cluster: pg5
postgres-operator.crunchydata.com/role: primary
name: pg5-primary
Expand Down Expand Up @@ -739,6 +749,11 @@ kind: Service
assert.Assert(t, marshalMatches(service.ObjectMeta, `
creationTimestamp: null
labels:
app.kubernetes.io/component: pg
app.kubernetes.io/instance: pg2
app.kubernetes.io/managed-by: percona-postgresql-operator
app.kubernetes.io/name: percona-postgresql
app.kubernetes.io/part-of: percona-postgresql
postgres-operator.crunchydata.com/cluster: pg2
postgres-operator.crunchydata.com/role: replica
name: pg2-replicas
Expand Down Expand Up @@ -780,6 +795,11 @@ some: note

// Labels present in the metadata.
assert.Assert(t, marshalMatches(service.ObjectMeta.Labels, `
app.kubernetes.io/component: pg
app.kubernetes.io/instance: pg2
app.kubernetes.io/managed-by: percona-postgresql-operator
app.kubernetes.io/name: percona-postgresql
app.kubernetes.io/part-of: percona-postgresql
happy: label
postgres-operator.crunchydata.com/cluster: pg2
postgres-operator.crunchydata.com/role: replica
Expand Down
16 changes: 8 additions & 8 deletions internal/controller/postgrescluster/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1157,12 +1157,12 @@ func generateInstanceStatefulSetIntent(_ context.Context,
sts.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
spec.Metadata.GetLabelsOrNil(),
map[string]string{
naming.WithPerconaLabels(map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelInstanceSet: spec.Name,
naming.LabelInstance: sts.Name,
naming.LabelData: naming.DataPostgres,
})
}, cluster.Name, "pg"))
sts.Spec.Selector = &metav1.LabelSelector{
MatchLabels: map[string]string{
naming.LabelCluster: cluster.Name,
Expand All @@ -1177,12 +1177,12 @@ func generateInstanceStatefulSetIntent(_ context.Context,
sts.Spec.Template.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
spec.Metadata.GetLabelsOrNil(),
map[string]string{
naming.WithPerconaLabels(map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelInstanceSet: spec.Name,
naming.LabelInstance: sts.Name,
naming.LabelData: naming.DataPostgres,
})
}, cluster.Name, "pg"))

// Don't clutter the namespace with extra ControllerRevisions.
// The "controller-revision-hash" label still exists on the Pod.
Expand Down Expand Up @@ -1305,11 +1305,11 @@ func (r *Reconciler) reconcileInstanceConfigMap(
instanceConfigMap.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
spec.Metadata.GetLabelsOrNil(),
map[string]string{
naming.WithPerconaLabels(map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelInstanceSet: spec.Name,
naming.LabelInstance: instance.Name,
})
}, cluster.Name, "pg"))

if err == nil {
err = patroni.InstanceConfigMap(ctx, cluster, spec, instanceConfigMap)
Expand Down Expand Up @@ -1351,11 +1351,11 @@ func (r *Reconciler) reconcileInstanceCertificates(
instanceCerts.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
spec.Metadata.GetLabelsOrNil(),
map[string]string{
naming.WithPerconaLabels(map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelInstanceSet: spec.Name,
naming.LabelInstance: instance.Name,
})
}, cluster.Name, ""))

// This secret is holding certificates, but the "kubernetes.io/tls" type
// expects an *unencrypted* private key. We're also adding other values and
Expand Down
12 changes: 6 additions & 6 deletions internal/controller/postgrescluster/patroni.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ func (r *Reconciler) reconcilePatroniDistributedConfiguration(
cluster.Spec.Metadata.GetAnnotationsOrNil())
dcsService.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
map[string]string{
naming.WithPerconaLabels(map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelPatroni: naming.PatroniScope(cluster),
})
}, cluster.Name, ""))

// Allocate no IP address (headless) and create no Endpoints.
// - https://docs.k8s.io/concepts/services-networking/service/#headless-services
Expand Down Expand Up @@ -248,10 +248,10 @@ func (r *Reconciler) generatePatroniLeaderLeaseService(

// add our labels last so they aren't overwritten
service.Labels = naming.Merge(service.Labels,
map[string]string{
naming.WithPerconaLabels(map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelPatroni: naming.PatroniScope(cluster),
})
}, cluster.Name, ""))

// Allocate an IP address and/or node port and let Patroni manage the Endpoints.
// Patroni will ensure that they always route to the elected leader.
Expand Down Expand Up @@ -404,10 +404,10 @@ func (r *Reconciler) reconcileReplicationSecret(
cluster.Spec.Metadata.GetAnnotationsOrNil())
intent.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
map[string]string{
naming.WithPerconaLabels(map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelClusterCertificate: "replication-client-tls",
})
}, cluster.Name, ""))

// K8SPG-330: Keep this commented in case of conflicts.
// We don't want to delete TLS secrets on cluster deletion.
Expand Down
12 changes: 8 additions & 4 deletions internal/controller/postgrescluster/patroni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ kind: Service
assert.Assert(t, marshalMatches(service.ObjectMeta, `
creationTimestamp: null
labels:
app.kubernetes.io/instance: pg2
app.kubernetes.io/managed-by: percona-postgresql-operator
app.kubernetes.io/name: percona-postgresql
app.kubernetes.io/part-of: percona-postgresql
postgres-operator.crunchydata.com/cluster: pg2
postgres-operator.crunchydata.com/patroni: pg2-ha
name: pg2-ha
Expand Down Expand Up @@ -116,11 +120,11 @@ ownerReferences:
})

// Labels present in the metadata.
assert.DeepEqual(t, service.ObjectMeta.Labels, map[string]string{
assert.DeepEqual(t, service.ObjectMeta.Labels, map[string]string(naming.WithPerconaLabels(map[string]string{
"b": "v2",
"postgres-operator.crunchydata.com/cluster": "pg2",
"postgres-operator.crunchydata.com/patroni": "pg2-ha",
})
}, "pg2", "")))

// Labels not in the selector.
assert.Assert(t, service.Spec.Selector == nil,
Expand All @@ -145,12 +149,12 @@ ownerReferences:
})

// Labels present in the metadata.
assert.DeepEqual(t, service.ObjectMeta.Labels, map[string]string{
assert.DeepEqual(t, service.ObjectMeta.Labels, map[string]string(naming.WithPerconaLabels(map[string]string{
"b": "v2",
"d": "v4",
"postgres-operator.crunchydata.com/cluster": "pg2",
"postgres-operator.crunchydata.com/patroni": "pg2-ha",
})
}, "pg2", "")))

// Labels not in the selector.
assert.Assert(t, service.Spec.Selector == nil,
Expand Down
18 changes: 9 additions & 9 deletions internal/controller/postgrescluster/pgadmin.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ func (r *Reconciler) generatePGAdminConfigMap(
configmap.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
cluster.Spec.UserInterface.PGAdmin.Metadata.GetLabelsOrNil(),
map[string]string{
naming.WithPerconaLabels(map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelRole: naming.RolePGAdmin,
})
}, cluster.Name, ""))

err := errors.WithStack(pgadmin.ConfigMap(cluster, configmap))
if err == nil {
Expand Down Expand Up @@ -148,10 +148,10 @@ func (r *Reconciler) generatePGAdminService(

// add our labels last so they aren't overwritten
service.Labels = naming.Merge(service.Labels,
map[string]string{
naming.WithPerconaLabels(map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelRole: naming.RolePGAdmin,
})
}, cluster.Name, ""))

// Allocate an IP address and/or node port and let Kubernetes manage the
// Endpoints by selecting Pods with the pgAdmin role.
Expand Down Expand Up @@ -255,11 +255,11 @@ func (r *Reconciler) reconcilePGAdminStatefulSet(
sts.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
cluster.Spec.UserInterface.PGAdmin.Metadata.GetLabelsOrNil(),
map[string]string{
naming.WithPerconaLabels(map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelRole: naming.RolePGAdmin,
naming.LabelData: naming.DataPGAdmin,
})
}, cluster.Name, ""))
sts.Spec.Selector = &metav1.LabelSelector{
MatchLabels: map[string]string{
naming.LabelCluster: cluster.Name,
Expand All @@ -272,11 +272,11 @@ func (r *Reconciler) reconcilePGAdminStatefulSet(
sts.Spec.Template.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
cluster.Spec.UserInterface.PGAdmin.Metadata.GetLabelsOrNil(),
map[string]string{
naming.WithPerconaLabels(map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelRole: naming.RolePGAdmin,
naming.LabelData: naming.DataPGAdmin,
})
}, cluster.Name, ""))

// if the shutdown flag is set, set pgAdmin replicas to 0
if cluster.Spec.Shutdown != nil && *cluster.Spec.Shutdown {
Expand Down Expand Up @@ -383,7 +383,7 @@ func (r *Reconciler) reconcilePGAdminDataVolume(
)
pvc.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
labelMap,
naming.WithPerconaLabels(labelMap, cluster.Name, ""),
)
pvc.Spec = cluster.Spec.UserInterface.PGAdmin.DataVolumeClaimSpec

Expand Down
28 changes: 22 additions & 6 deletions internal/controller/postgrescluster/pgadmin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ kind: ConfigMap
assert.Assert(t, cmp.MarshalMatches(configmap.ObjectMeta, `
creationTimestamp: null
labels:
app.kubernetes.io/instance: pg1
app.kubernetes.io/managed-by: percona-postgresql-operator
app.kubernetes.io/name: percona-postgresql
app.kubernetes.io/part-of: percona-postgresql
postgres-operator.crunchydata.com/cluster: pg1
postgres-operator.crunchydata.com/role: pgadmin
name: pg1-pgadmin
Expand Down Expand Up @@ -123,11 +127,11 @@ ownerReferences:
})

// Labels present in the metadata.
assert.DeepEqual(t, configmap.ObjectMeta.Labels, map[string]string{
assert.DeepEqual(t, configmap.ObjectMeta.Labels, map[string]string(naming.WithPerconaLabels(map[string]string{
"c": "v7", "d": "v4", "f": "v8",
"postgres-operator.crunchydata.com/cluster": "pg1",
"postgres-operator.crunchydata.com/role": "pgadmin",
})
}, "pg1", "")))
})
}

Expand Down Expand Up @@ -175,6 +179,10 @@ kind: Service
assert.Assert(t, marshalMatches(service.ObjectMeta, `
creationTimestamp: null
labels:
app.kubernetes.io/instance: my-cluster
app.kubernetes.io/managed-by: percona-postgresql-operator
app.kubernetes.io/name: percona-postgresql
app.kubernetes.io/part-of: percona-postgresql
postgres-operator.crunchydata.com/cluster: my-cluster
postgres-operator.crunchydata.com/role: pgadmin
name: my-cluster-pgadmin
Expand Down Expand Up @@ -213,11 +221,11 @@ ownerReferences:
})

// Labels present in the metadata.
assert.DeepEqual(t, service.ObjectMeta.Labels, map[string]string{
assert.DeepEqual(t, service.ObjectMeta.Labels, map[string]string(naming.WithPerconaLabels(map[string]string{
"b": "v2",
"postgres-operator.crunchydata.com/cluster": "my-cluster",
"postgres-operator.crunchydata.com/role": "pgadmin",
})
}, "my-cluster", "")))

// Labels not in the selector.
assert.DeepEqual(t, service.Spec.Selector, map[string]string{
Expand Down Expand Up @@ -245,12 +253,12 @@ ownerReferences:
})

// Labels present in the metadata.
assert.DeepEqual(t, service.ObjectMeta.Labels, map[string]string{
assert.DeepEqual(t, service.ObjectMeta.Labels, map[string]string(naming.WithPerconaLabels(map[string]string{
"b": "v2",
"d": "v4",
"postgres-operator.crunchydata.com/cluster": "my-cluster",
"postgres-operator.crunchydata.com/role": "pgadmin",
})
}, "my-cluster", "")))

// Labels not in the selector.
assert.DeepEqual(t, service.Spec.Selector, map[string]string{
Expand Down Expand Up @@ -516,6 +524,10 @@ func TestReconcilePGAdminStatefulSet(t *testing.T) {
assert.Assert(t, cmp.MarshalMatches(template.ObjectMeta, `
creationTimestamp: null
labels:
app.kubernetes.io/instance: test-cluster
app.kubernetes.io/managed-by: percona-postgresql-operator
app.kubernetes.io/name: percona-postgresql
app.kubernetes.io/part-of: percona-postgresql
postgres-operator.crunchydata.com/cluster: test-cluster
postgres-operator.crunchydata.com/data: pgadmin
postgres-operator.crunchydata.com/role: pgadmin
Expand Down Expand Up @@ -629,6 +641,10 @@ annotations:
annotation1: annotationvalue
creationTimestamp: null
labels:
app.kubernetes.io/instance: custom-cluster
app.kubernetes.io/managed-by: percona-postgresql-operator
app.kubernetes.io/name: percona-postgresql
app.kubernetes.io/part-of: percona-postgresql
label1: labelvalue
postgres-operator.crunchydata.com/cluster: custom-cluster
postgres-operator.crunchydata.com/data: pgadmin
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/postgrescluster/pgbackrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -1942,7 +1942,7 @@ func (r *Reconciler) reconcilePGBackRestSecret(ctx context.Context,
intent.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
cluster.Spec.Backups.PGBackRest.Metadata.GetLabelsOrNil(),
naming.PGBackRestConfigLabels(cluster.Name),
naming.WithPerconaLabels(naming.PGBackRestConfigLabels(cluster.Name), cluster.Name, ""),
)

existing := &corev1.Secret{}
Expand Down
Loading

0 comments on commit f9f8255

Please sign in to comment.