From 53ea5f26b4bd39fe947eeb7a78165a5d2166285c Mon Sep 17 00:00:00 2001 From: Benedikt Fichtner <62395480+Pulsar7@users.noreply.github.com> Date: Sun, 19 Jul 2026 22:02:39 +0200 Subject: [PATCH 1/2] Add PVC-Resource with option to create new PVC for db-dumps along with PVC-helm-unittest and adjust PVC-related Helm-fields at CronJob-Resource --- charts/cnpg-cluster/Chart.yaml | 2 +- charts/cnpg-cluster/README.md | 8 +- .../templates/backup-cronjob.yaml | 2 +- charts/cnpg-cluster/templates/pvc.yaml | 18 ++++ .../tests/backup-cronjob_test.yaml | 8 +- charts/cnpg-cluster/tests/pvc_test.yaml | 91 +++++++++++++++++++ charts/cnpg-cluster/values.schema.json | 13 ++- charts/cnpg-cluster/values.yaml | 32 ++++++- 8 files changed, 162 insertions(+), 12 deletions(-) create mode 100644 charts/cnpg-cluster/templates/pvc.yaml create mode 100644 charts/cnpg-cluster/tests/pvc_test.yaml diff --git a/charts/cnpg-cluster/Chart.yaml b/charts/cnpg-cluster/Chart.yaml index 195f328..d77ba6d 100644 --- a/charts/cnpg-cluster/Chart.yaml +++ b/charts/cnpg-cluster/Chart.yaml @@ -1,4 +1,4 @@ apiVersion: v2 name: cnpg-cluster description: Custom Helm-Chart for a CNPG-Cluster with db-dump CronJob functionality -version: 0.1.0 \ No newline at end of file +version: 0.1.1 \ No newline at end of file diff --git a/charts/cnpg-cluster/README.md b/charts/cnpg-cluster/README.md index 6181ac8..55129e5 100644 --- a/charts/cnpg-cluster/README.md +++ b/charts/cnpg-cluster/README.md @@ -9,7 +9,7 @@ Custom Helm-Chart for a CNPG-Cluster with db-dump CronJob functionality > The chart does not configure or guarantee the backup of the PV - this responsibility remains with the user. > This chart is tailored to a specific CNPG backup use case. If you try to use it in your cluster, please verify that it fits your environment and requirements (including PV provisioning and backup/retention setup). -![Version: 0.1.0](https://img.shields.io/badge/Version-0.1.0-informational?style=flat-square) +![Version: 0.1.1](https://img.shields.io/badge/Version-0.1.1-informational?style=flat-square) ## Values @@ -19,7 +19,11 @@ Custom Helm-Chart for a CNPG-Cluster with db-dump CronJob functionality |-----|------|---------|-------------| | backupCronjob.containerImage | string | `"postgres:16-alpine"` | Container-Image with 'pg_dumpall'-binary at PATH | | backupCronjob.enabled | bool | `false` | Whether to enable backups | -| backupCronjob.persistentVolumeClaimName | string | `"cnpg-db-backup-pvc"` | Name of existing PVC where the database-dump is being stored at | +| backupCronjob.persistentVolumeClaim.accessModes | list | `["ReadWriteOnce"]` | Access-modes for new created PVC. Gets ignored when `useExistingPVC: true` | +| backupCronjob.persistentVolumeClaim.claimName | string | `"cnpg-db-backup-pvc"` | Name of PVC where the database-dump is being stored at | +| backupCronjob.persistentVolumeClaim.storageClassName | string | `"local-path"` | Storage request for new created PVC. Gets ignored when `useExistingPVC: true` | +| backupCronjob.persistentVolumeClaim.storageRequest | string | `"10Gi"` | Storage request for new created PVC. Gets ignored when `useExistingPVC: true` | +| backupCronjob.persistentVolumeClaim.useExisting | bool | `false` | Use existing PVC and don't create new PVC-resource with the given `persistentVolumeClaimName`. | | backupCronjob.restartPolicy | string | `"OnFailure"` | Policy to restart Backup-Cronjob | | backupCronjob.schedule | string | `"10 * * * *"` | Schedule of the Cronjob (NOTE: Align with the actual PV-Backup-Schedule) | | backupCronjob.targetDatabaseHost | string | `"cnpg-cluster-app-rw.cnpg-system"` | Target CNPG-database Host | diff --git a/charts/cnpg-cluster/templates/backup-cronjob.yaml b/charts/cnpg-cluster/templates/backup-cronjob.yaml index b23a29e..f967d3d 100644 --- a/charts/cnpg-cluster/templates/backup-cronjob.yaml +++ b/charts/cnpg-cluster/templates/backup-cronjob.yaml @@ -46,5 +46,5 @@ spec: volumes: - name: backup-volume persistentVolumeClaim: - claimName: {{ $cronjob.persistentVolumeClaimName }} + claimName: {{ $cronjob.persistentVolumeClaim.claimName }} {{- end -}} \ No newline at end of file diff --git a/charts/cnpg-cluster/templates/pvc.yaml b/charts/cnpg-cluster/templates/pvc.yaml new file mode 100644 index 0000000..514b72b --- /dev/null +++ b/charts/cnpg-cluster/templates/pvc.yaml @@ -0,0 +1,18 @@ +{{ include "cnpgdb.validateValues" . }} +{{- $postgresCluster := .Values.postgresCluster -}} +{{- $backupCronjob := .Values.backupCronjob -}} +{{- $pvc := $backupCronjob.persistentVolumeClaim -}} +{{- if and $postgresCluster.enableSuperuserAccess $backupCronjob.enabled (not $pvc.useExisting) -}} +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: {{ $pvc.claimName }} + namespace: {{ .Release.Namespace | quote }} + labels: {{- include "cnpgdb.labels" . }} +spec: + storageClassName: {{ $pvc.storageClassName }} + accessModes: {{- toYaml $pvc.accessModes | nindent 4 }} + resources: + requests: + storage: {{ $pvc.storageRequest | quote }} +{{- end -}} \ No newline at end of file diff --git a/charts/cnpg-cluster/tests/backup-cronjob_test.yaml b/charts/cnpg-cluster/tests/backup-cronjob_test.yaml index 0088115..c90fdc0 100644 --- a/charts/cnpg-cluster/tests/backup-cronjob_test.yaml +++ b/charts/cnpg-cluster/tests/backup-cronjob_test.yaml @@ -45,11 +45,12 @@ tests: cnpgSuperUserSecretName: "test-cnpg-superuser-secret" backupCronjob: enabled: true + persistentVolumeClaim: + claimName: "1337-test-cronjob-pvc" schedule: "1337 1337 1337" timeZone: "EARTH" restartPolicy: OnFailure containerImage: "postgres:1337-alpine" - persistentVolumeClaimName: "test-cronjob-backup-pvc" targetDatabaseHost: "cnpg-db.1337-db-host" targetDatabasePort: 1337 # @@ -94,5 +95,10 @@ tests: > /backup/cnpg-sqlbkp.sql ls -lah /backup/cnpg-sqlbkp.sql + - equal: + path: spec.jobTemplate.spec.template.spec.volumes[?(@.name=="backup-volume")].persistentVolumeClaim.claimName + value: "1337-test-cronjob-pvc" + + \ No newline at end of file diff --git a/charts/cnpg-cluster/tests/pvc_test.yaml b/charts/cnpg-cluster/tests/pvc_test.yaml new file mode 100644 index 0000000..4b9303d --- /dev/null +++ b/charts/cnpg-cluster/tests/pvc_test.yaml @@ -0,0 +1,91 @@ +#------------------------------------------------------------------------------ +# +# PVC-Unittests Test-Suite for CNPG-Cluster +# +# > https://github.com/helm-unittest/helm-unittest/blob/main/DOCUMENT.md +# +#------------------------------------------------------------------------------ +# +# Test suite name to show on test result output +suite: Test PVC +# +# The template files scope to test in this suite +# Only the selected files will be rendered +templates: + - templates/pvc.yaml +# +tests: + - it: should NOT render new PVC-Resource with disabled superUserAccess + # + set: + postgresCluster: + enableSuperuserAccess: false + cnpgSuperUserSecretName: "test-cnpg-superuser-secret" + backupCronjob.enabled: true + # + asserts: + - failedTemplate: + errorMessage: "VALUES VALIDATION:\ncnpgdb: enableSuperuserAccess\n You have to enable external superUserAccess when enabling Backups" + + - it: should NOT render new PVC-Resource with disabled Cronjob + # + set: + postgresCluster: + enableSuperuserAccess: true + cnpgSuperUserSecretName: "test-cnpg-superuser-secret" + backupCronjob.enabled: false + # + asserts: + - notExists: + path: spec + + - it: should NOT render new PVC-Resource with useExisting + # + set: + postgresCluster: + enableSuperuserAccess: true + cnpgSuperUserSecretName: "test-cnpg-superuser-secret" + backupCronjob: + enabled: true + persistentVolumeClaim: + useExisting: true + # + asserts: + - notExists: + path: spec + + - it: should render new PVC-Resource with adjusted Values + # + set: + postgresCluster: + enableSuperuserAccess: true + cnpgSuperUserSecretName: "test-cnpg-superuser-secret" + backupCronjob: + enabled: true + persistentVolumeClaim: + useExisting: false + claimName: "1337-test-cronjob-pvc" + storageRequest: "1337Gi" + storageClassName: "test-storage-class-name-for-backup-cronjob" + accessModes: ["ReadWriteOnce"] + # + asserts: + - isKind: + of: PersistentVolumeClaim + + - equal: + path: metadata.name + value: "1337-test-cronjob-pvc" + + - equal: + path: spec.storageClassName + value: "test-storage-class-name-for-backup-cronjob" + + - equal: + path: spec.accessModes + value: + - "ReadWriteOnce" + + - equal: + path: spec.resources.requests.storage + value: "1337Gi" diff --git a/charts/cnpg-cluster/values.schema.json b/charts/cnpg-cluster/values.schema.json index 06cf7f4..0448eaa 100644 --- a/charts/cnpg-cluster/values.schema.json +++ b/charts/cnpg-cluster/values.schema.json @@ -27,9 +27,18 @@ "timeZone": {"type": "string"}, "restartPolicy": {"type": "string"}, "containerImage": {"type": "string"}, - "persistentVolumeClaimName": {"type": "string"}, "targetDatabaseHost": {"type": "string"}, - "targetDatabasePort": {"type": "integer"} + "targetDatabasePort": {"type": "integer"}, + "persistentVolumeClaim": { + "type": "object", + "properties": { + "useExisting": {"type": "boolean"}, + "claimName": {"type": "string"}, + "storageRequest": {"type": "string"}, + "storageClassName": {"type": "string"}, + "accessModes": {"type": "array"} + } + } } } }, diff --git a/charts/cnpg-cluster/values.yaml b/charts/cnpg-cluster/values.yaml index 6474728..14e5735 100644 --- a/charts/cnpg-cluster/values.yaml +++ b/charts/cnpg-cluster/values.yaml @@ -87,14 +87,36 @@ backupCronjob: # @section -- Backup-Cronjob specifications containerImage: "postgres:16-alpine" # - # -- Name of existing PVC where the database-dump is being stored at - # @section -- Backup-Cronjob specifications - persistentVolumeClaimName: "cnpg-db-backup-pvc" - # # -- Target CNPG-database Host # @section -- Backup-Cronjob specifications targetDatabaseHost: "cnpg-cluster-app-rw.cnpg-system" # # -- Target CNPG-database Port # @section -- Backup-Cronjob specifications - targetDatabasePort: 5432 \ No newline at end of file + targetDatabasePort: 5432 + # + persistentVolumeClaim: + # + # -- Use existing PVC and don't create new PVC-resource with the given + # `persistentVolumeClaimName`. + # @section -- Backup-Cronjob specifications + useExisting: false + # + # -- Name of PVC where the database-dump is being stored at + # @section -- Backup-Cronjob specifications + claimName: "cnpg-db-backup-pvc" + # + # -- Storage request for new created PVC. + # Gets ignored when `useExistingPVC: true` + # @section -- Backup-Cronjob specifications + storageRequest: "10Gi" + # + # -- Storage request for new created PVC. + # Gets ignored when `useExistingPVC: true` + # @section -- Backup-Cronjob specifications + storageClassName: "local-path" + # + # -- Access-modes for new created PVC. + # Gets ignored when `useExistingPVC: true` + # @section -- Backup-Cronjob specifications + accessModes: ["ReadWriteOnce"] \ No newline at end of file From d3ed59ac065b4eac1ddc9f6a898783ab71c1d32e Mon Sep 17 00:00:00 2001 From: Benedikt Fichtner <62395480+Pulsar7@users.noreply.github.com> Date: Sun, 19 Jul 2026 22:12:40 +0200 Subject: [PATCH 2/2] Bump cnpg-cluster Helm-Chart Version to 0.2.0 - since it contains breaking changes and add CHANGELOG-entry for 0.2.0 --- charts/cnpg-cluster/CHANGELOG.md | 8 ++++++++ charts/cnpg-cluster/Chart.yaml | 2 +- charts/cnpg-cluster/README.md | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/charts/cnpg-cluster/CHANGELOG.md b/charts/cnpg-cluster/CHANGELOG.md index a4bc8bd..4cf0b39 100644 --- a/charts/cnpg-cluster/CHANGELOG.md +++ b/charts/cnpg-cluster/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog for the 'cnpg-cluster' Helm-Chart +# v0.2.0 + +> [!CAUTION] +> Upgrading from `0.1.0` needs adjusting Helm-Values, since the Helm-field `backupCronjob.persistentVolumeClaimName` doesn't exist anymore. Please configure the claim-name of the PVC at `backupCronjob.persistentVolumeClaim.claimName`. +> By default, the Chart is creating a **new** PVC-Resource based on the Helm-Values specificed at `backupCronjob.persistentVolumeClaim`. Set `backupCronjob.persistentVolumeClaim.useExisting: true` if you want to use an existing PVC with the provided Claim-Name instead. + +- Create new **PVC**-Resource for **CronJob** db-dump by default instead of using an existing PVC + # v0.1.0 - Initial version diff --git a/charts/cnpg-cluster/Chart.yaml b/charts/cnpg-cluster/Chart.yaml index d77ba6d..9c4e460 100644 --- a/charts/cnpg-cluster/Chart.yaml +++ b/charts/cnpg-cluster/Chart.yaml @@ -1,4 +1,4 @@ apiVersion: v2 name: cnpg-cluster description: Custom Helm-Chart for a CNPG-Cluster with db-dump CronJob functionality -version: 0.1.1 \ No newline at end of file +version: 0.2.0 \ No newline at end of file diff --git a/charts/cnpg-cluster/README.md b/charts/cnpg-cluster/README.md index 55129e5..6ef693a 100644 --- a/charts/cnpg-cluster/README.md +++ b/charts/cnpg-cluster/README.md @@ -9,7 +9,7 @@ Custom Helm-Chart for a CNPG-Cluster with db-dump CronJob functionality > The chart does not configure or guarantee the backup of the PV - this responsibility remains with the user. > This chart is tailored to a specific CNPG backup use case. If you try to use it in your cluster, please verify that it fits your environment and requirements (including PV provisioning and backup/retention setup). -![Version: 0.1.1](https://img.shields.io/badge/Version-0.1.1-informational?style=flat-square) +![Version: 0.2.0](https://img.shields.io/badge/Version-0.2.0-informational?style=flat-square) ## Values