Skip to content

Commit a864024

Browse files
committed
chore: fix k8s proxy; re-export in jasyncio
1 parent d79029d commit a864024

3 files changed

Lines changed: 71 additions & 32 deletions

File tree

juju/client/proxy/kubernetes/proxy.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Copyright 2023 Canonical Ltd.
22
# Licensed under the Apache V2, see LICENCE file for details.
33
import logging
4-
import os
54
import tempfile
65

76
from kubernetes import client
@@ -36,11 +35,15 @@ def __init__(
3635
except ValueError:
3736
raise ValueError(f"Invalid port number: {remote_port}")
3837

38+
self.port_forwarder = None
39+
3940
if ca_cert:
40-
self.temp_ca_file = tempfile.NamedTemporaryFile(delete=False)
41+
self.temp_ca_file = tempfile.NamedTemporaryFile() # noqa: SIM115
4142
self.temp_ca_file.write(bytes(ca_cert, "utf-8"))
4243
self.temp_ca_file.flush()
4344
config.ssl_ca_cert = self.temp_ca_file.name
45+
else:
46+
self.temp_ca_file = None
4447

4548
self.api_client = client.ApiClient(config)
4649

@@ -64,15 +67,13 @@ def connect(self):
6467

6568
def __del__(self):
6669
self.close()
67-
try:
68-
os.unlink(self.temp_ca_file.name)
69-
except FileNotFoundError:
70-
log.debug(f"file {self.temp_ca_file.name} not found")
7170

7271
def close(self):
7372
try:
74-
self.port_forwarder.close()
75-
self.temp_ca_file.close()
73+
if self.port_forwarder:
74+
self.port_forwarder.close()
75+
if self.temp_ca_file:
76+
self.temp_ca_file.close()
7677
except AttributeError:
7778
pass
7879

juju/controller.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ async def destroy_models(
397397
398398
"""
399399
uuids = await self.model_uuids()
400-
models = [uuids[model] if model in uuids else model for model in models]
400+
models = [uuids.get(model) or model for model in models]
401401

402402
model_facade = client.ModelManagerFacade.from_connection(self.connection())
403403

@@ -574,7 +574,7 @@ async def get_models(self, all=False, username=None): # noqa: A002
574574
"""
575575
return await self.list_models(username, all)
576576

577-
async def model_uuids(self, username=None, all=False):
577+
async def model_uuids(self, username=None, all=False): # noqa: A002
578578
"""Return a mapping of model names to UUIDs the given user can access.
579579
580580
:param str username: Optional username argument, defaults to
@@ -599,7 +599,7 @@ async def model_uuids(self, username=None, all=False):
599599
model_summary.name: model_summary.uuid for model_summary in model_summaries
600600
}
601601

602-
async def list_models(self, username=None, all=False):
602+
async def list_models(self, username=None, all=False): # noqa: A002
603603
"""Return list of names of the available models on this controller.
604604
605605
Equivalent to ``sorted((await self.model_uuids()).keys())``
@@ -885,7 +885,7 @@ async def _watcher(stop_event):
885885
jasyncio.ensure_future(_watcher(stop_event))
886886
return stop_event
887887

888-
async def add_secret_backends(self, id, name, backend_type, config):
888+
async def add_secret_backends(self, id_, name, backend_type, config):
889889
"""Add a new secret backend.
890890
891891
Parameters
@@ -907,7 +907,7 @@ async def add_secret_backends(self, id, name, backend_type, config):
907907
facade = client.SecretBackendsFacade.from_connection(self.connection())
908908
return await facade.AddSecretBackends([
909909
{
910-
"id": id,
910+
"id": id_,
911911
"backend-type": backend_type,
912912
"config": config,
913913
"name": name,

juju/jasyncio.py

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,71 @@
1313
import functools
1414
import logging
1515
import signal
16+
from asyncio import (
17+
ALL_COMPLETED as ALL_COMPLETED,
18+
)
19+
from asyncio import (
20+
FIRST_COMPLETED as FIRST_COMPLETED,
21+
)
1622
from asyncio import (
1723
CancelledError,
1824
create_task,
1925
wait,
20-
Event as Event, TimeoutError as TimeoutError, Queue as Queue, ensure_future as ensure_future,
21-
gather as gather,
22-
sleep as sleep,
23-
wait_for as wait_for,
24-
create_subprocess_exec as create_subprocess_exec,
25-
subprocess as subprocess,
26-
27-
FIRST_COMPLETED as FIRST_COMPLETED,
28-
Lock as Lock,
29-
as_completed as as_completed,
30-
new_event_loop as new_event_loop,
26+
)
3127

28+
# FIXME: integration tests don't use these, but some are used in this repo
29+
# Use primitives from asyncio within this repo and remove these re-exports
30+
from asyncio import (
31+
Event as Event,
32+
)
33+
from asyncio import (
34+
Lock as Lock,
35+
)
36+
from asyncio import (
37+
Queue as Queue,
38+
)
39+
from asyncio import (
40+
TimeoutError as TimeoutError, # noqa: A004
41+
)
42+
from asyncio import (
43+
all_tasks as all_tasks,
44+
)
45+
from asyncio import (
46+
as_completed as as_completed,
47+
)
48+
from asyncio import (
49+
create_subprocess_exec as create_subprocess_exec,
50+
)
51+
from asyncio import (
52+
current_task as current_task,
53+
)
54+
from asyncio import (
55+
ensure_future as ensure_future,
56+
)
57+
from asyncio import (
58+
gather as gather,
59+
)
60+
from asyncio import (
3261
get_event_loop_policy as get_event_loop_policy,
33-
get_running_loop as
34-
get_running_loop,
35-
ALL_COMPLETED as
36-
ALL_COMPLETED,
37-
all_tasks as
38-
all_tasks,
39-
current_task as
40-
current_task,
62+
)
63+
from asyncio import (
64+
get_running_loop as get_running_loop,
65+
)
66+
from asyncio import (
67+
new_event_loop as new_event_loop,
68+
)
69+
from asyncio import (
4170
shield as shield,
4271
)
72+
from asyncio import (
73+
sleep as sleep,
74+
)
75+
from asyncio import (
76+
subprocess as subprocess,
77+
)
78+
from asyncio import (
79+
wait_for as wait_for,
80+
)
4381

4482
import websockets
4583

0 commit comments

Comments
 (0)