From e017bd2b2bd4ea93983c544454d1324d9dd8dc70 Mon Sep 17 00:00:00 2001 From: Alastair Flynn Date: Wed, 17 Apr 2024 10:30:10 +0100 Subject: [PATCH 1/2] Fix issue with microk8s proxy The temporary file used to store the ca_cert was set to delete itself on close. This meant that when it was accessed multiple times it would no longer be present. --- juju/client/proxy/kubernetes/proxy.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/juju/client/proxy/kubernetes/proxy.py b/juju/client/proxy/kubernetes/proxy.py index 0c77ef62..fabb01e6 100644 --- a/juju/client/proxy/kubernetes/proxy.py +++ b/juju/client/proxy/kubernetes/proxy.py @@ -1,6 +1,6 @@ # Copyright 2023 Canonical Ltd. # Licensed under the Apache V2, see LICENCE file for details. - +import os import tempfile from juju.client.proxy.proxy import Proxy, ProxyNotConnectedError @@ -33,7 +33,7 @@ def __init__( raise ValueError("Invalid port number: {}".format(remote_port)) if ca_cert: - self.temp_ca_file = tempfile.NamedTemporaryFile() + self.temp_ca_file = tempfile.NamedTemporaryFile(delete=False) self.temp_ca_file.write(bytes(ca_cert, 'utf-8')) self.temp_ca_file.flush() config.ssl_ca_cert = self.temp_ca_file.name @@ -60,6 +60,7 @@ def connect(self): def __del__(self): self.close() + os.unlink(self.temp_ca_file.name) def close(self): try: From 70021220c2e0c298f78a9372aef7c22bf43e53e8 Mon Sep 17 00:00:00 2001 From: Alastair Flynn Date: Thu, 18 Apr 2024 11:16:18 +0100 Subject: [PATCH 2/2] Handle error in Kubernetes proxy __del__ --- juju/client/proxy/kubernetes/proxy.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/juju/client/proxy/kubernetes/proxy.py b/juju/client/proxy/kubernetes/proxy.py index fabb01e6..3eb53c5c 100644 --- a/juju/client/proxy/kubernetes/proxy.py +++ b/juju/client/proxy/kubernetes/proxy.py @@ -2,11 +2,14 @@ # Licensed under the Apache V2, see LICENCE file for details. import os import tempfile +import logging from juju.client.proxy.proxy import Proxy, ProxyNotConnectedError from kubernetes import client from kubernetes.stream import portforward +log = logging.getLogger('juju.client.connection') + class KubernetesProxy(Proxy): def __init__( @@ -60,7 +63,10 @@ def connect(self): def __del__(self): self.close() - os.unlink(self.temp_ca_file.name) + try: + os.unlink(self.temp_ca_file.name) + except FileNotFoundError: + log.debug(f"file {self.temp_ca_file.name} not found") def close(self): try: