From 47878e4329d0f3165b0d804b95e2dbb0bb49648b Mon Sep 17 00:00:00 2001 From: Kjeld Schouten Date: Fri, 2 Jun 2023 23:37:31 +0200 Subject: [PATCH] feat(common): add support for additional templates defined in values.yaml (#448) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description** add extraTpl support for custom template file defining in values.yaml **โš™๏ธ 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._ --------- Co-authored-by: Stavros kois --- library/common-test/Chart.yaml | 2 +- .../tests/defaults/defaults-test.yaml | 32 +++++++++ .../tests/extraTpl/extratpl_test.yaml | 65 +++++++++++++++++++ library/common/Chart.yaml | 2 +- library/common/templates/loader/_apply.tpl | 3 + .../common/templates/spawner/_extraTpl.tpl | 13 ++++ library/common/values.yaml | 3 + 7 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 library/common-test/tests/extraTpl/extratpl_test.yaml create mode 100644 library/common/templates/spawner/_extraTpl.tpl diff --git a/library/common-test/Chart.yaml b/library/common-test/Chart.yaml index 345f373e..4e8c6369 100644 --- a/library/common-test/Chart.yaml +++ b/library/common-test/Chart.yaml @@ -3,7 +3,7 @@ appVersion: "" dependencies: - name: common repository: file://../common - version: ~12.10.0 + version: ~12.11.0 deprecated: false description: Helper chart to test different use cases of the common library home: https://github.com/truecharts/apps/tree/master/charts/library/common-test diff --git a/library/common-test/tests/defaults/defaults-test.yaml b/library/common-test/tests/defaults/defaults-test.yaml index 18dab5fb..f3c61a9e 100644 --- a/library/common-test/tests/defaults/defaults-test.yaml +++ b/library/common-test/tests/defaults/defaults-test.yaml @@ -82,3 +82,35 @@ tests: - name: varrun emptyDir: medium: Memory + - documentIndex: *deploymentDoc + isSubset: + path: spec.template.spec.containers[0] + content: + livenessProbe: + failureThreshold: 5 + httpGet: + path: / + port: 1025 + scheme: HTTP + initialDelaySeconds: 10 + periodSeconds: 10 + successThreshold: 1 + timeoutSeconds: 5 + readinessProbe: + failureThreshold: 5 + httpGet: + path: / + port: 1025 + scheme: HTTP + initialDelaySeconds: 10 + periodSeconds: 10 + successThreshold: 2 + timeoutSeconds: 5 + startupProbe: + failureThreshold: 60 + tcpSocket: + port: 1025 + initialDelaySeconds: 10 + periodSeconds: 5 + successThreshold: 1 + timeoutSeconds: 2 diff --git a/library/common-test/tests/extraTpl/extratpl_test.yaml b/library/common-test/tests/extraTpl/extratpl_test.yaml new file mode 100644 index 00000000..4e0a81b5 --- /dev/null +++ b/library/common-test/tests/extraTpl/extratpl_test.yaml @@ -0,0 +1,65 @@ +suite: extra tpl test +templates: + - common.yaml +tests: + - it: should pass with extra tpl + set: + someKey: someValue + extraTpl: + - | + apiVersion: v1 + kind: Service + metadata: + name: release-name-common-test + spec: + type: ClusterIP + publishNotReadyAddresses: false + ports: + - name: main + port: 80 + protocol: TCP + targetPort: 80 + - | + apiVersion: apps/v1 + kind: Deployment + metadata: + name: release-name-common-test + labels: + some-label: {{ .Values.someKey }} + spec: + template: + spec: + containers: + - name: release-name-common-test + - "apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: {{ .Values.someKey }}" + asserts: + - documentIndex: &serviceDoc 0 + isKind: + of: Service + - documentIndex: *serviceDoc + equal: + path: spec.ports[0].name + value: main + - documentIndex: &deploymentDoc 1 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + equal: + path: metadata.labels.some-label + value: someValue + - documentIndex: &otherDeploymentDoc 2 + isKind: + of: Deployment + - documentIndex: *otherDeploymentDoc + equal: + path: metadata.name + value: someValue + + # Failures + - it: should fail with empty item in extra tpl + set: + extraTpl: + - "" + asserts: + - failedTemplate: + errorMessage: Extra tpl - Expected non-empty item diff --git a/library/common/Chart.yaml b/library/common/Chart.yaml index 4a04b17b..b92bacfc 100644 --- a/library/common/Chart.yaml +++ b/library/common/Chart.yaml @@ -15,4 +15,4 @@ maintainers: name: common sources: null type: library -version: 12.10.14 +version: 12.11.0 diff --git a/library/common/templates/loader/_apply.tpl b/library/common/templates/loader/_apply.tpl index a8575a88..26921408 100644 --- a/library/common/templates/loader/_apply.tpl +++ b/library/common/templates/loader/_apply.tpl @@ -1,6 +1,9 @@ {{/* Loads all spawners */}} {{- define "tc.v1.common.loader.apply" -}} + {{/* Inject custom tpl files, as defined in values.yaml */}} + {{- include "tc.v1.common.spawner.extraTpl" . | nindent 0 -}} + {{/* Make sure there are not any YAML errors */}} {{- include "tc.v1.common.values.validate" .Values -}} diff --git a/library/common/templates/spawner/_extraTpl.tpl b/library/common/templates/spawner/_extraTpl.tpl new file mode 100644 index 00000000..30b54603 --- /dev/null +++ b/library/common/templates/spawner/_extraTpl.tpl @@ -0,0 +1,13 @@ +{{- define "tc.v1.common.spawner.extraTpl" -}} + {{- range $item := .Values.extraTpl }} + {{- if not $item -}} + {{- fail "Extra tpl - Expected non-empty item" -}} + {{- end }} +--- + {{- if kindIs "string" $item }} + {{- tpl $item $ | nindent 0 }} + {{- else }} + {{- tpl ($item | toYaml) $ | nindent 0 }} + {{- end }} + {{- end }} +{{- end }} diff --git a/library/common/values.yaml b/library/common/values.yaml index 1555b80a..70f1adf0 100644 --- a/library/common/values.yaml +++ b/library/common/values.yaml @@ -810,3 +810,6 @@ solr: creds: {} manifestManager: enabled: false + +# -- List of extra objects to deploy with the release +extraTpl: []