chore(Endpoint): Add endpoint class and tests (#401)

**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  # <!--(issue)-->

**⚙️ 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?**
<!--
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:**

- [ ] ⚖️ 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._
This commit is contained in:
Stavros Kois
2023-04-23 14:28:25 +03:00
committed by GitHub
parent 26202f215f
commit ae2e104fc7
6 changed files with 227 additions and 4 deletions

View File

@@ -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

View File

@@ -15,4 +15,4 @@ maintainers:
name: common
sources: null
type: library
version: 12.4.24
version: 12.5.0

View File

@@ -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 -}}

View File

@@ -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 -}}

View File

@@ -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 <externalIP>" -}}
{{- end -}}
{{- if not (kindIs "string" $objectData.externalIP) -}} {{/* Only single IP is supported currently on this lib */}}
{{- fail (printf "EndpointSlice - Expected <externalIP> to be a [string], but got [%s]" (kindOf $objectData.externalIP)) -}}
{{- end }}
- ip: {{ tpl $objectData.externalIP $rootCtx }}
{{- end -}}

View File

@@ -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 -}}