Skip to content

Commit

Permalink
Merge pull request #655 from dolfinus/ingress_annotations
Browse files Browse the repository at this point in the history
[KubeIngressProxy] Add ingress_extra_annotations and ingress_extra_labels
  • Loading branch information
consideRatio committed Nov 6, 2022
2 parents deca2b9 + d63a091 commit 49f4109
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 13 deletions.
29 changes: 23 additions & 6 deletions kubespawner/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,8 @@ def make_ingress(
target: str,
data: dict,
common_labels: Optional[dict] = None,
ingress_extra_labels: Optional[dict] = None,
ingress_extra_annotations: Optional[dict] = None,
):
"""
Returns an ingress, service, endpoint object that'll work for this service
Expand All @@ -747,13 +749,15 @@ def make_ingress(
common_labels = (common_labels or {}).copy()
common_labels.update(default_labels)

common_annotations = {
'hub.jupyter.org/proxy-data': json.dumps(data),
'hub.jupyter.org/proxy-routespec': routespec,
'hub.jupyter.org/proxy-target': target,
}

meta = V1ObjectMeta(
name=name,
annotations={
'hub.jupyter.org/proxy-data': json.dumps(data),
'hub.jupyter.org/proxy-routespec': routespec,
'hub.jupyter.org/proxy-target': target,
},
annotations=common_annotations,
labels=common_labels,
)

Expand Down Expand Up @@ -812,9 +816,22 @@ def make_ingress(
)

# Make Ingress object

ingress_labels = common_labels.copy()
ingress_labels.update(ingress_extra_labels or {})

ingress_annotations = common_annotations.copy()
ingress_annotations.update(ingress_extra_annotations or {})

ingress_meta = V1ObjectMeta(
name=name,
annotations=ingress_annotations,
labels=ingress_labels,
)

ingress = V1Ingress(
kind='Ingress',
metadata=meta,
metadata=ingress_meta,
spec=V1IngressSpec(
rules=[
V1IngressRule(
Expand Down
52 changes: 51 additions & 1 deletion kubespawner/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,45 @@ def _namespace_default(self):
""",
)

ingress_extra_labels = Dict(
config=True,
help="""
Extra kubernetes labels to set to Ingress objects.
The keys and values must both be strings that match the kubernetes
label key / value constraints.
See `the Kubernetes documentation <https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/>`__
for more info on what labels are and why you might want to use them!
`{username}`, `{servername}`, `{servicename}`, `{routespec}`, `{hubnamespace}`,
`{unescaped_username}`, `{unescaped_servername}`, `{unescaped_servicename}` and `{unescaped_routespec}` will be expanded if
found within strings of this configuration.
Names have to be are escaped to follow the [DNS label
standard](https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names).
""",
)

ingress_extra_annotations = Dict(
config=True,
help="""
Extra kubernetes annotations to set on the Ingress object.
The keys and values must both be strings.
See `the Kubernetes documentation <https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/>`__
for more info on what labels are and why you might want to use them!
`{username}`, `{servername}`, `{servicename}`, `{routespec}`, `{hubnamespace}`,
`{unescaped_username}`, `{unescaped_servername}`, `{unescaped_servicename}` and `{unescaped_routespec}` will be expanded if
found within strings of this configuration.
Names have to be are escaped to follow the [DNS label
standard](https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names).
""",
)

