Browse Source

When there is no matching node in the node list. just assume no GPU cost to avoid nil panic (#2474) (#2475)

Signed-off-by: Alex Meijer <ameijer@kubecost.com>
Signed-off-by: Michael Dresser <michaelmdresser@gmail.com>
Co-authored-by: Alex Meijer <ameijer@users.noreply.github.com>
Michael Dresser 2 years ago
parent
commit
f22dcd4040
1 changed files with 11 additions and 2 deletions
  1. 11 2
      pkg/costmodel/metrics.go

+ 11 - 2
pkg/costmodel/metrics.go

@@ -619,8 +619,17 @@ func (cmme *CostModelMetricsEmitter) Start() bool {
 				if len(costs.GPUReq) > 0 {
 					// allocation here is set to the request because shared GPU usage not yet supported.
 					// if VPGUs, request x (actual/virtual)
-					vgpu, verr := strconv.ParseFloat(nodes[nodeName].VGPU, 64)
-					gpu, err := strconv.ParseFloat(nodes[nodeName].GPU, 64)
+					vgpu := 0.0
+					gpu := 0.0
+					var err, verr error
+					if matchedNode, found := nodes[nodeName]; found {
+						vgpu, verr = strconv.ParseFloat(matchedNode.VGPU, 64)
+						gpu, err = strconv.ParseFloat(matchedNode.GPU, 64)
+					} else {
+						log.Tracef("cost data for node %s had GPUReq, but there was no cost data available for the node", nodeName)
+						log.Trace("defaulting GPU to 0 cost")
+					}
+
 					gpualloc := costs.GPUReq[0].Value
 					if verr != nil && err != nil && vgpu != 0 {
 						gpualloc = gpualloc * (gpu / vgpu)