Skip to content

Commit 0387830

Browse files
committed
Accumulate service annotations
First, global config, then ServiceAnnotations overriding, then MasterServiceAnnotations and ReplicaServiceAnnotations. This addresses zalando#2161 (comment).
1 parent 3725471 commit 0387830

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

pkg/cluster/cluster_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,13 +672,15 @@ func TestServiceAnnotations(t *testing.T) {
672672
enableTeamIdClusterPrefix: false,
673673
operatorAnnotations: make(map[string]string),
674674
serviceAnnotations: map[string]string{
675+
"service.beta.kubernetes.io/aws-load-balancer-nlb-target-type": "ip",
675676
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "1800",
676677
},
677678
masterServiceAnnotations: map[string]string{
678679
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "2000",
679680
},
680681
expect: map[string]string{
681682
"external-dns.alpha.kubernetes.io/hostname": "acid-test.test.db.example.com,test.acid.db.example.com",
683+
"service.beta.kubernetes.io/aws-load-balancer-nlb-target-type": "ip",
682684
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "2000",
683685
},
684686
},
@@ -825,13 +827,15 @@ func TestServiceAnnotations(t *testing.T) {
825827
enableTeamIdClusterPrefix: false,
826828
operatorAnnotations: make(map[string]string),
827829
serviceAnnotations: map[string]string{
830+
"service.beta.kubernetes.io/aws-load-balancer-nlb-target-type": "ip",
828831
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "1800",
829832
},
830833
replicaServiceAnnotations: map[string]string{
831834
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "2000",
832835
},
833836
expect: map[string]string{
834837
"external-dns.alpha.kubernetes.io/hostname": "acid-test-repl.test.db.example.com,test-repl.acid.db.example.com",
838+
"service.beta.kubernetes.io/aws-load-balancer-nlb-target-type": "ip",
835839
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "2000",
836840
},
837841
},

pkg/cluster/k8sres.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,32 +1906,17 @@ func (c *Cluster) generateServiceAnnotations(role PostgresRole, spec *acidv1.Pos
19061906
annotations[k] = v
19071907
}
19081908

1909-
var specServiceAnnotations map[string]string
19101909
if spec != nil {
1910+
annotations = mergeAnnotations(annotations, spec.ServiceAnnotations)
1911+
19111912
switch role {
19121913
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-
}
1914+
annotations = mergeAnnotations(annotations, spec.MasterServiceAnnotations)
19191915
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
1916+
annotations = mergeAnnotations(annotations, spec.ReplicaServiceAnnotations)
19281917
}
19291918
}
19301919

1931-
for k, v := range specServiceAnnotations {
1932-
annotations[k] = v
1933-
}
1934-
19351920
if c.shouldCreateLoadBalancerForService(role, spec) {
19361921
dnsName := c.dnsName(role)
19371922

@@ -2401,3 +2386,18 @@ func ensurePath(file string, defaultDir string, defaultFile string) string {
24012386
}
24022387
return file
24032388
}
2389+
2390+
// mergeAnnotations merged annotations that entries in b override the ones in a.
2391+
func mergeAnnotations(a, b map[string]string) map[string]string {
2392+
result := make(map[string]string)
2393+
2394+
for k, v := range a {
2395+
result[k] = v
2396+
}
2397+
2398+
for k, v := range b {
2399+
result[k] = v
2400+
}
2401+
2402+
return result
2403+
}

0 commit comments

Comments
 (0)