1
+ import os
1
2
import sys
2
3
3
4
import click
10
11
@click .argument ("auth_config" , type = str )
11
12
def auth (auth_config ):
12
13
"""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
14
15
auth_config = yaml_try_with_open (auth_config )
15
16
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
21
22
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 )
25
24
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
+ )
31
41
32
42
new_current_context = auth_config .get ("current-context" )
33
43
base_config ["current-context" ] = new_current_context
@@ -42,15 +52,23 @@ def auth(auth_config):
42
52
fg = "yellow" ,
43
53
)
44
54
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 )
48
62
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 )
54
72
55
73
56
74
def merge_entries (base_list , auth_list , key , entry_type ):
0 commit comments