mirror of
https://github.com/portainer/portainer.git
synced 2025-08-04 21:35:23 +02:00
feat(kubernetes/resource-usage): k8s resource usage for cluster, node and namespace EE-3 EE-1112 (#5301)
* backported resource usage functionality from EE * utilising view bound endpoint object instead of depracated EndpointProvider * refactor flatmap * addressed merge conflict issues
This commit is contained in:
parent
cee7ac26e9
commit
ce31de5e9e
13 changed files with 188 additions and 30 deletions
|
@ -12,11 +12,14 @@
|
|||
<!-- resource-reservation -->
|
||||
<form class="form-horizontal" ng-if="ctrl.resourceReservation">
|
||||
<kubernetes-resource-reservation
|
||||
cpu="ctrl.resourceReservation.CPU"
|
||||
cpu-limit="ctrl.CPULimit"
|
||||
memory="ctrl.resourceReservation.Memory"
|
||||
memory-limit="ctrl.MemoryLimit"
|
||||
description="Resource reservation represents the total amount of resource assigned to all the applications inside the cluster."
|
||||
cpu-reservation="ctrl.resourceReservation.CPU"
|
||||
cpu-limit="ctrl.CPULimit"
|
||||
memory-reservation="ctrl.resourceReservation.Memory"
|
||||
memory-limit="ctrl.MemoryLimit"
|
||||
display-usage="ctrl.isAdmin"
|
||||
cpu-usage="ctrl.resourceUsage.CPU"
|
||||
memory-usage="ctrl.resourceUsage.Memory"
|
||||
>
|
||||
</kubernetes-resource-reservation>
|
||||
</form>
|
||||
|
|
|
@ -2,4 +2,7 @@ angular.module('portainer.kubernetes').component('kubernetesClusterView', {
|
|||
templateUrl: './cluster.html',
|
||||
controller: 'KubernetesClusterController',
|
||||
controllerAs: 'ctrl',
|
||||
bindings: {
|
||||
endpoint: '<',
|
||||
},
|
||||
});
|
||||
|
|
|
@ -13,10 +13,10 @@ class KubernetesClusterController {
|
|||
Notifications,
|
||||
LocalStorage,
|
||||
KubernetesNodeService,
|
||||
KubernetesMetricsService,
|
||||
KubernetesApplicationService,
|
||||
KubernetesComponentStatusService,
|
||||
KubernetesEndpointService,
|
||||
EndpointProvider
|
||||
KubernetesEndpointService
|
||||
) {
|
||||
this.$async = $async;
|
||||
this.$state = $state;
|
||||
|
@ -24,10 +24,10 @@ class KubernetesClusterController {
|
|||
this.Notifications = Notifications;
|
||||
this.LocalStorage = LocalStorage;
|
||||
this.KubernetesNodeService = KubernetesNodeService;
|
||||
this.KubernetesMetricsService = KubernetesMetricsService;
|
||||
this.KubernetesApplicationService = KubernetesApplicationService;
|
||||
this.KubernetesComponentStatusService = KubernetesComponentStatusService;
|
||||
this.KubernetesEndpointService = KubernetesEndpointService;
|
||||
this.EndpointProvider = EndpointProvider;
|
||||
|
||||
this.onInit = this.onInit.bind(this);
|
||||
this.getNodes = this.getNodes.bind(this);
|
||||
|
@ -106,6 +106,10 @@ class KubernetesClusterController {
|
|||
new KubernetesResourceReservation()
|
||||
);
|
||||
this.resourceReservation.Memory = KubernetesResourceReservationHelper.megaBytesValue(this.resourceReservation.Memory);
|
||||
|
||||
if (this.isAdmin) {
|
||||
await this.getResourceUsage(this.endpoint.Id);
|
||||
}
|
||||
} catch (err) {
|
||||
this.Notifications.error('Failure', 'Unable to retrieve applications', err);
|
||||
} finally {
|
||||
|
@ -117,14 +121,31 @@ class KubernetesClusterController {
|
|||
return this.$async(this.getApplicationsAsync);
|
||||
}
|
||||
|
||||
async getResourceUsage(endpointId) {
|
||||
try {
|
||||
const nodeMetrics = await this.KubernetesMetricsService.getNodes(endpointId);
|
||||
const resourceUsageList = nodeMetrics.items.map((i) => i.usage);
|
||||
const clusterResourceUsage = resourceUsageList.reduce((total, u) => {
|
||||
total.CPU += KubernetesResourceReservationHelper.parseCPU(u.cpu);
|
||||
total.Memory += KubernetesResourceReservationHelper.megaBytesValue(u.memory);
|
||||
return total;
|
||||
}, new KubernetesResourceReservation());
|
||||
this.resourceUsage = clusterResourceUsage;
|
||||
} catch (err) {
|
||||
this.Notifications.error('Failure', 'Unable to retrieve cluster resource usage', err);
|
||||
}
|
||||
}
|
||||
|
||||
async onInit() {
|
||||
this.state = {
|
||||
applicationsLoading: true,
|
||||
viewReady: false,
|
||||
hasUnhealthyComponentStatus: false,
|
||||
useServerMetrics: false,
|
||||
};
|
||||
|
||||
this.isAdmin = this.Authentication.isAdmin();
|
||||
this.state.useServerMetrics = this.endpoint.Kubernetes.Configuration.UseServerMetrics;
|
||||
|
||||
await this.getNodes();
|
||||
if (this.isAdmin) {
|
||||
|
@ -134,7 +155,6 @@ class KubernetesClusterController {
|
|||
}
|
||||
|
||||
this.state.viewReady = true;
|
||||
this.state.useServerMetrics = this.EndpointProvider.currentEndpoint().Kubernetes.Configuration.UseServerMetrics;
|
||||
}
|
||||
|
||||
$onInit() {
|
||||
|
|
|
@ -78,11 +78,14 @@
|
|||
<div style="padding: 8px;">
|
||||
<kubernetes-resource-reservation
|
||||
ng-if="ctrl.resourceReservation"
|
||||
cpu="ctrl.resourceReservation.CPU"
|
||||
cpu-reservation="ctrl.resourceReservation.CPU"
|
||||
cpu-usage="ctrl.resourceUsage.CPU"
|
||||
cpu-limit="ctrl.node.CPU"
|
||||
memory="ctrl.resourceReservation.Memory"
|
||||
memory-reservation="ctrl.resourceReservation.Memory"
|
||||
memory-usage="ctrl.resourceUsage.Memory"
|
||||
memory-limit="ctrl.memoryLimit"
|
||||
description="Resource reservation represents the total amount of resource assigned to all the applications running on this node."
|
||||
display-usage="ctrl.state.isAdmin"
|
||||
>
|
||||
</kubernetes-resource-reservation>
|
||||
</div>
|
||||
|
|
|
@ -3,6 +3,7 @@ angular.module('portainer.kubernetes').component('kubernetesNodeView', {
|
|||
controller: 'KubernetesNodeController',
|
||||
controllerAs: 'ctrl',
|
||||
bindings: {
|
||||
endpoint: '<',
|
||||
$transition$: '<',
|
||||
},
|
||||
});
|
||||
|
|
|
@ -21,7 +21,9 @@ class KubernetesNodeController {
|
|||
KubernetesEventService,
|
||||
KubernetesPodService,
|
||||
KubernetesApplicationService,
|
||||
KubernetesEndpointService
|
||||
KubernetesEndpointService,
|
||||
KubernetesMetricsService,
|
||||
Authentication
|
||||
) {
|
||||
this.$async = $async;
|
||||
this.$state = $state;
|
||||
|
@ -33,6 +35,8 @@ class KubernetesNodeController {
|
|||
this.KubernetesPodService = KubernetesPodService;
|
||||
this.KubernetesApplicationService = KubernetesApplicationService;
|
||||
this.KubernetesEndpointService = KubernetesEndpointService;
|
||||
this.KubernetesMetricsService = KubernetesMetricsService;
|
||||
this.Authentication = Authentication;
|
||||
|
||||
this.onInit = this.onInit.bind(this);
|
||||
this.getNodesAsync = this.getNodesAsync.bind(this);
|
||||
|
@ -42,6 +46,7 @@ class KubernetesNodeController {
|
|||
this.getEndpointsAsync = this.getEndpointsAsync.bind(this);
|
||||
this.updateNodeAsync = this.updateNodeAsync.bind(this);
|
||||
this.drainNodeAsync = this.drainNodeAsync.bind(this);
|
||||
this.getNodeUsageAsync = this.getNodeUsageAsync.bind(this);
|
||||
}
|
||||
|
||||
selectTab(index) {
|
||||
|
@ -327,6 +332,22 @@ class KubernetesNodeController {
|
|||
return this.$async(this.getNodesAsync);
|
||||
}
|
||||
|
||||
async getNodeUsageAsync() {
|
||||
try {
|
||||
const nodeName = this.$transition$.params().name;
|
||||
const node = await this.KubernetesMetricsService.getNode(nodeName);
|
||||
this.resourceUsage = new KubernetesResourceReservation();
|
||||
this.resourceUsage.CPU = KubernetesResourceReservationHelper.parseCPU(node.usage.cpu);
|
||||
this.resourceUsage.Memory = KubernetesResourceReservationHelper.megaBytesValue(node.usage.memory);
|
||||
} catch (err) {
|
||||
this.Notifications.error('Failure', 'Unable to retrieve node resource usage', err);
|
||||
}
|
||||
}
|
||||
|
||||
getNodeUsage() {
|
||||
return this.$async(this.getNodeUsageAsync);
|
||||
}
|
||||
|
||||
hasEventWarnings() {
|
||||
return this.state.eventWarningCount;
|
||||
}
|
||||
|
@ -375,6 +396,10 @@ class KubernetesNodeController {
|
|||
this.resourceReservation.Memory = KubernetesResourceReservationHelper.megaBytesValue(this.resourceReservation.Memory);
|
||||
this.memoryLimit = KubernetesResourceReservationHelper.megaBytesValue(this.node.Memory);
|
||||
this.state.isContainPortainer = _.find(this.applications, { ApplicationName: 'portainer' });
|
||||
|
||||
if (this.state.isAdmin) {
|
||||
await this.getNodeUsage();
|
||||
}
|
||||
} catch (err) {
|
||||
this.Notifications.error('Failure', err, 'Unable to retrieve applications');
|
||||
} finally {
|
||||
|
@ -388,6 +413,7 @@ class KubernetesNodeController {
|
|||
|
||||
async onInit() {
|
||||
this.state = {
|
||||
isAdmin: this.Authentication.isAdmin(),
|
||||
activeTab: 0,
|
||||
currentName: this.$state.$current.name,
|
||||
dataLoading: true,
|
||||
|
@ -402,10 +428,13 @@ class KubernetesNodeController {
|
|||
hasDuplicateLabelKeys: false,
|
||||
isDrainOperation: false,
|
||||
isContainPortainer: false,
|
||||
useServerMetrics: false,
|
||||
};
|
||||
|
||||
this.availabilities = KubernetesNodeAvailabilities;
|
||||
|
||||
this.state.useServerMetrics = this.endpoint.Kubernetes.Configuration.UseServerMetrics;
|
||||
|
||||
this.state.activeTab = this.LocalStorage.getActiveTab('node');
|
||||
|
||||
await this.getNodes();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue