Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve vGPU allocation #11399

Merged
merged 6 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 64 additions & 71 deletions pkg/harvester-manager/machine-config/harvester.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import YamlEditor from '@shell/components/YamlEditor';
import { Checkbox } from '@components/Form/Checkbox';
import { Banner } from '@components/Banner';
import { clone, get } from '@shell/utils/object';
import { uniq, removeObject } from '@shell/utils/array';

import { _CREATE, _VIEW } from '@shell/config/query-params';

import { _CREATE } from '@shell/config/query-params';
import { removeObject } from '@shell/utils/array';
import { mapGetters } from 'vuex';
import {
HCI,
Expand Down Expand Up @@ -315,7 +316,7 @@ export default {
this.networksObj = JSON.parse(this.value.networkInfo);
this.networksHistoric = this.value.networkInfo;

this.getEnabledVGpuDevices();
this.getAvailableVGpuDevices();

this.update();
} catch (e) {
Expand Down Expand Up @@ -375,7 +376,7 @@ export default {
if (this.value.vgpuInfo) {
const vGPURequests = JSON.parse(this.value.vgpuInfo)?.vGPURequests;

vGpus = vGPURequests?.map((r) => r?.name).filter((r) => r) || [];
vGpus = vGPURequests?.map((r) => r?.deviceName).filter((f) => f) || [];
}

return {
Expand Down Expand Up @@ -407,7 +408,8 @@ export default {
networkDataIsBase64,
vmAffinityIsBase64,
SOURCE_TYPE,
vGpuEnabledDevices: {},
vGpuDevices: {},
vGpusInit: vGpus,
vGpus,
};
},
Expand Down Expand Up @@ -479,34 +481,15 @@ export default {
};
},

vGpusAllocatable() {
const allocatable = this.allNodeObjects.reduce((acc, node) => [
...acc,
...Object.keys(node.status.allocatable || {}).filter((k) => k.startsWith(VGPU_PREFIX.NVIDIA)),
], []);

return allocatable.reduce((acc, v) => {
let available = 0;

this.allNodeObjects.forEach((n) => {
if (n.status.allocatable[v]) {
available += Number(n.status.allocatable[v]);
}
});

if (available > 0) {
return {
...acc,
[v]: available
};
}

return acc;
}, {});
},

vGpuOptions() {
return Object.keys(this.vGpuEnabledDevices).filter((x) => !this.vGpus.includes(x));
const vGpuTypes = uniq([
...this.vGpusInit,
...Object.values(this.vGpuDevices)
.filter((vGpu) => vGpu.enabled && vGpu.allocatable > 0 && !!vGpu.type)
.map((vGpu) => vGpu.type),
]);

return vGpuTypes;
}
},

Expand Down Expand Up @@ -626,8 +609,6 @@ export default {

this.validatorDiskAndNetowrk(errors);

this.validatorVGpus(errors);

podAffinityValidator(this.vmAffinity.affinity, this.$store.getters, errors);

return { errors };
Expand Down Expand Up @@ -695,21 +676,6 @@ export default {
}
},

validatorVGpus(errors) {
const notAllocatable = this.vGpus
.map((id) => this.vGpuEnabledDevices[id])
.filter((vGpu) => this.vGpusAllocatable[vGpu?.type] < this.machinePools[this.poolIndex]?.pool?.quantity);

notAllocatable.forEach((vGpu) => {
const message = this.$store.getters['i18n/t']('cluster.credential.harvester.vGpus.errors.notAllocatable', {
vGpus: vGpu?.type,
pool: this.machinePools[this.poolIndex]?.pool?.name || '',
});

errors.push(message);
});
},

valuesChanged(value, type) {
this.value[type] = base64Encode(value);
},
Expand All @@ -733,22 +699,35 @@ export default {
}
},

async getEnabledVGpuDevices() {
async getAvailableVGpuDevices() {
const clusterId = get(this.credential, 'decodedData.clusterId');

if (clusterId) {
const url = `/k8s/clusters/${ clusterId }/v1`;
const res = await this.$store.dispatch('cluster/request', { url: `${ url }/${ HCI.VGPU_DEVICE }s` });

this.vGpuEnabledDevices = (res?.data || [])
.filter((v) => v.spec.enabled)
.reduce((acc, v) => ({
...acc,
[v.id]: {
type: VGPU_PREFIX.NVIDIA + v.spec.vGPUTypeName?.replace(' ', '_'),
id: v.id
},
}), {});

const vGpus = await this.$store.dispatch('cluster/request', { url: `${ url }/${ HCI.VGPU_DEVICE }` });
const harvesterCluster = await this.$store.dispatch('cluster/request', { url: `${ url }/harvester/cluster/local` });

let deviceCapacity = {};

if (harvesterCluster?.links?.deviceCapacity) {
deviceCapacity = await this.$store.dispatch('cluster/request', { url: harvesterCluster?.links?.deviceCapacity });
}

this.vGpuDevices = (vGpus?.data || [])
.reduce((acc, v) => {
const type = v.spec.vGPUTypeName ? `${ VGPU_PREFIX.NVIDIA }${ v.spec.vGPUTypeName.replace(' ', '_') }` : '';

return {
...acc,
[v.id]: {
id: v.id,
enabled: v.spec.enabled,
allocatable: deviceCapacity[type] ? Number(deviceCapacity[type]) : 0,
type,
},
};
}, {});
}
},

Expand Down Expand Up @@ -909,14 +888,14 @@ export default {
},

updateVGpu() {
const vGPURequests = this.vGpus?.filter((name) => name).reduce((acc, name, i) => ([
...acc,
{
name,
deviceName: this.vGpuEnabledDevices[name]?.type,
}
])
, []);
/**
* We are assigning the first vGpu profile found for each vGpu type selected by the user.
* This will not work if we will remove the limit of only one vGpu assignable to each cluster.
*/
const vGPURequests = this.vGpus?.filter((f) => f).map((deviceName) => ({
name: Object.values(this.vGpuDevices).filter((f) => f.type === deviceName)?.[0]?.id || '',
a110605 marked this conversation as resolved.
Show resolved Hide resolved
deviceName,
})) || [];

this.value.vgpuInfo = vGPURequests.length > 0 ? JSON.stringify({ vGPURequests }) : '';
},
Expand Down Expand Up @@ -1117,9 +1096,23 @@ export default {
},

vGpuOptionLabel(opt) {
const vGpu = this.vGpuEnabledDevices[opt];
let label = opt.replace(VGPU_PREFIX.NVIDIA, '');

if (this.mode === _VIEW) {
return label;
}

/**
* We get the allocatable label from the first vGpu profile found for each vGpu type.
* This is consistent as long as vGpu profiles with the same vGpu type, have the same allocable number.
*/
const vGpu = Object.values(this.vGpuDevices).filter((f) => f.type === opt)?.[0];

if (vGpu?.allocatable > 0) {
label += ` (allocatable: ${ vGpu.allocatable })`;
}

return `${ vGpu?.type?.replace(VGPU_PREFIX.NVIDIA, '') } - ${ vGpu?.id } (allocatable: ${ this.vGpusAllocatable[vGpu?.type] })`;
return label;
}
}
};
Expand Down
3 changes: 0 additions & 3 deletions shell/assets/translations/en-us.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1357,9 +1357,6 @@ cluster:
networkName: Network Name
macAddress: Mac Address
macFormat: 'Invalid MAC address format.'
vGpus:
errors:
notAllocatable: '"VGPUs" not allocatable. There are not enough [{vGpus}] devices to be allocated to each node in machine pool [{pool}]'
volume:
title: Volumes
volume: Volume
Expand Down
Loading