Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions kubernetes/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 21 additions & 0 deletions kubernetes/client/helpers/patch.py
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions scripts/insert_patch_import.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand Down