Skip to content

Commit

Permalink
Enable customization of Patroni Kubernetes labels. (#230)
Browse files Browse the repository at this point in the history
Add environment variables to customize names and values of labels used
by Patroni on Kubernets to locate its metadata. At least one of those
labels (cluster name) has different default value between Spilo
('version'), Patroni and the postgres-operator ('cluster-name').  This
change allows passing the operator label names directly to Spilo (and
Patroni), w/o relying on defaults to match.
  • Loading branch information
alexeyklyukin authored and CyberDem0n committed May 22, 2018
1 parent 33e7391 commit 1404d1c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions ENVIRONMENT.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ Environment Configuration Settings
- **WAL_GCS_BUCKET**: ditto for the Google Cloud Storage (WAL-E supports both S3 and GCS).
- **GOOGLE_APPLICATION_CREDENTIALS**: credentials for WAL-E when running in Google Cloud.
- **CALLBACK_SCRIPT**: the callback script to run on various cluster actions (on start, on stop, on restart, on role change). The script will receive the cluster name, connection string and the current action. See `Patroni <http://patroni.readthedocs.io/en/latest/SETTINGS.html?highlight=callback#postgresql>`__ documentation for details.
- **DCS_ENABLE_KUBERNETES_API**: a non-empty value forces Patroni to use Kubernetes as a DCS. Default is empty.
- **KUBERNETES_USE_CONFIGMAPS**: a non-empty value makes Patroni store its metadata in ConfigMaps instead of Endpoints when running on Kubernetes. Default is empty.
- **KUBERNETES_ROLE_LABEL**: name of the label containing Postgres role when running on Kubernetens. Default is 'spilo-role'.
- **KUBERNETES_SCOPE_LABEL**: name of the label containing cluster name. Default is 'version'.
- **KUBERNETES_LABELS**: a JSON describing names and values of other labels used by Patroni on Kubernetes to locate its metadata. Default is '{"application": "spilo"}'.
17 changes: 15 additions & 2 deletions postgres-appliance/configure_spilo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-

import argparse
import json
import logging
import re
import os
Expand All @@ -23,6 +24,7 @@
PROVIDER_LOCAL = "local"
PROVIDER_UNSUPPORTED = "unsupported"
USE_KUBERNETES = os.environ.get('KUBERNETES_SERVICE_HOST') is not None
KUBERNETES_DEFAULT_LABELS = '{"application": "spilo"}'
MEMORY_LIMIT_IN_BYTES_PATH = '/sys/fs/cgroup/memory/memory.limit_in_bytes'


Expand Down Expand Up @@ -369,6 +371,9 @@ def get_placeholders(provider):
placeholders.setdefault('PAM_OAUTH2', '')
placeholders.setdefault('CALLBACK_SCRIPT', '')
placeholders.setdefault('DCS_ENABLE_KUBERNETES_API', '')
placeholders.setdefault('KUBERNETES_ROLE_LABEL', 'spilo-role')
placeholders.setdefault('KUBERNETES_SCOPE_LABEL', 'version')
placeholders.setdefault('KUBERNETES_LABELS', KUBERNETES_DEFAULT_LABELS)
placeholders.setdefault('KUBERNETES_USE_CONFIGMAPS', '')
placeholders.setdefault('USE_PAUSE_AT_RECOVERY_TARGET', False)
placeholders.setdefault('CLONE_METHOD', '')
Expand Down Expand Up @@ -447,8 +452,16 @@ def pystache_render(*args, **kwargs):

def get_dcs_config(config, placeholders):
if USE_KUBERNETES and placeholders.get('DCS_ENABLE_KUBERNETES_API'):
config = {'kubernetes': {'role_label': 'spilo-role', 'scope_label': 'version',
'labels': {'application': 'spilo'}}}
try:
kubernetes_labels = json.loads(placeholders.get('KUBERNETES_LABELS'))
except (TypeError, ValueError) as e:
logging.warning("could not parse kubernetes labels as a JSON: {0}, "
"reverting to the default: {1}".format(e, KUBERNETES_DEFAULT_LABELS))
kubernetes_labels = json.loads(KUBERNETES_DEFAULT_LABELS)

config = {'kubernetes': {'role_label': placeholders.get('KUBERNETES_ROLE_LABEL'),
'scope_label': placeholders.get('KUBERNETES_SCOPE_LABEL'),
'labels': kubernetes_labels}}
if not placeholders.get('KUBERNETES_USE_CONFIGMAPS'):
config['kubernetes'].update({'use_endpoints': True, 'pod_ip': placeholders['instance_data']['ip'],
'ports': [{'port': 5432, 'name': 'postgresql'}]})
Expand Down

0 comments on commit 1404d1c

Please sign in to comment.