Skip to content

Commit 6042288

Browse files
authored
chore: Fix port name in workloads (#209)
* chore: Fix port name in workloads * chore: move it to a helper * chore: add tests
1 parent 3b007a0 commit 6042288

7 files changed

Lines changed: 84 additions & 11 deletions

File tree

mozcloud/application/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apiVersion: v2
22
name: mozcloud
33
description: Opinionated application chart used to deploy MozCloud Kubernetes
44
resources supporting resources
5-
version: 0.13.1
5+
version: 0.13.2
66
type: application
77
dependencies:
88
- name: mozcloud-gateway-lib

mozcloud/application/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# mozcloud
22

3-
![Version: 0.13.1](https://img.shields.io/badge/Version-0.13.1-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square)
3+
![Version: 0.13.2](https://img.shields.io/badge/Version-0.13.2-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square)
44

55
Opinionated application chart used to deploy MozCloud Kubernetes resources supporting resources
66

@@ -19,7 +19,7 @@ version: 0.1.0
1919
type: application
2020
dependencies:
2121
- name: mozcloud
22-
version: ~0.13.1
22+
version: ~0.13.2
2323
repository: oci://us-west1-docker.pkg.dev/moz-fx-platform-artifacts/mozcloud-charts
2424
```
2525

mozcloud/application/templates/_helpers.tpl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,27 @@ Returns:
6161
{{- end }}
6262

6363

64+
{{- /*
65+
Produces an RFC 6335-compliant port name from an arbitrary string. Lowercases
66+
the input, replaces any character that is not a letter, digit, or hyphen with a
67+
hyphen, truncates to 15 characters, and strips any trailing hyphen.
68+
69+
Note: regexReplaceAll takes (regex, inputString, replacement) — the input must
70+
be passed as an explicit argument, not via pipeline, to avoid it being treated
71+
as the replacement value.
72+
73+
Params:
74+
. (string): (required) The source string (e.g. a container name).
75+
76+
Returns:
77+
(string) An RFC 6335-compliant port name.
78+
*/ -}}
79+
{{- define "mozcloud.portName" -}}
80+
{{- $s := . | toString | lower -}}
81+
{{- regexReplaceAll "[^a-z0-9-]" $s "-" | trunc 15 | trimSuffix "-" -}}
82+
{{- end }}
83+
84+
6485
{{- /*
6586
A debug utility that serializes the piped-in value as pretty-printed JSON and
6687
immediately fails the template render with that output as the error message.

mozcloud/application/templates/workload/deployment.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ spec:
159159
App containers
160160
*/}}
161161
{{- range $containerName, $containerConfig := $containers }}
162+
{{- $portName := include "mozcloud.portName" $containerName }}
162163
- name: {{ $containerName }}
163164
{{- /*
164165
We first look for an image and tag in the container config YAML. If
@@ -227,7 +228,7 @@ spec:
227228
(($containerConfig.healthCheck).liveness).enabled
228229
}}
229230
ports:
230-
- name: {{ $containerName }}
231+
- name: {{ $portName }}
231232
containerPort: {{ $containerConfig.port }}
232233
{{- end }}
233234
{{- if (dig "healthCheck" "liveness" "enabled" true $containerConfig) }}
@@ -247,7 +248,7 @@ spec:
247248
{{- end }}
248249
{{- end }}
249250
path: {{ default "/__lbheartbeat__" $containerConfig.healthCheck.liveness.path }}
250-
port: {{ $containerName }}
251+
port: {{ $portName }}
251252
{{- /* If liveness probe settings are defined, use those */}}
252253
{{- if (($containerConfig.healthCheck).liveness).probes }}
253254
{{- range $k, $v := $containerConfig.healthCheck.liveness.probes }}
@@ -271,7 +272,7 @@ spec:
271272
{{- end }}
272273
{{- end }}
273274
path: {{ default "/__lbheartbeat__" $containerConfig.healthCheck.readiness.path }}
274-
port: {{ $containerName }}
275+
port: {{ $portName }}
275276
{{- if (($containerConfig.healthCheck).readiness).probes }}
276277
{{- range $k, $v := $containerConfig.healthCheck.readiness.probes }}
277278
{{ $k }}: {{ $v }}
@@ -311,6 +312,7 @@ spec:
311312
{{- if $initContainers }}
312313
initContainers:
313314
{{- range $containerName, $containerConfig := $initContainers }}
315+
{{- $portName := include "mozcloud.portName" $containerName }}
314316
- name: {{ $containerName }}
315317
{{- /*
316318
We first look for an image and tag in the container config YAML. If
@@ -371,7 +373,7 @@ spec:
371373
(($containerConfig.healthCheck).liveness).enabled
372374
}}
373375
ports:
374-
- name: {{ $containerName }}
376+
- name: {{ $portName }}
375377
containerPort: {{ $containerConfig.port }}
376378
{{- end }}
377379
resources:

mozcloud/application/templates/workload/rollout.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ spec:
165165
App containers
166166
*/}}
167167
{{- range $containerName, $containerConfig := $containers }}
168+
{{- $portName := include "mozcloud.portName" $containerName }}
168169
- name: {{ $containerName }}
169170
{{- /*
170171
We first look for an image and tag in the container config YAML. If
@@ -233,7 +234,7 @@ spec:
233234
(($containerConfig.healthCheck).liveness).enabled
234235
}}
235236
ports:
236-
- name: app
237+
- name: {{ $portName }}
237238
containerPort: {{ $containerConfig.port }}
238239
{{- end }}
239240
{{- if (dig "healthCheck" "liveness" "enabled" true $containerConfig) }}
@@ -253,7 +254,7 @@ spec:
253254
{{- end }}
254255
{{- end }}
255256
path: {{ default "/__lbheartbeat__" $containerConfig.healthCheck.liveness.path }}
256-
port: app
257+
port: {{ $portName }}
257258
{{- /* If liveness probe settings are defined, use those */}}
258259
{{- if (($containerConfig.healthCheck).liveness).probes }}
259260
{{- range $k, $v := $containerConfig.healthCheck.liveness.probes }}
@@ -277,7 +278,7 @@ spec:
277278
{{- end }}
278279
{{- end }}
279280
path: {{ default "/__lbheartbeat__" $containerConfig.healthCheck.readiness.path }}
280-
port: app
281+
port: {{ $portName }}
281282
{{- if (($containerConfig.healthCheck).readiness).probes }}
282283
{{- range $k, $v := $containerConfig.healthCheck.readiness.probes }}
283284
{{ $k }}: {{ $v }}
@@ -317,6 +318,7 @@ spec:
317318
{{- if $initContainers }}
318319
initContainers:
319320
{{- range $containerName, $containerConfig := $initContainers }}
321+
{{- $portName := include "mozcloud.portName" $containerName }}
320322
- name: {{ $containerName }}
321323
{{- /*
322324
We first look for an image and tag in the container config YAML. If
@@ -377,7 +379,7 @@ spec:
377379
(($containerConfig.healthCheck).liveness).enabled
378380
}}
379381
ports:
380-
- name: {{ $containerName }}
382+
- name: {{ $portName }}
381383
containerPort: {{ $containerConfig.port }}
382384
{{- end }}
383385
resources:
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
suite: "mozcloud: Port name sanitization"
3+
release:
4+
name: mozcloud-test
5+
namespace: mozcloud-test-dev
6+
# By overriding the chart version, we are preventing the labels chart from
7+
# updating the mozcloud_chart_version label for every snapshot every time we
8+
# bump chart versions.
9+
chart:
10+
version: 1.0.0
11+
values:
12+
- values/globals.yaml
13+
- values/port-name-sanitization.yaml
14+
templates:
15+
- workload/deployment.yaml
16+
tests:
17+
- it: Ensure no failures occur
18+
asserts:
19+
- notFailedTemplate: {}
20+
- it: Port name is truncated to 15 characters for RFC 6335 compliance
21+
documentSelector:
22+
path: $[?(@.kind == "Deployment")].metadata.name
23+
value: test-service
24+
asserts:
25+
- equal:
26+
path: spec.template.spec.containers[?(@.name=="my-long_container")].ports[0].name
27+
value: my-long-contain
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
workloads:
3+
test-service:
4+
component: web
5+
containers:
6+
# This name is intentionally > 15 characters to test truncation.
7+
# It also intentionally contains `_` characters to test substitution.
8+
# Expected port name: "my-long-contain" (truncated from "my-long-container")
9+
my-long_container:
10+
image:
11+
repository: test-repo/test-image
12+
tag: 1.0.0
13+
hosts:
14+
test-service:
15+
domains:
16+
- test-service.dev.test-domain.com
17+
addresses:
18+
- mozcloud-dev-ip-v4
19+
tls:
20+
certs:
21+
- test-service-nonprod-dev

0 commit comments

Comments
 (0)