<p>On Cloud Composer I have long running DAG tasks, each of them running for 4 to 6 hours. The task ends with an error which is caused by Kubernetes API. The error message states 401 Unauthorized.</p>
<p>The error message:</p>
<pre><code>kubernetes.client.rest.ApiException: (401)
Reason: Unauthorized
HTTP response headers: HTTPHeaderDict({'Audit-Id': 'e1a37278-0693-4f36-8b04-0a7ce0b7f7a0', 'Cache-Control': 'no-cache, private', 'Content-Type': 'application/json', 'Date': 'Fri, 07 Jul 2023 08:10:15 GMT', 'Content-Length': '129'})
HTTP response body: {&quot;kind&quot;:&quot;Status&quot;,&quot;apiVersion&quot;:&quot;v1&quot;,&quot;metadata&quot;:{},&quot;status&quot;:&quot;Failure&quot;,&quot;message&quot;:&quot;Unauthorized&quot;,&quot;reason&quot;:&quot;Unauthorized&quot;,&quot;code&quot;:401}
</code></pre>
<p>The kubernetes API token has an expiry of 1 hour and the Composer is not renewing the token before it expires.
This issue never happened with Composer1, it started showing only when I migrated from Composer1 to Composer2</p>
<p>Additional details:
There is an option in GKEStartPodOperator called is_delete_operator_pod
that is set to true. This option deletes the pod from the cluster after the job is done. So, after the task is completed in about 4 hours, the Composer tries to delete the pod, and that time this 401 Unauthorized error is shown.</p>
<p>I have checked some Airflow configs like kubernetes.enable_tcp_keepalive that enables TCP keepalive mechanism for kubernetes clusters, but it doesn't help resolving the problem.</p>
<p>What can be done to prevent this error?</p>

Kavya
<p>As mentioned in the comment This issue might occur when you try to run a kubectl command in your GKE cluster from a local environment. The command fails and displays an error message, usually with HTTP status code (Unauthorized).</p>
<p>The cause of this issue might be one of the following:</p>
<ul>
<li><p>The gke-gcloud-auth-plugin authentication plugin is not correctly installed or configured.</p>
</li>
<li><p>You lack the permissions to connect to the cluster API server and run kubectl commands.</p>
</li>
</ul>
<p>To diagnose the cause, do the following this <a href="https://cloud.google.com/kubernetes-engine/docs/troubleshooting#connect_to_the_cluster_using_curl" rel="nofollow noreferrer">Link</a></p>
<p>If you get a 401 error or a similar authorization error, ensure that you have the correct permissions to perform the operation. And for more information you can see <a href="https://github.com/apache/airflow/issues/31648" rel="nofollow noreferrer">Git Link</a></p>
