debian-mirror-gitlab/app/assets/javascripts/clusters_list/components/clusters.vue

344 lines
10 KiB
Vue
Raw Normal View History

2020-04-08 14:13:33 +05:30
<script>
2020-06-23 00:09:42 +05:30
import {
2021-01-29 00:20:46 +05:30
GlBadge,
2020-06-23 00:09:42 +05:30
GlLink,
GlLoadingIcon,
GlPagination,
2020-11-24 15:15:51 +05:30
GlDeprecatedSkeletonLoading as GlSkeletonLoading,
2020-06-23 00:09:42 +05:30
GlSprintf,
GlTable,
2021-01-29 00:20:46 +05:30
GlTooltipDirective,
2020-06-23 00:09:42 +05:30
} from '@gitlab/ui';
2021-03-11 19:13:27 +05:30
import { mapState, mapActions } from 'vuex';
import { __, sprintf } from '~/locale';
import { CLUSTER_TYPES, STATUSES } from '../constants';
2020-07-28 23:09:34 +05:30
import AncestorNotice from './ancestor_notice.vue';
2021-01-03 14:25:43 +05:30
import NodeErrorHelpText from './node_error_help_text.vue';
2021-12-11 22:18:48 +05:30
import ClustersEmptyState from './clusters_empty_state.vue';
2020-04-08 14:13:33 +05:30
export default {
2020-06-23 00:09:42 +05:30
nodeMemoryText: __('%{totalMemory} (%{freeSpacePercentage}%{percentSymbol} free)'),
nodeCpuText: __('%{totalCpu} (%{freeSpacePercentage}%{percentSymbol} free)'),
2020-04-08 14:13:33 +05:30
components: {
2020-07-28 23:09:34 +05:30
AncestorNotice,
2020-04-08 14:13:33 +05:30
GlBadge,
2020-05-24 23:13:21 +05:30
GlLink,
GlLoadingIcon,
GlPagination,
2020-07-28 23:09:34 +05:30
GlSkeletonLoading,
2020-06-23 00:09:42 +05:30
GlSprintf,
2020-05-24 23:13:21 +05:30
GlTable,
2021-01-03 14:25:43 +05:30
NodeErrorHelpText,
2021-12-11 22:18:48 +05:30
ClustersEmptyState,
2020-04-08 14:13:33 +05:30
},
directives: {
2021-01-29 00:20:46 +05:30
GlTooltip: GlTooltipDirective,
2020-04-08 14:13:33 +05:30
},
2021-12-11 22:18:48 +05:30
props: {
isChildComponent: {
default: false,
required: false,
type: Boolean,
},
limit: {
default: null,
required: false,
type: Number,
},
},
2020-05-24 23:13:21 +05:30
computed: {
2020-07-28 23:09:34 +05:30
...mapState([
'clusters',
'clustersPerPage',
'loadingClusters',
'loadingNodes',
'page',
'providers',
2021-12-11 22:18:48 +05:30
'totalClusters',
2020-07-28 23:09:34 +05:30
]),
contentAlignClasses() {
2022-04-04 11:22:00 +05:30
return 'gl-display-flex gl-align-items-center gl-justify-content-end gl-md-justify-content-start';
2020-07-28 23:09:34 +05:30
},
2020-05-24 23:13:21 +05:30
currentPage: {
get() {
return this.page;
},
set(newVal) {
this.setPage(newVal);
this.fetchClusters();
},
2020-04-08 14:13:33 +05:30
},
2020-05-24 23:13:21 +05:30
fields() {
2021-12-11 22:18:48 +05:30
const tdClass = 'gl-py-5!';
2020-05-24 23:13:21 +05:30
return [
{
key: 'name',
label: __('Kubernetes cluster'),
2021-12-11 22:18:48 +05:30
tdClass,
2020-05-24 23:13:21 +05:30
},
{
key: 'environment_scope',
label: __('Environment scope'),
2021-12-11 22:18:48 +05:30
tdClass,
2020-05-24 23:13:21 +05:30
},
2020-06-23 00:09:42 +05:30
{
key: 'node_size',
label: __('Nodes'),
2021-12-11 22:18:48 +05:30
tdClass,
2020-06-23 00:09:42 +05:30
},
{
key: 'total_cpu',
label: __('Total cores (CPUs)'),
2021-12-11 22:18:48 +05:30
tdClass,
2020-06-23 00:09:42 +05:30
},
{
key: 'total_memory',
label: __('Total memory (GB)'),
2021-12-11 22:18:48 +05:30
tdClass,
2020-06-23 00:09:42 +05:30
},
2020-05-24 23:13:21 +05:30
{
key: 'cluster_type',
label: __('Cluster level'),
2021-12-11 22:18:48 +05:30
tdClass,
2021-03-08 18:12:59 +05:30
formatter: (value) => CLUSTER_TYPES[value],
2020-05-24 23:13:21 +05:30
},
];
2020-04-08 14:13:33 +05:30
},
2021-12-11 22:18:48 +05:30
hasClustersPerPage() {
2020-05-24 23:13:21 +05:30
return this.clustersPerPage > 0;
2020-04-08 14:13:33 +05:30
},
2021-12-11 22:18:48 +05:30
hasClusters() {
return this.totalClusters > 0;
},
2020-04-08 14:13:33 +05:30
},
mounted() {
2021-12-11 22:18:48 +05:30
if (this.limit) {
this.setClustersPerPage(this.limit);
}
2020-05-24 23:13:21 +05:30
this.fetchClusters();
2020-04-08 14:13:33 +05:30
},
methods: {
2021-12-11 22:18:48 +05:30
...mapActions(['fetchClusters', 'reportSentryError', 'setPage', 'setClustersPerPage']),
2020-06-23 00:09:42 +05:30
k8sQuantityToGb(quantity) {
if (!quantity) {
return 0;
} else if (quantity.endsWith(__('Ki'))) {
return parseInt(quantity.substr(0, quantity.length - 2), 10) * 0.000001024;
} else if (quantity.endsWith(__('Mi'))) {
return parseInt(quantity.substr(0, quantity.length - 2), 10) * 0.001048576;
}
// We are trying to track quantity types coming from Kubernetes.
// Sentry will notify us if we are missing types.
throw new Error(`UnknownK8sMemoryQuantity:${quantity}`);
},
k8sQuantityToCpu(quantity) {
if (!quantity) {
return 0;
} else if (quantity.endsWith('m')) {
return parseInt(quantity.substr(0, quantity.length - 1), 10) / 1000.0;
} else if (quantity.endsWith('n')) {
return parseInt(quantity.substr(0, quantity.length - 1), 10) / 1000000000.0;
}
// We are trying to track quantity types coming from Kubernetes.
// Sentry will notify us if we are missing types.
throw new Error(`UnknownK8sCpuQuantity:${quantity}`);
},
selectedProvider(provider) {
return this.providers[provider] || this.providers.default;
2020-04-08 14:13:33 +05:30
},
statusTitle(status) {
2020-05-24 23:13:21 +05:30
const iconTitle = STATUSES[status] || STATUSES.default;
return sprintf(__('Status: %{title}'), { title: iconTitle.title }, false);
2020-04-08 14:13:33 +05:30
},
2020-06-23 00:09:42 +05:30
totalMemoryAndUsage(nodes) {
try {
// For EKS node.usage will not be present unless the user manually
// install the metrics server
if (nodes && nodes[0].usage) {
let totalAllocatableMemory = 0;
let totalUsedMemory = 0;
nodes.reduce((total, node) => {
const allocatableMemoryQuantity = node.status.allocatable.memory;
const allocatableMemoryGb = this.k8sQuantityToGb(allocatableMemoryQuantity);
totalAllocatableMemory += allocatableMemoryGb;
const usedMemoryQuantity = node.usage.memory;
const usedMemoryGb = this.k8sQuantityToGb(usedMemoryQuantity);
totalUsedMemory += usedMemoryGb;
return null;
}, 0);
const freeSpacePercentage = (1 - totalUsedMemory / totalAllocatableMemory) * 100;
return {
totalMemory: totalAllocatableMemory.toFixed(2),
freeSpacePercentage: Math.round(freeSpacePercentage),
};
}
} catch (error) {
2020-07-28 23:09:34 +05:30
this.reportSentryError({ error, tag: 'totalMemoryAndUsageError' });
2020-06-23 00:09:42 +05:30
}
return { totalMemory: null, freeSpacePercentage: null };
},
totalCpuAndUsage(nodes) {
try {
// For EKS node.usage will not be present unless the user manually
// install the metrics server
if (nodes && nodes[0].usage) {
let totalAllocatableCpu = 0;
let totalUsedCpu = 0;
nodes.reduce((total, node) => {
const allocatableCpuQuantity = node.status.allocatable.cpu;
const allocatableCpu = this.k8sQuantityToCpu(allocatableCpuQuantity);
totalAllocatableCpu += allocatableCpu;
const usedCpuQuantity = node.usage.cpu;
const usedCpuGb = this.k8sQuantityToCpu(usedCpuQuantity);
totalUsedCpu += usedCpuGb;
return null;
}, 0);
const freeSpacePercentage = (1 - totalUsedCpu / totalAllocatableCpu) * 100;
return {
totalCpu: totalAllocatableCpu.toFixed(2),
freeSpacePercentage: Math.round(freeSpacePercentage),
};
}
} catch (error) {
2020-07-28 23:09:34 +05:30
this.reportSentryError({ error, tag: 'totalCpuAndUsageError' });
2020-06-23 00:09:42 +05:30
}
return { totalCpu: null, freeSpacePercentage: null };
},
2020-04-08 14:13:33 +05:30
},
};
</script>
<template>
2021-12-11 22:18:48 +05:30
<gl-loading-icon v-if="loadingClusters" size="md" />
2020-05-24 23:13:21 +05:30
<section v-else>
2020-07-28 23:09:34 +05:30
<ancestor-notice />
2021-01-03 14:25:43 +05:30
<gl-table
2021-12-11 22:18:48 +05:30
v-if="hasClusters"
2021-01-03 14:25:43 +05:30
:items="clusters"
:fields="fields"
2021-12-11 22:18:48 +05:30
fixed
2021-01-03 14:25:43 +05:30
stacked="md"
2021-11-11 11:23:49 +05:30
head-variant="white"
2021-12-11 22:18:48 +05:30
thead-class="gl-border-b-solid gl-border-b-2 gl-border-b-gray-100"
class="qa-clusters-table gl-mb-4!"
2021-01-03 14:25:43 +05:30
data-testid="cluster_list_table"
>
2020-05-24 23:13:21 +05:30
<template #cell(name)="{ item }">
2020-07-28 23:09:34 +05:30
<div :class="[contentAlignClasses, 'js-status']">
2020-06-23 00:09:42 +05:30
<img
:src="selectedProvider(item.provider_type).path"
:alt="selectedProvider(item.provider_type).text"
class="gl-w-6 gl-h-6 gl-display-flex gl-align-items-center"
/>
<gl-link
data-qa-selector="cluster"
:data-qa-cluster-name="item.name"
:href="item.path"
class="gl-px-3"
>
2020-05-24 23:13:21 +05:30
{{ item.name }}
</gl-link>
<gl-loading-icon
2020-06-23 00:09:42 +05:30
v-if="item.status === 'deleting' || item.status === 'creating'"
2021-01-29 00:20:46 +05:30
v-gl-tooltip
2020-05-24 23:13:21 +05:30
:title="statusTitle(item.status)"
size="sm"
/>
</div>
</template>
2020-06-23 00:09:42 +05:30
<template #cell(node_size)="{ item }">
<span v-if="item.nodes">{{ item.nodes.length }}</span>
2020-07-28 23:09:34 +05:30
<gl-skeleton-loading v-else-if="loadingNodes" :lines="1" :class="contentAlignClasses" />
2021-12-11 22:18:48 +05:30
<node-error-help-text
2021-01-03 14:25:43 +05:30
v-else-if="item.kubernetes_errors"
:class="contentAlignClasses"
:error-type="item.kubernetes_errors.connection_error"
:popover-id="`nodeSizeError${item.id}`"
/>
2020-06-23 00:09:42 +05:30
</template>
<template #cell(total_cpu)="{ item }">
<span v-if="item.nodes">
<gl-sprintf :message="$options.nodeCpuText">
<template #totalCpu>{{ totalCpuAndUsage(item.nodes).totalCpu }}</template>
<template #freeSpacePercentage>{{
totalCpuAndUsage(item.nodes).freeSpacePercentage
}}</template>
2021-03-08 18:12:59 +05:30
<template #percentSymbol>%</template>
2020-06-23 00:09:42 +05:30
</gl-sprintf>
</span>
2020-07-28 23:09:34 +05:30
<gl-skeleton-loading v-else-if="loadingNodes" :lines="1" :class="contentAlignClasses" />
2021-01-03 14:25:43 +05:30
2021-12-11 22:18:48 +05:30
<node-error-help-text
2021-01-03 14:25:43 +05:30
v-else-if="item.kubernetes_errors"
:class="contentAlignClasses"
:error-type="item.kubernetes_errors.node_connection_error"
:popover-id="`nodeCpuError${item.id}`"
/>
2020-06-23 00:09:42 +05:30
</template>
<template #cell(total_memory)="{ item }">
<span v-if="item.nodes">
<gl-sprintf :message="$options.nodeMemoryText">
<template #totalMemory>{{ totalMemoryAndUsage(item.nodes).totalMemory }}</template>
<template #freeSpacePercentage>{{
totalMemoryAndUsage(item.nodes).freeSpacePercentage
}}</template>
2021-03-08 18:12:59 +05:30
<template #percentSymbol>%</template>
2020-06-23 00:09:42 +05:30
</gl-sprintf>
</span>
2020-07-28 23:09:34 +05:30
<gl-skeleton-loading v-else-if="loadingNodes" :lines="1" :class="contentAlignClasses" />
2021-01-03 14:25:43 +05:30
2021-12-11 22:18:48 +05:30
<node-error-help-text
2021-01-03 14:25:43 +05:30
v-else-if="item.kubernetes_errors"
:class="contentAlignClasses"
:error-type="item.kubernetes_errors.metrics_connection_error"
:popover-id="`nodeMemoryError${item.id}`"
/>
2020-06-23 00:09:42 +05:30
</template>
2021-03-08 18:12:59 +05:30
<template #cell(cluster_type)="{ value }">
2021-01-29 00:20:46 +05:30
<gl-badge variant="muted">
2020-05-24 23:13:21 +05:30
{{ value }}
</gl-badge>
</template>
</gl-table>
2021-12-11 22:18:48 +05:30
<clusters-empty-state v-else :is-child-component="isChildComponent" />
2020-05-24 23:13:21 +05:30
<gl-pagination
2021-12-11 22:18:48 +05:30
v-if="hasClustersPerPage && !limit"
2020-05-24 23:13:21 +05:30
v-model="currentPage"
:per-page="clustersPerPage"
2021-12-11 22:18:48 +05:30
:total-items="totalClusters"
2020-05-24 23:13:21 +05:30
:prev-text="__('Prev')"
:next-text="__('Next')"
align="center"
/>
</section>
2020-04-08 14:13:33 +05:30
</template>