fix(common): ensure no loadbalancer reservation is made and ingress is not connected to ingresscontroller with Stop-All set (#652)

**Description**
Currently ingress and loadbalancer reservations are still being made for
stopped charts.

With this change we set services to clusterip, to prevent external
connections.
We also set ingress to a special `stopped` ingressClass, this ensures we
don't nuke things like certificates when stopping

**⚙️ Type of change**

- [x] ⚙️ 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:**

- [ ] ⚖️ 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._

---------

Co-authored-by: Stavros kois <s.kois@outlook.com>
This commit is contained in:
Kjeld Schouten
2023-12-24 22:45:22 +01:00
committed by GitHub
parent 276e87447e
commit 193df99eb0
7 changed files with 254 additions and 5 deletions

View File

@@ -0,0 +1,93 @@
suite: ingress - stop test
templates:
- common.yaml
chart:
appVersion: &appVer v9.9.9
release:
name: test-release-name
namespace: test-release-namespace
tests:
- it: should pass with stopAll
set:
operator: &operator
verify:
enabled: false
global:
stopAll: true
service: &service
my-service:
enabled: true
primary: true
ports:
main:
enabled: true
primary: true
port: 80
ingress: &ingress
my-ingress:
enabled: true
primary: true
hosts:
- host: test-host
paths:
- path: /test-path
integrations:
traefik:
enabled: false
asserts:
- documentIndex: &ingressDoc 1
isKind:
of: Ingress
- documentIndex: *ingressDoc
equal:
path: metadata.name
value: test-release-name-common-test
- documentIndex: *ingressDoc
equal:
path: spec.ingressClassName
value: tc-stopped
- it: should pass with ixChartContext - isStopped (true)
set:
operator: *operator
global:
namespace: ix-something
ixChartContext:
storageClassName: some-storage-class
isStopped: true
service: *service
ingress: *ingress
asserts:
- documentIndex: &ingressDoc 1
isKind:
of: Ingress
- documentIndex: *ingressDoc
equal:
path: metadata.name
value: test-release-name-common-test
- documentIndex: *ingressDoc
equal:
path: spec.ingressClassName
value: tc-stopped
- it: should pass with ixChartContext - isStopped (false)
set:
operator: *operator
global:
namespace: ix-something
ixChartContext:
storageClassName: some-storage-class
isStopped: false
service: *service
ingress: *ingress
asserts:
- documentIndex: &ingressDoc 1
isKind:
of: Ingress
- documentIndex: *ingressDoc
equal:
path: metadata.name
value: test-release-name-common-test
- documentIndex: *ingressDoc
isNull:
path: spec.ingressClassName

View File

@@ -610,3 +610,17 @@ tests:
asserts:
- failedTemplate:
errorMessage: Ingress - [tls.scaleCert] can only be used in TrueNAS SCALE
- it: should fail with ingressClassName set to tc-stopped
set:
operator: *operator
service: *service
ingress:
my-ingress:
enabled: true
primary: true
hosts: *hosts
ingressClassName: tc-stopped
asserts:
- failedTemplate:
errorMessage: Ingress - Expected [ingressClassName] to not be [stopped], this is reserved for internal use

View File

@@ -0,0 +1,120 @@
suite: service stop test
templates:
- common.yaml
release:
name: test-release-name
namespace: test-release-namespace
tests:
- it: should pass with stopAll
set:
global:
stopAll: true
service: &service
my-service1:
enabled: true
primary: true
type: LoadBalancer
ports:
port-name:
enabled: true
primary: true
port: 12344
workload: &workload
my-workload:
enabled: true
primary: true
type: Deployment
podSpec: {}
asserts:
- documentIndex: &serviceDoc 1
isKind:
of: Service
- documentIndex: *serviceDoc
isAPIVersion:
of: v1
- documentIndex: *serviceDoc
equal:
path: metadata.name
value: test-release-name-common-test
- documentIndex: *serviceDoc
equal:
path: spec.type
value: ClusterIP
- it: should pass with isStopped (true)
set:
global:
namespace: ix-something
ixChartContext:
storageClassName: some-storage-class
isStopped: true
service: &service
my-service1:
enabled: true
primary: true
type: LoadBalancer
ports:
port-name:
enabled: true
primary: true
port: 12344
workload: &workload
my-workload:
enabled: true
primary: true
type: Deployment
podSpec: {}
asserts:
- documentIndex: &serviceDoc 1
isKind:
of: Service
- documentIndex: *serviceDoc
isAPIVersion:
of: v1
- documentIndex: *serviceDoc
equal:
path: metadata.name
value: test-release-name-common-test
- documentIndex: *serviceDoc
equal:
path: spec.type
value: ClusterIP
- it: should pass with isStopped (false)
set:
global:
namespace: ix-something
ixChartContext:
storageClassName: some-storage-class
isStopped: false
service: &service
my-service1:
enabled: true
primary: true
type: LoadBalancer
ports:
port-name:
enabled: true
primary: true
port: 12344
workload: &workload
my-workload:
enabled: true
primary: true
type: Deployment
podSpec: {}
asserts:
- documentIndex: &serviceDoc 1
isKind:
of: Service
- documentIndex: *serviceDoc
isAPIVersion:
of: v1
- documentIndex: *serviceDoc
equal:
path: metadata.name
value: test-release-name-common-test
- documentIndex: *serviceDoc
equal:
path: spec.type
value: LoadBalancer

View File

@@ -15,4 +15,4 @@ maintainers:
name: common
sources: null
type: library
version: 16.2.21
version: 16.2.22

View File

@@ -23,7 +23,18 @@ objectData: The object data to be used to render the Ingress.
{{- include "tc.v1.common.lib.ingress.integration.certManager" (dict "rootCtx" $rootCtx "objectData" $objectData) -}}
{{- include "tc.v1.common.lib.ingress.integration.traefik" (dict "rootCtx" $rootCtx "objectData" $objectData) -}}
{{- include "tc.v1.common.lib.ingress.integration.homepage" (dict "rootCtx" $rootCtx "objectData" $objectData) }}
{{- include "tc.v1.common.lib.ingress.integration.homepage" (dict "rootCtx" $rootCtx "objectData" $objectData) -}}
{{- $ingressClassName := "" -}}
{{- if $objectData.ingressClassName -}}
{{- $ingressClassName = (tpl $objectData.ingressClassName $rootCtx) -}}
{{- end -}}
{{/* When Stop All is set, force ingressClass "stopped"
to yeet ingress from the ingresscontroller */}}
{{- if (include "tc.v1.common.lib.util.stopAll" $rootCtx) -}}
{{- $ingressClassName = "tc-stopped" -}}
{{- end }}
---
apiVersion: networking.k8s.io/v1
kind: Ingress
@@ -41,8 +52,8 @@ metadata:
{{- . | nindent 4 }}
{{- end }}
spec:
{{- if $objectData.ingressClassName }}
ingressClassName: {{ tpl $objectData.ingressClassName $rootCtx }}
{{- if $ingressClassName }}
ingressClassName: {{ $ingressClassName }}
{{- end }}
rules:
{{- range $h := $objectData.hosts }}

View File

@@ -51,8 +51,12 @@ objectData: The service data, that will be used to render the Service object.
{{- $svcType = "ClusterIP" -}}
{{- end -}}
{{- end -}}
{{- $_ := set $objectData "type" $svcType }}
{{/* When Stop All is set, force ClusterIP as well */}}
{{- if (include "tc.v1.common.lib.util.stopAll" $rootCtx) -}}
{{- $svcType = "ClusterIP" -}}
{{- end -}}
{{- $_ := set $objectData "type" $svcType }}
---
apiVersion: v1
kind: Service

View File

@@ -31,6 +31,13 @@ objectData:
{{- end -}}
{{- end -}}
{{- if $objectData.ingressClassName -}}
{{- $icn := tpl $objectData.ingressClassName $rootCtx -}}
{{- if eq $icn "tc-stopped" -}}
{{- fail "Ingress - Expected [ingressClassName] to not be [stopped], this is reserved for internal use" -}}
{{- end -}}
{{- end -}}
{{- if not $objectData.hosts -}}
{{- fail "Ingress - Expected non-empty [hosts]" -}}
{{- end -}}