Skip to content

Commit 535517c

Browse files
runyontrFxKu
authored andcommitted
Custom annotations 329 (zalando#657)
* Add ability for custom annotations to database pods
1 parent 33e1d60 commit 535517c

File tree

15 files changed

+160
-13
lines changed

15 files changed

+160
-13
lines changed

charts/postgres-operator/values-crd.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ configKubernetes:
5353
cluster_domain: cluster.local
5454
# additional labels assigned to the cluster objects
5555
cluster_labels:
56-
application: spilo
56+
application: spilo
5757
# label assigned to Kubernetes objects created by the operator
5858
cluster_name_label: cluster-name
59+
# additional annotations to add to every database pod
60+
custom_pod_annotations:
5961
# toggles pod anti affinity on the Postgres pods
6062
enable_pod_antiaffinity: false
6163
# toggles PDB to set to MinAvailabe 0 or 1

charts/postgres-operator/values.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ configKubernetes:
5454
cluster_labels: application:spilo
5555
# label assigned to Kubernetes objects created by the operator
5656
cluster_name_label: version
57+
# annotations attached to each database pod
58+
# custom_pod_annotations: keya:valuea
5759
# toggles pod anti affinity on the Postgres pods
5860
enable_pod_antiaffinity: "false"
5961
# toggles PDB to set to MinAvailabe 0 or 1
@@ -127,8 +129,7 @@ configLoadBalancer:
127129
# DNS zone for cluster DNS name when load balancer is configured for cluster
128130
db_hosted_zone: db.example.com
129131
# annotations to apply to service when load balancing is enabled
130-
# custom_service_annotations:
131-
# "keyx:valuez,keya:valuea"
132+
# custom_service_annotations: "keyx:valuez,keya:valuea"
132133

133134
# toggles service type load balancer pointing to the master pod of the cluster
134135
enable_master_load_balancer: "true"

docs/reference/cluster_manifest.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ These parameters are grouped directly under the `spec` key in the manifest.
118118
then the default priority class is taken. The priority class itself must be
119119
defined in advance. Optional.
120120

121+
* **podAnnotations**
122+
A map of key value pairs that gets attached as [annotations](https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/)
123+
to each pod created for the database.
124+
125+
121126
* **enableShmVolume**
122127
Start a database pod without limitations on shm memory. By default docker
123128
limit `/dev/shm` to `64M` (see e.g. the [docker

docs/reference/operator_parameters.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ configuration they are grouped under the `kubernetes` key.
168168
Postgres pods are [terminated forcefully](https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods)
169169
after this timeout. The default is `5m`.
170170

171+
* **custom_pod_annotations**
172+
This key/value map provides a list of annotations that get attached to each pod
173+
of a database created by the operator. If the annotation key is also provided
174+
by the database definition, the database definition value is used.
175+
171176
* **watched_namespace**
172177
The operator watches for Postgres objects in the given namespace. If not
173178
specified, the value is taken from the operator namespace. A special `*`

manifests/complete-postgres-manifest.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ spec:
2525
- 127.0.0.1/32
2626
databases:
2727
foo: zalando
28-
28+
# podAnnotations:
29+
# annotation.key: value
2930
# Expert section
3031

3132
enableShmVolume: true

manifests/configmap.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ data:
1111
cluster_history_entries: "1000"
1212
cluster_labels: application:spilo
1313
cluster_name_label: version
14-
# custom_service_annotations:
15-
# "keyx:valuez,keya:valuea"
14+
# custom_service_annotations: "keyx:valuez,keya:valuea"
15+
# custom_pod_annotations: "keya:valuea"
1616
db_hosted_zone: db.example.com
1717
debug_logging: "true"
1818
# default_cpu_limit: "3"
@@ -37,7 +37,7 @@ data:
3737
# logical_backup_docker_image: "registry.opensource.zalan.do/acid/logical-backup"
3838
# logical_backup_s3_bucket: "my-bucket-url"
3939
# logical_backup_schedule: "30 00 * * *"
40-
master_dns_name_format: '{cluster}.{team}.staging.{hostedzone}'
40+
master_dns_name_format: "{cluster}.{team}.staging.{hostedzone}"
4141
# master_pod_move_timeout: 10m
4242
# max_instances: "-1"
4343
# min_instances: "-1"
@@ -60,13 +60,13 @@ data:
6060
ready_wait_interval: 3s
6161
ready_wait_timeout: 30s
6262
repair_period: 5m
63-
replica_dns_name_format: '{cluster}-repl.{team}.staging.{hostedzone}'
63+
replica_dns_name_format: "{cluster}-repl.{team}.staging.{hostedzone}"
6464
replication_username: standby
6565
resource_check_interval: 3s
6666
resource_check_timeout: 10m
6767
resync_period: 5m
6868
ring_log_lines: "100"
69-
secret_name_template: '{username}.{cluster}.credentials'
69+
secret_name_template: "{username}.{cluster}.credentials"
7070
# sidecar_docker_images: ""
7171
# set_memory_request_to_limit: "false"
7272
spilo_privileged: "false"

manifests/postgresql-operator-default-configuration.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ configuration:
2222
cluster_labels:
2323
application: spilo
2424
cluster_name_label: cluster-name
25+
# custom_pod_annotations:
26+
# keya: valuea
27+
# keyb: valueb
2528
enable_pod_antiaffinity: false
2629
enable_pod_disruption_budget: true
2730
# infrastructure_roles_secret_name: ""

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type KubernetesMetaConfiguration struct {
5959
InheritedLabels []string `json:"inherited_labels,omitempty"`
6060
ClusterNameLabel string `json:"cluster_name_label,omitempty"`
6161
NodeReadinessLabel map[string]string `json:"node_readiness_label,omitempty"`
62+
CustomPodAnnotations map[string]string `json:"custom_pod_annotations,omitempty"`
6263
// TODO: use a proper toleration structure?
6364
PodToleration map[string]string `json:"toleration,omitempty"`
6465
// TODO: use namespacedname

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type PostgresSpec struct {
5959
EnableLogicalBackup bool `json:"enableLogicalBackup,omitempty"`
6060
LogicalBackupSchedule string `json:"logicalBackupSchedule,omitempty"`
6161
StandbyCluster *StandbyDescription `json:"standby"`
62+
PodAnnotations map[string]string `json:"podAnnotations"`
6263

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

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,16 @@ var postgresqlList = []struct {
437437
PostgresqlList{},
438438
errors.New("unexpected end of JSON input")}}
439439

440+
var annotations = []struct {
441+
in []byte
442+
annotations map[string]string
443+
err error
444+
}{{
445+
in: []byte(`{"kind": "Postgresql","apiVersion": "acid.zalan.do/v1","metadata": {"name": "acid-testcluster1"}, "spec": {"podAnnotations": {"foo": "bar"},"teamId": "acid", "clone": {"cluster": "team-batman"}}}`),
446+
annotations: map[string]string{"foo": "bar"},
447+
err: nil},
448+
}
449+
440450
func mustParseTime(s string) metav1.Time {
441451
v, err := time.Parse("15:04", s)
442452
if err != nil {
@@ -482,6 +492,25 @@ func TestWeekdayTime(t *testing.T) {
482492
}
483493
}
484494

495+
func TestClusterAnnotations(t *testing.T) {
496+
for _, tt := range annotations {
497+
var cluster Postgresql
498+
err := cluster.UnmarshalJSON(tt.in)
499+
if err != nil {
500+
if tt.err == nil || err.Error() != tt.err.Error() {
501+
t.Errorf("Unable to marshal cluster with annotations: expected %v got %v", tt.err, err)
502+
}
503+
continue
504+
}
505+
for k, v := range cluster.Spec.PodAnnotations {
506+
found, expected := v, tt.annotations[k]
507+
if found != expected {
508+
t.Errorf("Didn't find correct value for key %v in for podAnnotations: Expected %v found %v", k, expected, found)
509+
}
510+
}
511+
}
512+
}
513+
485514
func TestClusterName(t *testing.T) {
486515
for _, tt := range clusterNames {
487516
name, err := extractClusterName(tt.in, tt.inTeam)

0 commit comments

Comments
 (0)