Skip to content

Commit

Permalink
Merge pull request #1044 from Aflynn50/fix-microk8s-proxy-issue
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
jujubot committed Apr 19, 2024
2 parents 5ed5ae4 + 7002122 commit 2c30901
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions juju/client/proxy/kubernetes/proxy.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# Copyright 2023 Canonical Ltd.
# 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__(
Expand All @@ -33,7 +36,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
Expand All @@ -60,6 +63,10 @@ def connect(self):

def __del__(self):
self.close()
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:
Expand Down

0 comments on commit 2c30901

Please sign in to comment.