dashboard/models/cluster.x-k8s.io.machinedep...

129 lines
3.5 KiB
JavaScript

import { CAPI } from '@/config/types';
import { escapeHtml } from '@/utils/string';
import { sortBy } from '@/utils/sort';
import SteveModel from '@/plugins/steve/steve-class';
export default class CapiMachineDeployment extends SteveModel {
get cluster() {
if ( !this.spec.clusterName ) {
return null;
}
const clusterId = `${ this.metadata.namespace }/${ this.spec.clusterName }`;
const cluster = this.$rootGetters['management/byId'](CAPI.RANCHER_CLUSTER, clusterId);
return cluster;
}
get groupByLabel() {
const name = this.cluster?.nameDisplay || this.spec.clusterName;
return this.$rootGetters['i18n/t']('resourceTable.groupLabel.cluster', { name: escapeHtml(name) });
}
get groupByPoolLabel() {
return `${ this.$rootGetters['i18n/t']('resourceTable.groupLabel.machinePool', { name: escapeHtml(this.nameDisplay) }) }`;
}
get groupByPoolShortLabel() {
return `${ this.$rootGetters['i18n/t']('resourceTable.groupLabel.machinePool', { name: escapeHtml(this.nameDisplay) }) }`;
}
get templateType() {
return this.spec.template.spec.infrastructureRef.kind ? `rke-machine.cattle.io.${ this.spec.template.spec.infrastructureRef.kind.toLowerCase() }` : null;
}
get template() {
const ref = this.spec.template.spec.infrastructureRef;
const id = `${ ref.namespace }/${ ref.name }`;
const template = this.$rootGetters['management/byId'](this.templateType, id);
return template;
}
get providerName() {
return this.template?.nameDisplay;
}
get providerDisplay() {
const provider = (this.template?.provider || '').toLowerCase();
return this.$rootGetters['i18n/withFallback'](`cluster.provider."${ provider }"`, null, 'generic.unknown', true);
}
get providerLocation() {
return this.template?.providerLocation || this.t('node.list.poolDescription.noLocation');
}
get providerSize() {
return this.template?.providerSize || this.t('node.list.poolDescription.noSize');
}
get desired() {
return this.spec?.replicas || 0;
}
get pending() {
return Math.max(0, this.desired - (this.status?.replicas || 0));
}
get outdated() {
return Math.max(0, (this.status?.replicas || 0) - (this.status?.updatedReplicas || 0));
}
get ready() {
return Math.max(0, (this.status?.replicas || 0) - (this.status?.unavailableReplicas || 0));
}
get unavailable() {
return this.status?.unavailableReplicas || 0;
}
scalePool(delta, save = true) {
const clustersMachinePool = this.cluster.spec.rkeConfig.machinePools.find(mp => `${ this.cluster.id }-${ mp.name }` === this.id);
if (clustersMachinePool) {
clustersMachinePool.quantity += delta;
if (save) {
this.cluster.save();
}
}
}
get stateParts() {
const out = [
{
label: 'Pending',
color: 'bg-info',
textColor: 'text-info',
value: this.pending,
sort: 1,
},
{
label: 'Outdated',
color: 'bg-warning',
textColor: 'text-warning',
value: this.outdated,
sort: 2,
},
{
label: 'Unavailable',
color: 'bg-error',
textColor: 'text-error',
value: this.unavailable,
sort: 3,
},
{
label: 'Ready',
color: 'bg-success',
textColor: 'text-success',
value: this.ready,
sort: 4,
},
].filter(x => x.value > 0);
return sortBy(out, 'sort:desc');
}
}