From bd5c10e907669a01e4a372f590086d174e1cf7f8 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Sun, 28 May 2023 19:56:51 -0700 Subject: [PATCH] Handle k8s widget metrics call fails --- src/pages/api/widgets/kubernetes.js | 31 ++++++++++++++++++----------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/pages/api/widgets/kubernetes.js b/src/pages/api/widgets/kubernetes.js index b0d7f5531..35202ce5d 100644 --- a/src/pages/api/widgets/kubernetes.js +++ b/src/pages/api/widgets/kubernetes.js @@ -52,18 +52,25 @@ export default async function handler(req, res) { memTotal += mem; }); - const nodeMetrics = await metricsApi.getNodeMetrics(); - nodeMetrics.items.forEach((nodeMetric) => { - const cpu = parseCpu(nodeMetric.usage.cpu); - const mem = parseMemory(nodeMetric.usage.memory); - cpuUsage += cpu; - memUsage += mem; - nodeMap[nodeMetric.metadata.name].cpu.load = cpu; - nodeMap[nodeMetric.metadata.name].cpu.percent = (cpu / nodeMap[nodeMetric.metadata.name].cpu.total) * 100; - nodeMap[nodeMetric.metadata.name].memory.used = mem; - nodeMap[nodeMetric.metadata.name].memory.free = nodeMap[nodeMetric.metadata.name].memory.total - mem; - nodeMap[nodeMetric.metadata.name].memory.percent = (mem / nodeMap[nodeMetric.metadata.name].memory.total) * 100; - }); + try { + const nodeMetrics = await metricsApi.getNodeMetrics(); + nodeMetrics.items.forEach((nodeMetric) => { + const cpu = parseCpu(nodeMetric.usage.cpu); + const mem = parseMemory(nodeMetric.usage.memory); + cpuUsage += cpu; + memUsage += mem; + nodeMap[nodeMetric.metadata.name].cpu.load = cpu; + nodeMap[nodeMetric.metadata.name].cpu.percent = (cpu / nodeMap[nodeMetric.metadata.name].cpu.total) * 100; + nodeMap[nodeMetric.metadata.name].memory.used = mem; + nodeMap[nodeMetric.metadata.name].memory.free = nodeMap[nodeMetric.metadata.name].memory.total - mem; + nodeMap[nodeMetric.metadata.name].memory.percent = (mem / nodeMap[nodeMetric.metadata.name].memory.total) * 100; + }); + } catch (error) { + logger.error("Error getting metrics, ensure you have metrics-server installed: s", JSON.stringify(error)); + return res.status(500).send({ + error: "Error getting metrics, check logs for more details" + }); + } const cluster = { cpu: {