Skip to content

Commit b1055db

Browse files
committed
Fix retrieval of shoot kubeconfig
Signed-off-by: Matthias Büchse <matthias.buechse@alasca.cloud>
1 parent d523e4e commit b1055db

2 files changed

Lines changed: 24 additions & 12 deletions

File tree

Tests/kaas/plugin/gardener_helper.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""helper functions for Gardener plugin"""
22
import base64
3+
import json
34
import os
45

5-
from kubernetes.client import Configuration, CoreV1Api, CustomObjectsApi
6+
from kubernetes.client import Configuration, CoreV1Api, CustomObjectsApi, ApiClient
67

78

89
GARDENER_GROUP = 'core.gardener.cloud'
@@ -68,10 +69,22 @@ def get_shoot_status(co_api: CustomObjectsApi, namespace: str, name: str):
6869
)
6970

7071

71-
# def get_secret_data(core_api: CoreV1Api, namespace: str, name: str):
72-
# secret = core_api.read_namespaced_secret(name, namespace)
73-
# return base64.b64decode(secret.data['kubeconfig'])
74-
def get_secret_data(api_instance: CoreV1Api, namespace, secret):
75-
"""mimic `kubectl get secrets NAME -o=jsonpath='{.data.value}' | base64 -d > kubeconfig.yaml`"""
76-
res = api_instance.read_namespaced_secret(secret, namespace)
77-
return base64.standard_b64decode(res.data['value'].encode())
72+
def request_kubeconfig(api_client: ApiClient, namespace: str, name: str, expiration_seconds=8*3600):
73+
# adapted from
74+
# https://gardener.cloud/docs/gardener/shoot/shoot_access/#shoots-adminkubeconfig-subresource
75+
kubeconfig_request = {
76+
'apiVersion': 'authentication.gardener.cloud/v1alpha1',
77+
'kind': 'AdminKubeconfigRequest',
78+
'spec': {
79+
'expirationSeconds': expiration_seconds,
80+
}
81+
}
82+
response = api_client.call_api(
83+
resource_path=f'/apis/core.gardener.cloud/v1beta1/namespaces/{namespace}/shoots/{name}/adminkubeconfig',
84+
method='POST',
85+
body=kubeconfig_request,
86+
auth_settings=['BearerToken'],
87+
_preload_content=False,
88+
_return_http_data_only=True,
89+
)
90+
return base64.standard_b64decode(json.loads(response.data)["status"]["kubeconfig"])

Tests/kaas/plugin/plugin_gardener.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class _ShootOps:
2121
def __init__(self, namespace: str, name: str):
2222
self.namespace = namespace
2323
self.name = name
24-
self.secret_name = f'{name}.kubeconfig'
2524

2625
def _get_last_operation(self, co_api: _gh.CustomObjectsApi):
2726
try:
@@ -94,8 +93,8 @@ def delete(self, co_api: _gh.CustomObjectsApi):
9493
logger.info(f"Shoot {self.name} deletion succeeded, but object still exists. Waiting 30 s for it to vanish.")
9594
time.sleep(30)
9695

97-
def get_kubeconfig(self, core_api: _gh.CoreV1Api):
98-
return _gh.get_secret_data(core_api, self.namespace, self.secret_name)
96+
def get_kubeconfig(self, api_client: ApiClient):
97+
return _gh.request_kubeconfig(api_client, self.namespace, self.name)
9998

10099
def wait_for_shoot_ready(self, co_api: _gh.CustomObjectsApi):
101100
last_op = self._get_last_operation(co_api)
@@ -172,7 +171,7 @@ def create_cluster(self):
172171
sops = _ShootOps(self.namespace, self.config['name'])
173172
sops.create(co_api=co_api, shoot_dict=shoot_dict)
174173
sops.wait_for_shoot_ready(co_api)
175-
self._write_kubeconfig(sops.get_kubeconfig(core_api))
174+
self._write_kubeconfig(sops.get_kubeconfig(api_client))
176175

177176
def delete_cluster(self):
178177
with ApiClient(self.client_config) as api_client:

0 commit comments

Comments
 (0)