Skip to content

Commit

Permalink
Merge pull request #770 from minrk/fix-trailing-dash
Browse files Browse the repository at this point in the history
only strip trailing '-' if it was added by a template variable
  • Loading branch information
manics committed Aug 24, 2023
2 parents 297cb3f + feaac0b commit 5a90351
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
7 changes: 5 additions & 2 deletions kubespawner/spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -1841,8 +1841,11 @@ def _expand_user_properties(self, template):
hubnamespace=hub_namespace,
)
# strip trailing - delimiter in case of empty servername.
# k8s object names cannot have trailing -
return rendered.rstrip("-")
# but only if trailing '-' is added by the template rendering,
# and not in the template itself
if not template.endswith("-"):
rendered = rendered.rstrip("-")
return rendered

def _expand_all(self, src):
if isinstance(src, list):
Expand Down
34 changes: 34 additions & 0 deletions tests/test_spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,40 @@ async def test_get_pod_manifest_tolerates_mixed_input():
assert isinstance(manifest.spec.init_containers[1], V1Container)


async def test_expansion_hyphens():
c = Config()

c.KubeSpawner.init_containers = [
{
'name': 'mock_name_1',
'image': 'mock_image_1',
'command': [
'mock_command_1',
'--',
'{unescaped_username}',
'{unescaped_username}-',
'-x',
],
}
]

spawner = KubeSpawner(config=c, _mock=True)

# this test ensures the following line doesn't raise an error
manifest = await spawner.get_pod_manifest()
assert isinstance(manifest, V1Pod)
container = manifest.spec.init_containers[0]
assert isinstance(container, V1Container)

assert container.command == [
'mock_command_1',
'--',
spawner.user.name,
spawner.user.name + "-",
'-x',
]


_test_profiles = [
{
'display_name': 'Training Env - Python',
Expand Down

0 comments on commit 5a90351

Please sign in to comment.