Files
truecharts/charts/library/common/templates/spawner/_pvc.tpl
T
astro-stan c0ff8684ce feat(common): Add support for secretRefs when defining custom CA for use with S3 credentials (#40385)
**Description**

This PR builds up on #40000 by adding an additional key -
`credentials.$name.customCASecretRef`. This allows referencing secrets
via the already familiar `configMapRef`/`secretRef` pattern, using the
`name` and `expandObjectName` keys to reference secrets defined under
`.Values.secret`. Additionally, it also adds a
`credentials.$name.customCASecretRef.key` forcing users to specify the
key in the secret which contains the CA, for maximum flexibility.

⚒️ Fixes  # <!--(issue)-->

**⚙️ Type of change**

- [X] ⚙️ Feature/App addition
- [ ] 🪛 Bugfix
- [ ] ⚠️ Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] 🔃 Refactor of current code
- [X] 📜 Documentation Changes

**🧪 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
-->

I tested rendering the same CA secret using both the current `customCA`
implementation and the new `.secret.$name` + `customCASecretRef` way.
Also added unit tests for CI.

**📃 Notes:**

Regarding CNPG support - the changes in this PR and from #40000 should
also work for CNPG. As far as i can tell it requires the CA secret to be
referenced under a key called `endpointCA` in the following way
([docs](https://cloudnative-pg.io/documentation/1.16/api_reference/#backupstatus),
[CRD
def](https://github.com/cloudnative-pg/cloudnative-pg/blob/6ae2fb61bee4f959545bceeacfc5a209b2358668/config/crd/bases/postgresql.cnpg.io_backups.yaml#L256)):

```yaml
...
barmanObjectStore:
  endpointURL: ...
  ...
  endpointCA:
    name: secret-name
    key: secret-key
```

Note that while the current [in-tree barman cloud support is being
deprecated in favour of a barman cloud
plugin](https://cloudnative-pg.io/releases/cloudnative-pg-1-26.0-released/#barman-cloud-deprecation-begins),
the [migration
guide](https://cloudnative-pg.io/plugin-barman-cloud/docs/migration/)
says the new `ObjectStore` CRD has a **direct mapping** between it and
the legacy `barmanObjectStore`. Therefore, the example above can be
translated to:

```yaml
apiVersion: barmancloud.cnpg.io/v1
kind: ObjectStore
...
spec:
  configuration:
    endpointURL: ...
    ...
    endpointCA:
      name: secret-name
      key: secret-key
```

**✔️ Checklist:**

- [X] ⚖️ My code follows the style guidelines of this project
- [X] 👀 I have performed a self-review of my own code
- [X] #️⃣ I have commented my code, particularly in hard-to-understand
areas
- [X] 📄 I have made changes to the documentation
- [X] 🧪 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
- [X] I made sure the title starts with `feat(chart-name):`,
`fix(chart-name):`, `chore(chart-name):`, `docs(chart-name):` or
`fix(docs):`

** App addition**

If this PR is an app addition please make sure you have done the
following.

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

---------

Signed-off-by: Kjeld Schouten <info@kjeldschouten.nl>
Co-authored-by: Kjeld Schouten <info@kjeldschouten.nl>
2025-10-06 12:04:39 +02:00

231 lines
11 KiB
Smarty

{{/* PVC Spawner */}}
{{/* Call this template:
{{ include "tc.v1.common.spawner.pvc" $ -}}
*/}}
{{- define "tc.v1.common.spawner.pvc" -}}
{{- range $name, $persistence := .Values.persistence -}}
{{- $enabled := (include "tc.v1.common.lib.util.enabled" (dict
"rootCtx" $ "objectData" $persistence
"name" $name "caller" "Persistence"
"key" "persistence")) -}}
{{- if eq $enabled "true" -}}
{{/* Create a copy of the persistence */}}
{{- $objectData := (mustDeepCopy $persistence) -}}
{{- $_ := set $objectData "type" ($objectData.type | default $.Values.global.fallbackDefaults.persistenceType) -}}
{{- include "tc.v1.common.lib.util.metaListToDict" (dict "objectData" $objectData) -}}
{{/* Perform general validations */}}
{{- include "tc.v1.common.lib.persistence.validation" (dict "rootCtx" $ "objectData" $objectData) -}}
{{- include "tc.v1.common.lib.metadata.validation" (dict "objectData" $objectData "caller" "Persistence") -}}
{{/* Only spawn PVC if its enabled and any type of "pvc" */}}
{{- $types := (list "pvc") -}}
{{- if and (mustHas $objectData.type $types) (not $objectData.existingClaim) -}}
{{/* Set the name of the PVC */}}
{{- $_ := set $objectData "name" (include "tc.v1.common.lib.storage.pvc.name" (dict "rootCtx" $ "objectName" $name "objectData" $objectData)) -}}
{{- $_ := set $objectData "shortName" $name -}}
{{- if and $objectData.static $objectData.static.mode (ne $objectData.static.mode "disabled") -}}
{{- $_ := set $objectData "storageClass" ($objectData.storageClass | default $objectData.name) -}}
{{- $_ := set $objectData "volumeName" $objectData.name -}}
{{- if eq $objectData.static.mode "smb" -}}
{{/* Validate SMB CSI */}}
{{- include "tc.v1.common.lib.storage.smbCSI.validation" (dict "rootCtx" $ "objectData" $objectData) -}}
{{- $_ := set $objectData "provisioner" "smb.csi.k8s.io" -}}
{{- $_ := set $objectData.static "driver" "smb.csi.k8s.io" -}}
{{/* Create secret with creds */}}
{{- $secretData := (dict
"name" $objectData.name
"labels" ($objectData.labels | default dict)
"annotations" ($objectData.annotations | default dict)
"data" (dict "username" $objectData.static.username "password" $objectData.static.password)
) -}}
{{- with $objectData.domain -}}
{{- $_ := set $secretData.data "domain" . -}}
{{- end -}}
{{- include "tc.v1.common.class.secret" (dict "rootCtx" $ "objectData" $secretData) -}}
{{- else if eq $objectData.static.mode "nfs" -}}
{{/* Validate NFS CSI */}}
{{- include "tc.v1.common.lib.storage.nfsCSI.validation" (dict "rootCtx" $ "objectData" $objectData) -}}
{{- $_ := set $objectData "provisioner" "nfs.csi.k8s.io" -}}
{{- $_ := set $objectData.static "driver" "nfs.csi.k8s.io" -}}
{{- else if eq $objectData.static.mode "custom" -}}
{{- $_ := set $objectData "provisioner" $objectData.static.provisioner -}}
{{- $_ := set $objectData.static "driver" $objectData.static.driver -}}
{{- end -}}
{{/* Create the PV */}}
{{- include "tc.v1.common.class.pv" (dict "rootCtx" $ "objectData" $objectData) -}}
{{- else if $objectData.volumeName -}}
{{- $_ := set $objectData "storageClass" ($objectData.storageClass | default $objectData.name) -}}
{{- end -}}
{{/* Create VolSync objects */}}
{{- range $volsync := $objectData.volsync -}}
{{- $srcEnabled := eq (include "tc.v1.common.lib.util.enabled" (dict
"rootCtx" $ "objectData" $volsync.src
"name" $volsync.name "caller" "VolSync Source"
"key" "volsync")) "true" -}}
{{- $destEnabled := eq (include "tc.v1.common.lib.util.enabled" (dict
"rootCtx" $ "objectData" $volsync.dest
"name" $volsync.name "caller" "VolSync Destination"
"key" "volsync")) "true" -}}
{{- if or $srcEnabled $destEnabled -}}
{{- $volsyncData := (mustDeepCopy $volsync) -}}
{{- include "tc.v1.common.lib.volsync.validation" (dict "objectData" $volsyncData "rootCtx" $) -}}
{{- include "tc.v1.common.lib.metadata.validation" (dict "objectData" $volsyncData "caller" "PVC - VolSync") -}}
{{/* Create Secret for VolSync */}}
{{- $volsyncSecretName := printf "%s-volsync-%s" $objectData.name $volsyncData.name -}}
{{- $_ := set $volsyncData "repository" $volsyncSecretName -}}
{{- $credentials := get $.Values.credentials $volsync.credentials -}}
{{/* Only amazon needs the https:// trimmed, anything else requires it */}}
{{- $url := $credentials.url -}}
{{- if hasPrefix "https://s3." $url -}}
{{- $url = trimPrefix "https://" $url -}}
{{- end -}}
{{- $baseRepo := printf "s3:%s/%s" $url $credentials.bucket -}}
{{- $repoSuffix := printf "%s/volsync/%s-volsync-%s" $.Release.Name $objectData.shortName $volsyncData.name -}}
{{- $resticrepository := printf "%s/%s" $baseRepo $repoSuffix -}}
{{- if $credentials.path -}}
{{- $resticrepository = printf "%s/%s/%s" $baseRepo ($credentials.path | trimSuffix "/") $repoSuffix -}}
{{- end -}}
{{- $volsyncSecretData := (dict
"name" $volsyncSecretName
"labels" ($volsync.labels | default dict)
"annotations" ($volsync.annotations | default dict)
"data" (dict
"RESTIC_REPOSITORY" $resticrepository
"RESTIC_PASSWORD" $credentials.encrKey
"AWS_ACCESS_KEY_ID" $credentials.accessKey
"AWS_SECRET_ACCESS_KEY" $credentials.secretKey
)
) -}}
{{- include "tc.v1.common.class.secret" (dict "rootCtx" $ "objectData" $volsyncSecretData) -}}
{{- if $credentials.customCASecretRef -}}
{{/* Get the customCA secret name */}}
{{- $customCASecretRef := $credentials.customCASecretRef -}}
{{- $expandName := (include "tc.v1.common.lib.util.expandName" (dict
"rootCtx" $ "objectData" $customCASecretRef
"name" $customCASecretRef.name "caller" "PVC - VolSync"
"key" (printf "credentials.%s.customCASecretRef.name" $volsyncData.credentials))) -}}
{{- $CAsecretName := tpl $customCASecretRef.name $ -}}
{{- if eq $expandName "true" -}}
{{- $fullname := include "tc.v1.common.lib.chart.names.fullname" $ -}}
{{- $CAsecretName = (printf "%s-%s" $fullname $CAsecretName) -}}
{{- end -}}
{{- $_ := set $volsyncData "customCA" (dict "name" $CAsecretName "key" $customCASecretRef.key) -}}
{{- else if $credentials.customCA -}}
{{/* Create Custom CA Secret for VolSync */}}
{{- $volsyncCASecretName := printf "%s-volsync-ca-%s" (include "tc.v1.common.lib.chart.names.fullname" $ ) $volsync.credentials -}}
{{- $volsyncCAKey := "ca.crt" -}}
{{- $_ := set $volsyncData "customCA" $volsyncCASecretName -}}
{{- $volsyncCASecretData := (dict
"name" $volsyncCASecretName
"labels" ($volsync.labels | default dict)
"annotations" ($volsync.annotations | default dict)
"data" (dict
$volsyncCAKey $credentials.customCA
)
) -}}
{{- include "tc.v1.common.class.secret" (dict "rootCtx" $ "objectData" $volsyncCASecretData) -}}
{{- $_ := set $volsyncData "customCA" (dict "name" $volsyncCASecretName "key" $volsyncCAKey) -}}
{{- end -}}
{{/* Create VolSync resources*/}}
{{- if $srcEnabled -}}
{{- include "tc.v1.common.class.replicationsource" (dict "rootCtx" $ "objectData" $objectData "volsyncData" $volsyncData) -}}
{{- end -}}
{{- if $destEnabled -}}
{{- include "tc.v1.common.class.replicationdestination" (dict "rootCtx" $ "objectData" $objectData "volsyncData" $volsyncData) -}}
{{/* modify PVC if enabled */}}
{{- $destname := printf "%s-%s-dest" $objectData.name $volsyncData.name -}}
{{- $datasourceref := dict "kind" "ReplicationDestination" "apiGroup" "volsync.backube" "name" $destname -}}
{{- $_ := set $objectData "dataSourceRef" $datasourceref -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{/* Call class to create the object */}}
{{- include "tc.v1.common.class.pvc" (dict "rootCtx" $ "objectData" $objectData) -}}
{{/* Create VolumeSnapshots */}}
{{- range $volSnap := $objectData.volumeSnapshots -}}
{{/* Create a copy of the volumesnapshot */}}
{{- $volSnapData := (mustDeepCopy $volSnap) -}}
{{/* PVC FullName - Snapshot Name*/}}
{{- $snapshotName := printf "%s-%s" $objectData.name $volSnap.name -}}
{{/* Perform validations */}} {{/* volumesnapshots have a max name length of 253 */}}
{{- include "tc.v1.common.lib.chart.names.validation" (dict "name" $snapshotName "length" 253) -}}
{{- include "tc.v1.common.lib.metadata.validation" (dict "objectData" $volSnapData "caller" "PVC - Volume Snapshot") -}}
{{/* Set the name of the volumesnapshot */}}
{{- $_ := set $volSnapData "name" $snapshotName -}}
{{- $_ := set $volSnapData "shortName" $volSnap.name -}}
{{- $_ := set $volSnapData "source" (dict "persistentVolumeClaimName" $objectData.name) -}}
{{- include "tc.v1.common.lib.volumesnapshot.validation" (dict "objectData" $volSnapData) -}}
{{/* Call class to create the object */}}
{{- include "tc.v1.common.class.volumesnapshot" (dict "rootCtx" $ "objectData" $volSnapData) -}}
{{- end -}}
{{- end -}}
{{- if eq $objectData.type "iscsi" -}}
{{- if or $objectData.iscsi.authSession $objectData.iscsi.authDiscovery -}}
{{/* Set the name of the PVC */}}
{{- $_ := set $objectData "name" (include "tc.v1.common.lib.storage.pvc.name" (dict "rootCtx" $ "objectName" $name "objectData" $objectData)) -}}
{{- $_ := set $objectData "shortName" $name -}}
{{- $secretData := (dict
"name" $objectData.name
"labels" ($objectData.labels | default dict)
"annotations" ($objectData.annotations | default dict)
"type" "kubernetes.io/iscsi-chap"
"data" (include "tc.v1.common.lib.storage.iscsi.chap" (dict "rootCtx" $ "objectData" $objectData) | fromJson)
) -}}
{{- include "tc.v1.common.class.secret" (dict "rootCtx" $ "objectData" $secretData) -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}