|
2 | 2 | from pathlib import Path |
3 | 3 |
|
4 | 4 | import click |
| 5 | +import yaml |
5 | 6 | from rich import print as richprint |
6 | 7 |
|
7 | 8 | from .constants import NETWORK_DIR, WARGAMES_NAMESPACE_PREFIX |
@@ -92,27 +93,32 @@ def create_kubeconfigs(kubeconfig_dir, token_duration): |
92 | 93 | # might not be worth it since we are just reading the yaml to then create a bunch of values and its not |
93 | 94 | # actually used to deploy anything into the cluster |
94 | 95 | # 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 |
114 | 120 | with open(kubeconfig_file, "w") as f: |
115 | | - f.write(kubeconfig_content) |
| 121 | + yaml.dump(kubeconfig_dict, f, default_flow_style=False) |
116 | 122 |
|
117 | 123 | click.echo(f" Created kubeconfig file for {sa}: {kubeconfig_file}") |
118 | 124 |
|
|
0 commit comments