Skip to content

Commit

Permalink
Merge pull request #631 from GeorgianaElena/profile-defaults
Browse files Browse the repository at this point in the history
Observe the default choice for `profile-option-{profile-slug}-{option-slug}`
  • Loading branch information
yuvipanda committed Sep 28, 2022
2 parents 7d6d82c + f5de75d commit 67b0304
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions kubespawner/spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -1649,7 +1649,7 @@ def _validate_image_pull_secrets(self, proposal):
'kubespawner_override': {
'cpu_limit': 2,
'cpu_guarantee': 1.8,
'node_selectors': {
'node_selector': {
'node.kubernetes.io/instance-type': 'n1-standard-2'
}
}
Expand All @@ -1659,7 +1659,7 @@ def _validate_image_pull_secrets(self, proposal):
'kubespawner_override': {
'cpu_limit': 4,
'cpu_guarantee': 3.5,
'node_selectors': {
'node_selector': {
'node.kubernetes.io/instance-type': 'n1-standard-4'
}
}
Expand Down Expand Up @@ -2949,7 +2949,7 @@ def _options_from_form(self, formdata):

return options

async def _load_profile(self, slug, user_options):
async def _load_profile(self, slug, selected_profile_user_options):
"""Load a profile by name
Called by load_user_options
Expand Down Expand Up @@ -2978,6 +2978,7 @@ async def _load_profile(self, slug, user_options):
self.log.debug(
"Applying KubeSpawner override for profile '%s'", profile['display_name']
)

kubespawner_override = profile.get('kubespawner_override', {})
for k, v in kubespawner_override.items():
if callable(v):
Expand All @@ -2997,13 +2998,27 @@ async def _load_profile(self, slug, user_options):
# are in the form data posted. This prevents users who may be authorized
# to only use one profile from being able to access options set for other
# profiles
for option_name, option in profile.get('profile_options').items():
chosen_option = user_options.get(option_name)
if not chosen_option:
for (user_selected_option_name,) in selected_profile_user_options.keys():
if (
user_selected_option_name
not in profile.get('profile_options').keys()
):
raise ValueError(
f'Expected option {k} for profile {slug}, not found in posted form'
f'Expected option {user_selected_option_name} for profile {slug}, not found in posted form'
)

# Get selected options or default to the first option if none is passed
for option_name, option in profile.get('profile_options').items():
chosen_option = selected_profile_user_options.get(option_name, None)
# If none was selected get the default
if not chosen_option:
default_option = list(option['choices'].keys())[0]
for choice_name, choice in option['choices'].items():
if choice.get('default', False):
# explicit default, not the first
default_option = choice_name
chosen_option = default_option

chosen_option_overrides = option['choices'][chosen_option][
'kubespawner_override'
]
Expand Down Expand Up @@ -3051,8 +3066,13 @@ async def load_user_options(self):
self._profile_list = self._init_profile_list(profile_list)

selected_profile = self.user_options.get('profile', None)
selected_profile_user_options = dict(self.user_options)
if selected_profile:
# Remove the 'profile' key so we are left with only selected profile options
del selected_profile_user_options['profile']

if self._profile_list:
await self._load_profile(selected_profile, self.user_options)
await self._load_profile(selected_profile, selected_profile_user_options)
elif selected_profile:
self.log.warning(
"Profile %r requested, but profiles are not enabled", selected_profile
Expand Down

0 comments on commit 67b0304

Please sign in to comment.