extras/postgres-hpa/kubernetes-manifests/pgpool-operator.yaml [106:171]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        global LOCK
        LOCK = asyncio.Lock()


    @kopf.on.startup()
    def configure(settings: kopf.OperatorSettings, **_):
        settings.posting.level = logging.WARNING
        settings.watching.connect_timeout = 1 * 60
        settings.watching.server_timeout = 10 * 60


    @kopf.on.probe(id='now')
    def get_current_timestamp(**kwargs):
        return datetime.datetime.utcnow().isoformat()


    @kopf.on.login()
    def login(**kwargs):
        global api

        conn = kopf.login_via_client(**kwargs)
        api = client.AppsV1Api()
        return conn


    def replicas_changed(old, new, **_):
        new_replicas = new.get('spec', {}).get('replicas', 0) if new else 0
        old_replicas = old.get('spec', {}).get('replicas', 0) if old else 0
        return new_replicas != old_replicas



    @kopf.on.update(kind="StatefulSet",
                    when=replicas_changed,
                    labels={
                        "app.kubernetes.io/component": "postgresql",
                        "app.kubernetes.io/instance": "accounts-db",
                    })
    def reconcile_backend_nodes(logger, namespace, new, **_):
        replicas = new.get('spec', {}).get('replicas', 0) if new else 0
        hosts = [
            f"{i}:accounts-db-postgresql-{i}.accounts-db-postgresql-headless:5432" \
            for i in range(replicas)
        ]

        def propagate_hostenv(envvar, hosts):
            if envvar.name == "PGPOOL_BACKEND_NODES":
                return {
                    "name": "PGPOOL_BACKEND_NODES",
                    "value": ",".join(hosts),
                }
            return envvar

        try:
            pgpool = api.read_namespaced_deployment(name="accounts-db-pgpool", namespace=namespace)
            for container in pgpool.spec.template.spec.containers:
                container.env = [propagate_hostenv(envvar, hosts) for envvar in container.env]

            api.patch_namespaced_deployment(
                name="accounts-db-pgpool",
                namespace=namespace,
                body=pgpool
            )
            logger.info("PGPool deployment updated")
        except ApiException as e:
            raise kopf.TemporaryError("Error when calling AppsV1Api: %s\n" % e, delay=60)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



