Skip to content

Commit e399665

Browse files
committed
Introduce masterServiceAnnotations & replicaServiceAnnotations
Introduce `masterServiceAnnotations` & `replicaServiceAnnotations` to the `Postgresql` CRD. `masterServiceAnnotations` overrides `serviceAnnotations` for master role if not empty. `replicaServiceAnnotations` overrides `serviceAnnotations` for replica role if not empty. Existing definition of `serviceAnnotations` continue to work for backward compatibitlity when neither `masterServiceAnnotations` nor `replicaServiceAnnotations` is defined. This closes zalando#1927
1 parent 3139c1f commit e399665

File tree

7 files changed

+138
-47
lines changed

7 files changed

+138
-47
lines changed

docs/reference/cluster_manifest.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ These parameters are grouped directly under the `spec` key in the manifest.
173173
[administrator docs](https://github.com/zalando/postgres-operator/blob/master/docs/administrator.md#load-balancers-and-allowed-ip-ranges)
174174
for more information regarding default values and overwrite rules.
175175

176+
* **masterServiceAnnotations**
177+
A map of key value pairs that gets attached as [annotations](https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/)
178+
to the master services created for the database cluster. Check the
179+
[administrator docs](https://github.com/zalando/postgres-operator/blob/master/docs/administrator.md#load-balancers-and-allowed-ip-ranges)
180+
for more information regarding default values and overwrite rules.
181+
This field takes precedence over `serviceAnnotations` for the master services if not empty.
182+
183+
* **replicaServiceAnnotations**
184+
A map of key value pairs that gets attached as [annotations](https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/)
185+
to the replica services created for the database cluster. Check the
186+
[administrator docs](https://github.com/zalando/postgres-operator/blob/master/docs/administrator.md#load-balancers-and-allowed-ip-ranges)
187+
for more information regarding default values and overwrite rules.
188+
This field takes precedence over `serviceAnnotations` for the replica services if not empty.
189+
176190
* **enableShmVolume**
177191
Start a database pod without limitations on shm memory. By default Docker
178192
limit `/dev/shm` to `64M` (see e.g. the [docker

pkg/apis/acid.zalan.do/v1/postgresql_type.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,14 @@ type PostgresSpec struct {
7979
StandbyCluster *StandbyDescription `json:"standby,omitempty"`
8080
PodAnnotations map[string]string `json:"podAnnotations,omitempty"`
8181
ServiceAnnotations map[string]string `json:"serviceAnnotations,omitempty"`
82-
TLS *TLSDescription `json:"tls,omitempty"`
83-
AdditionalVolumes []AdditionalVolume `json:"additionalVolumes,omitempty"`
84-
Streams []Stream `json:"streams,omitempty"`
85-
Env []v1.EnvVar `json:"env,omitempty"`
82+
// MasterServiceAnnotations takes precedence over ServiceAnnotations for master role if not empty
83+
MasterServiceAnnotations map[string]string `json:"masterServiceAnnotations,omitempty"`
84+
// ReplicaServiceAnnotations takes precedence over ServiceAnnotations for replica role if not empty
85+
ReplicaServiceAnnotations map[string]string `json:"replicaServiceAnnotations,omitempty"`
86+
TLS *TLSDescription `json:"tls,omitempty"`
87+
AdditionalVolumes []AdditionalVolume `json:"additionalVolumes,omitempty"`
88+
Streams []Stream `json:"streams,omitempty"`
89+
Env []v1.EnvVar `json:"env,omitempty"`
8690

8791
// deprecated json tags
8892
InitContainersOld []v1.Container `json:"init_containers,omitempty"`

pkg/apis/acid.zalan.do/v1/zz_generated.deepcopy.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cluster/cluster_test.go

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,9 @@ func TestServiceAnnotations(t *testing.T) {
524524
enableReplicaLoadBalancerOC bool
525525
enableTeamIdClusterPrefix bool
526526
operatorAnnotations map[string]string
527-
clusterAnnotations map[string]string
527+
serviceAnnotations map[string]string
528+
masterServiceAnnotations map[string]string
529+
replicaServiceAnnotations map[string]string
528530
expect map[string]string
529531
}{
530532
//MASTER
@@ -535,7 +537,7 @@ func TestServiceAnnotations(t *testing.T) {
535537
enableMasterLoadBalancerOC: false,
536538
enableTeamIdClusterPrefix: false,
537539
operatorAnnotations: make(map[string]string),
538-
clusterAnnotations: make(map[string]string),
540+
serviceAnnotations: make(map[string]string),
539541
expect: make(map[string]string),
540542
},
541543
{
@@ -545,7 +547,7 @@ func TestServiceAnnotations(t *testing.T) {
545547
enableMasterLoadBalancerOC: false,
546548
enableTeamIdClusterPrefix: false,
547549
operatorAnnotations: make(map[string]string),
548-
clusterAnnotations: make(map[string]string),
550+
serviceAnnotations: make(map[string]string),
549551
expect: map[string]string{
550552
"external-dns.alpha.kubernetes.io/hostname": "acid-test.test.db.example.com,test.acid.db.example.com",
551553
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "3600",
@@ -558,7 +560,7 @@ func TestServiceAnnotations(t *testing.T) {
558560
enableMasterLoadBalancerOC: true,
559561
enableTeamIdClusterPrefix: false,
560562
operatorAnnotations: make(map[string]string),
561-
clusterAnnotations: make(map[string]string),
563+
serviceAnnotations: make(map[string]string),
562564
expect: make(map[string]string),
563565
},
564566
{
@@ -567,7 +569,7 @@ func TestServiceAnnotations(t *testing.T) {
567569
enableMasterLoadBalancerOC: true,
568570
enableTeamIdClusterPrefix: false,
569571
operatorAnnotations: make(map[string]string),
570-
clusterAnnotations: make(map[string]string),
572+
serviceAnnotations: make(map[string]string),
571573
expect: map[string]string{
572574
"external-dns.alpha.kubernetes.io/hostname": "acid-test.test.db.example.com,test.acid.db.example.com",
573575
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "3600",
@@ -579,7 +581,7 @@ func TestServiceAnnotations(t *testing.T) {
579581
enableMasterLoadBalancerOC: true,
580582
enableTeamIdClusterPrefix: false,
581583
operatorAnnotations: make(map[string]string),
582-
clusterAnnotations: map[string]string{"foo": "bar"},
584+
serviceAnnotations: map[string]string{"foo": "bar"},
583585
expect: map[string]string{
584586
"external-dns.alpha.kubernetes.io/hostname": "acid-test.test.db.example.com,test.acid.db.example.com",
585587
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "3600",
@@ -593,7 +595,7 @@ func TestServiceAnnotations(t *testing.T) {
593595
enableMasterLoadBalancerOC: true,
594596
enableTeamIdClusterPrefix: false,
595597
operatorAnnotations: make(map[string]string),
596-
clusterAnnotations: map[string]string{"foo": "bar"},
598+
serviceAnnotations: map[string]string{"foo": "bar"},
597599
expect: map[string]string{"foo": "bar"},
598600
},
599601
{
@@ -602,7 +604,7 @@ func TestServiceAnnotations(t *testing.T) {
602604
enableMasterLoadBalancerOC: true,
603605
enableTeamIdClusterPrefix: false,
604606
operatorAnnotations: map[string]string{"foo": "bar"},
605-
clusterAnnotations: make(map[string]string),
607+
serviceAnnotations: make(map[string]string),
606608
expect: map[string]string{
607609
"external-dns.alpha.kubernetes.io/hostname": "acid-test.test.db.example.com,test.acid.db.example.com",
608610
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "3600",
@@ -617,7 +619,7 @@ func TestServiceAnnotations(t *testing.T) {
617619
operatorAnnotations: map[string]string{
618620
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "1800",
619621
},
620-
clusterAnnotations: make(map[string]string),
622+
serviceAnnotations: make(map[string]string),
621623
expect: map[string]string{
622624
"external-dns.alpha.kubernetes.io/hostname": "acid-test.test.db.example.com,test.acid.db.example.com",
623625
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "1800",
@@ -629,7 +631,7 @@ func TestServiceAnnotations(t *testing.T) {
629631
enableMasterLoadBalancerOC: true,
630632
enableTeamIdClusterPrefix: false,
631633
operatorAnnotations: make(map[string]string),
632-
clusterAnnotations: map[string]string{
634+
serviceAnnotations: map[string]string{
633635
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "1800",
634636
},
635637
expect: map[string]string{
@@ -643,7 +645,7 @@ func TestServiceAnnotations(t *testing.T) {
643645
enableMasterLoadBalancerOC: true,
644646
enableTeamIdClusterPrefix: false,
645647
operatorAnnotations: make(map[string]string),
646-
clusterAnnotations: map[string]string{
648+
serviceAnnotations: map[string]string{
647649
"external-dns.alpha.kubernetes.io/hostname": "wrong.external-dns-name.example.com",
648650
},
649651
expect: map[string]string{
@@ -656,13 +658,30 @@ func TestServiceAnnotations(t *testing.T) {
656658
role: "master",
657659
enableMasterLoadBalancerOC: true,
658660
enableTeamIdClusterPrefix: true,
659-
clusterAnnotations: make(map[string]string),
661+
serviceAnnotations: make(map[string]string),
660662
operatorAnnotations: make(map[string]string),
661663
expect: map[string]string{
662664
"external-dns.alpha.kubernetes.io/hostname": "acid-test.test.db.example.com,test.acid.db.example.com",
663665
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "3600",
664666
},
665667
},
668+
{
669+
about: "Master with master service annotations override service annotations",
670+
role: "master",
671+
enableMasterLoadBalancerOC: true,
672+
enableTeamIdClusterPrefix: false,
673+
operatorAnnotations: make(map[string]string),
674+
serviceAnnotations: map[string]string{
675+
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "1800",
676+
},
677+
masterServiceAnnotations: map[string]string{
678+
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "2000",
679+
},
680+
expect: map[string]string{
681+
"external-dns.alpha.kubernetes.io/hostname": "acid-test.test.db.example.com,test.acid.db.example.com",
682+
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "2000",
683+
},
684+
},
666685
// REPLICA
667686
{
668687
about: "Replica with no annotations and EnableReplicaLoadBalancer disabled on spec and OperatorConfig",
@@ -671,7 +690,7 @@ func TestServiceAnnotations(t *testing.T) {
671690
enableReplicaLoadBalancerOC: false,
672691
enableTeamIdClusterPrefix: false,
673692
operatorAnnotations: make(map[string]string),
674-
clusterAnnotations: make(map[string]string),
693+
serviceAnnotations: make(map[string]string),
675694
expect: make(map[string]string),
676695
},
677696
{
@@ -681,7 +700,7 @@ func TestServiceAnnotations(t *testing.T) {
681700
enableReplicaLoadBalancerOC: false,
682701
enableTeamIdClusterPrefix: false,
683702
operatorAnnotations: make(map[string]string),
684-
clusterAnnotations: make(map[string]string),
703+
serviceAnnotations: make(map[string]string),
685704
expect: map[string]string{
686705
"external-dns.alpha.kubernetes.io/hostname": "acid-test-repl.test.db.example.com,test-repl.acid.db.example.com",
687706
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "3600",
@@ -694,7 +713,7 @@ func TestServiceAnnotations(t *testing.T) {
694713
enableReplicaLoadBalancerOC: true,
695714
enableTeamIdClusterPrefix: false,
696715
operatorAnnotations: make(map[string]string),
697-
clusterAnnotations: make(map[string]string),
716+
serviceAnnotations: make(map[string]string),
698717
expect: make(map[string]string),
699718
},
700719
{
@@ -703,7 +722,7 @@ func TestServiceAnnotations(t *testing.T) {
703722
enableReplicaLoadBalancerOC: true,
704723
enableTeamIdClusterPrefix: false,
705724
operatorAnnotations: make(map[string]string),
706-
clusterAnnotations: make(map[string]string),
725+
serviceAnnotations: make(map[string]string),
707726
expect: map[string]string{
708727
"external-dns.alpha.kubernetes.io/hostname": "acid-test-repl.test.db.example.com,test-repl.acid.db.example.com",
709728
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "3600",
@@ -715,7 +734,7 @@ func TestServiceAnnotations(t *testing.T) {
715734
enableReplicaLoadBalancerOC: true,
716735
enableTeamIdClusterPrefix: false,
717736
operatorAnnotations: make(map[string]string),
718-
clusterAnnotations: map[string]string{"foo": "bar"},
737+
serviceAnnotations: map[string]string{"foo": "bar"},
719738
expect: map[string]string{
720739
"external-dns.alpha.kubernetes.io/hostname": "acid-test-repl.test.db.example.com,test-repl.acid.db.example.com",
721740
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "3600",
@@ -729,7 +748,7 @@ func TestServiceAnnotations(t *testing.T) {
729748
enableReplicaLoadBalancerOC: true,
730749
enableTeamIdClusterPrefix: false,
731750
operatorAnnotations: make(map[string]string),
732-
clusterAnnotations: map[string]string{"foo": "bar"},
751+
serviceAnnotations: map[string]string{"foo": "bar"},
733752
expect: map[string]string{"foo": "bar"},
734753
},
735754
{
@@ -738,7 +757,7 @@ func TestServiceAnnotations(t *testing.T) {
738757
enableReplicaLoadBalancerOC: true,
739758
enableTeamIdClusterPrefix: false,
740759
operatorAnnotations: map[string]string{"foo": "bar"},
741-
clusterAnnotations: make(map[string]string),
760+
serviceAnnotations: make(map[string]string),
742761
expect: map[string]string{
743762
"external-dns.alpha.kubernetes.io/hostname": "acid-test-repl.test.db.example.com,test-repl.acid.db.example.com",
744763
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "3600",
@@ -753,7 +772,7 @@ func TestServiceAnnotations(t *testing.T) {
753772
operatorAnnotations: map[string]string{
754773
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "1800",
755774
},
756-
clusterAnnotations: make(map[string]string),
775+
serviceAnnotations: make(map[string]string),
757776
expect: map[string]string{
758777
"external-dns.alpha.kubernetes.io/hostname": "acid-test-repl.test.db.example.com,test-repl.acid.db.example.com",
759778
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "1800",
@@ -765,7 +784,7 @@ func TestServiceAnnotations(t *testing.T) {
765784
enableReplicaLoadBalancerOC: true,
766785
enableTeamIdClusterPrefix: false,
767786
operatorAnnotations: make(map[string]string),
768-
clusterAnnotations: map[string]string{
787+
serviceAnnotations: map[string]string{
769788
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "1800",
770789
},
771790
expect: map[string]string{
@@ -779,7 +798,7 @@ func TestServiceAnnotations(t *testing.T) {
779798
enableReplicaLoadBalancerOC: true,
780799
enableTeamIdClusterPrefix: false,
781800
operatorAnnotations: make(map[string]string),
782-
clusterAnnotations: map[string]string{
801+
serviceAnnotations: map[string]string{
783802
"external-dns.alpha.kubernetes.io/hostname": "wrong.external-dns-name.example.com",
784803
},
785804
expect: map[string]string{
@@ -792,21 +811,38 @@ func TestServiceAnnotations(t *testing.T) {
792811
role: "replica",
793812
enableReplicaLoadBalancerOC: true,
794813
enableTeamIdClusterPrefix: true,
795-
clusterAnnotations: make(map[string]string),
814+
serviceAnnotations: make(map[string]string),
796815
operatorAnnotations: make(map[string]string),
797816
expect: map[string]string{
798817
"external-dns.alpha.kubernetes.io/hostname": "acid-test-repl.test.db.example.com,test-repl.acid.db.example.com",
799818
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "3600",
800819
},
801820
},
821+
{
822+
about: "Replica with replica service annotations override service annotations",
823+
role: "replica",
824+
enableReplicaLoadBalancerOC: true,
825+
enableTeamIdClusterPrefix: false,
826+
operatorAnnotations: make(map[string]string),
827+
serviceAnnotations: map[string]string{
828+
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "1800",
829+
},
830+
replicaServiceAnnotations: map[string]string{
831+
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "2000",
832+
},
833+
expect: map[string]string{
834+
"external-dns.alpha.kubernetes.io/hostname": "acid-test-repl.test.db.example.com,test-repl.acid.db.example.com",
835+
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "2000",
836+
},
837+
},
802838
// COMMON
803839
{
804840
about: "cluster annotations append to operator annotations",
805841
role: "replica",
806842
enableReplicaLoadBalancerOC: false,
807843
enableTeamIdClusterPrefix: false,
808844
operatorAnnotations: map[string]string{"foo": "bar"},
809-
clusterAnnotations: map[string]string{"post": "gres"},
845+
serviceAnnotations: map[string]string{"post": "gres"},
810846
expect: map[string]string{"foo": "bar", "post": "gres"},
811847
},
812848
{
@@ -815,7 +851,7 @@ func TestServiceAnnotations(t *testing.T) {
815851
enableReplicaLoadBalancerOC: false,
816852
enableTeamIdClusterPrefix: false,
817853
operatorAnnotations: map[string]string{"foo": "bar", "post": "gres"},
818-
clusterAnnotations: map[string]string{"post": "greSQL"},
854+
serviceAnnotations: map[string]string{"post": "greSQL"},
819855
expect: map[string]string{"foo": "bar", "post": "greSQL"},
820856
},
821857
}
@@ -833,7 +869,9 @@ func TestServiceAnnotations(t *testing.T) {
833869

834870
cl.Postgresql.Spec.ClusterName = ""
835871
cl.Postgresql.Spec.TeamID = "acid"
836-
cl.Postgresql.Spec.ServiceAnnotations = tt.clusterAnnotations
872+
cl.Postgresql.Spec.ServiceAnnotations = tt.serviceAnnotations
873+
cl.Postgresql.Spec.MasterServiceAnnotations = tt.masterServiceAnnotations
874+
cl.Postgresql.Spec.ReplicaServiceAnnotations = tt.replicaServiceAnnotations
837875
cl.Postgresql.Spec.EnableMasterLoadBalancer = tt.enableMasterLoadBalancerSpec
838876
cl.Postgresql.Spec.EnableReplicaLoadBalancer = tt.enableReplicaLoadBalancerSpec
839877

pkg/cluster/k8sres.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,12 +1905,33 @@ func (c *Cluster) generateServiceAnnotations(role PostgresRole, spec *acidv1.Pos
19051905
for k, v := range c.OpConfig.CustomServiceAnnotations {
19061906
annotations[k] = v
19071907
}
1908-
if spec != nil || spec.ServiceAnnotations != nil {
1909-
for k, v := range spec.ServiceAnnotations {
1910-
annotations[k] = v
1908+
1909+
var specServiceAnnotations map[string]string
1910+
if spec != nil {
1911+
switch role {
1912+
case Master:
1913+
// MasterServiceAnnotations take precedence over ServiceAnnotations if they are not empty
1914+
if len(spec.MasterServiceAnnotations) > 0 {
1915+
specServiceAnnotations = spec.MasterServiceAnnotations
1916+
} else {
1917+
specServiceAnnotations = spec.ServiceAnnotations
1918+
}
1919+
case Replica:
1920+
// ReplicaServiceAnnotations take precedence over ServiceAnnotations if they are not empty
1921+
if len(spec.ReplicaServiceAnnotations) > 0 {
1922+
specServiceAnnotations = spec.ReplicaServiceAnnotations
1923+
} else {
1924+
specServiceAnnotations = spec.ServiceAnnotations
1925+
}
1926+
default:
1927+
specServiceAnnotations = spec.ServiceAnnotations
19111928
}
19121929
}
19131930

1931+
for k, v := range specServiceAnnotations {
1932+
annotations[k] = v
1933+
}
1934+
19141935
if c.shouldCreateLoadBalancerForService(role, spec) {
19151936
dnsName := c.dnsName(role)
19161937

0 commit comments

Comments
 (0)