From ccbfef0b0fbdffc06c2242523b3e56eecc0b6bd5 Mon Sep 17 00:00:00 2001 From: Stavros Kois <47820033+stavros-k@users.noreply.github.com> Date: Sat, 15 Apr 2023 23:05:28 +0300 Subject: [PATCH] fix(portcheck,externalip): Convert everything to int for port check and fix protocol check logic for externalIP type (#391) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description** ⚒️ Fixes https://github.com/truecharts/charts/issues/7983 ⚒️ Fixes https://github.com/truecharts/charts/issues/8028 **⚙️ Type of change** - [ ] ⚙️ Feature/App addition - [ ] 🪛 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?** **📃 Notes:** **✔️ Checklist:** - [ ] ⚖️ My code follows the style guidelines of this project - [ ] 👀 I have performed a self-review of my own code - [ ] #️⃣ I have commented my code, particularly in hard-to-understand areas - [ ] 📄 I have made corresponding changes to the documentation - [ ] ⚠️ My changes generate no new warnings - [ ] 🧪 I have added tests to this description that prove my fix is effective or that my feature works - [ ] ⬆️ I increased versions for any altered app according to semantic versioning **➕ App addition** If this PR is an app addition please make sure you have done the following. - [ ] 🪞 I have opened a PR on [truecharts/containers](https://github.com/truecharts/containers) adding the container to TrueCharts mirror repo. - [ ] 🖼️ 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._ --- .../tests/service/external_ip_test.yaml | 53 +++++++++++++++++++ library/common/Chart.yaml | 2 +- library/common/templates/class/_service.tpl | 24 ++++----- .../templates/helpers/_getPortRange.tpl | 7 ++- 4 files changed, 69 insertions(+), 17 deletions(-) diff --git a/library/common-test/tests/service/external_ip_test.yaml b/library/common-test/tests/service/external_ip_test.yaml index 1e60c317..4489a371 100644 --- a/library/common-test/tests/service/external_ip_test.yaml +++ b/library/common-test/tests/service/external_ip_test.yaml @@ -143,3 +143,56 @@ tests: value: - addresses: - 1.1.1.1 + + - it: should pass with type ExternalIP and https + set: + service: + my-service: + enabled: true + primary: true + type: ExternalIP + externalIP: 1.1.1.1 + ports: + port-name: + enabled: true + primary: true + port: 443 + protocol: https + asserts: + - documentIndex: *serviceDoc + equal: + path: spec + value: + publishNotReadyAddresses: false + ports: + - name: port-name + port: 443 + protocol: TCP + targetPort: 443 + - documentIndex: &endpointSliceDoc 1 + isKind: + of: EndpointSlice + - documentIndex: *endpointSliceDoc + isAPIVersion: + of: discovery.k8s.io/v1 + - documentIndex: *endpointSliceDoc + equal: + path: ports + value: + - name: port-name + port: 443 + protocol: TCP + - documentIndex: *endpointSliceDoc + equal: + path: endpoints + value: + - addresses: + - 1.1.1.1 + - documentIndex: &serviceDoc 0 + isKind: + of: Service + - documentIndex: *serviceDoc + isSubset: + path: metadata.annotations + content: + traefik.ingress.kubernetes.io/service.serversscheme: https diff --git a/library/common/Chart.yaml b/library/common/Chart.yaml index 17c18316..27b0191b 100644 --- a/library/common/Chart.yaml +++ b/library/common/Chart.yaml @@ -15,4 +15,4 @@ maintainers: name: common sources: null type: library -version: 12.4.13 +version: 12.4.14 diff --git a/library/common/templates/class/_service.tpl b/library/common/templates/class/_service.tpl index 406d5df8..02ebe44f 100644 --- a/library/common/templates/class/_service.tpl +++ b/library/common/templates/class/_service.tpl @@ -19,6 +19,18 @@ objectData: The service data, that will be used to render the Service object. {{- $hostNetwork := false -}} {{- $podValues := dict -}} + {{- range $portName, $port := $objectData.ports -}} + {{- if $port.enabled -}} + {{- if eq (tpl ($port.protocol | default "") $rootCtx) "https" -}} + {{- $hasHTTPSPort = true -}} + {{- end -}} + + {{- if and (hasKey $port "hostPort") $port.hostPort -}} + {{- $hasHostPort = true -}} + {{- end -}} + {{- end -}} + {{- end -}} + {{- $specialTypes := (list "ExternalName" "ExternalIP") -}} {{/* External Name / External IP does not rely on any pod values */}} {{- if not (mustHas $svcType $specialTypes) -}} @@ -34,18 +46,6 @@ objectData: The service data, that will be used to render the Service object. {{- end -}} {{- end -}} - {{- range $portName, $port := $objectData.ports -}} - {{- if $port.enabled -}} - {{- if eq (tpl ($port.protocol | default "") $rootCtx) "https" -}} - {{- $hasHTTPSPort = true -}} - {{- end -}} - - {{- if and (hasKey $port "hostPort") $port.hostPort -}} - {{- $hasHostPort = true -}} - {{- end -}} - {{- end -}} - {{- end -}} - {{/* When hostPort is defined, force ClusterIP aswell */}} {{- if $hasHostPort -}} {{- $svcType = "ClusterIP" -}} diff --git a/library/common/templates/helpers/_getPortRange.tpl b/library/common/templates/helpers/_getPortRange.tpl index f31b64c9..8127fc5d 100644 --- a/library/common/templates/helpers/_getPortRange.tpl +++ b/library/common/templates/helpers/_getPortRange.tpl @@ -38,15 +38,14 @@ objectData: The object data to be used to render the Pod. {{- $portToCheck := ($portValues.targetPort | default $portValues.port) -}} {{- if kindIs "string" $portToCheck -}} - {{/* Helm stores ints as floats, so convert string to float before comparing */}} - {{- $portToCheck = (tpl $portToCheck $rootCtx) | float64 -}} + {{- $portToCheck = (tpl $portToCheck $rootCtx) | int -}} {{- end -}} - {{- if or (not $portRange.low) (lt $portToCheck ($portRange.low | float64)) -}} + {{- if or (not $portRange.low) (lt ($portToCheck | int) ($portRange.low | int)) -}} {{- $_ := set $portRange "low" $portToCheck -}} {{- end -}} - {{- if or (not $portRange.high) (gt $portToCheck ($portRange.high | float64)) -}} + {{- if or (not $portRange.high) (gt ($portToCheck | int) ($portRange.high | int)) -}} {{- $_ := set $portRange "high" $portToCheck -}} {{- end -}}