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"
memory-reservation="ctrl.resourceReservation.Memory"
memory-limit="ctrl.MemoryLimit"
display-usage="ctrl.isAdmin"
display-usage="ctrl.hasResourceUsageAccess()"
cpu-usage="ctrl.resourceUsage.CPU"
memory-usage="ctrl.resourceUsage.Memory"
>

View File

@ -36,6 +36,7 @@ class KubernetesClusterController {
this.getComponentStatus = this.getComponentStatus.bind(this);
this.getComponentStatusAsync = this.getComponentStatusAsync.bind(this);
this.getEndpointsAsync = this.getEndpointsAsync.bind(this);
this.hasResourceUsageAccess = this.hasResourceUsageAccess.bind(this);
}
async getComponentStatusAsync() {
@ -107,7 +108,7 @@ class KubernetesClusterController {
);
this.resourceReservation.Memory = KubernetesResourceReservationHelper.megaBytesValue(this.resourceReservation.Memory);
if (this.isAdmin) {
if (this.hasResourceUsageAccess()) {
await this.getResourceUsage(this.endpoint.Id);
}
} 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() {
this.isAdmin = this.Authentication.isAdmin();
const useServerMetrics = this.endpoint.Kubernetes.Configuration.UseServerMetrics;
this.state = {
applicationsLoading: true,
viewReady: false,
hasUnhealthyComponentStatus: false,
useServerMetrics: false,
useServerMetrics,
};
this.isAdmin = this.Authentication.isAdmin();
this.state.useServerMetrics = this.endpoint.Kubernetes.Configuration.UseServerMetrics;
await this.getNodes();
if (this.isAdmin) {
await this.getEndpoints();

View File

@ -85,7 +85,7 @@
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"
display-usage="ctrl.hasResourceUsageAccess()"
>
</kubernetes-resource-reservation>
</div>

View File

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

View File

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