From d793daf3b3103ee9c47aef1bf22d257e03622fbc Mon Sep 17 00:00:00 2001 From: Raj Bhargav <72274012+p172913@users.noreply.github.com> Date: Tue, 16 Sep 2025 00:28:33 +0530 Subject: [PATCH] Changes made for terminating pods --- kubernetes/client/__init__.py | 6 ++++++ kubernetes/client/helpers/patch.py | 21 +++++++++++++++++++ scripts/insert_patch_import.sh | 33 ++++++++++++++++++++++++++++++ scripts/release.sh | 2 ++ 4 files changed, 62 insertions(+) create mode 100644 kubernetes/client/helpers/patch.py create mode 100644 scripts/insert_patch_import.sh diff --git a/kubernetes/client/__init__.py b/kubernetes/client/__init__.py index 238c018d3..dce432edf 100644 --- a/kubernetes/client/__init__.py +++ b/kubernetes/client/__init__.py @@ -810,3 +810,9 @@ from kubernetes.client.models.v2_resource_metric_status import V2ResourceMetricStatus from kubernetes.client.models.version_info import VersionInfo + +try: + import kubernetes.client.helpers.patch as _patch + _patch.apply_patch() +except ImportError: + pass diff --git a/kubernetes/client/helpers/patch.py b/kubernetes/client/helpers/patch.py new file mode 100644 index 000000000..68dddec0e --- /dev/null +++ b/kubernetes/client/helpers/patch.py @@ -0,0 +1,21 @@ +from kubernetes import client +from kubernetes.client.models import V1PodList + +def apply_patch(): + """Monkeypatch ApiClient.deserialize safely.""" + if getattr(client.ApiClient, "_is_patched_for_terminating", False): + return # already patched + + original_deserialize = client.ApiClient.deserialize + + def _patched_deserialize(self, response, response_type): + result = original_deserialize(self, response, response_type) + if response_type == "V1PodList" and isinstance(result, V1PodList): + for pod in result.items: + if pod.metadata and pod.metadata.deletion_timestamp: + if pod.status and pod.status.phase == "Running": + pod.status.phase = "Terminating" + return result + + client.ApiClient.deserialize = _patched_deserialize + client.ApiClient._is_patched_for_terminating = True \ No newline at end of file diff --git a/scripts/insert_patch_import.sh b/scripts/insert_patch_import.sh new file mode 100644 index 000000000..daf4158af --- /dev/null +++ b/scripts/insert_patch_import.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +# add_patch_import.sh - Ensure monkey-patch loader is added to client/__init__.py + +SCRIPT_ROOT=$(dirname "${BASH_SOURCE}") +pushd "${SCRIPT_ROOT}" > /dev/null +SCRIPT_ROOT=`pwd` +popd > /dev/null +CLIENT_ROOT="${SCRIPT_ROOT}/../kubernetes" +CONFIG_PATH="$CLIENT_ROOT/client" +CONFIG_FILE="$CONFIG_PATH/__init__.py" +# Normalize Windows-style backslashes to Unix-style forward slashes +CONFIG_FILE="$(echo "$CONFIG_FILE" | sed 's|\\|/|g')" + +# Ensure file exists +if [ ! -f "$CONFIG_FILE" ]; then + echo "Error: $CONFIG_FILE does not exist." >&2 + exit 1 +fi + +PATCH_SNIPPET='try: + import kubernetes.client.helpers.patch as _patch + _patch.apply_patch() +except ImportError: + pass' + +# Check if snippet already exists +if grep -q "your_project.k8s_helpers.patch" "$CONFIG_FILE"; then + echo "Patch snippet already present in $CONFIG_FILE, skipping." +else + echo "" >> "$CONFIG_FILE" + echo "$PATCH_SNIPPET" >> "$CONFIG_FILE" + echo "Patch snippet added to $CONFIG_FILE." +fi diff --git a/scripts/release.sh b/scripts/release.sh index b037204c0..636456ce8 100755 --- a/scripts/release.sh +++ b/scripts/release.sh @@ -209,6 +209,8 @@ git diff-index --quiet --cached HEAD || git commit -am "update changelog" scripts/update-client.sh #edit comfiguration.py files scripts/insert_proxy_config.sh +#insert patch import +scripts/insert_patch_import.sh # Apply hotfixes rm -r kubernetes/test/ git add .