Skip to content

Commit f50dcf8

Browse files
committed
admin: make kubeconfig a dict
And use yaml.dump
1 parent d29b30e commit f50dcf8

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

src/warnet/admin.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from pathlib import Path
33

44
import click
5+
import yaml
56
from rich import print as richprint
67

78
from .constants import NETWORK_DIR, WARGAMES_NAMESPACE_PREFIX
@@ -92,27 +93,32 @@ def create_kubeconfigs(kubeconfig_dir, token_duration):
9293
# might not be worth it since we are just reading the yaml to then create a bunch of values and its not
9394
# actually used to deploy anything into the cluster
9495
# Then benefit would be making this code a bit cleaner and easy to follow, fwiw
95-
kubeconfig_content = f"""apiVersion: v1
96-
kind: Config
97-
clusters:
98-
- name: {cluster_name}
99-
cluster:
100-
server: {cluster_server}
101-
certificate-authority-data: {cluster_ca}
102-
users:
103-
- name: {sa}
104-
user:
105-
token: {token}
106-
contexts:
107-
- name: {sa}-{namespace}
108-
context:
109-
cluster: {cluster_name}
110-
namespace: {namespace}
111-
user: {sa}
112-
current-context: {sa}-{namespace}
113-
"""
96+
97+
kubeconfig_dict = {
98+
"apiVersion": "v1",
99+
"kind": "Config",
100+
"clusters": [
101+
{
102+
"name": cluster_name,
103+
"cluster": {
104+
"server": cluster_server,
105+
"certificate-authority-data": cluster_ca,
106+
},
107+
}
108+
],
109+
"users": [{"name": sa, "user": {"token": token}}],
110+
"contexts": [
111+
{
112+
"name": f"{sa}-{namespace}",
113+
"context": {"cluster": cluster_name, "namespace": namespace, "user": sa},
114+
}
115+
],
116+
"current-context": f"{sa}-{namespace}",
117+
}
118+
119+
# Write to a YAML file
114120
with open(kubeconfig_file, "w") as f:
115-
f.write(kubeconfig_content)
121+
yaml.dump(kubeconfig_dict, f, default_flow_style=False)
116122

117123
click.echo(f" Created kubeconfig file for {sa}: {kubeconfig_file}")
118124

0 commit comments

Comments
 (0)