mrk-andreev commented on code in PR #43853:
URL: https://github.com/apache/airflow/pull/43853#discussion_r1837279468


##########
providers/src/airflow/providers/cncf/kubernetes/operators/pod.py:
##########
@@ -655,6 +663,31 @@ def execute_sync(self, context: Context):
         if self.do_xcom_push:
             return result
 
+    @tenacity.retry(
+        wait=tenacity.wait_exponential(max=15),
+        retry=tenacity.retry_if_exception_type(PodCredentialsExpiredFailure),
+        reraise=True,
+    )
+    def await_init_containers_completion(self, pod: k8s.V1Pod):
+        try:
+            if self.init_container_logs:
+                self.pod_manager.fetch_requested_init_container_logs(
+                    pod=pod,
+                    init_containers=self.init_container_logs,
+                    follow_logs=True,
+                )
+        except kubernetes.client.exceptions.ApiException as exc:
+            if exc.status and str(exc.status) == "401":
+                self.log.warning(
+                    "Failed to check container status due to permission error. 
Refreshing credentials and retrying."
+                )
+                self._refresh_cached_properties()
+                self.pod_manager.read_pod(
+                    pod=pod
+                )  # attempt using refreshed credentials, raises if still 
invalid
+                raise PodCredentialsExpiredFailure("Kubernetes credentials 
expired, retrying after refresh.")
+            raise exc

Review Comment:
   I suggest using the simplest approach by handling exceptions in a separate 
method. Other solutions may require significant code changes, which would 
likely be better suited for a separate PR.
   
   ```
   
       def _handle_api_exception(self,
                                 exc: kubernetes.client.exceptions.ApiException,
                                 pod: k8s.V1Pod,
                                 ):
           if exc.status and str(exc.status) == "401":
               self.log.warning(
                   "Failed to check container status due to permission error. 
Refreshing credentials and retrying."
               )
               self._refresh_cached_properties()
               self.pod_manager.read_pod(
                   pod=pod
               )  # attempt using refreshed credentials, raises if still invalid
               raise PodCredentialsExpiredFailure("Kubernetes credentials 
expired, retrying after refresh.")
           raise exc
   ```
   
   For example, we could use a decorator to specify an interface for the class, 
which would be used within it. Alternatively, we could implement something 
based on the mixin pattern. This seems like a separate, significant task for 
exception handling in Kubernetes.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to