From d0cfad357c5546ada2ee94e4c346c89640739eb2 Mon Sep 17 00:00:00 2001 From: tamil vanan Date: Thu, 7 Aug 2025 03:53:24 +0000 Subject: [PATCH] Handle JSONDecodeError in dynamic client discovery #2429 When the Kubernetes API server returns a 503 Service Unavailable error with a text/plain content type (instead of application/json), the dynamic client fails with an unhandled JSONDecodeError exception while trying to parse the response as JSON. --- kubernetes/base/dynamic/discovery.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kubernetes/base/dynamic/discovery.py b/kubernetes/base/dynamic/discovery.py index c00dfa3ef8..3dd28af268 100644 --- a/kubernetes/base/dynamic/discovery.py +++ b/kubernetes/base/dynamic/discovery.py @@ -22,6 +22,7 @@ from collections import defaultdict from abc import abstractmethod, abstractproperty +from json.decoder import JSONDecodeError from urllib3.exceptions import ProtocolError, MaxRetryError from kubernetes import __version__ @@ -164,7 +165,8 @@ def get_resources_for_api_version(self, prefix, group, version, preferred): path = '/'.join(filter(None, [prefix, group, version])) try: resources_response = self.client.request('GET', path).resources or [] - except ServiceUnavailableError: + except (ServiceUnavailableError, JSONDecodeError): + # Handle both service unavailable errors and JSON decode errors resources_response = [] resources_raw = list(filter(lambda resource: '/' not in resource['name'], resources_response))