@@ -136,37 +136,58 @@ def init():
136
136
@click .argument ("kube_config" , type = str )
137
137
def auth (kube_config : str ) -> None :
138
138
"""
139
- Authorize access to a warnet cluster using a kube config file
139
+ Authenticate with a warnet cluster using a kube config file
140
140
"""
141
141
try :
142
142
current_kubeconfig = os .environ .get ("KUBECONFIG" , os .path .expanduser ("~/.kube/config" ))
143
143
combined_kubeconfig = (
144
144
f"{ current_kubeconfig } :{ kube_config } " if current_kubeconfig else kube_config
145
145
)
146
146
os .environ ["KUBECONFIG" ] = combined_kubeconfig
147
- command = "kubectl config view --flatten"
148
- result = subprocess .run (command , shell = True , check = True , capture_output = True , text = True )
147
+ with open (kube_config ) as file :
148
+ content = yaml .safe_load (file )
149
+ for elem in content :
150
+ print (elem )
151
+ cluster = content ["clusters" ][0 ]
152
+ user = content ["users" ][0 ]
153
+ user_name = user ["name" ]
154
+ user_token = user ["user" ]["token" ]
155
+ context = content ["contexts" ][0 ]
156
+ flatten_cmd = "kubectl config view --flatten"
157
+ result_flatten = subprocess .run (flatten_cmd , shell = True , check = True , capture_output = True , text = True )
149
158
except subprocess .CalledProcessError as e :
150
159
print ("Error occurred while executing kubectl config view --flatten:" )
151
160
print (e .stderr )
152
161
sys .exit (1 )
153
162
154
- if result .returncode == 0 :
163
+ if result_flatten .returncode == 0 :
155
164
with open (current_kubeconfig , "w" ) as file :
156
- file .write (result .stdout )
165
+ file .write (result_flatten .stdout )
157
166
print (f"Authorization file written to: { current_kubeconfig } " )
158
167
else :
159
168
print ("Could not create authorization file" )
160
- print (result .stderr )
161
- sys .exit (result .returncode )
169
+ print (result_flatten .stderr )
170
+ sys .exit (result_flatten .returncode )
171
+
172
+ try :
173
+ update_cmd = f"kubectl config set-credentials { user_name } --token { user_token } "
174
+ result_update = subprocess .run (update_cmd , shell = True , check = True , capture_output = True , text = True )
175
+ if result_update .returncode != 0 :
176
+ print ("Could not update authorization file" )
177
+ print (result_flatten .stderr )
178
+ sys .exit (result_flatten .returncode )
179
+ except subprocess .CalledProcessError as e :
180
+ print ("Error occurred while executing kubectl config view --flatten:" )
181
+ print (e .stderr )
182
+ sys .exit (1 )
162
183
163
184
with open (current_kubeconfig ) as file :
164
185
contents = yaml .safe_load (file )
165
186
print ("\n Use the following command to switch to a new user:" )
166
187
print (" kubectl config use-context [user]\n " )
167
188
print ("Available users:" )
168
- for context in contents ["contexts" ]:
169
- print (f" { context ['name' ]} " )
189
+ for c in contents ["contexts" ]:
190
+ print (f" { c ['name' ]} " )
170
191
171
192
172
193
if __name__ == "__main__" :
0 commit comments