1+ import os
12import sys
23
34import click
1011@click .argument ("auth_config" , type = str )
1112def auth (auth_config ):
1213 """Authenticate with a Warnet cluster using a kubernetes config file"""
13- base_config = yaml_try_with_open ( KUBECONFIG )
14+ # TODO: use os.replace for more atomic file writing
1415 auth_config = yaml_try_with_open (auth_config )
1516
16- clusters = "clusters"
17- if clusters in auth_config :
18- merge_entries (
19- base_config . setdefault ( clusters , []), auth_config [ clusters ], "name" , "cluster"
20- )
17+ is_first_config = False
18+ if not os . path . exists ( KUBECONFIG ) :
19+ with open ( KUBECONFIG , "w" ) as file :
20+ yaml . safe_dump ( auth_config , file )
21+ is_first_config = True
2122
22- users = "users"
23- if users in auth_config :
24- merge_entries (base_config .setdefault (users , []), auth_config [users ], "name" , "user" )
23+ base_config = yaml_try_with_open (KUBECONFIG )
2524
26- contexts = "contexts"
27- if contexts in auth_config :
28- merge_entries (
29- base_config .setdefault (contexts , []), auth_config [contexts ], "name" , "context"
30- )
25+ if not is_first_config :
26+ clusters = "clusters"
27+ if clusters in auth_config :
28+ merge_entries (
29+ base_config .setdefault (clusters , []), auth_config [clusters ], "name" , "cluster"
30+ )
31+
32+ users = "users"
33+ if users in auth_config :
34+ merge_entries (base_config .setdefault (users , []), auth_config [users ], "name" , "user" )
35+
36+ contexts = "contexts"
37+ if contexts in auth_config :
38+ merge_entries (
39+ base_config .setdefault (contexts , []), auth_config [contexts ], "name" , "context"
40+ )
3141
3242 new_current_context = auth_config .get ("current-context" )
3343 base_config ["current-context" ] = new_current_context
@@ -42,15 +52,23 @@ def auth(auth_config):
4252 fg = "yellow" ,
4353 )
4454
45- with open (KUBECONFIG , "w" ) as file :
46- yaml .safe_dump (base_config , file )
47- click .secho (f"Updated kubeconfig with authorization data: { KUBECONFIG } " , fg = "green" )
55+ 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" )
61+ sys .exit (1 )
4862
49- with open (KUBECONFIG ) as file :
50- contents = yaml .safe_load (file )
51- click .secho (
52- f"Warnet's current context is now set to: { contents ['current-context' ]} " , fg = "green"
53- )
63+ 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 :
70+ click .secho (f"Error reading from { KUBECONFIG } : { e } " , fg = "red" )
71+ sys .exit (1 )
5472
5573
5674def merge_entries (base_list , auth_list , key , entry_type ):
0 commit comments