From 8011e9a9e646bb3f7dbfe83348fc453a3ed76762 Mon Sep 17 00:00:00 2001 From: Raphael Torquato <89878688+raphaeltorquat0@users.noreply.github.com> Date: Sun, 26 Apr 2026 13:52:40 -0300 Subject: [PATCH] feat: allow overriding external-dns hostname annotation This change allows users to override the external-dns.alpha.kubernetes.io/hostname annotation by specifying it in serviceAnnotations, masterServiceAnnotations, or replicaServiceAnnotations in the cluster manifest. Previously, the operator always overwrote this annotation with the value from master_dns_name_format or replica_dns_name_format. Now, if the user has already defined the annotation, the operator will preserve the user's value. Changes: - Modified generateServiceAnnotations() to check if annotation exists before setting - Updated tests to verify user-defined annotations are preserved - Updated documentation to reflect the new behavior Closes #2591 --- docs/administrator.md | 5 +++-- pkg/cluster/cluster_test.go | 12 ++++++------ pkg/cluster/k8sres.go | 9 +++++---- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/docs/administrator.md b/docs/administrator.md index e854775ce..e579d1518 100644 --- a/docs/administrator.md +++ b/docs/administrator.md @@ -896,8 +896,9 @@ its services: - `external-dns.alpha.kubernetes.io/hostname` with the value defined by the operator configs `master_dns_name_format` and `replica_dns_name_format`. - This value can't be overwritten. If any changing in its value is needed, it - MUST be done changing the DNS format operator config parameters; and + This value can be overwritten by specifying the annotation in + `serviceAnnotations`, `masterServiceAnnotations`, or `replicaServiceAnnotations` + in the cluster manifest; and There are multiple options to specify service annotations that will be merged with each other and override in the following order (where latter take diff --git a/pkg/cluster/cluster_test.go b/pkg/cluster/cluster_test.go index 8046943d4..f08210e7f 100644 --- a/pkg/cluster/cluster_test.go +++ b/pkg/cluster/cluster_test.go @@ -767,16 +767,16 @@ func TestServiceAnnotations(t *testing.T) { }, }, { - about: "Master with cluster annotations do not override external-dns annotations", + about: "Master with user-defined external-dns annotation is preserved", role: "master", enableMasterLoadBalancerOC: true, enableTeamIdClusterPrefix: false, operatorAnnotations: make(map[string]string), serviceAnnotations: map[string]string{ - "external-dns.alpha.kubernetes.io/hostname": "wrong.external-dns-name.example.com", + "external-dns.alpha.kubernetes.io/hostname": "custom.user-defined.example.com", }, expect: map[string]string{ - "external-dns.alpha.kubernetes.io/hostname": "acid-test-stg.test.db.example.com,test-stg.acid.db.example.com", + "external-dns.alpha.kubernetes.io/hostname": "custom.user-defined.example.com", }, }, { @@ -916,16 +916,16 @@ func TestServiceAnnotations(t *testing.T) { }, }, { - about: "Replica with cluster annotations do not override external-dns annotations", + about: "Replica with user-defined external-dns annotation is preserved", role: "replica", enableReplicaLoadBalancerOC: true, enableTeamIdClusterPrefix: false, operatorAnnotations: make(map[string]string), serviceAnnotations: map[string]string{ - "external-dns.alpha.kubernetes.io/hostname": "wrong.external-dns-name.example.com", + "external-dns.alpha.kubernetes.io/hostname": "custom.user-defined-repl.example.com", }, expect: map[string]string{ - "external-dns.alpha.kubernetes.io/hostname": "acid-test-stg-repl.test.db.example.com,test-stg-repl.acid.db.example.com", + "external-dns.alpha.kubernetes.io/hostname": "custom.user-defined-repl.example.com", }, }, { diff --git a/pkg/cluster/k8sres.go b/pkg/cluster/k8sres.go index 2eb867f06..37113f15e 100644 --- a/pkg/cluster/k8sres.go +++ b/pkg/cluster/k8sres.go @@ -2027,10 +2027,11 @@ func (c *Cluster) generateServiceAnnotations(role PostgresRole, spec *acidv1.Pos annotations := c.getCustomServiceAnnotations(role, spec) if c.shouldCreateLoadBalancerForService(role, spec) { - dnsName := c.dnsName(role) - - // External DNS name annotation is not customizable - annotations[constants.ZalandoDNSNameAnnotation] = dnsName + // Only set the DNS annotation if not already defined by user in service annotations + if _, exists := annotations[constants.ZalandoDNSNameAnnotation]; !exists { + dnsName := c.dnsName(role) + annotations[constants.ZalandoDNSNameAnnotation] = dnsName + } } if len(annotations) == 0 {