Skip to content

Commit

Permalink
VM template flow
Browse files Browse the repository at this point in the history
  • Loading branch information
rawagner committed Oct 22, 2020
1 parent 4695f3b commit 5e8542e
Show file tree
Hide file tree
Showing 43 changed files with 2,247 additions and 523 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ const wizardDispatchToProps = (dispatch, props) => ({
isProviderImport: props.isProviderImport,
isUserTemplateInitialized: false,
commonTemplateName: props.commonTemplateName,
urlSource: props.urlSource,
containerSource: props.containerSource,
pvcSourceName: props.pvcSourceName,
pvcSourceNs: props.pvcSourceNs,
storageClassConfigMap: undefined,
isSimpleView: props.isSimpleView,
name: props.name,
Expand Down Expand Up @@ -476,6 +480,10 @@ export const CreateVMWizardPageComponent: React.FC<CreateVMWizardPageComponentPr
(userMode === VMWizardMode.TEMPLATE && searchParams.get('template')) ||
'';
const commonTemplateName = searchParams.get('common-template') || '';
const urlSource = searchParams.get('url');
const containerSource = searchParams.get('container');
const pvcSourceName = searchParams.get('pvc');
const pvcSourceNs = searchParams.get('pvcNs');
const isSimpleView =
userMode === VMWizardMode.IMPORT &&
searchParams.get('view')?.toLowerCase() !== VMWizardView.ADVANCED; // normal mode defaults to advanced
Expand All @@ -492,6 +500,10 @@ export const CreateVMWizardPageComponent: React.FC<CreateVMWizardPageComponentPr
storageClassConfigMap={storageClassConfigMap}
reduxID={reduxID}
onClose={history.goBack}
urlSource={urlSource}
containerSource={containerSource}
pvcSourceName={pvcSourceName}
pvcSourceNs={pvcSourceNs}
/>
</Firehose>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
DUMMY_VM_NAME,
TEMPLATE_BASE_IMAGE_NAME_PARAMETER,
TEMPLATE_BASE_IMAGE_NAMESPACE_PARAMETER,
ROOT_DISK_NAME,
} from '../../../../constants/vm';
import {
iGetVmSettingValue,
Expand All @@ -42,8 +43,9 @@ import {
} from '../../selectors/immutable/selectors';
import { iGetRelevantTemplate } from '../../../../selectors/immutable/template/combined';
import { iGetPrameterValue } from '../../../../selectors/immutable/common';
import { getStorages } from '../../selectors/selectors';
import { getNextIDResolver } from '../../../../utils/utils';

const ROOT_DISK_NAME = 'rootdisk';
const WINTOOLS_DISK_NAME = 'windows-guest-tools';

const containerStorage: VMWizardStorage = {
Expand Down Expand Up @@ -168,6 +170,79 @@ export const getNewProvisionSourceStorage = (state: any, id: string): VMWizardSt
const iUserTemplate = iGetCommonData(state, id, VMWizardProps.userTemplate);

if (provisionSource === ProvisionSource.URL) {
const urlSource = iGetCommonData(state, id, VMWizardProps.urlSource);
if (urlSource) {
const storagesUpdate = getStorages(state, id);
let rootStorage = storagesUpdate.find((s) => s.disk.bootOrder === 1) || storagesUpdate[0];
const storageID = getNextIDResolver(storagesUpdate)();
if (rootStorage?.volume.name === ROOT_DISK_NAME) {
rootStorage.id = storageID;
rootStorage.type = VMWizardStorageType.PROVISION_SOURCE_DISK;
rootStorage.volume = {
name: rootStorage.volume.name,
dataVolume: {
name: rootStorage.volume.name,
},
};
rootStorage.dataVolume = {
metadata: {
name: rootStorage.volume.name,
},
spec: {
pvc: {
resources: {
requests: {
storage: '20Gi',
},
},
},
source: {
http: {
url: urlSource,
},
},
},
};
} else {
rootStorage = {
id: storageID,
type: VMWizardStorageType.PROVISION_SOURCE_DISK,
volume: {
name: ROOT_DISK_NAME,
dataVolume: {
name: ROOT_DISK_NAME,
},
},
dataVolume: {
metadata: {
name: ROOT_DISK_NAME,
},
spec: {
pvc: {
resources: {
requests: {
storage: '20Gi',
},
},
},
source: {
http: {
url: urlSource,
},
},
},
},
disk: {
name: ROOT_DISK_NAME,
bootOrder: 1,
disk: {
bus: DiskBus.SATA.getValue(),
},
},
};
}
return rootStorage;
}
const iStorageClassConfigMap = iGetLoadedCommonData(
state,
id,
Expand All @@ -177,6 +252,42 @@ export const getNewProvisionSourceStorage = (state: any, id: string): VMWizardSt
return getUrlStorage(toShallowJS(iStorageClassConfigMap, undefined, true));
}
if (provisionSource === ProvisionSource.CONTAINER) {
const containerSource = iGetCommonData(state, id, VMWizardProps.containerSource);
if (containerSource) {
const storagesUpdate = getStorages(state, id);
let rootStorage = storagesUpdate.find((s) => s.disk.bootOrder === 1) || storagesUpdate[0];
const storageID = getNextIDResolver(storagesUpdate)();
// TODO this should use DataVolumes too
if (rootStorage?.volume.name === ROOT_DISK_NAME) {
rootStorage.id = storageID;
rootStorage.type = VMWizardStorageType.PROVISION_SOURCE_DISK;
rootStorage.volume = {
name: rootStorage.volume.name,
containerDisk: {
image: containerSource,
},
};
} else {
rootStorage = {
id: storageID,
type: VMWizardStorageType.PROVISION_SOURCE_DISK,
volume: {
name: ROOT_DISK_NAME,
containerDisk: {
image: ROOT_DISK_NAME,
},
},
disk: {
name: ROOT_DISK_NAME,
bootOrder: 1,
disk: {
bus: DiskBus.SATA.getValue(),
},
},
};
}
return rootStorage;
}
return containerStorage;
}
if (provisionSource === ProvisionSource.DISK && !iUserTemplate && cloneCommonBaseDiskImage) {
Expand All @@ -203,6 +314,82 @@ export const getNewProvisionSourceStorage = (state: any, id: string): VMWizardSt

return getBaseImageStorage(toShallowJS(iStorageClassConfigMap), pvcName, pvcNamespace, pvcSize);
}
const pvcSourceName = iGetCommonData(state, id, VMWizardProps.pvcSourceName);
const pvcSourceNs = iGetCommonData(state, id, VMWizardProps.pvcSourceNs);
if (provisionSource === ProvisionSource.DISK && !iUserTemplate && pvcSourceName && pvcSourceNs) {
const storagesUpdate = getStorages(state, id);
let rootStorage = storagesUpdate.find((s) => s.disk.bootOrder === 1) || storagesUpdate[0];
const storageID = getNextIDResolver(storagesUpdate)();
if (rootStorage?.volume.name === ROOT_DISK_NAME) {
rootStorage.id = storageID;
rootStorage.type = VMWizardStorageType.PROVISION_SOURCE_DISK;
rootStorage.volume = {
name: rootStorage.volume.name,
dataVolume: {
name: rootStorage.volume.name,
},
};
rootStorage.dataVolume = {
metadata: {
name: rootStorage.volume.name,
},
spec: {
pvc: {
resources: {
requests: {
storage: '20Gi',
},
},
},
source: {
pvc: {
name: pvcSourceName,
namespace: pvcSourceNs,
},
},
},
};
} else {
rootStorage = {
id: storageID,
type: VMWizardStorageType.PROVISION_SOURCE_DISK,
volume: {
name: ROOT_DISK_NAME,
dataVolume: {
name: ROOT_DISK_NAME,
},
},
dataVolume: {
metadata: {
name: ROOT_DISK_NAME,
},
spec: {
pvc: {
resources: {
requests: {
storage: '20Gi',
},
},
},
source: {
pvc: {
name: pvcSourceName,
namespace: pvcSourceNs,
},
},
},
},
disk: {
name: ROOT_DISK_NAME,
bootOrder: 1,
disk: {
bus: DiskBus.SATA.getValue(),
},
},
};
}
return rootStorage;
}
return null;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ import { InitialStepStateGetter, VMSettings } from './types';

export const getInitialVmSettings = (data: CommonData): VMSettings => {
const {
data: { isCreateTemplate, isProviderImport, name, commonTemplateName },
data: {
isCreateTemplate,
isProviderImport,
name,
commonTemplateName,
urlSource,
containerSource,
pvcSourceName,
pvcSourceNs,
},
} = data;

const hiddenByProvider = asHidden(isProviderImport, VMWizardProps.isProviderImport);
Expand Down Expand Up @@ -59,6 +68,7 @@ export const getInitialVmSettings = (data: CommonData): VMSettings => {
ProvisionSource.DISK,
].map((source) => source.getValue()),
),
initialized: !(urlSource || containerSource || (pvcSourceName && pvcSourceNs)),
},
[VMSettingsField.CONTAINER_IMAGE]: {
isHidden: hiddenByProviderOrCloneCommonBaseDiskImage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
NetworkInterfaceModel,
VolumeType,
} from '../../../../constants/vm';
import { DUMMY_VM_NAME } from '../../../../constants/vm/constants';
import { DUMMY_VM_NAME, ROOT_DISK_NAME } from '../../../../constants/vm/constants';
import {
DEFAULT_CPU,
getCPU,
Expand Down Expand Up @@ -269,6 +269,20 @@ export const prefillVmTemplateUpdater = ({ id, dispatch, getState }: UpdateOptio
: undefined,
};
});
let rootDiskIndex = templateStorages.findIndex((s) => s.disk.bootOrder === 1);

if (rootDiskIndex === -1) {
rootDiskIndex = templateStorages.findIndex((s) => s.disk.name === ROOT_DISK_NAME);
}

const urlSource = iGetCommonData(state, id, VMWizardProps.urlSource);
const containerSource = iGetCommonData(state, id, VMWizardProps.containerSource);
const pvcSourceName = iGetCommonData(state, id, VMWizardProps.pvcSourceName);
const pvcSourceNs = iGetCommonData(state, id, VMWizardProps.pvcSourceNs);
const hasCustomSource = urlSource || containerSource || (pvcSourceName && pvcSourceNs);
if (rootDiskIndex !== -1 && !!hasCustomSource) {
templateStorages.splice(rootDiskIndex, 1);
}
storagesUpdate.unshift(...templateStorages);
} else {
const newSourceStorage = getNewProvisionSourceStorage(state, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
hasVMSettingsValueChanged,
iGetProvisionSource,
iGetRelevantTemplateSelectors,
iGetVmSettingAttribute,
iGetVmSettingValue,
} from '../../selectors/immutable/vm-settings';
import { VMSettingsField, VMWizardProps } from '../../types';
Expand Down Expand Up @@ -234,6 +235,51 @@ const provisioningSourceUpdater = ({ id, prevState, dispatch, getState }: Update
);
};

const selectProvisionSourceOnLoadedUpdater = ({ id, getState, dispatch }: UpdateOptions) => {
const state = getState();
if (
iGetVmSettingAttribute(state, id, VMSettingsField.PROVISION_SOURCE_TYPE, 'initialized') ===
false &&
(iGetCommonData(state, id, VMWizardProps.userTemplate)
? iGetCommonData(state, id, VMWizardProps.isUserTemplateInitialized)
: true) &&
iGetVmSettingAttribute(state, id, VMSettingsField.OPERATING_SYSTEM, 'initialized')
) {
const urlSource = iGetCommonData(state, id, VMWizardProps.urlSource);
const containerSource = iGetCommonData(state, id, VMWizardProps.containerSource);
const pvcSourceName = iGetCommonData(state, id, VMWizardProps.pvcSourceName);
const pvcSourceNs = iGetCommonData(state, id, VMWizardProps.pvcSourceNs);
if (urlSource) {
dispatch(
vmWizardInternalActions[InternalActionType.UpdateVmSettings](id, {
[VMSettingsField.PROVISION_SOURCE_TYPE]: {
value: ProvisionSource.URL.getValue(),
initialized: true,
},
}),
);
} else if (containerSource) {
dispatch(
vmWizardInternalActions[InternalActionType.UpdateVmSettings](id, {
[VMSettingsField.PROVISION_SOURCE_TYPE]: {
value: ProvisionSource.CONTAINER.getValue(),
initialized: true,
},
}),
);
} else if (pvcSourceName && pvcSourceNs) {
dispatch(
vmWizardInternalActions[InternalActionType.UpdateVmSettings](id, {
[VMSettingsField.PROVISION_SOURCE_TYPE]: {
value: ProvisionSource.DISK.getValue(),
initialized: true,
},
}),
);
}
}
};

const nativeK8sUpdater = ({ id, dispatch, getState, changedCommonData }: UpdateOptions) => {
const state = getState();
if (!changedCommonData.has(VMWizardProps.openshiftFlag)) {
Expand Down Expand Up @@ -279,6 +325,7 @@ export const updateVmSettingsState = (options: UpdateOptions) =>
[
commonTemplateOnLoadedUpdater,
selectUserTemplateOnLoadedUpdater,
selectProvisionSourceOnLoadedUpdater,
osUpdater,
baseImageUpdater,
cloneCommonBaseDiskImageUpdater,
Expand Down

0 comments on commit 5e8542e

Please sign in to comment.