mirror of
https://github.com/harvester/harvester-ui-extension.git
synced 2026-11-25 01:50:53 +08:00
fix: retain existing StorageClass selection and add fallback logic (#1271)
Previously, the Image and Volume create/edit forms would lose the user's StorageClass selection or silently pick an unavailable class. Changes: - Retain the existing StorageClass if it is still available - Fall back in order: cluster default → longhorn → first selectable - Block creation and show an informational message when no selectable StorageClass exists Signed-off-by: pohanhuang <pohan.huang@suse.com>
This commit is contained in:
1 parent
0a49829658
commit
d80249773d
5 files changed
+375
-25
No files matched your search
@@ -0,0 +1,148 @@
|
||||
import EditImage from '../harvesterhci.io.virtualmachineimage.vue';
|
||||
|
||||
function storageClass(name, options = {}) {
|
||||
return {
|
||||
name,
|
||||
metadata: { name },
|
||||
...options,
|
||||
};
|
||||
}
|
||||
|
||||
function defaultStorageClassName(storageClasses) {
|
||||
return EditImage.methods.defaultStorageClassName.call({ selectableStorageClasses: selectableStorageClasses(storageClasses) });
|
||||
}
|
||||
|
||||
function selectableStorageClasses(storageClasses) {
|
||||
const availableStorageClasses = EditImage.computed.availableStorageClasses.call({
|
||||
value: {
|
||||
spec: { thirdPartyStorageFeatureEnabled: true },
|
||||
thirdPartyStorageFeatureEnabled: true,
|
||||
},
|
||||
encryptedStorageClasses: [],
|
||||
nonEncryptedStorageClasses: storageClasses,
|
||||
});
|
||||
|
||||
return EditImage.computed.selectableStorageClasses.call({ availableStorageClasses });
|
||||
}
|
||||
|
||||
async function fetchStorageClass(storageClasses, storageClassName, isCreate = true) {
|
||||
const context = {
|
||||
isCreate,
|
||||
storageClassName,
|
||||
selectableStorageClasses: storageClasses,
|
||||
images: [],
|
||||
storages: [],
|
||||
value: { spec: {} },
|
||||
$store: {
|
||||
getters: {
|
||||
currentProduct: { inStore: 'harvester' },
|
||||
'harvester/all': () => storageClasses,
|
||||
},
|
||||
dispatch: () => Promise.resolve([]),
|
||||
},
|
||||
defaultStorageClassName() {
|
||||
return EditImage.methods.defaultStorageClassName.call(this);
|
||||
},
|
||||
};
|
||||
|
||||
await EditImage.fetch.call(context);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
describe('component: EditImage', () => {
|
||||
it('leaves the storage class empty when none are selectable', () => {
|
||||
expect(defaultStorageClassName([])).toBe('');
|
||||
});
|
||||
|
||||
it('selects the cluster default storage class before longhorn', () => {
|
||||
expect(defaultStorageClassName([
|
||||
storageClass('fast', { isDefault: true }),
|
||||
storageClass('longhorn'),
|
||||
])).toBe('fast');
|
||||
});
|
||||
|
||||
it('selects longhorn when the cluster default storage class is unavailable', () => {
|
||||
expect(defaultStorageClassName([
|
||||
storageClass('slow'),
|
||||
storageClass('longhorn'),
|
||||
])).toBe('longhorn');
|
||||
});
|
||||
|
||||
it('selects the first selectable storage class when no preferred class is available', () => {
|
||||
expect(defaultStorageClassName([
|
||||
storageClass('backing-image', { parameters: { backingImage: 'image' } }),
|
||||
storageClass('fast'),
|
||||
storageClass('slow'),
|
||||
])).toBe('fast');
|
||||
});
|
||||
|
||||
it('does not include backing-image or internal classes among selectable classes', () => {
|
||||
expect(selectableStorageClasses([
|
||||
storageClass('longhorn', { parameters: { backingImage: 'image' } }),
|
||||
storageClass('longhorn-static'),
|
||||
storageClass('fast'),
|
||||
]).map((storageClass) => storageClass.name)).toStrictEqual(['fast']);
|
||||
});
|
||||
|
||||
it('falls back when the annotated storage class is unavailable during creation', async() => {
|
||||
const context = await fetchStorageClass([storageClass('fast')], 'missing');
|
||||
|
||||
expect(context.storageClassName).toBe('fast');
|
||||
expect(context.storageClassesLoaded).toBe(true);
|
||||
});
|
||||
|
||||
it('preserves a selectable annotated storage class during creation', async() => {
|
||||
const context = await fetchStorageClass([
|
||||
storageClass('fast', { isDefault: true }),
|
||||
storageClass('selected'),
|
||||
], 'selected');
|
||||
|
||||
expect(context.storageClassName).toBe('selected');
|
||||
});
|
||||
|
||||
it('preserves an unavailable annotated storage class when editing', async() => {
|
||||
const context = await fetchStorageClass([storageClass('fast')], 'missing', false);
|
||||
|
||||
expect(context.storageClassName).toBe('missing');
|
||||
});
|
||||
|
||||
it('uses encryption-specific guidance when no encrypted storage class is selectable', () => {
|
||||
const storageClassUnavailableMessage = EditImage.computed.storageClassUnavailableMessage;
|
||||
|
||||
expect(storageClassUnavailableMessage.call({ value: { spec: { securityParameters: { cryptoOperation: 'encrypt' } } } })).toBe('harvester.image.encryptedStorageClassUnavailable');
|
||||
expect(storageClassUnavailableMessage.call({ value: { spec: {} } })).toBe('harvester.image.storageClassUnavailable');
|
||||
});
|
||||
|
||||
it('prevents creation when no storage class is selectable', async() => {
|
||||
const buttonCb = jest.fn();
|
||||
const context = {
|
||||
showStorageClassUnavailableBanner: true,
|
||||
storageClassUnavailableMessage: 'harvester.image.storageClassUnavailable',
|
||||
t: (key) => key,
|
||||
value: { spec: { displayName: '' } },
|
||||
};
|
||||
|
||||
await EditImage.methods.saveImage.call(context, buttonCb);
|
||||
|
||||
expect(buttonCb).toHaveBeenCalledWith(false);
|
||||
expect(context.errors).toStrictEqual(['harvester.image.storageClassUnavailable']);
|
||||
});
|
||||
|
||||
it('waits for storage classes before showing the unavailable banner', () => {
|
||||
const showBanner = EditImage.computed.showStorageClassUnavailableBanner;
|
||||
|
||||
expect(showBanner.call({
|
||||
mode: 'create', isCreate: true, storageClassesLoaded: false, selectableStorageClasses: []
|
||||
})).toBe(false);
|
||||
expect(showBanner.call({
|
||||
mode: 'create', isCreate: true, storageClassesLoaded: true, selectableStorageClasses: []
|
||||
})).toBe(true);
|
||||
expect(showBanner.call({
|
||||
mode: 'view', isCreate: false, storageClassesLoaded: true, selectableStorageClasses: []
|
||||
})).toBe(false);
|
||||
expect(showBanner.call({
|
||||
mode: 'edit', isCreate: false, storageClassesLoaded: true, selectableStorageClasses: []
|
||||
})).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,135 @@
|
||||
import HarvesterVolume from '../harvesterhci.io.volume.vue';
|
||||
|
||||
async function fetchStorageClass(storageClasses, storageClassName, isCreate = true) {
|
||||
const context = {
|
||||
isCreate,
|
||||
value: { spec: {} },
|
||||
snapshots: [],
|
||||
images: [],
|
||||
$store: {
|
||||
getters: {
|
||||
currentProduct: { inStore: 'harvester' },
|
||||
'harvester/schemaFor': () => false,
|
||||
'harvester/all': () => storageClasses,
|
||||
},
|
||||
dispatch: () => Promise.resolve([]),
|
||||
},
|
||||
};
|
||||
|
||||
if (storageClassName) {
|
||||
context.value.spec.storageClassName = storageClassName;
|
||||
}
|
||||
|
||||
await HarvesterVolume.fetch.call(context);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
function storageClass(name, options = {}) {
|
||||
return {
|
||||
name,
|
||||
metadata: { name },
|
||||
...options,
|
||||
};
|
||||
}
|
||||
|
||||
describe('component: HarvesterVolume', () => {
|
||||
it('leaves the storage class unselected when the cluster has no selectable class', async() => {
|
||||
const context = await fetchStorageClass([]);
|
||||
|
||||
expect(context.value.spec.storageClassName).toBeUndefined();
|
||||
expect(context.storageClassesLoaded).toBe(true);
|
||||
});
|
||||
|
||||
it('selects the cluster default storage class before longhorn', async() => {
|
||||
const context = await fetchStorageClass([
|
||||
storageClass('fast', { isDefault: true }),
|
||||
storageClass('longhorn'),
|
||||
]);
|
||||
|
||||
expect(context.value.spec.storageClassName).toBe('fast');
|
||||
});
|
||||
|
||||
it('selects longhorn when the cluster default storage class is unavailable', async() => {
|
||||
const context = await fetchStorageClass([
|
||||
storageClass('slow'),
|
||||
storageClass('longhorn'),
|
||||
]);
|
||||
|
||||
expect(context.value.spec.storageClassName).toBe('longhorn');
|
||||
});
|
||||
|
||||
it('selects the first selectable storage class when no preferred class is available', async() => {
|
||||
const context = await fetchStorageClass([
|
||||
storageClass('backing-image', { parameters: { backingImage: 'image' } }),
|
||||
storageClass('fast'),
|
||||
storageClass('slow'),
|
||||
]);
|
||||
|
||||
expect(context.value.spec.storageClassName).toBe('fast');
|
||||
});
|
||||
|
||||
it('does not select backing-image or internal classes', async() => {
|
||||
const context = await fetchStorageClass([
|
||||
storageClass('longhorn', { parameters: { backingImage: 'image' } }),
|
||||
storageClass('longhorn-static'),
|
||||
storageClass('fast'),
|
||||
]);
|
||||
|
||||
expect(context.value.spec.storageClassName).toBe('fast');
|
||||
});
|
||||
|
||||
it('preserves an existing selectable storage class', async() => {
|
||||
const context = await fetchStorageClass([
|
||||
storageClass('existing'),
|
||||
storageClass('fast', { isDefault: true }),
|
||||
], 'existing');
|
||||
|
||||
expect(context.value.spec.storageClassName).toBe('existing');
|
||||
});
|
||||
|
||||
it('falls back when the existing storage class is unavailable during creation', async() => {
|
||||
const context = await fetchStorageClass([storageClass('fast')], 'missing');
|
||||
|
||||
expect(context.value.spec.storageClassName).toBe('fast');
|
||||
});
|
||||
|
||||
it('preserves an unavailable storage class when editing', async() => {
|
||||
const context = await fetchStorageClass([storageClass('fast')], 'missing', false);
|
||||
|
||||
expect(context.value.spec.storageClassName).toBe('missing');
|
||||
});
|
||||
|
||||
it('prevents blank volume creation when no storage class is selectable', async() => {
|
||||
const buttonDone = jest.fn();
|
||||
const context = {
|
||||
showStorageClassUnavailableBanner: true,
|
||||
t: (key) => key,
|
||||
};
|
||||
|
||||
await HarvesterVolume.methods.save.call(context, buttonDone);
|
||||
|
||||
expect(buttonDone).toHaveBeenCalledWith(false);
|
||||
expect(context.errors).toStrictEqual(['harvester.volume.storageClassUnavailable']);
|
||||
});
|
||||
|
||||
it('waits for storage classes before showing the unavailable banner', () => {
|
||||
const showBanner = HarvesterVolume.computed.showStorageClassUnavailableBanner;
|
||||
|
||||
expect(showBanner.call({
|
||||
mode: 'create', isCreate: true, storageClassesLoaded: false, selectableStorageClasses: [], isBlank: true
|
||||
})).toBe(false);
|
||||
expect(showBanner.call({
|
||||
mode: 'create', isCreate: true, storageClassesLoaded: true, selectableStorageClasses: [], isBlank: true
|
||||
})).toBe(true);
|
||||
expect(showBanner.call({
|
||||
mode: 'create', isCreate: true, storageClassesLoaded: true, selectableStorageClasses: [], isBlank: false
|
||||
})).toBe(false);
|
||||
expect(showBanner.call({
|
||||
mode: 'view', isCreate: false, storageClassesLoaded: true, selectableStorageClasses: [], isBlank: true
|
||||
})).toBe(false);
|
||||
expect(showBanner.call({
|
||||
mode: 'edit', isCreate: false, storageClassesLoaded: true, selectableStorageClasses: [], isBlank: true
|
||||
})).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -6,6 +6,7 @@ import { LabeledInput } from '@components/Form/LabeledInput';
|
||||
import KeyValue from '@shell/components/form/KeyValue';
|
||||
import NameNsDescription from '@shell/components/form/NameNsDescription';
|
||||
import { RadioGroup } from '@components/Form/Radio';
|
||||
import { Banner } from '@components/Banner';
|
||||
import Select from '@shell/components/form/Select';
|
||||
import LabeledSelect from '@shell/components/form/LabeledSelect';
|
||||
import CreateEditView from '@shell/mixins/create-edit-view';
|
||||
@@ -43,6 +44,7 @@ export default {
|
||||
LabeledInput,
|
||||
NameNsDescription,
|
||||
RadioGroup,
|
||||
Banner,
|
||||
LabeledSelect,
|
||||
},
|
||||
|
||||
@@ -63,11 +65,14 @@ export default {
|
||||
storageClasses: this.$store.dispatch(`${ inStore }/findAll`, { type: STORAGE_CLASS }),
|
||||
});
|
||||
|
||||
this['storageClassName'] = this.storageClassName || this.defaultStorageClassName();
|
||||
this.images = this.$store.getters[`${ inStore }/all`](HCI.IMAGE);
|
||||
|
||||
this.storages = this.$store.getters[`${ inStore }/all`](STORAGE_CLASS);
|
||||
|
||||
const storageClassName = this.storageClassName;
|
||||
|
||||
this['storageClassName'] = this.isCreate && !this.selectableStorageClasses.some((storageClass) => storageClass.name === storageClassName) ? this.defaultStorageClassName() : storageClassName || this.defaultStorageClassName();
|
||||
this.storageClassesLoaded = true;
|
||||
|
||||
const { securityParameters } = this.value.spec;
|
||||
|
||||
// edit and view mode should show the source image
|
||||
@@ -102,15 +107,16 @@ export default {
|
||||
}
|
||||
|
||||
return {
|
||||
selectedImage: null,
|
||||
storageClasses: [],
|
||||
images: [],
|
||||
url: this.value.spec.url,
|
||||
files: [],
|
||||
resource: '',
|
||||
headers: {},
|
||||
fileUrl: '',
|
||||
file: '',
|
||||
selectedImage: null,
|
||||
storageClasses: [],
|
||||
storageClassesLoaded: false,
|
||||
images: [],
|
||||
url: this.value.spec.url,
|
||||
files: [],
|
||||
resource: '',
|
||||
headers: {},
|
||||
fileUrl: '',
|
||||
file: '',
|
||||
HCI_ANNOTATIONS,
|
||||
};
|
||||
},
|
||||
@@ -162,12 +168,27 @@ export default {
|
||||
return storages.filter((s) => !s.isEncrypted);
|
||||
},
|
||||
|
||||
storageClassOptions() {
|
||||
availableStorageClasses() {
|
||||
const storages = this.value.spec?.securityParameters?.cryptoOperation === ENCRYPT ? this.encryptedStorageClasses : this.nonEncryptedStorageClasses;
|
||||
const filteredStorages = this.value.thirdPartyStorageFeatureEnabled ? storages.filter((s) => !s.parameters?.backingImage) : storages
|
||||
.filter((s) => !s.parameters?.backingImage && s.provisioner !== LVM_DRIVER) ;
|
||||
|
||||
return filteredStorages
|
||||
return this.value.thirdPartyStorageFeatureEnabled ? storages.filter((s) => !s.parameters?.backingImage) : storages
|
||||
.filter((s) => !s.parameters?.backingImage && s.provisioner !== LVM_DRIVER);
|
||||
},
|
||||
|
||||
selectableStorageClasses() {
|
||||
return this.availableStorageClasses.filter((storageClass) => !isInternalStorageClass(storageClass.name));
|
||||
},
|
||||
|
||||
showStorageClassUnavailableBanner() {
|
||||
return this.isCreate && this.storageClassesLoaded && !this.selectableStorageClasses.length;
|
||||
},
|
||||
|
||||
storageClassUnavailableMessage() {
|
||||
return this.value.spec?.securityParameters?.cryptoOperation === ENCRYPT ? 'harvester.image.encryptedStorageClassUnavailable' : 'harvester.image.storageClassUnavailable';
|
||||
},
|
||||
|
||||
storageClassOptions() {
|
||||
return this.availableStorageClasses
|
||||
.map((s) => {
|
||||
let label = s.isDefault ? `${ s.name } (${ this.t('generic.default') })` : s.name;
|
||||
let disabled = false;
|
||||
@@ -285,6 +306,13 @@ export default {
|
||||
|
||||
methods: {
|
||||
async saveImage(buttonCb) {
|
||||
if (this.showStorageClassUnavailableBanner) {
|
||||
this.errors = [this.t(this.storageClassUnavailableMessage)];
|
||||
buttonCb(false);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.value.spec.displayName = (this.value.spec.displayName || '').trim();
|
||||
|
||||
if (this.isEdit) return await this.handleEditImage(buttonCb);
|
||||
@@ -431,15 +459,10 @@ export default {
|
||||
},
|
||||
|
||||
defaultStorageClassName() {
|
||||
const inStore = this.$store.getters['currentProduct'].inStore;
|
||||
const defaultStorage = this.$store.getters[`${ inStore }/all`](STORAGE_CLASS).find((s) => s.isDefault);
|
||||
const storageClass = this.selectableStorageClasses.find((storageClass) => storageClass.isDefault) ||
|
||||
this.selectableStorageClasses.find((storageClass) => storageClass.name === LONGHORN) || this.selectableStorageClasses[0];
|
||||
|
||||
if (!defaultStorage) {
|
||||
return LONGHORN;
|
||||
}
|
||||
|
||||
// if default sc is encrypted, use longhorn as default
|
||||
return defaultStorage.isEncrypted ? LONGHORN : defaultStorage?.metadata?.name;
|
||||
return storageClass?.metadata?.name || '';
|
||||
}
|
||||
},
|
||||
};
|
||||
@@ -586,6 +609,13 @@ export default {
|
||||
:disabled="isEdit"
|
||||
class="mb-20"
|
||||
/>
|
||||
|
||||
<Banner
|
||||
v-if="showStorageClassUnavailableBanner"
|
||||
color="info"
|
||||
>
|
||||
<span>{{ t(storageClassUnavailableMessage) }}</span>
|
||||
</Banner>
|
||||
</div>
|
||||
</div>
|
||||
</Tab>
|
||||
|
||||
@@ -32,6 +32,10 @@ import { isInternalStorageClass } from '../utils/storage-class';
|
||||
|
||||
const { READ_WRITE_MANY, READ_WRITE_ONCE, READ_ONLY_MANY } = ACCESS_MODE;
|
||||
|
||||
function getSelectableStorageClasses(storageClasses = []) {
|
||||
return storageClasses.filter((storageClass) => !storageClass.parameters?.backingImage && !isInternalStorageClass(storageClass.name));
|
||||
}
|
||||
|
||||
export default {
|
||||
name: 'HarvesterVolume',
|
||||
|
||||
@@ -78,9 +82,15 @@ export default {
|
||||
this.snapshots = hash.snapshots;
|
||||
this.images = hash.images;
|
||||
|
||||
const defaultStorage = this.$store.getters[`harvester/all`](STORAGE_CLASS).find( (O) => O.isDefault);
|
||||
const storageClasses = getSelectableStorageClasses(this.$store.getters[`harvester/all`](STORAGE_CLASS));
|
||||
const selectedStorage = storageClasses.find((storageClass) => storageClass.isDefault) ||
|
||||
storageClasses.find((storageClass) => storageClass.name === 'longhorn') || storageClasses[0];
|
||||
|
||||
this.value.spec['storageClassName'] = this.value?.spec?.storageClassName || defaultStorage?.metadata?.name || 'longhorn';
|
||||
const existingStorageClassName = this.value?.spec?.storageClassName;
|
||||
const storageClassName = this.isCreate && !storageClasses.some((storageClass) => storageClass.name === existingStorageClassName) ? undefined : existingStorageClassName;
|
||||
|
||||
this.value.spec['storageClassName'] = storageClassName || selectedStorage?.metadata?.name;
|
||||
this.storageClassesLoaded = true;
|
||||
},
|
||||
|
||||
data() {
|
||||
@@ -102,6 +112,7 @@ export default {
|
||||
createWithDataVolume: false,
|
||||
snapshots: [],
|
||||
images: [],
|
||||
storageClassesLoaded: false,
|
||||
GIBIBYTE,
|
||||
// Default KubeVirt high-performance disk profile, stored as annotations and
|
||||
// applied when this volume is later attached to a VM as a disk.
|
||||
@@ -247,6 +258,14 @@ export default {
|
||||
return true;
|
||||
},
|
||||
|
||||
selectableStorageClasses() {
|
||||
return getSelectableStorageClasses(this.storageClasses);
|
||||
},
|
||||
|
||||
showStorageClassUnavailableBanner() {
|
||||
return this.isCreate && this.isBlank && this.storageClassesLoaded && !this.selectableStorageClasses.length;
|
||||
},
|
||||
|
||||
storageClassOptions() {
|
||||
return this.storageClasses.filter((s) => !s.parameters?.backingImage).map((s) => {
|
||||
let label = s.isDefault ? `${ s.name } (${ this.t('generic.default') })` : s.name;
|
||||
@@ -418,6 +437,13 @@ export default {
|
||||
},
|
||||
|
||||
async save(buttonDone) {
|
||||
if (this.showStorageClassUnavailableBanner) {
|
||||
this.errors = [this.t('harvester.volume.storageClassUnavailable')];
|
||||
buttonDone(false);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.isCreate && this.isBlank && this.createWithDataVolume) {
|
||||
try {
|
||||
this.update();
|
||||
@@ -580,6 +606,14 @@ export default {
|
||||
@update:value="update"
|
||||
/>
|
||||
|
||||
<Banner
|
||||
v-if="showStorageClassUnavailableBanner"
|
||||
color="info"
|
||||
class="mb-20"
|
||||
>
|
||||
<span>{{ t('harvester.volume.storageClassUnavailable') }}</span>
|
||||
</Banner>
|
||||
|
||||
<UnitInput
|
||||
v-model:value="storage"
|
||||
:label="t('harvester.volume.size')"
|
||||
|
||||
@@ -1194,6 +1194,7 @@ harvester:
|
||||
size: Size
|
||||
volumeMode: Volume Mode
|
||||
accessModes: Access Modes
|
||||
storageClassUnavailable: No selectable StorageClasses are available. Create a StorageClass before creating a volume.
|
||||
createWithDataVolume: Create with DataVolume
|
||||
createWithDataVolumeTooltip: Create Volume with Kubevirt/Containerized Data Importer way. It can fill accessMode/volumeMode automatically.
|
||||
showAdvanced: Show Advanced Options
|
||||
@@ -1236,6 +1237,8 @@ harvester:
|
||||
fileName: File Name
|
||||
uploadFile: Upload File
|
||||
source: Source Type
|
||||
storageClassUnavailable: No selectable StorageClasses are available. Create a StorageClass before creating an image.
|
||||
encryptedStorageClassUnavailable: No selectable encrypted StorageClasses are available. Create an encrypted StorageClass before creating an image.
|
||||
sourceType:
|
||||
download: URL
|
||||
upload: File
|
||||
|
||||
Reference in new issue
Block a user