|
1 | 1 | """helper functions for Gardener plugin""" |
2 | 2 | import base64 |
| 3 | +import json |
3 | 4 | import os |
4 | 5 |
|
5 | | -from kubernetes.client import Configuration, CoreV1Api, CustomObjectsApi |
| 6 | +from kubernetes.client import Configuration, CoreV1Api, CustomObjectsApi, ApiClient |
6 | 7 |
|
7 | 8 |
|
8 | 9 | GARDENER_GROUP = 'core.gardener.cloud' |
@@ -68,10 +69,22 @@ def get_shoot_status(co_api: CustomObjectsApi, namespace: str, name: str): |
68 | 69 | ) |
69 | 70 |
|
70 | 71 |
|
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"]) |
0 commit comments