Skip to content

Commit 42a385f

Browse files
committed
Add autovars feature to Gardener plugin
Signed-off-by: Matthias Büchse <matthias.buechse@alasca.cloud>
1 parent 3dcdf08 commit 42a385f

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

Tests/kaas/plugin/gardener_helper.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ def setup_client_config(client_config: Configuration, kubeconfig, cwd='.'):
2323
))
2424

2525

26+
def get_cloudprofile(api_instance: CustomObjectsApi, namespace, name):
27+
"""mimic `kubectl get cloudprofile`"""
28+
return api_instance.get_cluster_custom_object(
29+
GARDENER_GROUP, GARDENER_VERSION, 'cloudprofiles', name,
30+
)
31+
32+
2633
def create_shoot(co_api: CustomObjectsApi, namespace: str, body: dict):
2734
return co_api.create_namespaced_custom_object(
2835
group=GARDENER_GROUP,

Tests/kaas/plugin/plugin_gardener.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,38 @@ def __init__(self, plugin_config, basepath='.', cwd='.', name=None):
146146
def _render_template(self, key):
147147
return self.template_map[key].render(**self.vars, **self.secrets)
148148

149+
def _auto_vars_noris(self, api_instance: _gh.CustomObjectsApi):
150+
logging.debug('using autoVars/noris')
151+
cloud_profile = _gh.get_cloudprofile(api_instance, self.namespace, self.vars['cloud_profile_name'])
152+
version_items = cloud_profile['spec']['kubernetes']['versions']
153+
# filter by kubernetesVersion and classification==supported
154+
version_prefix = f"{self.config['kubernetesVersion']}."
155+
versions = [
156+
item['version']
157+
for item in version_items
158+
if item['classification'] == 'supported'
159+
if item['version'].startswith(version_prefix)
160+
]
161+
logging.debug(f'matching k8s versions: {versions}')
162+
if not versions:
163+
raise RuntimeError(f'autoVars/noris failed: no versions found for v{version_prefix}x')
164+
# select latest patch version (assume patch part is numeric)
165+
selected_version = max(versions, key=lambda ver: int(ver.rsplit('.', 1)[-1]))
166+
return {'kubernetes_version': selected_version}
167+
168+
def _auto_vars(self, auto_vars_kind, co_api: _gh.CustomObjectsApi):
169+
# set default values regardless of auto_vars_kind
170+
self.vars.setdefault('num_worker_nodes', 3)
171+
# now on to specifics
172+
if not auto_vars_kind:
173+
return
174+
if auto_vars_kind == 'noris':
175+
auto_vars = self._auto_vars_noris(co_api)
176+
else:
177+
raise RuntimeError(f'unknown kind of autoVars: {auto_vars_kind}')
178+
logger.debug(f'applying autoVars/{auto_vars_kind}: {auto_vars}')
179+
self.vars.update(auto_vars)
180+
149181
def _write_shoot_yaml(self, shoot_yaml):
150182
# write out shoot.yaml for purposes of documentation
151183
# we will however use the dict instead of calling the shell with `kubectl apply -f`
@@ -164,6 +196,7 @@ def _write_kubeconfig(self, kubeconfig):
164196
def create_cluster(self):
165197
with ApiClient(self.client_config) as api_client:
166198
co_api = _gh.CustomObjectsApi(api_client)
199+
self._auto_vars(self.config.get('autoVars'), co_api)
167200
shoot_yaml = self._render_template('shoot')
168201
shoot_dict = yaml.load(shoot_yaml, Loader=yaml.SafeLoader)
169202
self._write_shoot_yaml(shoot_yaml)

playbooks/clusters.yaml.j2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ clusters:
44
kind: gardener
55
config:
66
name: 'noris-1-33' # explicit name because dot in name not permissible
7+
kubernetesVersion: '1.33'
8+
autoVars: noris # determine vars.kubernetes_version automatically using kubernetesVersion
79
templates:
810
shoot: shoot.yaml
911
kubeconfig: garden-kubeconfig.yaml
@@ -29,6 +31,8 @@ clusters:
2931
kind: gardener
3032
config:
3133
name: 'noris-1-34' # explicit name because dot in name not permissible
34+
kubernetesVersion: '1.34'
35+
autoVars: noris # determine vars.kubernetes_version automatically using kubernetesVersion
3236
templates:
3337
shoot: shoot.yaml
3438
kubeconfig: garden-kubeconfig.yaml

0 commit comments

Comments
 (0)