@@ -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 )
0 commit comments