Added new K8sFieldValueForbidden exception.

During tests it turns out, that we didn't catch Forbidden exceptions,
since there was no forbidden http code sent from kubernetes API. In this
Patch we introduce new exception K8sFieldValueForbidden, which will be
raised on 422 Unprocessable Entity, k8s API returns.

Also, taken care of objects in state terminating in remove_finalizer
method.

Closes-Bug: 1895124
Change-Id: If4ac93190db3a56ee6b94ca122bfd2e95c29ffb9
This commit is contained in:
Roman Dobosz 2020-09-10 12:47:43 +02:00 committed by Luis Tomas Bolivar
parent 7894021931
commit 5c855d9611
2 changed files with 13 additions and 3 deletions

View File

@ -58,6 +58,10 @@ class K8sUnprocessableEntity(K8sClientException):
"Unprocessable: %r" % message)
class K8sFieldValueForbidden(K8sUnprocessableEntity):
pass
class InvalidKuryrNetworkAnnotation(Exception):
pass

View File

@ -88,6 +88,12 @@ class K8sClient(object):
raise exc.K8sNamespaceTerminating(response.text)
raise exc.K8sForbidden(response.text)
if response.status_code == requests.codes.unprocessable_entity:
# NOTE(gryf): on k8s API code 422 is also Forbidden, but specified
# to FieldValueForbidden. Perhaps there are other usages for
# throwing unprocessable entity errors in different cases.
if ('FieldValueForbidden' in response.text and
'Forbidden' in response.json()['message']):
raise exc.K8sFieldValueForbidden(response.text)
raise exc.K8sUnprocessableEntity(response.text)
if not response.ok:
raise exc.K8sClientException(response.text)
@ -261,7 +267,7 @@ class K8sClient(object):
try:
self._raise_from_response(response)
except (exc.K8sForbidden, exc.K8sResourceNotFound):
except (exc.K8sFieldValueForbidden, exc.K8sResourceNotFound):
# Object is being deleting or gone. Return.
return False
except exc.K8sConflict:
@ -306,8 +312,8 @@ class K8sClient(object):
self._raise_from_response(response)
except exc.K8sConflict:
obj = self.get(path)
except exc.K8sResourceNotFound:
# Object is gone already, stop.
except (exc.K8sFieldValueForbidden, exc.K8sResourceNotFound):
# Object is being deleted or gone already, stop.
return False
# If after 3 iterations there's still conflict, just raise.