|
1 | 1 | import os |
| 2 | +import sys |
2 | 3 | from pathlib import Path |
3 | 4 |
|
4 | 5 | import click |
5 | 6 | import yaml |
6 | 7 | from rich import print as richprint |
7 | 8 |
|
8 | | -from .constants import NETWORK_DIR, WARGAMES_NAMESPACE_PREFIX |
9 | | -from .k8s import get_kubeconfig_value, get_namespaces_by_type, get_service_accounts_in_namespace |
| 9 | +from .constants import KUBECONFIG, NETWORK_DIR, WARGAMES_NAMESPACE_PREFIX |
| 10 | +from .k8s import ( |
| 11 | + K8sError, |
| 12 | + get_cluster_of_current_context, |
| 13 | + get_namespaces_by_type, |
| 14 | + get_service_accounts_in_namespace, |
| 15 | + open_kubeconfig, |
| 16 | +) |
10 | 17 | from .namespaces import copy_namespaces_defaults, namespaces |
11 | 18 | from .network import copy_network_defaults |
12 | 19 | from .process import run_command |
@@ -54,9 +61,14 @@ def create_kubeconfigs(kubeconfig_dir, token_duration): |
54 | 61 | """Create kubeconfig files for ServiceAccounts""" |
55 | 62 | kubeconfig_dir = os.path.expanduser(kubeconfig_dir) |
56 | 63 |
|
57 | | - cluster_name = get_kubeconfig_value("{.clusters[0].name}") |
58 | | - cluster_server = get_kubeconfig_value("{.clusters[0].cluster.server}") |
59 | | - cluster_ca = get_kubeconfig_value("{.clusters[0].cluster.certificate-authority-data}") |
| 64 | + try: |
| 65 | + kubeconfig_data = open_kubeconfig(KUBECONFIG) |
| 66 | + except K8sError as e: |
| 67 | + click.secho(e, fg="yellow") |
| 68 | + click.secho(f"Could not open auth_config: {KUBECONFIG}", fg="red") |
| 69 | + sys.exit(1) |
| 70 | + |
| 71 | + cluster = get_cluster_of_current_context(kubeconfig_data) |
60 | 72 |
|
61 | 73 | os.makedirs(kubeconfig_dir, exist_ok=True) |
62 | 74 |
|
@@ -93,24 +105,15 @@ def create_kubeconfigs(kubeconfig_dir, token_duration): |
93 | 105 | # might not be worth it since we are just reading the yaml to then create a bunch of values and its not |
94 | 106 | # actually used to deploy anything into the cluster |
95 | 107 | # Then benefit would be making this code a bit cleaner and easy to follow, fwiw |
96 | | - |
97 | 108 | kubeconfig_dict = { |
98 | 109 | "apiVersion": "v1", |
99 | 110 | "kind": "Config", |
100 | | - "clusters": [ |
101 | | - { |
102 | | - "name": cluster_name, |
103 | | - "cluster": { |
104 | | - "server": cluster_server, |
105 | | - "certificate-authority-data": cluster_ca, |
106 | | - }, |
107 | | - } |
108 | | - ], |
| 111 | + "clusters": [cluster], |
109 | 112 | "users": [{"name": sa, "user": {"token": token}}], |
110 | 113 | "contexts": [ |
111 | 114 | { |
112 | 115 | "name": f"{sa}-{namespace}", |
113 | | - "context": {"cluster": cluster_name, "namespace": namespace, "user": sa}, |
| 116 | + "context": {"cluster": cluster["name"], "namespace": namespace, "user": sa}, |
114 | 117 | } |
115 | 118 | ], |
116 | 119 | "current-context": f"{sa}-{namespace}", |
|
0 commit comments