Skip to content

Commit 939687d

Browse files
committed
update auth command
1 parent 6850b47 commit 939687d

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

src/warnet/main.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,37 +136,58 @@ def init():
136136
@click.argument("kube_config", type=str)
137137
def auth(kube_config: str) -> None:
138138
"""
139-
Authorize access to a warnet cluster using a kube config file
139+
Authenticate with a warnet cluster using a kube config file
140140
"""
141141
try:
142142
current_kubeconfig = os.environ.get("KUBECONFIG", os.path.expanduser("~/.kube/config"))
143143
combined_kubeconfig = (
144144
f"{current_kubeconfig}:{kube_config}" if current_kubeconfig else kube_config
145145
)
146146
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)
149158
except subprocess.CalledProcessError as e:
150159
print("Error occurred while executing kubectl config view --flatten:")
151160
print(e.stderr)
152161
sys.exit(1)
153162

154-
if result.returncode == 0:
163+
if result_flatten.returncode == 0:
155164
with open(current_kubeconfig, "w") as file:
156-
file.write(result.stdout)
165+
file.write(result_flatten.stdout)
157166
print(f"Authorization file written to: {current_kubeconfig}")
158167
else:
159168
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)
162183

163184
with open(current_kubeconfig) as file:
164185
contents = yaml.safe_load(file)
165186
print("\nUse the following command to switch to a new user:")
166187
print(" kubectl config use-context [user]\n")
167188
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']}")
170191

171192

172193
if __name__ == "__main__":

0 commit comments

Comments
 (0)