Skip to content

Commit eb455ea

Browse files
committed
users: improve auth fn
Incorporates better error handling
1 parent cfbb402 commit eb455ea

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

src/warnet/users.py

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,38 @@
22
import sys
33

44
import click
5-
import yaml
65

76
from warnet.constants import KUBECONFIG
7+
from warnet.k8s import K8sError, open_kubeconfig, write_kubeconfig
88

99

1010
@click.command()
1111
@click.argument("auth_config", type=str)
1212
def auth(auth_config):
1313
"""Authenticate with a Warnet cluster using a kubernetes config file"""
14-
# TODO: use os.replace for more atomic file writing
15-
auth_config = yaml_try_with_open(auth_config)
14+
try:
15+
auth_config = open_kubeconfig(auth_config)
16+
except K8sError as e:
17+
click.secho(e, fg="yellow")
18+
click.secho(f"Could not open auth_config: {auth_config}", fg="red")
19+
sys.exit(1)
1620

1721
is_first_config = False
1822
if not os.path.exists(KUBECONFIG):
19-
with open(KUBECONFIG, "w") as file:
20-
yaml.safe_dump(auth_config, file)
23+
try:
24+
write_kubeconfig(auth_config)
2125
is_first_config = True
26+
except K8sError as e:
27+
click.secho(e, fg="yellow")
28+
click.secho(f"Could not write KUBECONFIG: {KUBECONFIG}", fg="red")
29+
sys.exit(1)
2230

23-
base_config = yaml_try_with_open(KUBECONFIG)
31+
try:
32+
base_config = open_kubeconfig(KUBECONFIG)
33+
except K8sError as e:
34+
click.secho(e, fg="yellow")
35+
click.secho(f"Could not open KUBECONFIG: {KUBECONFIG}", fg="red")
36+
sys.exit(1)
2437

2538
if not is_first_config:
2639
clusters = "clusters"
@@ -53,20 +66,19 @@ def auth(auth_config):
5366
)
5467

5568
try:
56-
with open(KUBECONFIG, "w") as file:
57-
yaml.safe_dump(base_config, file)
58-
click.secho(f"Updated kubeconfig with authorization data: {KUBECONFIG}", fg="green")
59-
except OSError as e:
60-
click.secho(f"Error writing to {KUBECONFIG}: {e}", fg="red")
69+
write_kubeconfig(base_config)
70+
click.secho(f"Updated kubeconfig with authorization data: {KUBECONFIG}", fg="green")
71+
except K8sError as e:
72+
click.secho(e, fg="yellow")
73+
click.secho(f"Could not write KUBECONFIG: {KUBECONFIG}", fg="red")
6174
sys.exit(1)
6275

6376
try:
64-
with open(KUBECONFIG) as file:
65-
contents = yaml.safe_load(file)
66-
click.secho(
67-
f"Warnet's current context is now set to: {contents['current-context']}", fg="green"
68-
)
69-
except (OSError, yaml.YAMLError) as e:
77+
base_config = open_kubeconfig(KUBECONFIG)
78+
click.secho(
79+
f"Warnet's current context is now set to: {base_config['current-context']}", fg="green"
80+
)
81+
except K8sError as e:
7082
click.secho(f"Error reading from {KUBECONFIG}: {e}", fg="red")
7183
sys.exit(1)
7284

@@ -86,18 +98,3 @@ def merge_entries(base_list, auth_list, key, entry_type):
8698
else:
8799
base_list.append(entry)
88100
click.secho(f"Added new {entry_type} '{entry[key]}'", fg="green")
89-
90-
91-
def yaml_try_with_open(filename: str):
92-
try:
93-
with open(filename) as f:
94-
return yaml.safe_load(f)
95-
except FileNotFoundError:
96-
click.secho(f"Could not find: {KUBECONFIG}", fg="red")
97-
sys.exit(1)
98-
except OSError as e:
99-
click.secho(f"An I/O error occurred: {e}", fg="red")
100-
sys.exit(1)
101-
except Exception as e:
102-
click.secho(f"An unexpected error occurred: {e}", fg="red")
103-
sys.exit(1)

0 commit comments

Comments
 (0)