From ae2e104fc7242d247d29c69320eaac82c7e58d93 Mon Sep 17 00:00:00 2001 From: Stavros Kois <47820033+stavros-k@users.noreply.github.com> Date: Sun, 23 Apr 2023 14:28:25 +0300 Subject: [PATCH] chore(Endpoint): Add endpoint class and tests (#401) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description** ⚒️ Fixes # **⚙️ 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 | 126 +++++++++++++++++- library/common/Chart.yaml | 2 +- library/common/templates/class/_endpoint.tpl | 32 +++++ library/common/templates/class/_service.tpl | 11 +- .../templates/lib/endpoint/_addresses.tpl | 20 +++ .../common/templates/lib/endpoint/_ports.tpl | 40 ++++++ 6 files changed, 227 insertions(+), 4 deletions(-) create mode 100644 library/common/templates/class/_endpoint.tpl create mode 100644 library/common/templates/lib/endpoint/_addresses.tpl create mode 100644 library/common/templates/lib/endpoint/_ports.tpl diff --git a/library/common-test/tests/service/external_ip_test.yaml b/library/common-test/tests/service/external_ip_test.yaml index cba5b9d8..549a126c 100644 --- a/library/common-test/tests/service/external_ip_test.yaml +++ b/library/common-test/tests/service/external_ip_test.yaml @@ -4,7 +4,7 @@ templates: chart: appVersion: &appVer v9.9.9 tests: - - it: should pass with type externalIP + - it: should pass with type externalIP and useSlice unset (default) set: service: my-service: @@ -144,13 +144,14 @@ tests: - addresses: - 1.1.1.1 - - it: should pass with type ExternalIP and https + - it: should pass with type ExternalIP and https and useSlice explicitly set to true set: service: my-service: enabled: true primary: true type: ExternalIP + useSlice: true externalIP: 1.1.1.1 ports: port-name: @@ -196,3 +197,124 @@ tests: path: metadata.annotations content: traefik.ingress.kubernetes.io/service.serversscheme: https + + - it: should pass with type ExternalIP and useSlice set to false + set: + service: + my-service: + enabled: true + primary: true + type: ExternalIP + useSlice: false + 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: &endpointDoc 1 + isKind: + of: Endpoint + - documentIndex: *endpointDoc + isAPIVersion: + of: v1 + - documentIndex: *endpointDoc + equal: + path: subsets + value: + - addresses: + - ip: 1.1.1.1 + ports: + - name: port-name + port: 443 + protocol: TCP + - documentIndex: &serviceDoc 0 + isKind: + of: Service + - documentIndex: *serviceDoc + isSubset: + path: metadata.annotations + content: + traefik.ingress.kubernetes.io/service.serversscheme: https + + - it: should pass with type ExternalIP and useSlice set to false, multiple ports + set: + service: + my-service: + enabled: true + primary: true + type: ExternalIP + useSlice: false + externalIP: 1.1.1.1 + ports: + port-name: + enabled: true + primary: true + port: 12345 + port-name2: + enabled: true + port: 12344 + targetPort: 12346 + protocol: http + appProtocol: http + asserts: + - documentIndex: *serviceDoc + equal: + path: spec + value: + publishNotReadyAddresses: false + ports: + - name: port-name + port: 12345 + protocol: TCP + targetPort: 12345 + - name: port-name2 + port: 12344 + protocol: TCP + targetPort: 12346 + - documentIndex: &endpointDoc 1 + isKind: + of: Endpoint + - documentIndex: *endpointDoc + isAPIVersion: + of: v1 + - documentIndex: *endpointDoc + equal: + path: metadata.name + value: release-name-common-test + - documentIndex: *endpointDoc + equal: + path: metadata.labels + value: + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/version: *appVer + helm.sh/chart: common-test-1.0.0 + helm-revision: "0" + app: common-test-1.0.0 + release: RELEASE-NAME + - documentIndex: *endpointDoc + equal: + path: subsets + value: + - addresses: + - ip: 1.1.1.1 + ports: + - name: port-name + port: 12345 + protocol: TCP + - name: port-name2 + port: 12346 + protocol: TCP + appProtocol: http diff --git a/library/common/Chart.yaml b/library/common/Chart.yaml index 94010122..8b48d081 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.24 +version: 12.5.0 diff --git a/library/common/templates/class/_endpoint.tpl b/library/common/templates/class/_endpoint.tpl new file mode 100644 index 00000000..bc1f1044 --- /dev/null +++ b/library/common/templates/class/_endpoint.tpl @@ -0,0 +1,32 @@ +{{/* Endpoint Class */}} +{{/* Call this template: +{{ include "tc.v1.common.class.endpoint" (dict "rootCtx" $ "objectData" $objectData) }} + +rootCtx: The root context of the chart. +objectData: The service data, that will be used to render the Service object. +*/}} + +{{- define "tc.v1.common.class.endpoint" -}} + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData }} +--- +apiVersion: v1 +kind: Endpoint +metadata: + name: {{ $objectData.name }} + {{- $labels := (mustMerge ($objectData.labels | default dict) (include "tc.v1.common.lib.metadata.allLabels" $rootCtx | fromYaml)) -}} + {{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "labels" $labels) | trim) }} + labels: + {{- . | nindent 4 }} + {{- end -}} + {{- $annotations := (mustMerge ($objectData.annotations | default dict) (include "tc.v1.common.lib.metadata.allAnnotations" $rootCtx | fromYaml)) -}} + {{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "annotations" $annotations) | trim) }} + annotations: + {{- . | nindent 4 }} + {{- end }} +subsets: + - addresses: + {{- include "tc.v1.common.lib.endpoint.addresses" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 6 }} + ports: + {{- include "tc.v1.common.lib.endpoint.ports" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 6 }} +{{- end -}} diff --git a/library/common/templates/class/_service.tpl b/library/common/templates/class/_service.tpl index 02ebe44f..35c6084a 100644 --- a/library/common/templates/class/_service.tpl +++ b/library/common/templates/class/_service.tpl @@ -100,6 +100,15 @@ spec: {{- end }} {{- end -}} {{- if eq $objectData.type "ExternalIP" -}} - {{- include "tc.v1.common.class.endpointSlice" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 0 }} + {{- $useSlice := true -}} + {{- if kindIs "bool" $objectData.useSlice -}} + {{- $useSlice = $objectData.useSlice -}} + {{- end -}} + + {{- if $useSlice -}} + {{- include "tc.v1.common.class.endpointSlice" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 0 }} + {{- else -}} + {{- include "tc.v1.common.class.endpoint" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 0 }} + {{- end -}} {{- end -}} {{- end -}} diff --git a/library/common/templates/lib/endpoint/_addresses.tpl b/library/common/templates/lib/endpoint/_addresses.tpl new file mode 100644 index 00000000..381bb1f6 --- /dev/null +++ b/library/common/templates/lib/endpoint/_addresses.tpl @@ -0,0 +1,20 @@ +{{/* Endpoint - addresses */}} +{{/* Call this template: +{{ include "tc.v1.common.lib.endpoint.addresses" (dict "rootCtx" $rootCtx "objectData" $objectData) -}} +rootCtx: The root context of the chart. +objectData: The object data of the service +*/}} + +{{- define "tc.v1.common.lib.endpoint.addresses" -}} + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData -}} + + {{- if not $objectData.externalIP -}} + {{- fail "EndpointSlice - Expected non-empty " -}} + {{- end -}} + + {{- if not (kindIs "string" $objectData.externalIP) -}} {{/* Only single IP is supported currently on this lib */}} + {{- fail (printf "EndpointSlice - Expected to be a [string], but got [%s]" (kindOf $objectData.externalIP)) -}} + {{- end }} + - ip: {{ tpl $objectData.externalIP $rootCtx }} +{{- end -}} diff --git a/library/common/templates/lib/endpoint/_ports.tpl b/library/common/templates/lib/endpoint/_ports.tpl new file mode 100644 index 00000000..f0f9b937 --- /dev/null +++ b/library/common/templates/lib/endpoint/_ports.tpl @@ -0,0 +1,40 @@ +{{/* Endpoint - Ports */}} +{{/* Call this template: +{{ include "tc.v1.common.lib.endpoint.ports" (dict "rootCtx" $rootCtx "objectData" $objectData) -}} +rootCtx: The root context of the chart. +objectData: The object data of the service +*/}} + +{{- define "tc.v1.common.lib.endpoint.ports" -}} + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData -}} + + {{- $tcpProtocols := (list "tcp" "http" "https") -}} + {{- range $name, $portValues := $objectData.ports -}} + {{- if $portValues.enabled -}} + {{- $protocol := $rootCtx.Values.fallbackDefaults.serviceProtocol -}} {{/* Default to fallback protocol, if no protocol is defined */}} + {{- $port := $portValues.targetPort | default $portValues.port -}} + + {{/* Expand targetPort */}} + {{- if (kindIs "string" $port) -}} + {{- $port = (tpl $port $rootCtx) -}} + {{- end -}} + {{- $port = int $port -}} + + {{- with $portValues.protocol -}} + {{- $protocol = tpl . $rootCtx -}} + + {{- if mustHas $protocol $tcpProtocols -}} + {{- $protocol = "tcp" -}} + {{- end -}} + {{- end }} +- name: {{ $name }} + port: {{ $port }} + protocol: {{ $protocol | upper }} + {{- with $portValues.appProtocol }} + appProtocol: {{ tpl . $rootCtx | lower }} + {{- end -}} + {{- end -}} + {{- end -}} + +{{- end -}}