Skip to content

Commit 6cc8180

Browse files
YanniHu1996litaocdlgbartolini
authored
refactor: centralize labels and annotations (cloudnative-pg#2804)
Labels and annotations are now all defined in the `pkg/utils/labels_annotations.go` file, providing more clarity for developers and users. Closes cloudnative-pg#2749 Signed-off-by: YanniHu1996 <yantian.hu@enterprisedb.com> Signed-off-by: Tao Li <tao.li@enterprisedb.com> Signed-off-by: Gabriele Bartolini <gabriele.bartolini@enterprisedb.com> Co-authored-by: Tao Li <tao.li@enterprisedb.com> Co-authored-by: Gabriele Bartolini <gabriele.bartolini@enterprisedb.com>
1 parent 86bf4f6 commit 6cc8180

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+356
-257
lines changed

api/v1/cluster_types.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -2829,9 +2829,7 @@ func (cluster *Cluster) SetInheritedData(obj *metav1.ObjectMeta) {
28292829
// ShouldForceLegacyBackup if present takes a backup without passing the name argument even on barman version 3.3.0+.
28302830
// This is needed to test both backup system in the E2E suite
28312831
func (cluster *Cluster) ShouldForceLegacyBackup() bool {
2832-
const legacyBackupAnnotationName = "cnpg.io/forceLegacyBackup"
2833-
2834-
return cluster.Annotations[legacyBackupAnnotationName] == "true"
2832+
return cluster.Annotations[utils.LegacyBackupAnnotationName] == "true"
28352833
}
28362834

28372835
// GetSeccompProfile return the proper SeccompProfile set in the cluster for Pods and Containers

controllers/cluster_controller.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ func (r *ClusterReconciler) ensureHealthyPVCsAnnotation(
739739
)
740740
}
741741

742-
if pvc.Annotations[persistentvolumeclaim.StatusAnnotationName] == persistentvolumeclaim.StatusReady {
742+
if pvc.Annotations[utils.PVCStatusAnnotationName] == persistentvolumeclaim.StatusReady {
743743
continue
744744
}
745745

@@ -1102,7 +1102,7 @@ func (r *ClusterReconciler) mapNodeToClusters() handler.MapFunc {
11021102
err := r.List(ctx, &childPods,
11031103
client.MatchingFields{".spec.nodeName": node.Name},
11041104
client.MatchingLabels{
1105-
specs.ClusterRoleLabelName: specs.ClusterRoleLabelPrimary,
1105+
utils.ClusterRoleLabelName: specs.ClusterRoleLabelPrimary,
11061106
utils.PodRoleLabelName: string(utils.PodRoleInstance),
11071107
},
11081108
)

controllers/cluster_create.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func (r *ClusterReconciler) reconcilePoolerSecrets(ctx context.Context, cluster
264264
&clientCaSecret,
265265
certs.CertTypeClient,
266266
nil,
267-
map[string]string{specs.WatchedLabelName: "true"})
267+
map[string]string{utils.WatchedLabelName: "true"})
268268
if err != nil {
269269
return err
270270
}
@@ -628,7 +628,7 @@ func (r *ClusterReconciler) createOrPatchDefaultMetricsConfigmap(ctx context.Con
628628
Name: apiv1.DefaultMonitoringConfigMapName,
629629
Namespace: cluster.Namespace,
630630
Labels: map[string]string{
631-
specs.WatchedLabelName: "true",
631+
utils.WatchedLabelName: "true",
632632
},
633633
},
634634
Data: map[string]string{
@@ -715,7 +715,7 @@ func (r *ClusterReconciler) createOrPatchDefaultMetricsSecret(ctx context.Contex
715715
Name: apiv1.DefaultMonitoringSecretName,
716716
Namespace: cluster.Namespace,
717717
Labels: map[string]string{
718-
specs.WatchedLabelName: "true",
718+
utils.WatchedLabelName: "true",
719719
},
720720
},
721721
Data: map[string][]byte{
@@ -1138,7 +1138,7 @@ func (r *ClusterReconciler) ensureInstancesAreCreated(
11381138
for _, instancePVC := range instancePVCs {
11391139
// This should not happen. However, we put this guard here
11401140
// as an assertion to catch unexpected events.
1141-
pvcStatus := instancePVC.Annotations[persistentvolumeclaim.StatusAnnotationName]
1141+
pvcStatus := instancePVC.Annotations[utils.PVCStatusAnnotationName]
11421142
if pvcStatus != persistentvolumeclaim.StatusReady {
11431143
contextLogger.Info("Selected PVC is not ready yet, waiting for 1 second",
11441144
"pvc", instancePVC.Name,
@@ -1163,11 +1163,11 @@ func (r *ClusterReconciler) ensureInstancesAreCreated(
11631163
}
11641164

11651165
// If this cluster has been restarted, mark the Pod with the latest restart time
1166-
if clusterRestart, ok := cluster.Annotations[specs.ClusterRestartAnnotationName]; ok {
1166+
if clusterRestart, ok := cluster.Annotations[utils.ClusterRestartAnnotationName]; ok {
11671167
if instanceToCreate.Annotations == nil {
11681168
instanceToCreate.Annotations = make(map[string]string)
11691169
}
1170-
instanceToCreate.Annotations[specs.ClusterRestartAnnotationName] = clusterRestart
1170+
instanceToCreate.Annotations[utils.ClusterRestartAnnotationName] = clusterRestart
11711171
}
11721172

11731173
contextLogger.Info("Creating new Pod to reattach a PVC",

controllers/cluster_predicates.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"sigs.k8s.io/controller-runtime/pkg/event"
2323
"sigs.k8s.io/controller-runtime/pkg/predicate"
2424

25-
"github.com/cloudnative-pg/cloudnative-pg/pkg/specs"
25+
"github.com/cloudnative-pg/cloudnative-pg/pkg/utils"
2626
)
2727

2828
var (
@@ -97,6 +97,6 @@ func isOwnedByClusterOrSatisfiesPredicate(
9797
}
9898

9999
func hasReloadLabelSet(obj client.Object) bool {
100-
_, hasLabel := obj.GetLabels()[specs.WatchedLabelName]
100+
_, hasLabel := obj.GetLabels()[utils.WatchedLabelName]
101101
return hasLabel
102102
}

controllers/cluster_restore.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func getOrphanPVCs(
136136
"pvcName", pvc.Name)
137137
continue
138138
}
139-
if _, ok := pvc.Annotations[specs.ClusterSerialAnnotationName]; !ok {
139+
if _, ok := pvc.Annotations[utils.ClusterSerialAnnotationName]; !ok {
140140
contextLogger.Warning("skipping pvc because it doesn't have serial annotation",
141141
"pvcName", pvc.Name)
142142
continue
@@ -162,7 +162,7 @@ func getNodeSerialsFromPVCs(
162162
if serial > highestSerial {
163163
highestSerial = serial
164164
}
165-
if pvc.ObjectMeta.Labels[specs.ClusterRoleLabelName] == specs.ClusterRoleLabelPrimary {
165+
if pvc.ObjectMeta.Labels[utils.ClusterRoleLabelName] == specs.ClusterRoleLabelPrimary {
166166
primarySerial = serial
167167
}
168168
}
@@ -185,7 +185,7 @@ func restoreOrphanPVCs(
185185

186186
pvcOrig := pvc.DeepCopy()
187187
cluster.SetInheritedDataAndOwnership(&pvc.ObjectMeta)
188-
pvc.Annotations[persistentvolumeclaim.StatusAnnotationName] = persistentvolumeclaim.StatusReady
188+
pvc.Annotations[utils.PVCStatusAnnotationName] = persistentvolumeclaim.StatusReady
189189
// we clean hibernation metadata if it exists
190190
delete(pvc.Annotations, utils.HibernateClusterManifestAnnotationName)
191191
delete(pvc.Annotations, utils.HibernatePgControlDataAnnotationName)

controllers/cluster_status.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func (r *ClusterReconciler) setPVCStatusReady(
235235
) error {
236236
contextLogger := log.FromContext(ctx)
237237

238-
if pvc.Annotations[persistentvolumeclaim.StatusAnnotationName] == persistentvolumeclaim.StatusReady {
238+
if pvc.Annotations[utils.PVCStatusAnnotationName] == persistentvolumeclaim.StatusReady {
239239
return nil
240240
}
241241

@@ -246,7 +246,7 @@ func (r *ClusterReconciler) setPVCStatusReady(
246246
if pvc.Annotations == nil {
247247
pvc.Annotations = make(map[string]string, 1)
248248
}
249-
pvc.Annotations[persistentvolumeclaim.StatusAnnotationName] = persistentvolumeclaim.StatusReady
249+
pvc.Annotations[utils.PVCStatusAnnotationName] = persistentvolumeclaim.StatusReady
250250

251251
return r.Patch(ctx, pvc, client.MergeFrom(oldPvc))
252252
}

controllers/cluster_upgrade.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,14 @@ func (r *ClusterReconciler) updateRestartAnnotation(
195195
primaryPod corev1.Pod,
196196
) error {
197197
contextLogger := log.FromContext(ctx)
198-
if clusterRestart, ok := cluster.Annotations[specs.ClusterRestartAnnotationName]; ok &&
199-
(primaryPod.Annotations == nil || primaryPod.Annotations[specs.ClusterRestartAnnotationName] != clusterRestart) {
200-
contextLogger.Info("Setting restart annotation on primary pod as needed", "label", specs.ClusterReloadAnnotationName)
198+
if clusterRestart, ok := cluster.Annotations[utils.ClusterRestartAnnotationName]; ok &&
199+
(primaryPod.Annotations == nil || primaryPod.Annotations[utils.ClusterRestartAnnotationName] != clusterRestart) {
200+
contextLogger.Info("Setting restart annotation on primary pod as needed", "label", utils.ClusterRestartAnnotationName)
201201
original := primaryPod.DeepCopy()
202202
if primaryPod.Annotations == nil {
203203
primaryPod.Annotations = make(map[string]string)
204204
}
205-
primaryPod.Annotations[specs.ClusterRestartAnnotationName] = clusterRestart
205+
primaryPod.Annotations[utils.ClusterRestartAnnotationName] = clusterRestart
206206
if err := r.Client.Patch(ctx, &primaryPod, client.MergeFrom(original)); err != nil {
207207
return err
208208
}
@@ -484,8 +484,8 @@ func checkClusterHasNewerRestartAnnotation(
484484
// If the cluster has been restarted and we are working with a Pod
485485
// which has not been restarted yet, or restarted at a different
486486
// time, let's restart it.
487-
if clusterRestart, ok := cluster.Annotations[specs.ClusterRestartAnnotationName]; ok {
488-
podRestart := status.Pod.Annotations[specs.ClusterRestartAnnotationName]
487+
if clusterRestart, ok := cluster.Annotations[utils.ClusterRestartAnnotationName]; ok {
488+
podRestart := status.Pod.Annotations[utils.ClusterRestartAnnotationName]
489489
if clusterRestart != podRestart {
490490
return rollout{
491491
required: true,

controllers/cluster_upgrade_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ var _ = Describe("Pod upgrade", Ordered, func() {
9898
pod := specs.PodWithExistingStorage(cluster, 1)
9999
clusterRestart := cluster
100100
clusterRestart.Annotations = make(map[string]string)
101-
clusterRestart.Annotations[specs.ClusterRestartAnnotationName] = "now"
101+
clusterRestart.Annotations[utils.ClusterRestartAnnotationName] = "now"
102102

103103
status := postgres.PostgresqlStatus{
104104
Pod: pod,

controllers/pooler_predicates_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"k8s.io/apimachinery/pkg/util/rand"
2323
"sigs.k8s.io/controller-runtime/pkg/client"
2424

25-
"github.com/cloudnative-pg/cloudnative-pg/pkg/specs"
2625
"github.com/cloudnative-pg/cloudnative-pg/pkg/utils"
2726

2827
. "github.com/onsi/ginkgo/v2"
@@ -48,7 +47,7 @@ var _ = Describe("pooler_predicates unit tests", func() {
4847
Name: rand.String(10),
4948
Namespace: namespace,
5049
Labels: map[string]string{
51-
specs.WatchedLabelName: "true",
50+
utils.WatchedLabelName: "true",
5251
},
5352
},
5453
}

controllers/pooler_update.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/cloudnative-pg/cloudnative-pg/internal/configuration"
3232
"github.com/cloudnative-pg/cloudnative-pg/pkg/management/log"
3333
"github.com/cloudnative-pg/cloudnative-pg/pkg/specs/pgbouncer"
34+
"github.com/cloudnative-pg/cloudnative-pg/pkg/utils"
3435
"github.com/cloudnative-pg/cloudnative-pg/pkg/utils/hash"
3536
)
3637

@@ -90,7 +91,7 @@ func (r *PoolerReconciler) updateDeployment(
9091
return nil
9192

9293
case resources.Deployment != nil:
93-
currentVersion := resources.Deployment.Annotations[pgbouncer.PgbouncerPoolerSpecHash]
94+
currentVersion := resources.Deployment.Annotations[utils.PoolerSpecHashAnnotationName]
9495
updatedVersion, err := hash.ComputeHash(pooler.Spec)
9596
if err != nil {
9697
return err
@@ -107,7 +108,7 @@ func (r *PoolerReconciler) updateDeployment(
107108
if updatedDeployment.Annotations == nil {
108109
updatedDeployment.Annotations = make(map[string]string)
109110
}
110-
updatedDeployment.Annotations[pgbouncer.PgbouncerPoolerSpecHash] = updatedVersion
111+
updatedDeployment.Annotations[utils.PoolerSpecHashAnnotationName] = updatedVersion
111112

112113
contextLog.Info("Updating deployment")
113114
err = r.Patch(ctx, updatedDeployment, client.MergeFrom(resources.Deployment))

controllers/pooler_update_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
"github.com/cloudnative-pg/cloudnative-pg/internal/configuration"
3030
"github.com/cloudnative-pg/cloudnative-pg/pkg/specs/pgbouncer"
31+
"github.com/cloudnative-pg/cloudnative-pg/pkg/utils"
3132

3233
. "github.com/onsi/ginkgo/v2"
3334
. "github.com/onsi/gomega"
@@ -76,8 +77,8 @@ var _ = Describe("unit test of pooler_update reconciliation logic", func() {
7677

7778
afterDep := getPoolerDeployment(ctx, pooler)
7879
Expect(beforeDep.ResourceVersion).To(Equal(afterDep.ResourceVersion))
79-
Expect(beforeDep.Annotations[pgbouncer.PgbouncerPoolerSpecHash]).
80-
To(Equal(afterDep.Annotations[pgbouncer.PgbouncerPoolerSpecHash]))
80+
Expect(beforeDep.Annotations[utils.PoolerSpecHashAnnotationName]).
81+
To(Equal(afterDep.Annotations[utils.PoolerSpecHashAnnotationName]))
8182
})
8283

8384
By("making sure that the deployments gets updated if the pooler.spec changes", func() {
@@ -93,8 +94,8 @@ var _ = Describe("unit test of pooler_update reconciliation logic", func() {
9394
afterDep := getPoolerDeployment(ctx, poolerUpdate)
9495

9596
Expect(beforeDep.ResourceVersion).ToNot(Equal(afterDep.ResourceVersion))
96-
Expect(beforeDep.Annotations[pgbouncer.PgbouncerPoolerSpecHash]).
97-
ToNot(Equal(afterDep.Annotations[pgbouncer.PgbouncerPoolerSpecHash]))
97+
Expect(beforeDep.Annotations[utils.PoolerSpecHashAnnotationName]).
98+
ToNot(Equal(afterDep.Annotations[utils.PoolerSpecHashAnnotationName]))
9899
Expect(*afterDep.Spec.Replicas).To(Equal(instancesNumber))
99100
})
100101
})

controllers/replicas_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
2424
"github.com/cloudnative-pg/cloudnative-pg/pkg/postgres"
25-
"github.com/cloudnative-pg/cloudnative-pg/pkg/specs"
25+
"github.com/cloudnative-pg/cloudnative-pg/pkg/utils"
2626

2727
. "github.com/onsi/ginkgo/v2"
2828
. "github.com/onsi/gomega"
@@ -34,7 +34,7 @@ var _ = Describe("Sacrificial Pod detection", func() {
3434
Name: "car-1",
3535
Namespace: "default",
3636
Annotations: map[string]string{
37-
specs.ClusterSerialAnnotationName: "1",
37+
utils.ClusterSerialAnnotationName: "1",
3838
},
3939
},
4040
Status: corev1.PodStatus{
@@ -52,7 +52,7 @@ var _ = Describe("Sacrificial Pod detection", func() {
5252
Name: "car-2",
5353
Namespace: "default",
5454
Annotations: map[string]string{
55-
specs.ClusterSerialAnnotationName: "2",
55+
utils.ClusterSerialAnnotationName: "2",
5656
},
5757
},
5858
Status: corev1.PodStatus{
@@ -70,7 +70,7 @@ var _ = Describe("Sacrificial Pod detection", func() {
7070
Name: "foo",
7171
Namespace: "default",
7272
Annotations: map[string]string{
73-
specs.ClusterSerialAnnotationName: "3",
73+
utils.ClusterSerialAnnotationName: "3",
7474
},
7575
},
7676
Status: corev1.PodStatus{
@@ -88,7 +88,7 @@ var _ = Describe("Sacrificial Pod detection", func() {
8888
Name: "bar",
8989
Namespace: "default",
9090
Annotations: map[string]string{
91-
specs.ClusterSerialAnnotationName: "4",
91+
utils.ClusterSerialAnnotationName: "4",
9292
},
9393
},
9494
Status: corev1.PodStatus{

controllers/scheduledbackup_controller.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333

3434
apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
3535
"github.com/cloudnative-pg/cloudnative-pg/pkg/management/log"
36-
"github.com/cloudnative-pg/cloudnative-pg/pkg/specs"
3736
"github.com/cloudnative-pg/cloudnative-pg/pkg/utils"
3837
)
3938

@@ -42,11 +41,11 @@ const (
4241

4342
// ImmediateBackupLabelName label is applied to backups to tell if a backup
4443
// is immediate or not
45-
ImmediateBackupLabelName = specs.MetadataNamespace + "/immediateBackup"
44+
ImmediateBackupLabelName = utils.ImmediateBackupLabelName
4645

4746
// ParentScheduledBackupLabelName label is applied to backups to easily tell the scheduled backup
4847
// it was created from.
49-
ParentScheduledBackupLabelName = specs.MetadataNamespace + "/scheduled-backup"
48+
ParentScheduledBackupLabelName = utils.ParentScheduledBackupLabelName
5049
)
5150

5251
// ScheduledBackupReconciler reconciles a ScheduledBackup object
@@ -197,8 +196,8 @@ func createBackup(
197196
metadata.Labels = make(map[string]string)
198197
}
199198
metadata.Labels[utils.ClusterLabelName] = scheduledBackup.Spec.Cluster.Name
200-
metadata.Labels[ImmediateBackupLabelName] = strconv.FormatBool(immediate)
201-
metadata.Labels[ParentScheduledBackupLabelName] = scheduledBackup.GetName()
199+
metadata.Labels[utils.ImmediateBackupLabelName] = strconv.FormatBool(immediate)
200+
metadata.Labels[utils.ParentScheduledBackupLabelName] = scheduledBackup.GetName()
202201

203202
switch scheduledBackup.Spec.BackupOwnerReference {
204203
case "cluster":

0 commit comments

Comments
 (0)