xDS interop: Add missing k8s 401 retries on get and delete ops (#32244)

PR https://github.com/grpc/grpc/pull/32210 missed adding 401 retries
to several ops:

- All deletes
- get_pod()
- list_pods_with_labels()
pull/31448/head
Sergii Tkachenko 2 years ago committed by GitHub
parent 905df9dee6
commit 03ea0bb28d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      tools/run_tests/xds_k8s_test_driver/framework/infrastructure/k8s.py

@ -68,6 +68,23 @@ def _simple_resource_get(func):
return _wrap_simple_resource_get return _wrap_simple_resource_get
def _simple_resource_delete(func):
def _wrap_simple_resource_delete(self: 'KubernetesNamespace', *args,
**kwargs):
try:
return func(self, *args, **kwargs)
except ApiException as e:
if e.status == 401:
# 401 Unauthorized: token might be expired, attempt auth refresh
self.refresh_auth()
return func(self, *args, **kwargs)
# Reraise for anything else.
raise
return _wrap_simple_resource_delete
def label_dict_to_selector(labels: dict) -> str: def label_dict_to_selector(labels: dict) -> str:
return ','.join(f'{k}=={v}' for k, v in labels.items()) return ','.join(f'{k}=={v}' for k, v in labels.items())
@ -144,6 +161,7 @@ class KubernetesNamespace: # pylint: disable=too-many-public-methods
def get_service_account(self, name) -> V1Service: def get_service_account(self, name) -> V1Service:
return self.api.core.read_namespaced_service_account(name, self.name) return self.api.core.read_namespaced_service_account(name, self.name)
@_simple_resource_delete
def delete_service(self, def delete_service(self,
name, name,
grace_period_seconds=DELETE_GRACE_PERIOD_SEC): grace_period_seconds=DELETE_GRACE_PERIOD_SEC):
@ -154,6 +172,7 @@ class KubernetesNamespace: # pylint: disable=too-many-public-methods
propagation_policy='Foreground', propagation_policy='Foreground',
grace_period_seconds=grace_period_seconds)) grace_period_seconds=grace_period_seconds))
@_simple_resource_delete
def delete_service_account(self, def delete_service_account(self,
name, name,
grace_period_seconds=DELETE_GRACE_PERIOD_SEC): grace_period_seconds=DELETE_GRACE_PERIOD_SEC):
@ -168,6 +187,7 @@ class KubernetesNamespace: # pylint: disable=too-many-public-methods
def get(self) -> V1Namespace: def get(self) -> V1Namespace:
return self.api.core.read_namespace(self.name) return self.api.core.read_namespace(self.name)
@_simple_resource_delete
def delete(self, grace_period_seconds=DELETE_GRACE_PERIOD_SEC): def delete(self, grace_period_seconds=DELETE_GRACE_PERIOD_SEC):
self.api.core.delete_namespace( self.api.core.delete_namespace(
name=self.name, name=self.name,
@ -236,6 +256,7 @@ class KubernetesNamespace: # pylint: disable=too-many-public-methods
def get_deployment(self, name) -> V1Deployment: def get_deployment(self, name) -> V1Deployment:
return self.api.apps.read_namespaced_deployment(name, self.name) return self.api.apps.read_namespaced_deployment(name, self.name)
@_simple_resource_delete
def delete_deployment( def delete_deployment(
self, self,
name: str, name: str,
@ -304,11 +325,13 @@ class KubernetesNamespace: # pylint: disable=too-many-public-methods
check_result=lambda deployment: deployment is None) check_result=lambda deployment: deployment is None)
retryer(self.get_deployment, deployment_name) retryer(self.get_deployment, deployment_name)
@_simple_resource_get
def list_pods_with_labels(self, labels: dict) -> List[V1Pod]: def list_pods_with_labels(self, labels: dict) -> List[V1Pod]:
pod_list: V1PodList = self.api.core.list_namespaced_pod( pod_list: V1PodList = self.api.core.list_namespaced_pod(
self.name, label_selector=label_dict_to_selector(labels)) self.name, label_selector=label_dict_to_selector(labels))
return pod_list.items return pod_list.items
@_simple_resource_get
def get_pod(self, name: str) -> V1Pod: def get_pod(self, name: str) -> V1Pod:
return self.api.core.read_namespaced_pod(name, self.name) return self.api.core.read_namespaced_pod(name, self.name)

Loading…
Cancel
Save