2
2
import sys
3
3
4
4
import click
5
- import yaml
6
5
7
6
from warnet .constants import KUBECONFIG
7
+ from warnet .k8s import K8sError , open_kubeconfig , write_kubeconfig
8
8
9
9
10
10
@click .command ()
11
11
@click .argument ("auth_config" , type = str )
12
12
def auth (auth_config ):
13
13
"""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 )
16
20
17
21
is_first_config = False
18
22
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 )
21
25
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 )
22
30
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 )
24
37
25
38
if not is_first_config :
26
39
clusters = "clusters"
@@ -53,20 +66,19 @@ def auth(auth_config):
53
66
)
54
67
55
68
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" )
61
74
sys .exit (1 )
62
75
63
76
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 :
70
82
click .secho (f"Error reading from { KUBECONFIG } : { e } " , fg = "red" )
71
83
sys .exit (1 )
72
84
@@ -86,18 +98,3 @@ def merge_entries(base_list, auth_list, key, entry_type):
86
98
else :
87
99
base_list .append (entry )
88
100
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