Skip to content

Commit

Permalink
kubevirt: take storageClassConfigMap into account when creating disks
Browse files Browse the repository at this point in the history
  • Loading branch information
suomiy committed Sep 19, 2019
1 parent 5057e1e commit 944a382
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,32 @@ import { HelpBlock, FormGroup } from 'patternfly-react';
import { StorageClassModel } from '@console/internal/models';
import { getName } from '@console/shared';
import { useSafetyFirst } from '@console/internal/components/safety-first';
import { k8sPatch, K8sResourceKind } from '@console/internal/module/k8s';
import { k8sGet, k8sPatch, K8sResourceKind } from '@console/internal/module/k8s';
import { getVmPreferableDiskBus } from '../../selectors/vm';
import { getVMLikeModel } from '../../selectors/selectors';
import { getAddDiskPatches } from '../../k8s/patches/vm/vm-disk-patches';
import { VMLikeEntityKind } from '../../types';
import { ValidationErrorType } from '../../utils/validations/common';
import { validateDiskName } from '../../utils/validations/vm';
import { GENERAL_ERROR_MSG } from '../../utils/validations/strings';
import { getStorageClassConfigMap } from '../../k8s/requests/config-map/storage-class';
import { VMDiskRowProps } from './types';
import './_create-device-row.scss';

const createDisk = ({
const createDisk = async ({
vmLikeEntity,
disk,
}: {
vmLikeEntity: VMLikeEntityKind;
disk: any;
}): Promise<VMLikeEntityKind> =>
k8sPatch(getVMLikeModel(vmLikeEntity), vmLikeEntity, getAddDiskPatches(vmLikeEntity, disk));
}): Promise<VMLikeEntityKind> => {
const storageClassConfigMap = await getStorageClassConfigMap({ k8sGet });
return k8sPatch(
getVMLikeModel(vmLikeEntity),
vmLikeEntity,
getAddDiskPatches(vmLikeEntity, disk, storageClassConfigMap),
);
};

type StorageClassColumn = {
storageClass: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getName } from '@console/shared';
import { getAddDiskPatch, getDeviceBootOrderPatch } from 'kubevirt-web-ui-components';
import { Patch } from '@console/internal/module/k8s';
import { ConfigMapKind, Patch } from '@console/internal/module/k8s';
import {
getDataVolumeTemplates,
getDiskBootOrder,
Expand Down Expand Up @@ -59,8 +59,12 @@ export const getRemoveDiskPatches = (vmLikeEntity: VMLikeEntityKind, disk): Patc
});
};

export const getAddDiskPatches = (vmLikeEntity: VMLikeEntityKind, disk: object): Patch[] => {
export const getAddDiskPatches = (
vmLikeEntity: VMLikeEntityKind,
disk: object,
storageClassConfigMap: ConfigMapKind,
): Patch[] => {
return getVMLikePatches(vmLikeEntity, (vm) => {
return getAddDiskPatch(vm, disk);
return getAddDiskPatch(vm, disk, storageClassConfigMap);
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const STORAGE_CLASS_CONFIG_MAP_NAME = 'kubevirt-storage-class-defaults';
// Different releases, different locations. Respect the order when resolving. Otherwise the configMap name/namespace is considered as well-known.
export const STORAGE_CLASS_CONFIG_MAP_NAMESPACES = ['openshift-cnv', 'openshift'];
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './storageClassConfigMap';
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { ConfigMapModel } from '@console/internal/models';
import { ConfigMapKind } from '@console/internal/module/k8s';
import { STORAGE_CLASS_CONFIG_MAP_NAME, STORAGE_CLASS_CONFIG_MAP_NAMESPACES } from './constants';

const { warn } = console;

const getStorageClassConfigMapInNamespace = async ({
k8sGet,
namespace,
}: {
namespace: string;
k8sGet: (...opts: any) => Promise<any>;
}): Promise<ConfigMapKind> => {
try {
return await k8sGet(ConfigMapModel, STORAGE_CLASS_CONFIG_MAP_NAME, namespace, null, {
disableHistory: true,
});
} catch (e) {
return null;
}
};

export const getStorageClassConfigMap = async (props: {
k8sGet: (...opts: any[]) => Promise<any>;
}): Promise<ConfigMapKind> => {
// query namespaces sequentially to respect order
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let index = 0; index < STORAGE_CLASS_CONFIG_MAP_NAMESPACES.length; index++) {
// eslint-disable-next-line no-await-in-loop
const configMap = await getStorageClassConfigMapInNamespace({
namespace: STORAGE_CLASS_CONFIG_MAP_NAMESPACES[index],
...props,
});
if (configMap) {
return configMap;
}
}
warn(
`The ${STORAGE_CLASS_CONFIG_MAP_NAME} can not be found in none of following namespaces: `,
JSON.stringify(STORAGE_CLASS_CONFIG_MAP_NAMESPACES),
'. The PVCs will be created with default values.',
);
return null;
};

0 comments on commit 944a382

Please sign in to comment.