k8s_api_ssl_ca_cert = Unicode(
"",
config=True,
Expand Down Expand Up @@ -317,14 +356,25 @@ async def add_route(self, routespec, target, data):
# 'data' is JSON encoded and put in an annotation - we don't need to query for it

safe_name = self._safe_name_for_routespec(routespec).lower()

common_labels = self._expand_all(self.common_labels, routespec, data)
common_labels.update({'component': self.component_label})

ingress_extra_labels = self._expand_all(
self.ingress_extra_labels, routespec, data
)
ingress_extra_annotations = self._expand_all(
self.ingress_extra_annotations, routespec, data
)

endpoint, service, ingress = make_ingress(
name=safe_name,
routespec=routespec,
target=target,
common_labels=common_labels,
data=data,
common_labels=common_labels,
ingress_extra_labels=ingress_extra_labels,
ingress_extra_annotations=ingress_extra_annotations,
)

async def ensure_object(create_func, patch_func, body, kind):
Expand Down
29 changes: 23 additions & 6 deletions tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1985,16 +1985,25 @@ def test_make_ingress(target, ip):
Test specification of the ingress objects
"""
common_labels = {
'app': 'jupyterhub',
'heritage': 'jupyterhub',
'component': 'singleuser-server',
}
ingress_extra_labels = {
'extra/label': 'value1',
}
ingress_extra_annotations = {
'extra/annotation': 'value2',
}
endpoint, service, ingress = api_client.sanitize_for_serialization(
make_ingress(
name='jupyter-test',
routespec='/my-path',
target=target,
data={"mykey": "myvalue"},
common_labels=common_labels,
ingress_extra_labels=ingress_extra_labels,
ingress_extra_annotations=ingress_extra_annotations,
)
)

Expand All @@ -2007,8 +2016,9 @@ def test_make_ingress(target, ip):
'hub.jupyter.org/proxy-target': target,
},
'labels': {
'component': 'singleuser-server',
'app': 'jupyterhub',
'heritage': 'jupyterhub',
'component': 'singleuser-server',
'hub.jupyter.org/proxy-route': 'true',
},
'name': 'jupyter-test',
Expand All @@ -2025,8 +2035,9 @@ def test_make_ingress(target, ip):
'hub.jupyter.org/proxy-target': target,
},
'labels': {
'component': 'singleuser-server',
'app': 'jupyterhub',
'heritage': 'jupyterhub',
'component': 'singleuser-server',
'hub.jupyter.org/proxy-route': 'true',
},
'name': 'jupyter-test',
Expand All @@ -2044,11 +2055,14 @@ def test_make_ingress(target, ip):
'hub.jupyter.org/proxy-data': '{"mykey": "myvalue"}',
'hub.jupyter.org/proxy-routespec': '/my-path',
'hub.jupyter.org/proxy-target': target,
'extra/annotation': 'value2',
},
'labels': {
'component': 'singleuser-server',
'app': 'jupyterhub',
'heritage': 'jupyterhub',
'component': 'singleuser-server',
'hub.jupyter.org/proxy-route': 'true',
'extra/label': 'value1',
},
'name': 'jupyter-test',
},
Expand Down Expand Up @@ -2082,6 +2096,7 @@ def test_make_ingress_external_name():
Test specification of the ingress objects
"""
common_labels = {
'app': 'jupyterhub',
'heritage': 'jupyterhub',
'component': 'singleuser-server',
}
Expand All @@ -2095,7 +2110,7 @@ def test_make_ingress_external_name():
)
)

assert endpoint == None
assert endpoint is None

assert service == {
'kind': 'Service',
Expand All @@ -2106,8 +2121,9 @@ def test_make_ingress_external_name():
'hub.jupyter.org/proxy-target': 'http://my-pod-name:9000',
},
'labels': {
'component': 'singleuser-server',
'app': 'jupyterhub',
'heritage': 'jupyterhub',
'component': 'singleuser-server',
'hub.jupyter.org/proxy-route': 'true',
},
'name': 'jupyter-test',
Expand All @@ -2128,8 +2144,9 @@ def test_make_ingress_external_name():
'hub.jupyter.org/proxy-target': 'http://my-pod-name:9000',
},
'labels': {
'component': 'singleuser-server',
'app': 'jupyterhub',
'heritage': 'jupyterhub',
'component': 'singleuser-server',
'hub.jupyter.org/proxy-route': 'true',
},
'name': 'jupyter-test',
Expand Down

0 comments on commit 49f4109

Please sign in to comment.