<p>I am using <a href="https://github.com/kubernetes-client/python" rel="nofollow noreferrer">kubernetes-client</a> library in python and looking at the <a href="https://github.com/kubernetes-client/python/tree/master/examples" rel="nofollow noreferrer">various examples</a>, it appears we don't need to explicitly close the client connection to the API server. Does the client connection gets terminated automatically or are the examples missing the call to close the connection? I also found the docs page for the APIs (<a href="https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/AppsV1Api.md" rel="nofollow noreferrer">AppsV1</a> for example) and the examples shown there use context manager for the calls so the connection gets disconnected automatically there but I still have questions for the scripts that don't use the context manager approach.</p>

<p>Kubernetes's API is HTTP-based, so you can often get away without explicitly closing a connection.  If you have a short script, things should get cleaned up automatically at the end of the script and it's okay to not explicitly close things.</p>
<p>The specific documentation page you link to shows a safe way to do it:</p>
<pre class="lang-py prettyprint-override"><code>with kubernetes.client.ApiClient(configuration) as api_client:
    api_instance = kubernetes.client.AppsV1Api(api_client)
    api_instance.create_namespaced_controller_revision(...)
</code></pre>
<p>The per-API-version client object is stateless if you pass in an <code>ApiClient</code> to its constructor, so it's safe to create these objects as needed.</p>
<p>The <code>ApiClient</code> class <a href="https://github.com/kubernetes-client/python/blob/master/kubernetes/client/api_client.py#L90" rel="nofollow noreferrer">includes</a> an explicit <code>close</code> method, so you could also do this (less safely) without the context-manager syntax:</p>
<pre class="lang-py prettyprint-override"><code>api_client = kubernetes.client.ApiClient(configuration)
apps_client = kubernetes.client.AppsV1Api(api_client)
...
api_client.close()
</code></pre>
<p>The library client front-page <code>README</code> suggests a path that doesn't explicitly create an <code>ApiClient</code>.  Looking at <a href="https://github.com/kubernetes-client/python/blob/master/kubernetes/client/api/apis_api.py#L36" rel="nofollow noreferrer">one of the generated models' code</a>, if you don't pass an <code>ApiClient</code> option explicitly, a new one will be created for each API-version client object; that includes a connection pool as well.  That can leak local memory and cause extra connections to the cluster, but this might not matter to you for small scripts.</p>
