From 720c056eaf7dae6ce1fdfd2120efc747ff387166 Mon Sep 17 00:00:00 2001 From: Kjeld Schouten Date: Thu, 7 Mar 2024 13:49:33 +0100 Subject: [PATCH] fix: correct cnpg naming behavior and seperate default vctAccessmodes from accessmodes (#726) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description** Current recovery behavior doesn't target the correct backup serverName if the serverName isn't explicitly set. Even when set it still sometimes misbehaves so we're going to also set the externCluster-name accordingly as a fail-safe **โš™๏ธ Type of change** - [ ] โš™๏ธ Feature/App addition - [x] ๐Ÿช› Bugfix - [x] โš ๏ธ 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:** Technically breaking, as we're changing the default behavior of VCT. However, we don't use VCT's a lot **โœ”๏ธ 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 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._ --- library/common-test/Chart.yaml | 2 +- .../tests/cnpg/cluster_backup_spec_test.yaml | 51 ++++++++++--------- .../cnpg/cluster_recovery_spec_test.yaml | 30 ++++++----- library/common/Chart.yaml | 2 +- .../common/templates/class/cnpg/_cluster.tpl | 14 +++++ .../lib/cnpg/barmanObjectStore/_azure.tpl | 4 +- .../lib/cnpg/barmanObjectStore/_google.tpl | 4 +- .../lib/cnpg/barmanObjectStore/_s3.tpl | 4 +- .../lib/cnpg/cluster/_boostrapRecovery.tpl | 6 ++- .../_bootstrapRecoveryExternalCluster.tpl | 6 ++- .../lib/cnpg/cluster/_validation.tpl | 19 +++++++ .../lib/storage/_volumeClaimTemplates.tpl | 11 +++- library/common/values.yaml | 5 +- 13 files changed, 109 insertions(+), 49 deletions(-) diff --git a/library/common-test/Chart.yaml b/library/common-test/Chart.yaml index a48846ba..0027efd9 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: ~18.2.0 + version: ~19.0.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/cnpg/cluster_backup_spec_test.yaml b/library/common-test/tests/cnpg/cluster_backup_spec_test.yaml index c56ea5b1..4e7570df 100644 --- a/library/common-test/tests/cnpg/cluster_backup_spec_test.yaml +++ b/library/common-test/tests/cnpg/cluster_backup_spec_test.yaml @@ -54,18 +54,7 @@ tests: path: spec content: backup: - target: primary - retentionPolicy: 10d barmanObjectStore: - wal: - compression: gzip - encryption: AES256 - data: - compression: gzip - encryption: AES256 - jobs: 2 - endpointURL: null - destinationPath: some-path azureCredentials: connectionString: key: CONNECTION_STRING @@ -79,6 +68,18 @@ tests: storageSasToken: key: STORAGE_SAS_TOKEN name: test-release-name-common-test-cnpg-my-pg-provider-backup-azure-creds + data: + compression: gzip + encryption: AES256 + jobs: 2 + destinationPath: some-path + endpointURL: null + serverName: test-release-name-common-test-cnpg-my-pg + wal: + compression: gzip + encryption: AES256 + retentionPolicy: 10d + target: primary - it: should generate correct spec with backups (google) set: @@ -126,23 +127,24 @@ tests: path: spec content: backup: - target: primary - retentionPolicy: 10d barmanObjectStore: - wal: - compression: gzip - encryption: AES256 data: compression: gzip encryption: AES256 jobs: 2 - endpointURL: null destinationPath: some-path + endpointURL: null googleCredentials: - gkeEnvironment: false applicationCredentials: key: APPLICATION_CREDENTIALS name: test-release-name-common-test-cnpg-my-pg-provider-backup-google-creds + gkeEnvironment: false + serverName: test-release-name-common-test-cnpg-my-pg + wal: + compression: gzip + encryption: AES256 + retentionPolicy: 10d + target: primary - it: should generate correct spec with backups (s3) set: @@ -193,18 +195,13 @@ tests: path: spec content: backup: - target: primary - retentionPolicy: 10d barmanObjectStore: - wal: - compression: gzip - encryption: AES256 data: compression: gzip encryption: AES256 jobs: 2 - endpointURL: some-url destinationPath: some-path + endpointURL: some-url s3Credentials: accessKeyId: key: ACCESS_KEY_ID @@ -212,6 +209,12 @@ tests: secretAccessKey: key: ACCESS_SECRET_KEY name: test-release-name-common-test-cnpg-my-pg-provider-backup-s3-creds + serverName: test-release-name-common-test-cnpg-my-pg + wal: + compression: gzip + encryption: AES256 + retentionPolicy: 10d + target: primary - it: should generate correct spec with empty target set: diff --git a/library/common-test/tests/cnpg/cluster_recovery_spec_test.yaml b/library/common-test/tests/cnpg/cluster_recovery_spec_test.yaml index 50019385..07717ad7 100644 --- a/library/common-test/tests/cnpg/cluster_recovery_spec_test.yaml +++ b/library/common-test/tests/cnpg/cluster_recovery_spec_test.yaml @@ -122,7 +122,7 @@ tests: name: test-release-name-common-test-cnpg-my-pg-user owner: test-user database: test-db - source: objectStoreRecoveryCluster + source: test-release-name-common-test-cnpg-my-pg recoveryTarget: targetTime: "2021-01-01T00:00:00Z" - documentIndex: *clusterDoc @@ -130,8 +130,9 @@ tests: path: spec content: externalClusters: - - name: objectStoreRecoveryCluster + - name: test-release-name-common-test-cnpg-my-pg barmanObjectStore: + serverName: test-release-name-common-test-cnpg-my-pg destinationPath: gs://some-bucket/some-path endpointURL: null googleCredentials: @@ -192,7 +193,7 @@ tests: name: test-release-name-common-test-cnpg-my-pg-user owner: test-user database: test-db - source: objectStoreRecoveryCluster + source: test-release-name-common-test-cnpg-my-pg recoveryTarget: targetTime: "2021-01-01T00:00:00Z" - documentIndex: *clusterDoc @@ -200,8 +201,9 @@ tests: path: spec content: externalClusters: - - name: objectStoreRecoveryCluster + - name: test-release-name-common-test-cnpg-my-pg barmanObjectStore: + serverName: test-release-name-common-test-cnpg-my-pg destinationPath: gs://some-bucket endpointURL: null googleCredentials: @@ -264,14 +266,15 @@ tests: name: test-release-name-common-test-cnpg-my-pg-user owner: test-user database: test-db - source: objectStoreRecoveryCluster + source: test-release-name-common-test-cnpg-my-pg - documentIndex: *clusterDoc isSubset: path: spec content: externalClusters: - - name: objectStoreRecoveryCluster + - name: test-release-name-common-test-cnpg-my-pg barmanObjectStore: + serverName: test-release-name-common-test-cnpg-my-pg destinationPath: s3://some-bucket/some-path endpointURL: https://s3.some-region.amazonaws.com s3Credentials: @@ -335,14 +338,15 @@ tests: name: test-release-name-common-test-cnpg-my-pg-user owner: test-user database: test-db - source: objectStoreRecoveryCluster + source: test-release-name-common-test-cnpg-my-pg - documentIndex: *clusterDoc isSubset: path: spec content: externalClusters: - - name: objectStoreRecoveryCluster + - name: test-release-name-common-test-cnpg-my-pg barmanObjectStore: + serverName: test-release-name-common-test-cnpg-my-pg destinationPath: s3://some-bucket endpointURL: some-endpoint-url s3Credentials: @@ -409,14 +413,15 @@ tests: name: test-release-name-common-test-cnpg-my-pg-user owner: test-user database: test-db - source: objectStoreRecoveryCluster + source: test-release-name-common-test-cnpg-my-pg - documentIndex: *clusterDoc isSubset: path: spec content: externalClusters: - - name: objectStoreRecoveryCluster + - name: test-release-name-common-test-cnpg-my-pg barmanObjectStore: + serverName: test-release-name-common-test-cnpg-my-pg destinationPath: https://some-storage-account.some-service-name.core.windows.net/some-container-name/some-path endpointURL: null azureCredentials: @@ -486,14 +491,15 @@ tests: name: test-release-name-common-test-cnpg-my-pg-user owner: test-user database: test-db - source: objectStoreRecoveryCluster + source: test-release-name-common-test-cnpg-my-pg - documentIndex: *clusterDoc isSubset: path: spec content: externalClusters: - - name: objectStoreRecoveryCluster + - name: test-release-name-common-test-cnpg-my-pg barmanObjectStore: + serverName: test-release-name-common-test-cnpg-my-pg destinationPath: https://some-storage-account.some-service-name.core.windows.net/some-container-name endpointURL: null azureCredentials: diff --git a/library/common/Chart.yaml b/library/common/Chart.yaml index 852d79f1..1ca5dfc2 100644 --- a/library/common/Chart.yaml +++ b/library/common/Chart.yaml @@ -15,7 +15,7 @@ maintainers: name: common sources: null type: library -version: 18.2.1 +version: 19.0.0 annotations: artifacthub.io/category: "integration-delivery" artifacthub.io/license: "BUSL-1.1" diff --git a/library/common/templates/class/cnpg/_cluster.tpl b/library/common/templates/class/cnpg/_cluster.tpl index 99571434..38104434 100644 --- a/library/common/templates/class/cnpg/_cluster.tpl +++ b/library/common/templates/class/cnpg/_cluster.tpl @@ -22,6 +22,8 @@ {{- $primaryUpdateStrategy := "unsupervised" -}} {{- $primaryUpdateMethod := "switchover" -}} {{- $logLevel := "info" -}} + {{- $accessModes := $rootCtx.Values.fallbackDefaults.vctAccessModes -}} + {{- $walAccessModes := $rootCtx.Values.fallbackDefaults.vctAccessModes -}} {{/* Make sure keys exist before try to access any sub keys */}} {{- if not (hasKey $objectData "cluster") -}} @@ -122,6 +124,14 @@ {{- $walSize = . -}} {{- end -}} + {{- with $objectData.cluster.storage.accessModes -}} + {{- $accessModes = . -}} + {{- end -}} + + {{- with $objectData.cluster.walStorage.accessModes -}} + {{- $walAccessModes = . -}} + {{- end -}} + {{- include "tc.v1.common.lib.util.verifycrd" (dict "rootCtx" $rootCtx "crd" "clusters.postgresql.cnpg.io" "missing" "CloudNative-PG") }} --- @@ -174,10 +184,14 @@ spec: storage: pvcTemplate: {{- $_ := set $objectData.cluster.storage "size" $size -}} + {{- $_ := set $objectData.cluster.storage "accessModes" $accessModes -}} + {{- include "tc.v1.common.lib.storage.pvc.spec" (dict "rootCtx" $rootCtx "objectData" $objectData.cluster.storage) | trim | nindent 6 }} walStorage: pvcTemplate: {{- $_ := set $objectData.cluster.walStorage "size" $walSize -}} + {{- $_ := set $objectData.cluster.storage "accessModes" $walAccessModes -}} + {{- include "tc.v1.common.lib.storage.pvc.spec" (dict "rootCtx" $rootCtx "objectData" $objectData.cluster.walStorage) | trim | nindent 6 }} {{- if $enableMonitoring }} monitoring: diff --git a/library/common/templates/lib/cnpg/barmanObjectStore/_azure.tpl b/library/common/templates/lib/cnpg/barmanObjectStore/_azure.tpl index 90cdde34..f0f0be38 100644 --- a/library/common/templates/lib/cnpg/barmanObjectStore/_azure.tpl +++ b/library/common/templates/lib/cnpg/barmanObjectStore/_azure.tpl @@ -19,7 +19,7 @@ {{- $serverName = $objectData.recovery.serverName -}} {{- end -}} {{- if $objectData.recovery.revision -}} - {{- $serverName = printf "%s-%s" $serverName $objectData.recovery.revision -}} + {{- $serverName = printf "%s-r%s" $serverName $objectData.recovery.revision -}} {{- end -}} {{- else if eq $type "backup" -}} {{- $endpointURL = $objectData.backups.endpointURL -}} @@ -29,7 +29,7 @@ {{- $serverName = $objectData.backups.serverName -}} {{- end -}} {{- if $objectData.backups.revision -}} - {{- $serverName = printf "%s-%s" $serverName $objectData.backups.revision -}} + {{- $serverName = printf "%s-r%s" $serverName $objectData.backups.revision -}} {{- end -}} {{- end -}} {{- if not $destinationPath -}} diff --git a/library/common/templates/lib/cnpg/barmanObjectStore/_google.tpl b/library/common/templates/lib/cnpg/barmanObjectStore/_google.tpl index 6a1e0fdf..da1a1f2d 100644 --- a/library/common/templates/lib/cnpg/barmanObjectStore/_google.tpl +++ b/library/common/templates/lib/cnpg/barmanObjectStore/_google.tpl @@ -24,7 +24,7 @@ {{- $serverName = $objectData.recovery.serverName -}} {{- end -}} {{- if $objectData.recovery.revision -}} - {{- $serverName = printf "%s-%s" $serverName $objectData.recovery.revision -}} + {{- $serverName = printf "%s-r%s" $serverName $objectData.recovery.revision -}} {{- end -}} {{- else if eq $type "backup" -}} {{- $endpointURL = $objectData.backups.endpointURL -}} @@ -34,7 +34,7 @@ {{- $serverName = $objectData.backups.serverName -}} {{- end -}} {{- if $objectData.backups.revision -}} - {{- $serverName = printf "%s-%s" $serverName $objectData.backups.revision -}} + {{- $serverName = printf "%s-r%s" $serverName $objectData.backups.revision -}} {{- end -}} {{- end -}} {{- if not $destinationPath -}} diff --git a/library/common/templates/lib/cnpg/barmanObjectStore/_s3.tpl b/library/common/templates/lib/cnpg/barmanObjectStore/_s3.tpl index 74dbb400..d3dec2a5 100644 --- a/library/common/templates/lib/cnpg/barmanObjectStore/_s3.tpl +++ b/library/common/templates/lib/cnpg/barmanObjectStore/_s3.tpl @@ -19,7 +19,7 @@ {{- $serverName = $objectData.recovery.serverName -}} {{- end -}} {{- if $objectData.recovery.revision -}} - {{- $serverName = printf "%s-%s" $serverName $objectData.recovery.revision -}} + {{- $serverName = printf "%s-r%s" $serverName $objectData.recovery.revision -}} {{- end -}} {{- else if eq $type "backup" -}} {{- $endpointURL = $objectData.backups.endpointURL -}} @@ -29,7 +29,7 @@ {{- $serverName = $objectData.backups.serverName -}} {{- end -}} {{- if $objectData.backups.revision -}} - {{- $serverName = printf "%s-%s" $serverName $objectData.backups.revision -}} + {{- $serverName = printf "%s-r%s" $serverName $objectData.backups.revision -}} {{- end -}} {{- end -}} {{- if not $destinationPath -}} diff --git a/library/common/templates/lib/cnpg/cluster/_boostrapRecovery.tpl b/library/common/templates/lib/cnpg/cluster/_boostrapRecovery.tpl index bbd0651b..cdb2cfbf 100644 --- a/library/common/templates/lib/cnpg/cluster/_boostrapRecovery.tpl +++ b/library/common/templates/lib/cnpg/cluster/_boostrapRecovery.tpl @@ -10,7 +10,11 @@ recovery: backup: name: {{ $objectData.recovery.backupName }} {{- else if eq $objectData.recovery.method "object_store" }} - source: objectStoreRecoveryCluster + {{- $serverName := $objectData.recovery.serverName | default $objectData.clusterName }} + {{- if $objectData.recovery.revision }} + {{- $serverName = printf "%s-r%s" $serverName $objectData.recovery.revision }} + {{- end }} + source: {{ $serverName }} {{- end -}} {{- if $objectData.recovery.pitrTarget -}} {{- with $objectData.recovery.pitrTarget.time }} diff --git a/library/common/templates/lib/cnpg/cluster/_bootstrapRecoveryExternalCluster.tpl b/library/common/templates/lib/cnpg/cluster/_bootstrapRecoveryExternalCluster.tpl index 0d607c8c..ad7add26 100644 --- a/library/common/templates/lib/cnpg/cluster/_bootstrapRecoveryExternalCluster.tpl +++ b/library/common/templates/lib/cnpg/cluster/_bootstrapRecoveryExternalCluster.tpl @@ -5,7 +5,11 @@ {{- if eq $objectData.recovery.method "object_store" }} externalClusters: - - name: objectStoreRecoveryCluster + {{- $serverName := $objectData.recovery.serverName | default $objectData.clusterName }} + {{- if $objectData.recovery.revision }} + {{- $serverName = printf "%s-r%s" $serverName $objectData.recovery.revision }} + {{- end }} + - name: {{ $serverName }} barmanObjectStore: {{- $provider := $objectData.recovery.provider -}} diff --git a/library/common/templates/lib/cnpg/cluster/_validation.tpl b/library/common/templates/lib/cnpg/cluster/_validation.tpl index a69a60bf..e6e05c97 100644 --- a/library/common/templates/lib/cnpg/cluster/_validation.tpl +++ b/library/common/templates/lib/cnpg/cluster/_validation.tpl @@ -102,7 +102,26 @@ {{- if not (mustRegexMatch $regexPolicy $objectData.backups.retentionPolicy) -}} {{- fail (printf "CNPG Backup - Expected [backups.retentionPolicy] to match regex [%s], got [%s]" $regexPolicy $objectData.backups.retentionPolicy) -}} {{- end -}} + + {{- if eq $objectData.mode "recovery" -}} + {{- $serverNameBackup := $objectData.backups.serverName | default $objectData.clusterName -}} + {{- $serverNameRecovery := $objectData.recovery.serverName | default $objectData.clusterName -}} + {{- if $objectData.backups.revision -}} + {{- $serverNameBackup = printf "%s-r%s" $serverNameBackup $objectData.backups.revision -}} + {{- end -}} + {{- if $objectData.recovery.revision -}} + {{- $serverNameRecovery = printf "%s-r%s" $serverNameRecovery $objectData.recovery.revision -}} + {{- end -}} + {{- if eq $serverNameBackup $serverNameRecovery -}} + {{- if $objectData.backups.serverName -}} + {{- fail (printf "CNPG Backup/Recovery - [backups.serverName] and [backups.revision] cannot match [recovery.serverName] and [recovery.revision] when in recovery mode and backup is enabled, for cnpg cluster [%s]" $objectData.clusterName) -}} + {{- else -}} + {{- fail (printf "CNPG Backup/Recovery - [backups.revision] cannot match [recovery.revision] when in recovery mode and backup is enabled, for cnpg cluster [%s]" $objectData.clusterName) -}} + {{- end -}} + {{- end -}} + {{- end -}} {{- end -}} {{- end -}} + {{- end -}} diff --git a/library/common/templates/lib/storage/_volumeClaimTemplates.tpl b/library/common/templates/lib/storage/_volumeClaimTemplates.tpl index a01fec1f..a24c3f36 100644 --- a/library/common/templates/lib/storage/_volumeClaimTemplates.tpl +++ b/library/common/templates/lib/storage/_volumeClaimTemplates.tpl @@ -43,8 +43,15 @@ objectData: The object data to be used to render the Pod. {{- with $vct.size -}} {{- $vctSize = tpl . $rootCtx -}} {{- end }} + {{- $_ := set $vct "size" $vctSize }} + + {{- $vctAccessModes := $rootCtx.Values.fallbackDefaults.vctAccessModes -}} + {{- with $vct.accessModes -}} + {{- $vctAccessModes = $vct.accessModes -}} + {{- end }} + {{- $_ := set $vct "accessModes" $vctAccessModes }} - metadata: - name: {{ include "tc.v1.common.lib.storage.pvc.name" (dict "rootCtx" $rootCtx "objectName" $vct.shortName "objectData" $vctValues) }} + name: {{ include "tc.v1.common.lib.storage.pvc.name" (dict "rootCtx" $rootCtx "objectName" $vct.shortName "objectData" $vct) }} {{- $labels := $vct.labels | default dict -}} {{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "labels" $labels) | trim) }} labels: @@ -56,7 +63,7 @@ objectData: The object data to be used to render the Pod. {{- . | nindent 6 }} {{- end }} spec: - {{- include "tc.v1.common.lib.storage.pvc.spec" (dict "rootCtx" $rootCtx "objectData" $vctValues) | trim | nindent 4 }} + {{- include "tc.v1.common.lib.storage.pvc.spec" (dict "rootCtx" $rootCtx "objectData" $vct) | trim | nindent 4 }} {{- end -}} {{- end -}} {{- end -}} diff --git a/library/common/values.yaml b/library/common/values.yaml index e8d1f711..870e947f 100644 --- a/library/common/values.yaml +++ b/library/common/values.yaml @@ -53,9 +53,12 @@ fallbackDefaults: pvcSize: 100Gi # -- Default VCT Size vctSize: 100Gi - # -- Default PVC/VCT Access Modes + # -- Default PVC Access Modes accessModes: - ReadWriteOnce + # -- Default VCT Access Modes + vctAccessModes: + - ReadWriteOnce # -- Default probe timeouts probeTimeouts: liveness: