Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Legacy PVC check can be overridden #874

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 31 additions & 22 deletions kubespawner/spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3058,6 +3058,25 @@ async def _check_pvc_exists(self, pvc_name, namespace):
raise
return True

async def _check_legacy_pvc_exists(self):
"""
Check if a legacy PVC exists.

Override this if pvc_name_template changed across upgrades,
or if you want to handle legacy PVCs differently.

Returns the PVC name if found and should be used, otherwise None
"""
legacy_pvc_name = self._expand_user_properties(
self.pvc_name_template, slug_scheme="escape"
)
if legacy_pvc_name != self.pvc_name:
self.log.debug(
f"Checking for legacy-named pvc {legacy_pvc_name} for {self.user.name}"
)
if await self._check_pvc_exists(legacy_pvc_name, self.namespace):
return legacy_pvc_name

async def _start(self):
"""Start the user's pod"""

Expand Down Expand Up @@ -3107,29 +3126,19 @@ async def _start(self):
# pvc name wasn't reliably persisted before kubespawner 7,
# so if the name changed check if a pvc with the legacy name exists and use it.
# This will be persisted in state on next launch in the future,
# so the comparison below will be False for launches after the first.
# this check will only work if pvc_name_template itself has not changed across the upgrade.
legacy_pvc_name = self._expand_user_properties(
self.pvc_name_template, slug_scheme="escape"
)
if legacy_pvc_name != self.pvc_name:
self.log.debug(
f"Checking for legacy-named pvc {legacy_pvc_name} for {self.user.name}"
)
if await self._check_pvc_exists(self.pvc_name, self.namespace):
# if current name exists: use it
# so the check below will be False for launches after the first.
if await self._check_pvc_exists(self.pvc_name, self.namespace):
# Current name exists: use it
self._pvc_exists = True
else:
legacy_pvc_name = await self._check_legacy_pvc_exists()
if legacy_pvc_name:
# Legacy PVC exists, use it to avoid data loss
self.log.warning(
f"Using legacy pvc {legacy_pvc_name} for {self.user.name}"
)
self.pvc_name = legacy_pvc_name
self._pvc_exists = True
else:
# current name doesn't exist, check if legacy name exists
if await self._check_pvc_exists(
legacy_pvc_name, self.namespace
):
# legacy name exists, use it to avoid data loss
self.log.warning(
f"Using legacy pvc {legacy_pvc_name} for {self.user.name}"
)
self.pvc_name = legacy_pvc_name
self._pvc_exists = True

pvc = self.get_pvc_manifest()
# If there's a timeout, just let it propagate
Expand Down