diff --git a/pkg/harvester/components/settings/__tests__/backup-target.test.js b/pkg/harvester/components/settings/__tests__/backup-target.test.js new file mode 100644 index 00000000..ca1f8d9c --- /dev/null +++ b/pkg/harvester/components/settings/__tests__/backup-target.test.js @@ -0,0 +1,102 @@ +import { shallowMount } from '@vue/test-utils'; +import BackupTarget from '../backup-target.vue'; + +function mountTarget(target) { + const setting = { value: JSON.stringify(target) }; + + return { + setting, + wrapper: shallowMount(BackupTarget, { + props: { value: setting, mode: 'edit' }, + global: { + mocks: { + t: (key) => key, + $store: { getters: { 'harvester-common/getFeatureEnabled': () => true } }, + }, + }, + }), + }; +} + +describe('component: backup-target', () => { + it('should keep virtualHostedStyle, which is part of the Restic and Kopia repository identity', () => { + const { setting } = mountTarget({ type: 's3', virtualHostedStyle: true }); + + expect(JSON.parse(setting.value).virtualHostedStyle).toBe(true); + }); + + it('should default virtualHostedStyle to false', () => { + const { setting } = mountTarget({ type: 's3' }); + + expect(JSON.parse(setting.value).virtualHostedStyle).toBe(false); + }); + + it.each([ + ['kopiaGCEnabled', { kopiaGCEnabled: true, thirdPartyBackupEnabled: true }], + ['thirdPartyBackupEnabled', { thirdPartyBackupEnabled: true }], + ])('should keep %s on S3 and drop it on NFS, where only S3 uses it', (field, fields) => { + const { wrapper, setting } = mountTarget({ type: 's3', ...fields }); + + expect(JSON.parse(setting.value)[field]).toBe(true); + + wrapper.vm.parseDefaultValue.type = 'nfs'; + wrapper.vm.update(); + + expect(JSON.parse(setting.value)[field]).toBeUndefined(); + }); + + it('should offer the Restic and Kopia switch on S3', () => { + const { wrapper } = mountTarget({ type: 's3' }); + + expect(wrapper.find('[data-testid="backup-target-third-party-backup"]').exists()).toBe(true); + }); + + it('should ask for the repository password only while Restic and Kopia are on', () => { + const off = mountTarget({ type: 's3' }).wrapper; + const on = mountTarget({ type: 's3', thirdPartyBackupEnabled: true }).wrapper; + + expect(off.find('[data-testid="backup-target-repository-password"]').exists()).toBe(false); + expect(on.find('[data-testid="backup-target-repository-password"]').exists()).toBe(true); + }); + + it('should offer Kopia garbage collection only while Restic and Kopia are on, the only time the backend runs it', () => { + const off = mountTarget({ type: 's3' }).wrapper; + const on = mountTarget({ type: 's3', thirdPartyBackupEnabled: true }).wrapper; + + expect(off.find('[data-testid="backup-target-kopia-gc"]').exists()).toBe(false); + expect(on.find('[data-testid="backup-target-kopia-gc"]').exists()).toBe(true); + }); + + it('should drop Kopia garbage collection when Restic and Kopia are turned off, so the setting matches what runs', () => { + const { wrapper, setting } = mountTarget({ + type: 's3', thirdPartyBackupEnabled: true, kopiaGCEnabled: true + }); + + expect(JSON.parse(setting.value).kopiaGCEnabled).toBe(true); + + wrapper.vm.parseDefaultValue.thirdPartyBackupEnabled = false; + wrapper.vm.update(); + + expect(JSON.parse(setting.value).kopiaGCEnabled).toBeUndefined(); + }); + + it.each([ + ['Restic and Kopia are turned off', (vm) => { + vm.parseDefaultValue.thirdPartyBackupEnabled = false; + }], + ['the target changes to NFS', (vm) => { + vm.parseDefaultValue.type = 'nfs'; + }], + ])('should drop the repository password when %s, which the backend rejects', (_, change) => { + const { wrapper, setting } = mountTarget({ + type: 's3', thirdPartyBackupEnabled: true, thirdPartyRepositoryPassword: 'secret' + }); + + expect(JSON.parse(setting.value).thirdPartyRepositoryPassword).toBe('secret'); + + change(wrapper.vm); + wrapper.vm.update(); + + expect(JSON.parse(setting.value).thirdPartyRepositoryPassword).toBeUndefined(); + }); +}); diff --git a/pkg/harvester/components/settings/__tests__/third-party-backup-config.test.js b/pkg/harvester/components/settings/__tests__/third-party-backup-config.test.js new file mode 100644 index 00000000..eb6b10dd --- /dev/null +++ b/pkg/harvester/components/settings/__tests__/third-party-backup-config.test.js @@ -0,0 +1,74 @@ +import { shallowMount } from '@vue/test-utils'; +import UnitInput from '@shell/components/form/UnitInput'; +import ThirdPartyBackupConfig from '../third-party-backup-config.vue'; + +const JOB = { + memoryRequest: '1Gi', memoryLimit: '4Gi', cacheSize: '2Gi', maxConcurrentJobs: 5, ephemeralRequest: '512Mi', ephemeralLimit: '3Gi' +}; +const DEFAULT = JSON.stringify({ restic: { ...JOB, noCache: false }, kopia: JOB }); + +function mountConfig(value) { + const setting = { value, default: DEFAULT }; + + return { + setting, + wrapper: shallowMount(ThirdPartyBackupConfig, { + props: { value: setting, registerBeforeHook: jest.fn() }, + global: { + mocks: { t: (key) => key }, + stubs: { RcSection: { template: '
' } }, + }, + }), + }; +} + +describe('component: third-party-backup-config', () => { + it('should fill omitted fields from the default', () => { + const { wrapper } = mountConfig(JSON.stringify({ kopia: { memoryLimit: '8Gi' } })); + + expect(wrapper.vm.config.kopia).toStrictEqual({ ...JOB, memoryLimit: '8Gi' }); + expect(wrapper.vm.config.restic.noCache).toBe(false); + }); + + it('should show the default values when the setting is empty', () => { + const { wrapper } = mountConfig(''); + + expect(wrapper.vm.config).toStrictEqual(JSON.parse(DEFAULT)); + }); + + it('should write the form to the setting before saving', async() => { + const { wrapper, setting } = mountConfig(''); + + wrapper.vm.config.kopia.maxConcurrentJobs = 0; + await wrapper.vm.willSave(); + + expect(JSON.parse(setting.value).kopia.maxConcurrentJobs).toBe(0); + }); + + it('should go back to the default', () => { + const { wrapper, setting } = mountConfig(JSON.stringify({ kopia: { memoryLimit: '8Gi' } })); + + wrapper.vm.useDefault(); + + expect(wrapper.vm.config).toStrictEqual(JSON.parse(DEFAULT)); + expect(JSON.parse(setting.value)).toStrictEqual(JSON.parse(DEFAULT)); + }); + + it('should take every size in MiB so a bare number is not saved as bytes', () => { + const { wrapper } = mountConfig(''); + const inputs = wrapper.findAllComponents(UnitInput); + + expect(inputs).toHaveLength(10); + inputs.forEach((input) => { + expect(input.props()).toMatchObject({ + increment: 1024, inputExponent: 2, outputModifier: true + }); + }); + }); + + it('should show the default values when the setting is not JSON', () => { + const { wrapper } = mountConfig('{not json'); + + expect(wrapper.vm.config).toStrictEqual(JSON.parse(DEFAULT)); + }); +}); diff --git a/pkg/harvester/components/settings/backup-target.vue b/pkg/harvester/components/settings/backup-target.vue index 9a4a4a56..7012d8a4 100644 --- a/pkg/harvester/components/settings/backup-target.vue +++ b/pkg/harvester/components/settings/backup-target.vue @@ -3,6 +3,7 @@ import Tip from '@shell/components/Tip'; import Password from '@shell/components/form/Password'; import MessageLink from '@shell/components/MessageLink'; import CreateEditView from '@shell/mixins/create-edit-view'; +import { Checkbox } from '@components/Form/Checkbox'; import { LabeledInput } from '@components/Form/LabeledInput'; import LabeledSelect from '@shell/components/form/LabeledSelect'; import { HCI_SETTING } from '../../config/settings'; @@ -14,7 +15,7 @@ export default { name: 'HarvesterEditBackupTarget', components: { - LabeledInput, LabeledSelect, Tip, Password, MessageLink, UnitInput + Checkbox, LabeledInput, LabeledSelect, Tip, Password, MessageLink, UnitInput }, mixins: [CreateEditView], @@ -36,7 +37,7 @@ export default { } // set default virtualHostedStyle to false - parseDefaultValue.virtualHostedStyle = false; + parseDefaultValue.virtualHostedStyle = !!parseDefaultValue.virtualHostedStyle; return { parseDefaultValue, @@ -69,6 +70,10 @@ export default { return this.$store.getters['harvester-common/getFeatureEnabled']('refreshIntervalInSecond'); }, + thirdPartyBackupFeatureEnabled() { + return this.$store.getters['harvester-common/getFeatureEnabled']('thirdPartyVMBackup'); + }, + isS3() { return this.parseDefaultValue.type === DEFAULT_TYPE; }, @@ -114,9 +119,17 @@ export default { delete this.parseDefaultValue.bucketName; delete this.parseDefaultValue.bucketRegion; delete this.parseDefaultValue.virtualHostedStyle; + delete this.parseDefaultValue.kopiaGCEnabled; + delete this.parseDefaultValue.thirdPartyBackupEnabled; delete this.parseDefaultValue.cert; } + // While Restic and Kopia are off, the backend rejects a repository password and runs no Kopia garbage collection + if (!this.parseDefaultValue.thirdPartyBackupEnabled) { + delete this.parseDefaultValue.thirdPartyRepositoryPassword; + delete this.parseDefaultValue.kopiaGCEnabled; + } + if (this.isS3 && !this.parseDefaultValue.virtualHostedStyle) { this.parseDefaultValue.virtualHostedStyle = false; } @@ -222,6 +235,45 @@ export default { @update:value="update" /> + + +
+import { Checkbox } from '@components/Form/Checkbox'; +import { LabeledInput } from '@components/Form/LabeledInput'; +import { RcSection } from '@components/RcSection'; +import UnitInput from '@shell/components/form/UnitInput'; +import { BACKUP_TYPE } from '../../config/types'; + +const ENGINES = [BACKUP_TYPE.RESTIC, BACKUP_TYPE.KOPIA]; +const QUANTITY_FIELDS = ['memoryRequest', 'memoryLimit', 'ephemeralRequest', 'ephemeralLimit', 'cacheSize']; + +// The webhook only stores JSON objects, so a value that doesn't parse falls back to the default like the other settings forms +function parseJSON(string) { + try { + return JSON.parse(string || '{}'); + } catch (e) { + return {}; + } +} + +// The backend fills omitted fields from the default, so show those values instead of empty inputs +function parseConfig(value, defaultValue) { + const defaults = parseJSON(defaultValue); + const current = parseJSON(value); + + return Object.fromEntries(ENGINES.map((engine) => [engine, { ...defaults[engine], ...current[engine] }])); +} + +export default { + name: 'ThirdPartyBackupConfig', + + components: { + Checkbox, + LabeledInput, + RcSection, + UnitInput, + }, + + props: { + registerBeforeHook: { + type: Function, + required: true + }, + mode: { + type: String, + default: 'edit' + }, + value: { + type: Object, + default: () => ({ + value: '', + default: '{}' + }) + }, + }, + + data() { + return { + ENGINES, + QUANTITY_FIELDS, + RESTIC: BACKUP_TYPE.RESTIC, + config: parseConfig(this.value?.value, this.value?.default), + }; + }, + + created() { + this.registerBeforeHook?.(this.willSave, 'willSave'); + }, + + methods: { + updateValue() { + if (this.value) { + this.value.value = JSON.stringify(this.config); + } + }, + + useDefault() { + this.config = parseConfig('', this.value?.default); + this.updateValue(); + }, + + willSave() { + this.updateValue(); + + return Promise.resolve(); + }, + }, +}; + + + + + diff --git a/pkg/harvester/config/feature-flags.js b/pkg/harvester/config/feature-flags.js index 240ccfda..80d91d9f 100644 --- a/pkg/harvester/config/feature-flags.js +++ b/pkg/harvester/config/feature-flags.js @@ -90,6 +90,7 @@ const FEATURE_FLAGS = { 'shutdownVMOnMigrationFailure', 'vmListPagination', 'kubeovnBgp', + 'thirdPartyVMBackup', ], }; diff --git a/pkg/harvester/config/labels-annotations.js b/pkg/harvester/config/labels-annotations.js index 9fa784c2..3f4436eb 100644 --- a/pkg/harvester/config/labels-annotations.js +++ b/pkg/harvester/config/labels-annotations.js @@ -91,6 +91,7 @@ export const HCI = { STATIC_IP: 'static-ip.harvesterhci.io', NODE_UPGRADE_PAUSE_MAP: 'harvesterhci.io/node-upgrade-pause-map', CDI_POPULATOR_KIND: 'cdi.kubevirt.io/storage.populator.kind', + CDI_CREATED_FOR_DATA_VOLUME: 'cdi.kubevirt.io/createdForDataVolume', CNI_NETWORKS: 'k8s.v1.cni.cncf.io/networks', KUBEOVN_BGP: 'ovn.kubernetes.io/bgp', WINDOWS_SYSPREP: 'harvesterhci.io/windows-sysprep', diff --git a/pkg/harvester/config/settings.ts b/pkg/harvester/config/settings.ts index 6dd981d0..4300cd96 100644 --- a/pkg/harvester/config/settings.ts +++ b/pkg/harvester/config/settings.ts @@ -30,6 +30,7 @@ export const HCI_SETTING = { RELEASE_DOWNLOAD_URL: 'release-download-url', CCM_CSI_VERSION: 'harvester-csi-ccm-versions', CSI_DRIVER_CONFIG: 'csi-driver-config', + THIRD_PARTY_BACKUP_CONFIG: 'third-party-backup-config', CSI_ONLINE_EXPAND_VALIDATION: 'csi-online-expand-validation', VM_TERMINATION_PERIOD: 'default-vm-termination-grace-period-seconds', NTP_SERVERS: 'ntp-servers', @@ -144,6 +145,9 @@ export const HCI_ALLOWED_SETTINGS = { [HCI_SETTING.KUBEVIRT_MIGRATION]: { kind: 'json', from: 'import', canReset: true, featureFlag: 'kubevirtMigration', }, + [HCI_SETTING.THIRD_PARTY_BACKUP_CONFIG]: { + kind: 'json', from: 'import', canReset: true, featureFlag: 'thirdPartyVMBackup', + }, [HCI_SETTING.INSTANCE_MANAGER_RESOURCES]: { kind: 'json', from: 'import', featureFlag: 'instanceManagerResourcesSetting' }, diff --git a/pkg/harvester/config/types.js b/pkg/harvester/config/types.js index 62799a10..717f9e88 100644 --- a/pkg/harvester/config/types.js +++ b/pkg/harvester/config/types.js @@ -1,6 +1,8 @@ export const BACKUP_TYPE = { BACKUP: 'backup', - SNAPSHOT: 'snapshot' + SNAPSHOT: 'snapshot', + RESTIC: 'restic', + KOPIA: 'kopia' }; export const NETWORK_TYPE = { diff --git a/pkg/harvester/dialog/HarvesterBackupModal.vue b/pkg/harvester/dialog/HarvesterBackupModal.vue index 40bc208e..fa2a47a9 100644 --- a/pkg/harvester/dialog/HarvesterBackupModal.vue +++ b/pkg/harvester/dialog/HarvesterBackupModal.vue @@ -3,10 +3,13 @@ import { exceptionToErrorsArray } from '@shell/utils/error'; import { mapGetters } from 'vuex'; import { Card } from '@components/Card'; import { Banner } from '@components/Banner'; -import AsyncButton from '@shell/components/AsyncButton'; import { LabeledInput } from '@components/Form/LabeledInput'; +import { RcButton } from '@components/RcButton'; +import { RcHeading } from '@components/RcHeading'; import LabeledSelect from '@shell/components/form/LabeledSelect'; import { DEFAULT_FS_FREEZE_DEADLINE, getFsFreezeDeadlineOptions } from '../utils/backup'; +import { BACKUP_TYPE } from '../config/types'; +import { HCI } from '../types'; export default { name: 'HarvesterBackupModal', @@ -14,11 +17,12 @@ export default { emits: ['close'], components: { - AsyncButton, Card, LabeledInput, LabeledSelect, - Banner + Banner, + RcButton, + RcHeading }, props: { @@ -28,11 +32,23 @@ export default { } }, + async fetch() { + if (!this.thirdPartyBackupFeatureEnabled) { + return; + } + + await this.actionResource.fetchBackupTypeRequirements(); + + this.backupType = this.backupTypeOptions.find((option) => !option.disabled)?.value; + }, + data() { return { backUpName: '', + backupType: BACKUP_TYPE.BACKUP, errors: [], - fsFreezeDeadline: DEFAULT_FS_FREEZE_DEADLINE + fsFreezeDeadline: DEFAULT_FS_FREEZE_DEADLINE, + saving: false }; }, @@ -49,6 +65,31 @@ export default { fsFreezeDeadlineOptions() { return getFsFreezeDeadlineOptions(this.t); + }, + + thirdPartyBackupFeatureEnabled() { + return !!this.actionResource?.thirdPartyBackupFeatureEnabled; + }, + + backupTypeOptions() { + return this.thirdPartyBackupFeatureEnabled ? this.actionResource.backupTypeOptions : []; + }, + + // Shown outside the select too: its disabled options can't be reached by keyboard + unavailableBackupTypeOptions() { + return this.backupTypeOptions.filter((option) => option.disabled); + }, + + isLoading() { + return this.thirdPartyBackupFeatureEnabled && this.$fetchState.pending; + }, + + noBackupTypeAvailable() { + return this.thirdPartyBackupFeatureEnabled && !this.isLoading && !this.backupType; + }, + + canCreate() { + return !!this.backUpName && !this.isLoading && !this.noBackupTypeAvailable && !this.saving; } }, @@ -60,42 +101,69 @@ export default { this.$emit('close'); }, - async save(buttonCb) { - if (this.actionResource) { - try { - const res = await this.actionResource.doAction( - 'backup', - { name: this.backUpName, fsFreezeDeadline: this.fsFreezeDeadline }, - {}, - false - ); + backupCreated() { + this.$store.dispatch( + 'growl/success', + { + title: this.t('generic.notification.title.succeed'), + message: this.t('harvester.modal.backup.success', { backUpName: this.backUpName }) + }, + { root: true } + ); - if (res._status === 200 || res._status === 204) { - this.$store.dispatch( - 'growl/success', - { - title: this.t('generic.notification.title.succeed'), - message: this.t('harvester.modal.backup.success', { backUpName: this.backUpName }) - }, - { root: true } - ); + this.close(); + }, - this.close(); + // No ownerReferences, like the backup action: deleting the VM must leave its backups and their remote data + async createDataMoverBackup() { + const backup = await this.$store.dispatch('harvester/create', { + metadata: { + name: this.backUpName, + namespace: this.actionResource.metadata.namespace, + }, + spec: { + fsFreezeDeadline: this.fsFreezeDeadline, + source: { + apiGroup: 'kubevirt.io', + kind: 'VirtualMachine', + name: this.actionResource.metadata.name + }, + type: this.backupType + }, + type: HCI.BACKUP + }); - buttonCb(true); - } else { - const error = [res?.data] || exceptionToErrorsArray(res); + await backup.save(); + }, - this['errors'] = error; - buttonCb(false); - } - } catch (err) { - const error = err?.data || err; - const message = exceptionToErrorsArray(error); + async save() { + this.saving = true; + this.errors = []; - this['errors'] = message; - buttonCb(false); + try { + if (this.backupType !== BACKUP_TYPE.BACKUP) { + await this.createDataMoverBackup(); + this.backupCreated(); + + return; } + + const res = await this.actionResource.doAction( + 'backup', + { name: this.backUpName, fsFreezeDeadline: this.fsFreezeDeadline }, + {}, + false + ); + + if (res._status === 200 || res._status === 204) { + this.backupCreated(); + } else { + this.errors = [res?.data] || exceptionToErrorsArray(res); + } + } catch (err) { + this.errors = exceptionToErrorsArray(err?.data || err); + } finally { + this.saving = false; } } } @@ -105,27 +173,73 @@ export default {