Skip to content

Commit

Permalink
Merge pull request #630 from TomHellier/629-add-custom-annotations-to…
Browse files Browse the repository at this point in the history
…-pvcs-created-for-single-user-pods

Add `storage_extra_annotations` configuration, used when PVCs are created
  • Loading branch information
consideRatio committed Aug 13, 2022
2 parents 2526830 + 718b7f9 commit f3d44e6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
26 changes: 22 additions & 4 deletions kubespawner/spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,9 +717,7 @@ def _deprecated_changed(self, change):
`{username}`, `{userid}`, `{servername}`, `{hubnamespace}`,
`{unescaped_username}`, and `{unescaped_servername}` will be expanded if
found within strings of this configuration. The username and servername
come escaped to follow the [DNS label
standard](https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names).
found within strings of this configuration.
""",
)

Expand Down Expand Up @@ -1102,6 +1100,24 @@ def _validate_image_pull_secrets(self, proposal):
""",
)

storage_extra_annotations = Dict(
config=True,
help="""
Extra kubernetes annotations to set on the user PVCs.
The keys and values specified here would be set as annotations on the PVCs
created by kubespawner for the user. Note that these are only set
when the PVC is created, not later when this setting is updated.
See `the Kubernetes documentation <https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/>`__
for more info on what annotations are and why you might want to use them!
`{username}`, `{userid}`, `{servername}`, `{hubnamespace}`,
`{unescaped_username}`, and `{unescaped_servername}` will be expanded if
found within strings of this configuration.
""",
)

storage_extra_labels = Dict(
config=True,
help="""
Expand Down Expand Up @@ -2099,7 +2115,9 @@ def get_pvc_manifest(self):
labels = self._build_common_labels(self._expand_all(self.storage_extra_labels))
labels.update({'component': 'singleuser-storage'})

annotations = self._build_common_annotations({})
annotations = self._build_common_annotations(
self._expand_all(self.storage_extra_annotations)
)

storage_selector = self._expand_all(self.storage_selector)

Expand Down
9 changes: 9 additions & 0 deletions tests/test_spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ async def test_get_pvc_manifest():

c.KubeSpawner.pvc_name_template = "user-{username}"
c.KubeSpawner.storage_extra_labels = {"user": "{username}"}
c.KubeSpawner.storage_extra_annotations = {"user": "{username}"}
c.KubeSpawner.storage_selector = {"matchLabels": {"user": "{username}"}}

spawner = KubeSpawner(config=c, _mock=True)
Expand All @@ -579,6 +580,10 @@ async def test_get_pvc_manifest():
"component": "singleuser-storage",
"heritage": "jupyterhub",
}
assert manifest.metadata.annotations == {
"user": "mock-5fname",
"hub.jupyter.org/username": "mock_name",
}
assert manifest.spec.selector == {"matchLabels": {"user": "mock-5fname"}}


Expand Down Expand Up @@ -612,6 +617,10 @@ async def test_variable_expansion(ssl_app):
"configured_value": {"dummy": "storage-extra-labels-{username}"},
"findable_in": ["pvc"],
},
"storage_extra_annotations": {
"configured_value": {"dummy": "storage-extra-annotations-{username}"},
"findable_in": ["pvc"],
},
"extra_labels": {
"configured_value": {"dummy": "common-extra-labels-{username}"},
"findable_value": "common-extra-labels-user1",
Expand Down

0 comments on commit f3d44e6

Please sign in to comment.