Skip to content

Commit e7fe884

Browse files
authored
Merge pull request #3404 from consideRatio/pr/modern-label-naming
Add recommended chart labels alongside old labels (`app.kubernetes.io/...`, `helm.sh/chart`)
2 parents bd766de + 15f72f7 commit e7fe884

File tree

10 files changed

+98
-31
lines changed

10 files changed

+98
-31
lines changed

ci/common

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ await_autohttps_tls_cert_acquisition() {
5050
kubectl logs deploy/pebble --all-containers # --prefix
5151
kubectl describe pod -l app.kubernetes.io/name=pebble-coredns
5252
kubectl logs deploy/pebble-coredns --all-containers # --prefix
53-
kubectl describe pod -l component=autohttps
53+
kubectl describe pod -l app.kubernetes.io/component=autohttps
5454
kubectl logs deploy/autohttps --all-containers # --prefix
5555
exit 1
5656
fi
@@ -72,7 +72,7 @@ await_autohttps_tls_cert_save() {
7272
kubectl logs deploy/pebble --all-containers # --prefix
7373
kubectl describe pod -l app.kubernetes.io/name=pebble-coredns
7474
kubectl logs deploy/pebble-coredns --all-containers # --prefix
75-
kubectl describe pod -l component=autohttps
75+
kubectl describe pod -l app.kubernetes.io/component=autohttps
7676
kubectl logs deploy/autohttps --all-containers # --prefix
7777
exit 1
7878
fi

docs/source/administrator/services.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ proxy:
8282
- to:
8383
- podSelector:
8484
matchLabels:
85-
app: jupyterhub
86-
component: hub
85+
app.kubernetes.io/name: jupyterhub
86+
app.kubernetes.io/component: hub
8787
ports:
8888
- port: 8181
8989
```

docs/source/resources/community.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ user pods. You can search for all user pods with the following label query:
9494

9595
```bash
9696
kubectl --namespace=<YOUR-NAMESPACE> get pod \
97-
-l "component=singleuser-server"
97+
-l "app.kubernetes.io/component=singleuser-server"
9898
```
9999

100100
For more information, see the [Kubernetes labels and selectors page](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/).

jupyterhub/files/hub/jupyterhub_config.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,28 +107,35 @@ def camelCaseify(s):
107107
)
108108

109109
# implement common labels
110-
# this duplicates the jupyterhub.commonLabels helper
110+
# This mimics the jupyterhub.commonLabels helper, but declares managed-by to
111+
# kubespawner instead of helm.
112+
#
113+
# The labels app and release are old labels enabled to be deleted in z2jh 5, but
114+
# for now retained to avoid a breaking change in z2jh 4 that would force user
115+
# server restarts. Restarts would be required because NetworkPolicy resources
116+
# must select old/new pods with labels that then needs to be seen on both
117+
# old/new pods, and we want these resources to keep functioning for old/new user
118+
# server pods during an upgrade.
119+
#
111120
common_labels = c.KubeSpawner.common_labels = {}
112-
common_labels["app"] = get_config(
121+
common_labels["app.kubernetes.io/name"] = common_labels["app"] = get_config(
113122
"nameOverride",
114123
default=get_config("Chart.Name", "jupyterhub"),
115124
)
116-
common_labels["heritage"] = "jupyterhub"
125+
release = get_config("Release.Name")
126+
if release:
127+
common_labels["app.kubernetes.io/instance"] = common_labels["release"] = release
117128
chart_name = get_config("Chart.Name")
118129
chart_version = get_config("Chart.Version")
119130
if chart_name and chart_version:
120-
common_labels["chart"] = "{}-{}".format(
121-
chart_name,
122-
chart_version.replace("+", "_"),
131+
common_labels["helm.sh/chart"] = common_labels["chart"] = (
132+
f"{chart_name}-{chart_version.replace('+', '_')}"
123133
)
124-
release = get_config("Release.Name")
125-
if release:
126-
common_labels["release"] = release
134+
common_labels["app.kubernetes.io/managed-by"] = "kubespawner"
127135

128136
c.KubeSpawner.namespace = os.environ.get("POD_NAMESPACE", "default")
129137

130138
# Max number of consecutive failures before the Hub restarts itself
131-
# requires jupyterhub 0.9.2
132139
set_config_if_not_none(
133140
c.Spawner,
134141
"consecutive_failure_limit",

jupyterhub/templates/_helpers.tpl

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
- commonLabels | uses appLabel
4949
- labels | uses commonLabels
5050
- matchLabels | uses labels
51-
- podCullerSelector | uses matchLabels
5251

5352

5453
## Example usage
@@ -112,31 +111,62 @@
112111
{{- /*
113112
jupyterhub.commonLabels:
114113
Foundation for "jupyterhub.labels".
115-
Provides labels: app, release, (chart and heritage).
114+
115+
Provides old labels:
116+
app
117+
release
118+
chart (omitted for matchLabels)
119+
heritage (omitted for matchLabels)
120+
Provides modern labels (omitted for matchLabels):
121+
app.kubernetes.io/name ("app")
122+
app.kubernetes.io/instance ("release")
123+
helm.sh/chart ("chart")
124+
app.kubernetes.io/managed-by ("heritage")
116125
*/}}
117126
{{- define "jupyterhub.commonLabels" -}}
118-
app: {{ .appLabel | default (include "jupyterhub.appLabel" .) }}
119-
release: {{ .Release.Name }}
127+
app: {{ .appLabel | default (include "jupyterhub.appLabel" .) | quote }}
128+
release: {{ .Release.Name | quote }}
120129
{{- if not .matchLabels }}
121130
chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
122-
heritage: {{ .heritageLabel | default .Release.Service }}
131+
heritage: {{ .Release.Service }}
132+
app.kubernetes.io/name: {{ .appLabel | default (include "jupyterhub.appLabel" .) | quote }}
133+
app.kubernetes.io/instance: {{ .Release.Name | quote }}
134+
helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
135+
app.kubernetes.io/managed-by: {{ .Release.Service }}
123136
{{- end }}
124137
{{- end }}
125138
126139
127140
{{- /*
128141
jupyterhub.labels:
129-
Provides labels: component, app, release, (chart and heritage).
142+
Provides old labels:
143+
component
144+
app
145+
release
146+
chart (omitted for matchLabels)
147+
heritage (omitted for matchLabels)
148+
Provides modern labels (omitted for matchLabels):
149+
app.kubernetes.io/component ("component")
150+
app.kubernetes.io/name ("app")
151+
app.kubernetes.io/instance release ("release")
152+
helm.sh/chart ("chart")
153+
app.kubernetes.io/managed-by ("heritage")
130154
*/}}
131155
{{- define "jupyterhub.labels" -}}
132156
component: {{ include "jupyterhub.componentLabel" . }}
157+
{{- if not .matchLabels }}
158+
app.kubernetes.io/component: {{ include "jupyterhub.componentLabel" . }}
159+
{{- end }}
133160
{{ include "jupyterhub.commonLabels" . }}
134161
{{- end }}
135162
136163
137164
{{- /*
138165
jupyterhub.matchLabels:
139-
Used to provide pod selection labels: component, app, release.
166+
Provides old labels:
167+
component
168+
app
169+
release
140170
*/}}
141171
{{- define "jupyterhub.matchLabels" -}}
142172
{{- $_ := merge (dict "matchLabels" true) . -}}

jupyterhub/templates/hub/secret.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type: Opaque
88
data:
99
{{- $values := merge dict .Values }}
1010
{{- /* also passthrough subset of Chart / Release */}}
11-
{{- $_ := set $values "Chart" (dict "Name" .Chart.Name "Version" .Chart.Version) }}
11+
{{- $_ := set $values "Chart" (dict "Name" .Chart.Name "Version" .Chart.Version "AppVersion" .Chart.AppVersion) }}
1212
{{- $_ := set $values "Release" (pick .Release "Name" "Namespace" "Service") }}
1313
values.yaml: {{ $values | toYaml | b64enc | quote }}
1414

jupyterhub/templates/proxy/autohttps/deployment.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ spec:
130130
{{- end }}
131131
args:
132132
- watch-save
133-
- --label=app={{ include "jupyterhub.appLabel" . }}
134-
- --label=release={{ .Release.Name }}
135-
- --label=chart={{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
136-
- --label=heritage=secret-sync
133+
- --label=app.kubernetes.io/name={{ include "jupyterhub.appLabel" . }}
134+
- --label=app.kubernetes.io/instance={{ .Release.Name }}
135+
- --label=helm.sh/chart={{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
136+
- --label=app.kubernetes.io/managed-by=secret-sync
137137
- {{ include "jupyterhub.proxy-public-tls.fullname" . }}
138138
- acme.json
139139
- /etc/acme/acme.json

jupyterhub/values.schema.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ properties:
625625
- to:
626626
- podSelector:
627627
matchLabels:
628-
app: my-k8s-local-service
628+
app.kubernetes.io/name: my-k8s-local-service
629629
ports:
630630
- protocol: TCP
631631
port: 5978

tests/test_spawn.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import subprocess
23
import time
34

@@ -22,7 +23,7 @@ def test_spawn_basic(
2223
r = api_request.post("/users/" + jupyter_user + "/server")
2324
assert r.status_code in (201, 202)
2425
try:
25-
# check successfull spawn
26+
# check successful spawn
2627
server_model = _wait_for_user_to_spawn(
2728
api_request, jupyter_user, request_data["test_timeout"]
2829
)
@@ -36,8 +37,37 @@ def test_spawn_basic(
3637
assert r.status_code == 200
3738
assert "version" in r.json()
3839

39-
# check user pod's extra environment variable
4040
pod_name = server_model["state"]["pod_name"]
41+
42+
# check user pod's labels
43+
pod_json = subprocess.check_output(
44+
[
45+
"kubectl",
46+
"get",
47+
"pod",
48+
"--output=json",
49+
pod_name,
50+
]
51+
)
52+
pod = json.loads(pod_json)
53+
pod_labels = pod["metadata"]["labels"]
54+
55+
# check for modern labels
56+
assert pod_labels["app.kubernetes.io/name"] == "jupyterhub"
57+
assert "app.kubernetes.io/instance" in pod_labels
58+
# FIXME: app.kubernetes.io/component for user pods require kubespawner
59+
# with https://github.com/jupyterhub/kubespawner/pull/835.
60+
# assert pod_labels["app.kubernetes.io/component"] == "singleuser-server"
61+
assert pod_labels["helm.sh/chart"].startswith("jupyterhub-")
62+
assert pod_labels["app.kubernetes.io/managed-by"] == "kubespawner"
63+
64+
# check for legacy labels still meant to be around
65+
assert pod_labels["app"] == "jupyterhub"
66+
assert "release" in pod_labels
67+
assert pod_labels["chart"].startswith("jupyterhub-")
68+
assert pod_labels["component"] == "singleuser-server"
69+
70+
# check user pod's extra environment variable
4171
c = subprocess.run(
4272
[
4373
"kubectl",

tools/templates/lint-and-validate-values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ hub:
186186
- to:
187187
- podSelector:
188188
matchLabels:
189-
app: my-k8s-local-service
189+
app.kubernetes.io/name: my-k8s-local-service
190190
ports:
191191
- protocol: TCP
192192
port: 5978

0 commit comments

Comments
 (0)