Skip to content

Commit ed5fb00

Browse files
committed
admin: get raw cluster for auth file
1 parent a9239a7 commit ed5fb00

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/warnet/admin.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import os
2+
import sys
23
from pathlib import Path
34

45
import click
56
import yaml
67
from rich import print as richprint
78

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+
)
1017
from .namespaces import copy_namespaces_defaults, namespaces
1118
from .network import copy_network_defaults
1219
from .process import run_command
@@ -54,9 +61,14 @@ def create_kubeconfigs(kubeconfig_dir, token_duration):
5461
"""Create kubeconfig files for ServiceAccounts"""
5562
kubeconfig_dir = os.path.expanduser(kubeconfig_dir)
5663

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)
6072

6173
os.makedirs(kubeconfig_dir, exist_ok=True)
6274

@@ -93,24 +105,15 @@ def create_kubeconfigs(kubeconfig_dir, token_duration):
93105
# might not be worth it since we are just reading the yaml to then create a bunch of values and its not
94106
# actually used to deploy anything into the cluster
95107
# Then benefit would be making this code a bit cleaner and easy to follow, fwiw
96-
97108
kubeconfig_dict = {
98109
"apiVersion": "v1",
99110
"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],
109112
"users": [{"name": sa, "user": {"token": token}}],
110113
"contexts": [
111114
{
112115
"name": f"{sa}-{namespace}",
113-
"context": {"cluster": cluster_name, "namespace": namespace, "user": sa},
116+
"context": {"cluster": cluster["name"], "namespace": namespace, "user": sa},
114117
}
115118
],
116119
"current-context": f"{sa}-{namespace}",

0 commit comments

Comments
 (0)