From b0c7b1fefea2f25c7c796c77887ea5e5819d43dd Mon Sep 17 00:00:00 2001 From: "andy.lee" Date: Thu, 16 Jan 2025 16:18:43 +0800 Subject: [PATCH 1/2] fix: when choose encrypt can only choose encrypted storage class when creating image Signed-off-by: andy.lee --- .../harvesterhci.io.virtualmachineimage.vue | 35 ++++++++++++++++--- .../harvester/storage.k8s.io.storageclass.js | 4 +++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/pkg/harvester/edit/harvesterhci.io.virtualmachineimage.vue b/pkg/harvester/edit/harvesterhci.io.virtualmachineimage.vue index 5256ef6a..2d3aba44 100644 --- a/pkg/harvester/edit/harvesterhci.io.virtualmachineimage.vue +++ b/pkg/harvester/edit/harvesterhci.io.virtualmachineimage.vue @@ -24,6 +24,7 @@ const CLONE = 'clone'; const DOWNLOAD = 'download'; const UPLOAD = 'upload'; const rawORqcow2 = 'raw_qcow2'; +const LONGHORN = 'longhorn'; export default { name: 'EditImage', @@ -61,9 +62,7 @@ export default { storageClasses: this.$store.dispatch(`${ inStore }/findAll`, { type: STORAGE_CLASS }), }); - const defaultStorage = this.$store.getters[`${ inStore }/all`](STORAGE_CLASS).find((s) => s.isDefault); - - this['storageClassName'] = this.storageClassName || defaultStorage?.metadata?.name || 'longhorn'; + this['storageClassName'] = this.storageClassName || this.defaultStorageClassName(); this.images = this.$store.getters[`${ inStore }/all`](HCI.IMAGE); const { securityParameters } = this.value.spec; @@ -144,10 +143,23 @@ export default { ]; }, - storageClassOptions() { + encryptedStorageClasses() { const inStore = this.$store.getters['currentProduct'].inStore; const storages = this.$store.getters[`${ inStore }/all`](STORAGE_CLASS); + return storages.filter((s) => s.isEncrypted); + }, + + nonEncryptedStorageClasses() { + const inStore = this.$store.getters['currentProduct'].inStore; + const storages = this.$store.getters[`${ inStore }/all`](STORAGE_CLASS); + + return storages.filter((s) => !s.isEncrypted); + }, + + storageClassOptions() { + const storages = this.value.spec?.securityParameters?.cryptoOperation === ENCRYPT ? this.encryptedStorageClasses : this.nonEncryptedStorageClasses; + return storages .filter((s) => !s.parameters?.backingImage && s.provisioner !== LVM_DRIVER) // Lvm storage is not supported. .map((s) => { @@ -239,6 +251,13 @@ export default { this.$refs.file.value = null; } }, + 'value.spec.securityParameters.cryptoOperation'() { + if (this.value.spec?.securityParameters?.cryptoOperation === ENCRYPT) { + this.storageClassName = this.encryptedStorageClasses[0]?.name || LONGHORN; + } else { // URL / FILE / DECRYPT should use default storage class + this.storageClassName = this.defaultStorageClassName(); + } + } }, methods: { @@ -385,6 +404,14 @@ export default { return str.toLowerCase().includes(os.value.toLowerCase()) ? os.value : false; } }); + }, + + defaultStorageClassName() { + const inStore = this.$store.getters['currentProduct'].inStore; + const defaultStorage = this.$store.getters[`${ inStore }/all`](STORAGE_CLASS).find((s) => s.isDefault); + + // if default sc is encrypted, use longhorn as default + return defaultStorage.isEncrypted ? LONGHORN : defaultStorage?.metadata?.name; } }, }; diff --git a/pkg/harvester/models/harvester/storage.k8s.io.storageclass.js b/pkg/harvester/models/harvester/storage.k8s.io.storageclass.js index b075dde1..372f7a5f 100644 --- a/pkg/harvester/models/harvester/storage.k8s.io.storageclass.js +++ b/pkg/harvester/models/harvester/storage.k8s.io.storageclass.js @@ -58,6 +58,10 @@ export default class HciStorageClass extends StorageClass { return key ? this.$rootGetters['i18n/t'](key) : this.provisioner; } + get isEncrypted() { + return this.parameters?.encrypted === 'true'; + } + get isLonghornV2() { return this.provisioner === LONGHORN_DRIVER && this.longhornVersion === DATA_ENGINE_V2; } From 6e9350e63d1d90ec82cedca885a932380fe249d0 Mon Sep 17 00:00:00 2001 From: "andy.lee" Date: Fri, 17 Jan 2025 14:57:39 +0800 Subject: [PATCH 2/2] refactor: fallback to longhorn if not default storage class Signed-off-by: andy.lee --- pkg/harvester/edit/harvesterhci.io.virtualmachineimage.vue | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/harvester/edit/harvesterhci.io.virtualmachineimage.vue b/pkg/harvester/edit/harvesterhci.io.virtualmachineimage.vue index 2d3aba44..4eb8a9d7 100644 --- a/pkg/harvester/edit/harvesterhci.io.virtualmachineimage.vue +++ b/pkg/harvester/edit/harvesterhci.io.virtualmachineimage.vue @@ -61,7 +61,6 @@ export default { images: this.$store.dispatch(`${ inStore }/findAll`, { type: HCI.IMAGE }), storageClasses: this.$store.dispatch(`${ inStore }/findAll`, { type: STORAGE_CLASS }), }); - this['storageClassName'] = this.storageClassName || this.defaultStorageClassName(); this.images = this.$store.getters[`${ inStore }/all`](HCI.IMAGE); @@ -253,7 +252,7 @@ export default { }, 'value.spec.securityParameters.cryptoOperation'() { if (this.value.spec?.securityParameters?.cryptoOperation === ENCRYPT) { - this.storageClassName = this.encryptedStorageClasses[0]?.name || LONGHORN; + this.storageClassName = this.encryptedStorageClasses[0]?.name || ''; } else { // URL / FILE / DECRYPT should use default storage class this.storageClassName = this.defaultStorageClassName(); } @@ -410,6 +409,10 @@ export default { const inStore = this.$store.getters['currentProduct'].inStore; const defaultStorage = this.$store.getters[`${ inStore }/all`](STORAGE_CLASS).find((s) => s.isDefault); + if (!defaultStorage) { + return LONGHORN; + } + // if default sc is encrypted, use longhorn as default return defaultStorage.isEncrypted ? LONGHORN : defaultStorage?.metadata?.name; }