From a2fc0f8217f83bf4341834b25ebaec7e535d7b46 Mon Sep 17 00:00:00 2001 From: Stavros Kois <47820033+stavros-k@users.noreply.github.com> Date: Sat, 9 Dec 2023 01:22:41 +0200 Subject: [PATCH] chore(common): use the expandName template in more places - also fixes a small quote bug on Certificate (#630) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description** ⚒️ Fixes #623 **⚙️ Type of change** - [ ] ⚙️ Feature/App addition - [ ] 🪛 Bugfix - [ ] ⚠️ Breaking change (fix or feature that would cause existing functionality to not work as expected) - [x] 🔃 Refactor of current code **🧪 How Has This Been Tested?** **📃 Notes:** **✔️ Checklist:** - [x] ⚖️ My code follows the style guidelines of this project - [x] 👀 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 - [x] ⚠️ My changes generate no new warnings - [ ] 🧪 I have added tests to this description that prove my fix is effective or that my feature works - [x] ⬆️ 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/certificate/data_test.yaml | 2 + library/common/Chart.yaml | 2 +- .../class/cert-manager/_certificate.tpl | 2 +- .../common/templates/spawner/_configmap.tpl | 14 ++++--- .../spawner/_podDisruptionBudget.tpl | 39 +++++++------------ .../templates/spawner/_priorityClass.tpl | 14 ++++--- library/common/templates/spawner/_secret.tpl | 14 ++++--- .../templates/spawner/_storageClass.tpl | 14 ++++--- .../templates/spawner/_volumeSnapshot.tpl | 14 ++++--- .../spawner/_volumeSnapshotClass.tpl | 14 ++++--- library/common/templates/spawner/_webhook.tpl | 14 ++++--- .../spawner/cert-manager/_certificate.tpl | 14 ++++--- .../spawner/velero/_backupstoragelocation.tpl | 13 ++++--- .../templates/spawner/velero/_schedule.tpl | 14 ++++--- .../velero/_volumeSnapshotLocation.tpl | 14 ++++--- 15 files changed, 116 insertions(+), 82 deletions(-) diff --git a/library/common-test/tests/certificate/data_test.yaml b/library/common-test/tests/certificate/data_test.yaml index 06620e44..2768ed31 100644 --- a/library/common-test/tests/certificate/data_test.yaml +++ b/library/common-test/tests/certificate/data_test.yaml @@ -21,6 +21,7 @@ tests: enabled: true hosts: - host2 + - "*.host2.com" certificateIssuer: some-other-issuer certificateSecretTemplate: labels: @@ -72,6 +73,7 @@ tests: secretName: certificate-issuer-my-certificate2 dnsNames: - host2 + - "*.host2.com" issuerRef: name: some-other-issuer kind: ClusterIssuer diff --git a/library/common/Chart.yaml b/library/common/Chart.yaml index f3ef2c3a..30e28c46 100644 --- a/library/common/Chart.yaml +++ b/library/common/Chart.yaml @@ -15,4 +15,4 @@ maintainers: name: common sources: null type: library -version: 16.2.3 +version: 16.2.4 diff --git a/library/common/templates/class/cert-manager/_certificate.tpl b/library/common/templates/class/cert-manager/_certificate.tpl index d4a41687..b07bda2b 100644 --- a/library/common/templates/class/cert-manager/_certificate.tpl +++ b/library/common/templates/class/cert-manager/_certificate.tpl @@ -33,7 +33,7 @@ spec: secretName: {{ $objectData.name }} dnsNames: {{- range $h := $objectData.hosts }} - - {{ tpl $h $rootCtx }} + - {{ (tpl $h $rootCtx) | quote }} {{- end }} privateKey: algorithm: ECDSA diff --git a/library/common/templates/spawner/_configmap.tpl b/library/common/templates/spawner/_configmap.tpl index 718a7368..1a84b043 100644 --- a/library/common/templates/spawner/_configmap.tpl +++ b/library/common/templates/spawner/_configmap.tpl @@ -18,11 +18,15 @@ {{/* Create a copy of the configmap */}} {{- $objectData := (mustDeepCopy $configmap) -}} - {{- $objectName := (printf "%s-%s" $fullname $name) -}} - {{- if hasKey $objectData "expandObjectName" -}} - {{- if not $objectData.expandObjectName -}} - {{- $objectName = $name -}} - {{- end -}} + {{- $objectName := $name -}} + + {{- $expandName := (include "tc.v1.common.lib.util.expandName" (dict + "rootCtx" $ "objectData" $objectData + "name" $name "caller" "ConfigMap" + "key" "configmap")) -}} + + {{- if eq $expandName "true" -}} + {{- $objectName = (printf "%s-%s" $fullname $name) -}} {{- end -}} {{/* Perform validations */}} {{/* Configmaps have a max name length of 253 */}} diff --git a/library/common/templates/spawner/_podDisruptionBudget.tpl b/library/common/templates/spawner/_podDisruptionBudget.tpl index 321c864c..20b10c99 100644 --- a/library/common/templates/spawner/_podDisruptionBudget.tpl +++ b/library/common/templates/spawner/_podDisruptionBudget.tpl @@ -7,36 +7,25 @@ {{- $fullname := include "tc.v1.common.lib.chart.names.fullname" $ -}} {{- range $name, $pdb := .Values.podDisruptionBudget -}} - {{- $enabled := false -}} - {{- if hasKey $pdb "enabled" -}} - {{- if not (kindIs "invalid" $pdb.enabled) -}} - {{- $enabled = $pdb.enabled -}} - {{- else -}} - {{- fail (printf "Pod Disruption Budget - Expected the defined key [enabled] in [podDisruptionBudget.%s] to not be empty" $name) -}} - {{- end -}} - {{- end -}} + {{- $enabled := (include "tc.v1.common.lib.util.enabled" (dict + "rootCtx" $ "objectData" $pdb + "name" $name "caller" "Pod Disruption Budget" + "key" "podDistruptionBudget")) -}} - {{- if kindIs "string" $enabled -}} - {{- $enabled = tpl $enabled $ -}} - - {{/* After tpl it becomes a string, not a bool */}} - {{- if eq $enabled "true" -}} - {{- $enabled = true -}} - {{- else if eq $enabled "false" -}} - {{- $enabled = false -}} - {{- end -}} - {{- end -}} - - {{- if $enabled -}} + {{- if eq $enabled "true" -}} {{/* Create a copy of the poddisruptionbudget */}} {{- $objectData := (mustDeepCopy $pdb) -}} - {{- $objectName := (printf "%s-%s" $fullname $name) -}} - {{- if hasKey $objectData "expandObjectName" -}} - {{- if not $objectData.expandObjectName -}} - {{- $objectName = $name -}} - {{- end -}} + {{- $objectName := $name -}} + + {{- $expandName := (include "tc.v1.common.lib.util.expandName" (dict + "rootCtx" $ "objectData" $objectData + "name" $name "caller" "Pod Disruption Budget" + "key" "podDistruptionBudget")) -}} + + {{- if eq $expandName "true" -}} + {{- $objectName = (printf "%s-%s" $fullname $name) -}} {{- end -}} {{/* Perform validations */}} diff --git a/library/common/templates/spawner/_priorityClass.tpl b/library/common/templates/spawner/_priorityClass.tpl index 07ecb992..0f013244 100644 --- a/library/common/templates/spawner/_priorityClass.tpl +++ b/library/common/templates/spawner/_priorityClass.tpl @@ -18,11 +18,15 @@ {{/* Create a copy of the priorityclass */}} {{- $objectData := (mustDeepCopy $priorityclass) -}} - {{- $objectName := (printf "%s-%s" $fullname $name) -}} - {{- if hasKey $objectData "expandObjectName" -}} - {{- if not $objectData.expandObjectName -}} - {{- $objectName = $name -}} - {{- end -}} + {{- $objectName := $name -}} + + {{- $expandName := (include "tc.v1.common.lib.util.expandName" (dict + "rootCtx" $ "objectData" $objectData + "name" $name "caller" "Priority Class" + "key" "priorityClass")) -}} + + {{- if eq $expandName "true" -}} + {{- $objectName = (printf "%s-%s" $fullname $name) -}} {{- end -}} {{/* Perform validations */}} {{/* priorityclasss have a max name length of 253 */}} diff --git a/library/common/templates/spawner/_secret.tpl b/library/common/templates/spawner/_secret.tpl index f6e68c23..480634f6 100644 --- a/library/common/templates/spawner/_secret.tpl +++ b/library/common/templates/spawner/_secret.tpl @@ -17,11 +17,15 @@ {{/* Create a copy of the secret */}} {{- $objectData := (mustDeepCopy $secret) -}} - {{- $objectName := (printf "%s-%s" $fullname $name) -}} - {{- if hasKey $objectData "expandObjectName" -}} - {{- if not $objectData.expandObjectName -}} - {{- $objectName = $name -}} - {{- end -}} + {{- $objectName := $name -}} + + {{- $expandName := (include "tc.v1.common.lib.util.expandName" (dict + "rootCtx" $ "objectData" $objectData + "name" $name "caller" "Secret" + "key" "secret")) -}} + + {{- if eq $expandName "true" -}} + {{- $objectName = (printf "%s-%s" $fullname $name) -}} {{- end -}} {{/* Perform validations */}} {{/* Secrets have a max name length of 253 */}} diff --git a/library/common/templates/spawner/_storageClass.tpl b/library/common/templates/spawner/_storageClass.tpl index 088045ba..894deea3 100644 --- a/library/common/templates/spawner/_storageClass.tpl +++ b/library/common/templates/spawner/_storageClass.tpl @@ -18,11 +18,15 @@ {{/* Create a copy of the storageclass */}} {{- $objectData := (mustDeepCopy $storageclass) -}} - {{- $objectName := (printf "%s-%s" $fullname $name) -}} - {{- if hasKey $objectData "expandObjectName" -}} - {{- if not $objectData.expandObjectName -}} - {{- $objectName = $name -}} - {{- end -}} + {{- $objectName := $name -}} + + {{- $expandName := (include "tc.v1.common.lib.util.expandName" (dict + "rootCtx" $ "objectData" $objectData + "name" $name "caller" "Storage Class" + "key" "storageClass")) -}} + + {{- if eq $expandName "true" -}} + {{- $objectName = (printf "%s-%s" $fullname $name) -}} {{- end -}} {{/* Perform validations */}} {{/* Configmaps have a max name length of 253 */}} diff --git a/library/common/templates/spawner/_volumeSnapshot.tpl b/library/common/templates/spawner/_volumeSnapshot.tpl index f8fb545a..d12ce78d 100644 --- a/library/common/templates/spawner/_volumeSnapshot.tpl +++ b/library/common/templates/spawner/_volumeSnapshot.tpl @@ -18,11 +18,15 @@ {{/* Create a copy of the volumesnapshot */}} {{- $objectData := (mustDeepCopy $volumesnapshot) -}} - {{- $objectName := (printf "%s-%s" $fullname $name) -}} - {{- if hasKey $objectData "expandObjectName" -}} - {{- if not $objectData.expandObjectName -}} - {{- $objectName = $name -}} - {{- end -}} + {{- $objectName := $name -}} + + {{- $expandName := (include "tc.v1.common.lib.util.expandName" (dict + "rootCtx" $ "objectData" $objectData + "name" $name "caller" "Volume Snapshot" + "key" "volumeSnapshots")) -}} + + {{- if eq $expandName "true" -}} + {{- $objectName = (printf "%s-%s" $fullname $name) -}} {{- end -}} {{/* Perform validations */}} {{/* volumesnapshots have a max name length of 253 */}} diff --git a/library/common/templates/spawner/_volumeSnapshotClass.tpl b/library/common/templates/spawner/_volumeSnapshotClass.tpl index 6b15825c..e628c03b 100644 --- a/library/common/templates/spawner/_volumeSnapshotClass.tpl +++ b/library/common/templates/spawner/_volumeSnapshotClass.tpl @@ -18,11 +18,15 @@ {{/* Create a copy of the volumesnapshotclass */}} {{- $objectData := (mustDeepCopy $volumesnapshotclass) -}} - {{- $objectName := (printf "%s-%s" $fullname $name) -}} - {{- if hasKey $objectData "expandObjectName" -}} - {{- if not $objectData.expandObjectName -}} - {{- $objectName = $name -}} - {{- end -}} + {{- $objectName := $name -}} + + {{- $expandName := (include "tc.v1.common.lib.util.expandName" (dict + "rootCtx" $ "objectData" $objectData + "name" $name "caller" "Volume Snapshot Class" + "key" "volumeSnapshotClass")) -}} + + {{- if eq $expandName "true" -}} + {{- $objectName = (printf "%s-%s" $fullname $name) -}} {{- end -}} {{/* Perform validations */}} {{/* volumesnapshotclasss have a max name length of 253 */}} diff --git a/library/common/templates/spawner/_webhook.tpl b/library/common/templates/spawner/_webhook.tpl index 90c0db10..106075c3 100644 --- a/library/common/templates/spawner/_webhook.tpl +++ b/library/common/templates/spawner/_webhook.tpl @@ -18,11 +18,15 @@ {{/* Create a copy of the mutatingWebhookConfiguration */}} {{- $objectData := (mustDeepCopy $mutatingWebhookConfiguration) -}} - {{- $objectName := (printf "%s-%s" $fullname $name) -}} - {{- if hasKey $objectData "expandObjectName" -}} - {{- if not $objectData.expandObjectName -}} - {{- $objectName = $name -}} - {{- end -}} + {{- $objectName := $name -}} + + {{- $expandName := (include "tc.v1.common.lib.util.expandName" (dict + "rootCtx" $ "objectData" $objectData + "name" $name "caller" "Webhook" + "key" "webhook")) -}} + + {{- if eq $expandName "true" -}} + {{- $objectName = (printf "%s-%s" $fullname $name) -}} {{- end -}} {{/* Perform validations */}} diff --git a/library/common/templates/spawner/cert-manager/_certificate.tpl b/library/common/templates/spawner/cert-manager/_certificate.tpl index 92389382..16657183 100644 --- a/library/common/templates/spawner/cert-manager/_certificate.tpl +++ b/library/common/templates/spawner/cert-manager/_certificate.tpl @@ -15,11 +15,15 @@ {{- if eq $enabled "true" -}} {{- $objectData := (mustDeepCopy $cert) -}} - {{- $objectName := (printf "%s-%s" $fullname $name) -}} - {{- if hasKey $objectData "expandObjectName" -}} - {{- if not $objectData.expandObjectName -}} - {{- $objectName = $name -}} - {{- end -}} + {{- $objectName := $name -}} + + {{- $expandName := (include "tc.v1.common.lib.util.expandName" (dict + "rootCtx" $ "objectData" $objectData + "name" $name "caller" "Cert Manager Certificate" + "key" "certificate")) -}} + + {{- if eq $expandName "true" -}} + {{- $objectName = (printf "%s-%s" $fullname $name) -}} {{- end -}} {{/* If a certificateSecretTemplate is defined, adjust name */}} diff --git a/library/common/templates/spawner/velero/_backupstoragelocation.tpl b/library/common/templates/spawner/velero/_backupstoragelocation.tpl index 86f45faa..c9d02a03 100644 --- a/library/common/templates/spawner/velero/_backupstoragelocation.tpl +++ b/library/common/templates/spawner/velero/_backupstoragelocation.tpl @@ -17,12 +17,15 @@ {{/* Create a copy of the backupstoragelocation */}} {{- $objectData := (mustDeepCopy $backupStorageLoc) -}} + {{- $objectName := $name -}} - {{- $objectName := (printf "%s-%s" $fullname $name) -}} - {{- if hasKey $objectData "expandObjectName" -}} - {{- if not $objectData.expandObjectName -}} - {{- $objectName = $name -}} - {{- end -}} + {{- $expandName := (include "tc.v1.common.lib.util.expandName" (dict + "rootCtx" $ "objectData" $objectData + "name" $name "caller" "Velero Backup Storage Location" + "key" "backupStorageLocation")) -}} + + {{- if eq $expandName "true" -}} + {{- $objectName = (printf "%s-%s" $fullname $name) -}} {{- end -}} {{/* Set namespace to velero location or itself, just in case its used from within velero */}} diff --git a/library/common/templates/spawner/velero/_schedule.tpl b/library/common/templates/spawner/velero/_schedule.tpl index 4a01372b..922c2d90 100644 --- a/library/common/templates/spawner/velero/_schedule.tpl +++ b/library/common/templates/spawner/velero/_schedule.tpl @@ -17,11 +17,15 @@ {{/* Create a copy of the schedule */}} {{- $objectData := (mustDeepCopy $schedule) -}} - {{- $objectName := (printf "%s-%s" $fullname $name) -}} - {{- if hasKey $objectData "expandObjectName" -}} - {{- if not $objectData.expandObjectName -}} - {{- $objectName = $name -}} - {{- end -}} + {{- $objectName := $name -}} + + {{- $expandName := (include "tc.v1.common.lib.util.expandName" (dict + "rootCtx" $ "objectData" $objectData + "name" $name "caller" "Velero Schedule" + "key" "schedules")) -}} + + {{- if eq $expandName "true" -}} + {{- $objectName = (printf "%s-%s" $fullname $name) -}} {{- end -}} {{/* Perform validations */}} {{/* schedules have a max name length of 253 */}} diff --git a/library/common/templates/spawner/velero/_volumeSnapshotLocation.tpl b/library/common/templates/spawner/velero/_volumeSnapshotLocation.tpl index 8ade18b2..1d664ce3 100644 --- a/library/common/templates/spawner/velero/_volumeSnapshotLocation.tpl +++ b/library/common/templates/spawner/velero/_volumeSnapshotLocation.tpl @@ -18,11 +18,15 @@ {{/* Create a copy of the volumesnapshotlocation */}} {{- $objectData := (mustDeepCopy $volSnapLoc) -}} - {{- $objectName := (printf "%s-%s" $fullname $name) -}} - {{- if hasKey $objectData "expandObjectName" -}} - {{- if not $objectData.expandObjectName -}} - {{- $objectName = $name -}} - {{- end -}} + {{- $objectName := $name -}} + + {{- $expandName := (include "tc.v1.common.lib.util.expandName" (dict + "rootCtx" $ "objectData" $objectData + "name" $name "caller" "Velero Volume Snapshot Location" + "key" "volumeSnapshotLocation")) -}} + + {{- if eq $expandName "true" -}} + {{- $objectName = (printf "%s-%s" $fullname $name) -}} {{- end -}} {{/* Set namespace to velero location or itself, just in case its used from within velero */}}