From 36123128d9145fee43618e8d8831f421688cfd7d Mon Sep 17 00:00:00 2001 From: Arcadiy Ivanov Date: Wed, 27 Dec 2023 03:10:40 -0500 Subject: [PATCH] Improve 400 error messaging (report as modification failure) Fix K8S Client not supporting `field_validation` on custom objects fixes #39 fixes #40 --- build.py | 3 ++- src/main/python/kubernator/plugins/k8s.py | 21 ++++++++++++------- src/main/python/kubernator/plugins/k8s_api.py | 6 ++++-- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/build.py b/build.py index c90c1e7..2e75392 100644 --- a/build.py +++ b/build.py @@ -27,7 +27,7 @@ use_plugin("filter_resources") name = "kubernator" -version = "1.0.11.dev" +version = "1.0.11" summary = "Kubernator is the a pluggable framework for K8S provisioning" authors = [Author("Express Systems USA, Inc.", "")] @@ -91,6 +91,7 @@ def set_properties(project): "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", "Operating System :: MacOS :: MacOS X", "Operating System :: POSIX", "Operating System :: POSIX :: Linux", diff --git a/src/main/python/kubernator/plugins/k8s.py b/src/main/python/kubernator/plugins/k8s.py index 6d7a429..97b05c1 100644 --- a/src/main/python/kubernator/plugins/k8s.py +++ b/src/main/python/kubernator/plugins/k8s.py @@ -377,14 +377,19 @@ def handle_400_strict_validation_error(e: ApiException): if e.status == 400: status = json.loads(e.body) - if status["status"] == "Failure" and FIELD_VALIDATION_STRICT_MARKER in status["message"]: - message = status["message"] - messages = message[message.find(FIELD_VALIDATION_STRICT_MARKER) + - len(FIELD_VALIDATION_STRICT_MARKER):].split(",") - for m in messages: - self._api_warnings(resource, m.strip()) - - raise e from None + if status["status"] == "Failure": + if FIELD_VALIDATION_STRICT_MARKER in status["message"]: + message = status["message"] + messages = message[message.find(FIELD_VALIDATION_STRICT_MARKER) + + len(FIELD_VALIDATION_STRICT_MARKER):].split(",") + for m in messages: + self._api_warnings(resource, m.strip()) + + raise e from None + else: + logger.error("FAILED MODIFYING resource %s from %s: %s", + resource, resource.source, status["message"]) + raise e from None def create(exists_ok=False): logger.info("Creating resource %s%s%s", resource, status_msg, diff --git a/src/main/python/kubernator/plugins/k8s_api.py b/src/main/python/kubernator/plugins/k8s_api.py index 2abbf1d..4a7d0bb 100644 --- a/src/main/python/kubernator/plugins/k8s_api.py +++ b/src/main/python/kubernator/plugins/k8s_api.py @@ -410,7 +410,8 @@ def create(self, dry_run=True): "field_manager": "kubernator", } - if self._k8s_client_version[0] > 22: + # `and not self.rdef.custom` to be removed after solving https://github.com/kubernetes-client/gen/issues/259 + if self._k8s_client_version[0] > 22 and not self.rdef.custom: kwargs["field_validation"] = self._k8s_field_validation if rdef.namespaced: kwargs["namespace"] = self.namespace @@ -430,7 +431,8 @@ def patch(self, json_patch, *, patch_type: K8SResourcePatchType, force=False, dr "field_manager": "kubernator", } - if self._k8s_client_version[0] > 22: + # `and not self.rdef.custom` to be removed after solving https://github.com/kubernetes-client/gen/issues/259 + if self._k8s_client_version[0] > 22 and not self.rdef.custom: kwargs["field_validation"] = self._k8s_field_validation if patch_type == K8SResourcePatchType.SERVER_SIDE_PATCH: kwargs["force"] = force