fix(metrics): disable metric server api calls if metric server is disabled on k8s endpoint EE-1273 EE-1274 (#5377)

* - metric server api call disabled on cluster view
- metric server api call disabled on node view
- metric server api call disabled on namespace view

* enforcing resource access to function to ensure similarity to ee implementation
pull/5412/merge
zees-dev 2021-08-13 16:46:18 +12:00 committed by GitHub
parent 7b6a31181e
commit 5fe90db36a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 17 deletions

View File

@ -17,7 +17,7 @@
cpu-limit="ctrl.CPULimit" cpu-limit="ctrl.CPULimit"
memory-reservation="ctrl.resourceReservation.Memory" memory-reservation="ctrl.resourceReservation.Memory"
memory-limit="ctrl.MemoryLimit" memory-limit="ctrl.MemoryLimit"
display-usage="ctrl.isAdmin" display-usage="ctrl.hasResourceUsageAccess()"
cpu-usage="ctrl.resourceUsage.CPU" cpu-usage="ctrl.resourceUsage.CPU"
memory-usage="ctrl.resourceUsage.Memory" memory-usage="ctrl.resourceUsage.Memory"
> >

View File

@ -36,6 +36,7 @@ class KubernetesClusterController {
this.getComponentStatus = this.getComponentStatus.bind(this); this.getComponentStatus = this.getComponentStatus.bind(this);
this.getComponentStatusAsync = this.getComponentStatusAsync.bind(this); this.getComponentStatusAsync = this.getComponentStatusAsync.bind(this);
this.getEndpointsAsync = this.getEndpointsAsync.bind(this); this.getEndpointsAsync = this.getEndpointsAsync.bind(this);
this.hasResourceUsageAccess = this.hasResourceUsageAccess.bind(this);
} }
async getComponentStatusAsync() { async getComponentStatusAsync() {
@ -107,7 +108,7 @@ class KubernetesClusterController {
); );
this.resourceReservation.Memory = KubernetesResourceReservationHelper.megaBytesValue(this.resourceReservation.Memory); this.resourceReservation.Memory = KubernetesResourceReservationHelper.megaBytesValue(this.resourceReservation.Memory);
if (this.isAdmin) { if (this.hasResourceUsageAccess()) {
await this.getResourceUsage(this.endpoint.Id); await this.getResourceUsage(this.endpoint.Id);
} }
} catch (err) { } catch (err) {
@ -136,17 +137,25 @@ class KubernetesClusterController {
} }
} }
/**
* Check if resource usage stats can be displayed
* @returns {boolean}
*/
hasResourceUsageAccess() {
return this.isAdmin && this.state.useServerMetrics;
}
async onInit() { async onInit() {
this.isAdmin = this.Authentication.isAdmin();
const useServerMetrics = this.endpoint.Kubernetes.Configuration.UseServerMetrics;
this.state = { this.state = {
applicationsLoading: true, applicationsLoading: true,
viewReady: false, viewReady: false,
hasUnhealthyComponentStatus: false, hasUnhealthyComponentStatus: false,
useServerMetrics: false, useServerMetrics,
}; };
this.isAdmin = this.Authentication.isAdmin();
this.state.useServerMetrics = this.endpoint.Kubernetes.Configuration.UseServerMetrics;
await this.getNodes(); await this.getNodes();
if (this.isAdmin) { if (this.isAdmin) {
await this.getEndpoints(); await this.getEndpoints();

View File

@ -85,7 +85,7 @@
memory-usage="ctrl.resourceUsage.Memory" memory-usage="ctrl.resourceUsage.Memory"
memory-limit="ctrl.memoryLimit" memory-limit="ctrl.memoryLimit"
description="Resource reservation represents the total amount of resource assigned to all the applications running on this node." description="Resource reservation represents the total amount of resource assigned to all the applications running on this node."
display-usage="ctrl.state.isAdmin" display-usage="ctrl.hasResourceUsageAccess()"
> >
</kubernetes-resource-reservation> </kubernetes-resource-reservation>
</div> </div>

View File

@ -46,6 +46,7 @@ class KubernetesNodeController {
this.getEndpointsAsync = this.getEndpointsAsync.bind(this); this.getEndpointsAsync = this.getEndpointsAsync.bind(this);
this.updateNodeAsync = this.updateNodeAsync.bind(this); this.updateNodeAsync = this.updateNodeAsync.bind(this);
this.drainNodeAsync = this.drainNodeAsync.bind(this); this.drainNodeAsync = this.drainNodeAsync.bind(this);
this.hasResourceUsageAccess = this.hasResourceUsageAccess.bind(this);
this.getNodeUsageAsync = this.getNodeUsageAsync.bind(this); this.getNodeUsageAsync = this.getNodeUsageAsync.bind(this);
} }
@ -332,6 +333,10 @@ class KubernetesNodeController {
return this.$async(this.getNodesAsync); return this.$async(this.getNodesAsync);
} }
hasResourceUsageAccess() {
return this.state.isAdmin && this.state.useServerMetrics;
}
async getNodeUsageAsync() { async getNodeUsageAsync() {
try { try {
const nodeName = this.$transition$.params().name; const nodeName = this.$transition$.params().name;
@ -397,7 +402,7 @@ class KubernetesNodeController {
this.memoryLimit = KubernetesResourceReservationHelper.megaBytesValue(this.node.Memory); this.memoryLimit = KubernetesResourceReservationHelper.megaBytesValue(this.node.Memory);
this.state.isContainPortainer = _.find(this.applications, { ApplicationName: 'portainer' }); this.state.isContainPortainer = _.find(this.applications, { ApplicationName: 'portainer' });
if (this.state.isAdmin) { if (this.hasResourceUsageAccess()) {
await this.getNodeUsage(); await this.getNodeUsage();
} }
} catch (err) { } catch (err) {
@ -412,9 +417,11 @@ class KubernetesNodeController {
} }
async onInit() { async onInit() {
this.availabilities = KubernetesNodeAvailabilities;
this.state = { this.state = {
isAdmin: this.Authentication.isAdmin(), isAdmin: this.Authentication.isAdmin(),
activeTab: 0, activeTab: this.LocalStorage.getActiveTab('node'),
currentName: this.$state.$current.name, currentName: this.$state.$current.name,
dataLoading: true, dataLoading: true,
eventsLoading: true, eventsLoading: true,
@ -428,15 +435,9 @@ class KubernetesNodeController {
hasDuplicateLabelKeys: false, hasDuplicateLabelKeys: false,
isDrainOperation: false, isDrainOperation: false,
isContainPortainer: false, isContainPortainer: false,
useServerMetrics: false, useServerMetrics: this.endpoint.Kubernetes.Configuration.UseServerMetrics,
}; };
this.availabilities = KubernetesNodeAvailabilities;
this.state.useServerMetrics = this.endpoint.Kubernetes.Configuration.UseServerMetrics;
this.state.activeTab = this.LocalStorage.getActiveTab('node');
await this.getNodes(); await this.getNodes();
await this.getEvents(); await this.getEvents();
await this.getApplications(); await this.getApplications();

View File

@ -244,7 +244,9 @@ class KubernetesResourcePoolController {
return app; return app;
}); });
await this.getResourceUsage(this.pool.Namespace.Name); if (this.state.useServerMetrics) {
await this.getResourceUsage(this.pool.Namespace.Name);
}
} catch (err) { } catch (err) {
this.Notifications.error('Failure', err, 'Unable to retrieve applications.'); this.Notifications.error('Failure', err, 'Unable to retrieve applications.');
} finally { } finally {