From f14928b0a9dd3d85664605f4f6a206236ea94614 Mon Sep 17 00:00:00 2001 From: Utku Ozdemir Date: Thu, 13 Apr 2023 16:46:23 +0200 Subject: [PATCH] fix: fix dashboard crash when a non-existent node is specified Prevent dashboard from crashing when a dead/non-existent node is specified on `talosctl --nodes`. Signed-off-by: Utku Ozdemir --- internal/pkg/dashboard/components/header.go | 25 ++++++++++++--------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/internal/pkg/dashboard/components/header.go b/internal/pkg/dashboard/components/header.go index 375e6a8f93..f4cc857be5 100644 --- a/internal/pkg/dashboard/components/header.go +++ b/internal/pkg/dashboard/components/header.go @@ -122,38 +122,43 @@ func (widget *Header) redraw() { } func (widget *Header) updateNodeAPIData(node string, data *apidata.Node) { - sss := widget.getOrCreateNodeData(node) + nodeData := widget.getOrCreateNodeData(node) if data == nil { return } - sss.cpuUsagePercent = fmt.Sprintf("%.1f%%", data.CPUUsageByName("usage")*100.0) - sss.memUsagePercent = fmt.Sprintf("%.1f%%", data.MemUsage()*100.0) + nodeData.cpuUsagePercent = fmt.Sprintf("%.1f%%", data.CPUUsageByName("usage")*100.0) + nodeData.memUsagePercent = fmt.Sprintf("%.1f%%", data.MemUsage()*100.0) if data.Hostname != nil { - sss.hostname = data.Hostname.GetHostname() + nodeData.hostname = data.Hostname.GetHostname() } if data.Version != nil { - sss.version = data.Version.GetVersion().GetTag() + nodeData.version = data.Version.GetVersion().GetTag() } if data.SystemStat != nil { - sss.uptime = time.Since(time.Unix(int64(data.SystemStat.GetBootTime()), 0)).Round(time.Second).String() + nodeData.uptime = time.Since(time.Unix(int64(data.SystemStat.GetBootTime()), 0)).Round(time.Second).String() } if data.CPUsInfo != nil { - sss.numCPUs = fmt.Sprintf("%d", len(data.CPUsInfo.GetCpuInfo())) - sss.cpuFreq = widget.humanizeCPUFrequency(data.CPUsInfo.GetCpuInfo()[0].GetCpuMhz()) + numCPUs := len(data.CPUsInfo.GetCpuInfo()) + + nodeData.numCPUs = fmt.Sprintf("%d", numCPUs) + + if numCPUs > 0 { + nodeData.cpuFreq = widget.humanizeCPUFrequency(data.CPUsInfo.GetCpuInfo()[0].GetCpuMhz()) + } } if data.Processes != nil { - sss.numProcesses = fmt.Sprintf("%d", len(data.Processes.GetProcesses())) + nodeData.numProcesses = fmt.Sprintf("%d", len(data.Processes.GetProcesses())) } if data.Memory != nil { - sss.totalMem = humanize.IBytes(data.Memory.GetMeminfo().GetMemtotal() << 10) + nodeData.totalMem = humanize.IBytes(data.Memory.GetMeminfo().GetMemtotal() << 10) } }