From 19f2fabe7a05f95ea136c631401f3880c557c41d Mon Sep 17 00:00:00 2001 From: Johnathan Kupferer Date: Mon, 15 Jul 2024 10:11:12 -0400 Subject: [PATCH] Add pool manager daemon --- kopf-opt.sh | 2 +- operator/operator.py | 35 +++++++++++++++++++++++++++++++++++ operator/poolboy.py | 1 + 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/kopf-opt.sh b/kopf-opt.sh index 81de9b6..c5c8607 100644 --- a/kopf-opt.sh +++ b/kopf-opt.sh @@ -1,5 +1,5 @@ #!/bin/sh -KOPF_OPTIONS="--log-format=json" +KOPF_OPTIONS="${KOPF_OPTIONS:---log-format=json}" # Restrict watch to operator namespace. KOPF_NAMESPACED=false diff --git a/operator/operator.py b/operator/operator.py index 8ce280e..0e44f96 100755 --- a/operator/operator.py +++ b/operator/operator.py @@ -335,6 +335,41 @@ async def resource_pool_delete( ) await resource_pool.handle_delete(logger=logger) +@kopf.daemon(Poolboy.operator_domain, Poolboy.operator_version, 'resourcepools', + cancellation_timeout = 1, + initial_delay = Poolboy.manage_pools_interval, + labels = {Poolboy.ignore_label: kopf.ABSENT}, +) +async def resource_pool_daemon( + annotations: kopf.Annotations, + labels: kopf.Labels, + logger: kopf.ObjectLogger, + meta: kopf.Meta, + name: str, + namespace: str, + spec: kopf.Spec, + status: kopf.Status, + stopped: Optional[datetime], + uid: str, + **_ +): + resource_pool = await ResourcePool.register( + annotations = annotations, + labels = labels, + meta = meta, + name = name, + namespace = namespace, + spec = spec, + status = status, + uid = uid, + ) + try: + while not stopped: + await resource_pool.manage(logger=logger) + await asyncio.sleep(Poolboy.manage_pools_interval) + except asyncio.CancelledError: + pass + @kopf.on.event(Poolboy.operator_domain, Poolboy.operator_version, 'resourceproviders') async def resource_provider_event(event: Mapping, logger: kopf.ObjectLogger, **_) -> None: diff --git a/operator/poolboy.py b/operator/poolboy.py index 796decc..6ec658f 100644 --- a/operator/poolboy.py +++ b/operator/poolboy.py @@ -4,6 +4,7 @@ class Poolboy(): manage_claims_interval = int(os.environ.get('MANAGE_CLAIMS_INTERVAL', 60)) manage_handles_interval = int(os.environ.get('MANAGE_HANDLES_INTERVAL', 60)) + manage_pools_interval = int(os.environ.get('MANAGE_POOLS_INTERVAL', 10)) operator_domain = os.environ.get('OPERATOR_DOMAIN', 'poolboy.gpte.redhat.com') operator_version = os.environ.get('OPERATOR_VERSION', 'v1') operator_api_version = f"{operator_domain}/{operator_version}"