@@ -136,37 +136,62 @@ 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
+ content ["clusters" ][0 ]
152
+ user = content ["users" ][0 ]
153
+ user_name = user ["name" ]
154
+ user_token = user ["user" ]["token" ]
155
+ content ["contexts" ][0 ]
156
+ flatten_cmd = "kubectl config view --flatten"
157
+ result_flatten = subprocess .run (
158
+ flatten_cmd , shell = True , check = True , capture_output = True , text = True
159
+ )
149
160
except subprocess .CalledProcessError as e :
150
161
print ("Error occurred while executing kubectl config view --flatten:" )
151
162
print (e .stderr )
152
163
sys .exit (1 )
153
164
154
- if result .returncode == 0 :
165
+ if result_flatten .returncode == 0 :
155
166
with open (current_kubeconfig , "w" ) as file :
156
- file .write (result .stdout )
167
+ file .write (result_flatten .stdout )
157
168
print (f"Authorization file written to: { current_kubeconfig } " )
158
169
else :
159
170
print ("Could not create authorization file" )
160
- print (result .stderr )
161
- sys .exit (result .returncode )
171
+ print (result_flatten .stderr )
172
+ sys .exit (result_flatten .returncode )
173
+
174
+ try :
175
+ update_cmd = f"kubectl config set-credentials { user_name } --token { user_token } "
176
+ result_update = subprocess .run (
177
+ update_cmd , shell = True , check = True , capture_output = True , text = True
178
+ )
179
+ if result_update .returncode != 0 :
180
+ print ("Could not update authorization file" )
181
+ print (result_flatten .stderr )
182
+ sys .exit (result_flatten .returncode )
183
+ except subprocess .CalledProcessError as e :
184
+ print ("Error occurred while executing kubectl config view --flatten:" )
185
+ print (e .stderr )
186
+ sys .exit (1 )
162
187
163
188
with open (current_kubeconfig ) as file :
164
189
contents = yaml .safe_load (file )
165
190
print ("\n Use the following command to switch to a new user:" )
166
191
print (" kubectl config use-context [user]\n " )
167
192
print ("Available users:" )
168
- for context in contents ["contexts" ]:
169
- print (f" { context ['name' ]} " )
193
+ for c in contents ["contexts" ]:
194
+ print (f" { c ['name' ]} " )
170
195
171
196
172
197
if __name__ == "__main__" :
0 commit comments