Skip to content

Commit 53e2d14

Browse files
author
Alan Christie
committed
style: Better logging
1 parent 2428a40 commit 53e2d14

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

operator/handlers.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -262,47 +262,52 @@ def create(spec: Dict[str, Any], name: str, namespace: str, **_: Any) -> Dict[st
262262
core_api.create_namespaced_config_map(
263263
namespace, config_cm_body, _request_timeout=_REQUEST_TIMEOUT
264264
)
265+
logging.info("Created CONFIG ConfigMap %s", cm_name)
265266

267+
cm_name = f"bp-{name}"
266268
bp_cm_body = {
267269
"apiVersion": "v1",
268270
"kind": "ConfigMap",
269-
"metadata": {"name": f"bp-{name}", "labels": {"app": name}},
271+
"metadata": {"name": cm_name, "labels": {"app": name}},
270272
"data": {".bash_profile": _BASH_PROFILE},
271273
}
272274
kopf.adopt(bp_cm_body)
273275
try:
274276
core_api.create_namespaced_config_map(
275277
namespace, bp_cm_body, _request_timeout=_REQUEST_TIMEOUT
276278
)
279+
logging.info("Created BP ConfigMap (%s)", cm_name)
277280
except kubernetes.client.exceptions.ApiException as ex:
278281
if ex.status != 409 or ex.reason != "Conflict":
279282
raise ex
280283
# Warn, but ignore and return a valid 'create' response now.
281-
logging.warning(
282-
"Got ApiException [409/Conflict] creating BP ConfigMap. Ignoring"
284+
logging.info(
285+
"Got 409/Conflict creating BP ConfigMap %s. Ignoring - object already present",
286+
cm_name,
283287
)
284288

289+
cm_name = f"startup-{name}"
285290
startup_cm_body = {
286291
"apiVersion": "v1",
287292
"kind": "ConfigMap",
288-
"metadata": {"name": f"startup-{name}", "labels": {"app": name}},
293+
"metadata": {"name": cm_name, "labels": {"app": name}},
289294
"data": {"start.sh": _NOTEBOOK_STARTUP},
290295
}
291296
kopf.adopt(startup_cm_body)
292297
try:
293298
core_api.create_namespaced_config_map(
294299
namespace, startup_cm_body, _request_timeout=_REQUEST_TIMEOUT
295300
)
301+
logging.info("Created STARTUP ConfigMap (%s)", cm_name)
296302
except kubernetes.client.exceptions.ApiException as ex:
297303
if ex.status != 409 or ex.reason != "Conflict":
298304
raise ex
299305
# Warn, but ignore and return a valid 'create' response now.
300-
logging.warning(
301-
"Got ApiException [409/Conflict] creating STARTUP ConfigMap. Ignoring"
306+
logging.info(
307+
"Got 409/Conflict creating STARTUP ConfigMap %s. Ignoring - object already present",
308+
cm_name,
302309
)
303310

304-
logging.info("Created ConfigMaps")
305-
306311
# Deployment
307312
# ----------
308313

@@ -420,16 +425,16 @@ def create(spec: Dict[str, Any], name: str, namespace: str, **_: Any) -> Dict[st
420425
apps_api.create_namespaced_deployment(
421426
namespace, deployment_body, _request_timeout=_REQUEST_TIMEOUT
422427
)
428+
logging.info("Created Deployment %s", name)
423429
except kubernetes.client.exceptions.ApiException as ex:
424430
if ex.status != 409 or ex.reason != "Conflict":
425431
raise ex
426432
# Warn, but ignore and return a valid 'create' response now.
427-
logging.warning(
428-
"Got ApiException [409/Conflict] creating CONFIG ConfigMap. Ignoring"
433+
logging.info(
434+
"Got 409/Conflict creating Deployment %s. Ignoring - object already present",
435+
name,
429436
)
430437

431-
logging.info("Created deployment")
432-
433438
# Service
434439
# -------
435440

@@ -458,16 +463,16 @@ def create(spec: Dict[str, Any], name: str, namespace: str, **_: Any) -> Dict[st
458463
core_api.create_namespaced_service(
459464
namespace, service_body, _request_timeout=_REQUEST_TIMEOUT
460465
)
466+
logging.info("Created Service %s", name)
461467
except kubernetes.client.exceptions.ApiException as ex:
462468
if ex.status != 409 or ex.reason != "Conflict":
463469
raise ex
464470
# Warn, but ignore and return a valid 'create' response now.
465-
logging.warning(
466-
"Got ApiException [409/Conflict] creating CONFIG ConfigMap. Ignoring"
471+
logging.info(
472+
"Got 409/Conflict creating Service %s. Ignoring - object already present",
473+
name,
467474
)
468475

469-
logging.info("Created service")
470-
471476
# Ingress
472477
# -------
473478

@@ -518,18 +523,20 @@ def create(spec: Dict[str, Any], name: str, namespace: str, **_: Any) -> Dict[st
518523
ext_api.create_namespaced_ingress(
519524
namespace, ingress_body, _request_timeout=_REQUEST_TIMEOUT
520525
)
526+
logging.info("Created Ingress %s", name)
521527
except kubernetes.client.exceptions.ApiException as ex:
522528
if ex.status != 409 or ex.reason != "Conflict":
523529
raise ex
524530
# Warn, but ignore and return a valid 'create' response now.
525-
logging.warning(
526-
"Got ApiException [409/Conflict] creating CONFIG ConfigMap. Ignoring"
531+
logging.info(
532+
"Got 409/Conflict creating Ingress %s. Ignoring - object already present",
533+
name,
527534
)
528535

529-
logging.info("Created ingress")
530-
531536
# Done
532537
# ----
538+
logging.info("Success! (name=%s namespace=%s)", name, namespace)
539+
533540
return {
534541
"notebook": {
535542
"url": f"http://{ingress_domain}{ingress_path}?token={token}",

0 commit comments

Comments
 (0)