Skip to content

Commit

Permalink
Force recreation of resources on the startup of the operator (#17)
Browse files Browse the repository at this point in the history
Co-authored-by: Kaushal Vora <[email protected]>
  • Loading branch information
kaushall and Kaushal Vora authored Jul 16, 2024
1 parent 0b28756 commit 3c277c1
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions tools/nopo11y-operator/get_services_deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
}
}
O11Y_NAMEPSACE = str(os.getenv("NOPO11Y_STACK_NAMESPACE", "observability"))
FORCE_RECREATE = False

GROUP = "znsio.nopo11y.com"
VERSION = "v1alpha"
Expand Down Expand Up @@ -119,20 +120,26 @@ def get_service_deployment_map(service=None):
@kopf.on.startup()
def configure(settings: kopf.OperatorSettings, **_):
""" This function overwrites default kopf operator configuration """
global FORCE_RECREATE
settings.watching.client_timeout = 4 * 60

# At every startup of the operator update all the existing resources using CRDs
FORCE_RECREATE = True


@kopf.on.create(GROUP, VERSION, COMPONENTS_PLURAL)
@kopf.on.update(GROUP, VERSION, COMPONENTS_PLURAL)
@kopf.on.resume(GROUP, VERSION, COMPONENTS_PLURAL)
def generate_dashboard_alerts(spec, namespace, old, new, **kwargs):
""" This function will generate dashboards, slos and alert rules for your service """
global FORCE_RECREATE

old = old if isinstance(old, dict) and old else {}
new = new if isinstance(new, dict) and new else {}

spec_dict = dict(spec)
old_spec, new_spec = transform_spec(old, new)
if old_spec == new_spec:
if old_spec == new_spec and not FORCE_RECREATE:
logger.info("There is no change in specs, skipping")
return

Expand All @@ -159,7 +166,7 @@ def generate_dashboard_alerts(spec, namespace, old, new, **kwargs):
for service in spec_dict.get("services", []):
service_name = service.get("serviceName")
if new_spec.get(service_name) == old_spec.get(service_name) and \
old_spec.get("defaults") == new_spec.get("defaults"):
old_spec.get("defaults") == new_spec.get("defaults") and not FORCE_RECREATE:
logger.info(f"Found no changes for following service: {service_name}")
continue

Expand Down Expand Up @@ -240,6 +247,8 @@ def generate_dashboard_alerts(spec, namespace, old, new, **kwargs):

logger.info(f"Updated resources for following service: {service_name}")

FORCE_RECREATE = False


def update_custom_obj(group, version, name, namespace, plural, body, **kwargs):
""" This will patch the custom object in the k8s cluster """
Expand Down

0 comments on commit 3c277c1

Please sign in to comment.