Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions juju/client/proxy/kubernetes/proxy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2023 Canonical Ltd.
# Licensed under the Apache V2, see LICENCE file for details.
import logging
import os
import tempfile

from kubernetes import client
Expand Down Expand Up @@ -29,21 +30,18 @@ def __init__(
self.namespace = namespace
self.remote_port = remote_port
self.service = service
self.temp_ca_file = None
Comment thread
dimaqq marked this conversation as resolved.
Outdated
self.port_forwarder = None

try:
self.remote_port = int(remote_port)
except ValueError:
raise ValueError(f"Invalid port number: {remote_port}")

self.port_forwarder = None

if ca_cert:
self.temp_ca_file = tempfile.NamedTemporaryFile() # noqa: SIM115
self.temp_ca_file.write(bytes(ca_cert, "utf-8"))
self.temp_ca_file.flush()
config.ssl_ca_cert = self.temp_ca_file.name
else:
self.temp_ca_file = None
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(bytes(ca_cert, "utf-8"))
self.temp_ca_file = config.ssl_ca_cert = f.name
Comment thread
dimaqq marked this conversation as resolved.
Outdated

self.api_client = client.ApiClient(config)

Expand All @@ -67,13 +65,16 @@ def connect(self):

def __del__(self):
self.close()
try:
if self.temp_ca_file:
os.unlink(self.temp_ca_file)
except FileNotFoundError:
Comment thread
dimaqq marked this conversation as resolved.
Outdated
log.debug(f"file {self.temp_ca_file} not found")

def close(self):
try:
if self.port_forwarder:
self.port_forwarder.close()
if self.temp_ca_file:
self.temp_ca_file.close()
except AttributeError:
pass

Expand Down
Loading