Skip to content

Commit c1d1005

Browse files
author
Alan Christie
committed
fix: 409 errors now handled while progress continues
1 parent 50763f6 commit c1d1005

File tree

1 file changed

+59
-18
lines changed

1 file changed

+59
-18
lines changed

operator/handlers.py

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
# As part of the startup we erase the existing '~/.bashrc' and,
7272
# as a minimum, set a more suitable PS1 (see ch2385).
7373
# 'conda init' then puts its stuff into the same file.
74-
_NOTEBOOK_STARTUP: str = """#!/bin/bash
74+
_NOTEBOOK_STARTUP: str = r"""#!/bin/bash
7575
echo "PS1='\$(pwd) \$UID$ '" > ~/.bashrc
7676
echo "umask 0002" >> ~/.bashrc
7777
conda init
@@ -251,7 +251,7 @@ def create(spec: Dict[str, Any], name: str, namespace: str, **_: Any) -> Dict[st
251251
# in kopf of kubernetes. For now, if the first object we create
252252
# already exists let us assume they all do?
253253
#
254-
# Added as a work-wround for sc-
254+
# Added as a work-around for sc-
255255
try:
256256
core_api.create_namespaced_config_map(
257257
namespace, bp_cm_body, _request_timeout=_REQUEST_TIMEOUT
@@ -263,13 +263,30 @@ def create(spec: Dict[str, Any], name: str, namespace: str, **_: Any) -> Dict[st
263263
logging.warning(
264264
"Got ApiException [409/Conflict] creating BP ConfigMap. Ignoring [#10]"
265265
)
266-
return create_response
267-
core_api.create_namespaced_config_map(
268-
namespace, startup_cm_body, _request_timeout=_REQUEST_TIMEOUT
269-
)
270-
core_api.create_namespaced_config_map(
271-
namespace, config_cm_body, _request_timeout=_REQUEST_TIMEOUT
272-
)
266+
267+
try:
268+
core_api.create_namespaced_config_map(
269+
namespace, startup_cm_body, _request_timeout=_REQUEST_TIMEOUT
270+
)
271+
except kubernetes.client.exceptions.ApiException as ex:
272+
if ex.status != 409 or ex.reason != "Conflict":
273+
raise ex
274+
# Warn, but ignore and return a valid 'create' response now.
275+
logging.warning(
276+
"Got ApiException [409/Conflict] creating STARTUP ConfigMap. Ignoring [#10]"
277+
)
278+
279+
try:
280+
core_api.create_namespaced_config_map(
281+
namespace, config_cm_body, _request_timeout=_REQUEST_TIMEOUT
282+
)
283+
except kubernetes.client.exceptions.ApiException as ex:
284+
if ex.status != 409 or ex.reason != "Conflict":
285+
raise ex
286+
# Warn, but ignore and return a valid 'create' response now.
287+
logging.warning(
288+
"Got ApiException [409/Conflict] creating CONFIG ConfigMap. Ignoring [#10]"
289+
)
273290

274291
logging.info("Created ConfigMaps")
275292

@@ -385,9 +402,17 @@ def create(spec: Dict[str, Any], name: str, namespace: str, **_: Any) -> Dict[st
385402

386403
kopf.adopt(deployment_body)
387404
apps_api = kubernetes.client.AppsV1Api()
388-
apps_api.create_namespaced_deployment(
389-
namespace, deployment_body, _request_timeout=_REQUEST_TIMEOUT
390-
)
405+
try:
406+
apps_api.create_namespaced_deployment(
407+
namespace, deployment_body, _request_timeout=_REQUEST_TIMEOUT
408+
)
409+
except kubernetes.client.exceptions.ApiException as ex:
410+
if ex.status != 409 or ex.reason != "Conflict":
411+
raise ex
412+
# Warn, but ignore and return a valid 'create' response now.
413+
logging.warning(
414+
"Got ApiException [409/Conflict] creating CONFIG ConfigMap. Ignoring [#10]"
415+
)
391416

392417
logging.info("Created deployment")
393418

@@ -415,9 +440,17 @@ def create(spec: Dict[str, Any], name: str, namespace: str, **_: Any) -> Dict[st
415440
}
416441

417442
kopf.adopt(service_body)
418-
core_api.create_namespaced_service(
419-
namespace, service_body, _request_timeout=_REQUEST_TIMEOUT
420-
)
443+
try:
444+
core_api.create_namespaced_service(
445+
namespace, service_body, _request_timeout=_REQUEST_TIMEOUT
446+
)
447+
except kubernetes.client.exceptions.ApiException as ex:
448+
if ex.status != 409 or ex.reason != "Conflict":
449+
raise ex
450+
# Warn, but ignore and return a valid 'create' response now.
451+
logging.warning(
452+
"Got ApiException [409/Conflict] creating CONFIG ConfigMap. Ignoring [#10]"
453+
)
421454

422455
logging.info("Created service")
423456

@@ -466,9 +499,17 @@ def create(spec: Dict[str, Any], name: str, namespace: str, **_: Any) -> Dict[st
466499

467500
kopf.adopt(ingress_body)
468501
ext_api = kubernetes.client.NetworkingV1Api()
469-
ext_api.create_namespaced_ingress(
470-
namespace, ingress_body, _request_timeout=_REQUEST_TIMEOUT
471-
)
502+
try:
503+
ext_api.create_namespaced_ingress(
504+
namespace, ingress_body, _request_timeout=_REQUEST_TIMEOUT
505+
)
506+
except kubernetes.client.exceptions.ApiException as ex:
507+
if ex.status != 409 or ex.reason != "Conflict":
508+
raise ex
509+
# Warn, but ignore and return a valid 'create' response now.
510+
logging.warning(
511+
"Got ApiException [409/Conflict] creating CONFIG ConfigMap. Ignoring [#10]"
512+
)
472513

473514
logging.info("Created ingress")
474515

0 commit comments

Comments
 (0)