Skip to content

Commit 2c30901

Browse files
authored
Merge pull request #1044 from Aflynn50/fix-microk8s-proxy-issue
#1044 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. Set to not delete on close and to remove file when the proxy is deleted. #### QA Steps ``` juju bootstrap microk8s juju add-model default juju deploy cos-lite ``` In python ``` from juju import model m = model.Model() await m.connect() await m.create_offer("grafana:grafana-dashboard", "grafana-dashboards") await m.disconnect() ``` Check all resource in /tmp have been cleaned up Fixes #1040
2 parents 5ed5ae4 + 7002122 commit 2c30901

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

juju/client/proxy/kubernetes/proxy.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# Copyright 2023 Canonical Ltd.
22
# Licensed under the Apache V2, see LICENCE file for details.
3-
3+
import os
44
import tempfile
5+
import logging
56

67
from juju.client.proxy.proxy import Proxy, ProxyNotConnectedError
78
from kubernetes import client
89
from kubernetes.stream import portforward
910

11+
log = logging.getLogger('juju.client.connection')
12+
1013

1114
class KubernetesProxy(Proxy):
1215
def __init__(
@@ -33,7 +36,7 @@ def __init__(
3336
raise ValueError("Invalid port number: {}".format(remote_port))
3437

3538
if ca_cert:
36-
self.temp_ca_file = tempfile.NamedTemporaryFile()
39+
self.temp_ca_file = tempfile.NamedTemporaryFile(delete=False)
3740
self.temp_ca_file.write(bytes(ca_cert, 'utf-8'))
3841
self.temp_ca_file.flush()
3942
config.ssl_ca_cert = self.temp_ca_file.name
@@ -60,6 +63,10 @@ def connect(self):
6063

6164
def __del__(self):
6265
self.close()
66+
try:
67+
os.unlink(self.temp_ca_file.name)
68+
except FileNotFoundError:
69+
log.debug(f"file {self.temp_ca_file.name} not found")
6370

6471
def close(self):
6572
try:

0 commit comments

Comments
 (0)