common(ports): detect potential issue with port names (#29051)
**Description** <!-- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. --> ⚒️ Fixes #29041 **⚙️ Type of change** - [ ] ⚙️ Feature/App addition - [x] 🪛 Bugfix - [ ] ⚠️ Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] 🔃 Refactor of current code **🧪 How Has This Been Tested?** <!-- Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration --> **📃 Notes:** <!-- Please enter any other relevant information here --> **✔️ Checklist:** - [x] ⚖️ My code follows the style guidelines of this project - [x] 👀 I have performed a self-review of my own code - [x] #️⃣ I have commented my code, particularly in hard-to-understand areas - [ ] 📄 I have made corresponding changes to the documentation - [x] ⚠️ My changes generate no new warnings - [x] 🧪 I have added tests to this description that prove my fix is effective or that my feature works - [x] ⬆️ I increased versions for any altered app according to semantic versioning - [x] I made sure the title starts with `feat(chart-name):`, `fix(chart-name):` or `chore(chart-name):` **➕ App addition** If this PR is an app addition please make sure you have done the following. - [ ] 🖼️ I have added an icon in the Chart's root directory called `icon.png` --- _Please don't blindly check all the boxes. Read them and only check those that apply. Those checkboxes are there for the reviewer to see what is this all about and the status of this PR with a quick glance._
This commit is contained in:
@@ -303,3 +303,100 @@ tests:
|
||||
containerPort: 1234
|
||||
hostPort: null
|
||||
protocol: UDP
|
||||
|
||||
- it: should fail if there are duplicate ports but they are not adjacent
|
||||
set:
|
||||
service:
|
||||
my-service1:
|
||||
enabled: true
|
||||
primary: true
|
||||
targetSelector: workload-name1
|
||||
ports:
|
||||
my-port:
|
||||
enabled: true
|
||||
primary: true
|
||||
port: 1234
|
||||
protocol: tcp
|
||||
targetSelector: container-name1
|
||||
my-port2:
|
||||
enabled: true
|
||||
port: 1235
|
||||
protocol: tcp
|
||||
targetSelector: container-name1
|
||||
my-port3:
|
||||
enabled: true
|
||||
port: 1234
|
||||
protocol: udp
|
||||
targetSelector: container-name1
|
||||
workload:
|
||||
workload-name1:
|
||||
enabled: true
|
||||
primary: true
|
||||
type: Deployment
|
||||
podSpec:
|
||||
containers:
|
||||
container-name1:
|
||||
enabled: true
|
||||
primary: true
|
||||
imageSelector: image
|
||||
probes: *probes
|
||||
asserts:
|
||||
- failedTemplate:
|
||||
errorMessage: "Port number [1234] is used by multiple ports [my-port, my-port3] in the service [my-service1] but their names are not adjacent when sorted alphabetically (Other ports in this container sorted: [my-port, my-port2, my-port3]). This can cause issues with Kubernetes port updates."
|
||||
|
||||
- it: should pass if there are duplicate ports but they are adjacent
|
||||
set:
|
||||
service:
|
||||
my-service1:
|
||||
enabled: true
|
||||
primary: true
|
||||
targetSelector: workload-name1
|
||||
ports:
|
||||
my-port:
|
||||
enabled: true
|
||||
primary: true
|
||||
port: 1234
|
||||
protocol: tcp
|
||||
targetSelector: container-name1
|
||||
my-port2:
|
||||
enabled: true
|
||||
port: 1234
|
||||
protocol: udp
|
||||
targetSelector: container-name1
|
||||
my-port3:
|
||||
enabled: true
|
||||
port: 1235
|
||||
protocol: udp
|
||||
targetSelector: container-name1
|
||||
workload:
|
||||
workload-name1:
|
||||
enabled: true
|
||||
primary: true
|
||||
type: Deployment
|
||||
podSpec:
|
||||
containers:
|
||||
container-name1:
|
||||
enabled: true
|
||||
primary: true
|
||||
imageSelector: image
|
||||
probes: *probes
|
||||
asserts:
|
||||
- documentIndex: &deploymentDoc 0
|
||||
isKind:
|
||||
of: Deployment
|
||||
- documentIndex: *deploymentDoc
|
||||
equal:
|
||||
path: spec.template.spec.containers[0].ports
|
||||
value:
|
||||
- name: my-port
|
||||
containerPort: 1234
|
||||
protocol: TCP
|
||||
hostPort: null
|
||||
- name: my-port2
|
||||
containerPort: 1234
|
||||
protocol: UDP
|
||||
hostPort: null
|
||||
- name: my-port3
|
||||
containerPort: 1235
|
||||
protocol: UDP
|
||||
hostPort: null
|
||||
|
||||
@@ -19,7 +19,7 @@ keywords:
|
||||
- truecharts
|
||||
- library-chart
|
||||
- common
|
||||
kubeVersion: '>=1.24.0-0'
|
||||
kubeVersion: ">=1.24.0-0"
|
||||
maintainers:
|
||||
- name: TrueCharts
|
||||
email: info@truecharts.org
|
||||
@@ -48,4 +48,4 @@ sources:
|
||||
- https://hub.docker.com/r/mikefarah/yq
|
||||
- https://hub.docker.com/r/traefik/whoami
|
||||
type: library
|
||||
version: 25.1.6
|
||||
version: 25.1.7
|
||||
|
||||
@@ -8,6 +8,8 @@ objectData: The object data to be used to render the container.
|
||||
{{- $rootCtx := .rootCtx -}}
|
||||
{{- $objectData := .objectData -}}
|
||||
|
||||
{{- $portsByName := dict -}}
|
||||
|
||||
{{- range $serviceName, $serviceValues := $rootCtx.Values.service -}}
|
||||
{{- $podSelected := false -}}
|
||||
{{/* If service is enabled... */}}
|
||||
@@ -73,15 +75,58 @@ objectData: The object data to be used to render the container.
|
||||
{{- else }}
|
||||
hostPort: null
|
||||
{{- end -}}
|
||||
{{- $_ := set $portsByName $portName (dict "containerPort" (toString $containerPort) "serviceName" $serviceName) -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- include "tc.v1.common.lib.container.ports.detectSortingIssues" (dict "portsByName" $portsByName "rootCtx" $rootCtx) -}}
|
||||
|
||||
{{- end -}}
|
||||
{{/* Turning hostNetwork on, it creates hostPort automatically and turning it back off does not remove them. Setting hostPort explicitly to null will remove them.
|
||||
There are still cases that hostPort is not removed, for example, if you have a TCP and UDP port with the same number. Only the TCPs hostPort will be removed.
|
||||
Also note that setting hostPort to null always, it will NOT affect hostNetwork, as it will still create the hostPorts.
|
||||
It only helps to remove them when hostNetwork is turned off.
|
||||
*/}}
|
||||
|
||||
|
||||
{{- define "tc.v1.common.lib.container.ports.detectSortingIssues" -}}
|
||||
{{- $rootCtx := .rootCtx -}}
|
||||
{{- $portsByName := .portsByName -}}
|
||||
|
||||
{{- $portCounts := dict -}}
|
||||
{{- range $name, $portValues := $portsByName -}}
|
||||
{{- $count := 1 -}}
|
||||
{{- $port := (get $portValues "containerPort") -}}
|
||||
{{- if hasKey $portCounts $port -}}
|
||||
{{- $count = add1 (get $portCounts $port) -}}
|
||||
{{- end -}}
|
||||
{{- $_ := set $portCounts $port $count -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- $sorted := keys $portsByName | sortAlpha -}}
|
||||
{{- range $idx, $name := $sorted -}}
|
||||
{{- $portValues := (get $portsByName $name) -}}
|
||||
{{- $port := $portValues.containerPort -}}
|
||||
{{- if eq (get $portCounts $port) 1 -}}
|
||||
{{- continue -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- if lt $idx (sub (len $sorted) 1) -}}
|
||||
{{- $nextPort := (get $portsByName (index $sorted (add1 $idx))).containerPort -}}
|
||||
{{- if ne $port $nextPort -}}
|
||||
{{- $portNamesUsingNum := list -}}
|
||||
{{- range $name, $p := $portsByName -}}
|
||||
{{- if eq $p.containerPort $port -}}
|
||||
{{- $portNamesUsingNum = mustAppend $portNamesUsingNum $name -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- fail (printf "Port number [%s] is used by multiple ports [%s] in the service [%s] but their names are not adjacent when sorted alphabetically (Other ports in this container sorted: [%s]). This can cause issues with Kubernetes port updates." $port (join ", " $portNamesUsingNum) $portValues.serviceName (join ", " (keys $portsByName | sortAlpha))) -}}
|
||||
{{- end -}}
|
||||
{{- $_ := set $portCounts $port 1 -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
@@ -38,7 +38,7 @@ limits:
|
||||
{{- if not $objectData.resources.excludeExtra -}}
|
||||
{{- range $k, $v := (omit $resources.limits "cpu" "memory") }} {{/* Omit cpu and memory, as they are handled above */}}
|
||||
{{- if or (not $v) (eq (toString $v) "0") -}}
|
||||
{{ continue }}
|
||||
{{- continue -}}
|
||||
{{- end }}
|
||||
{{ $k }}: {{ $v }}
|
||||
{{- end -}}
|
||||
|
||||
Reference in New Issue
Block a user