Selaa lähdekoodia

Merge pull request #269 from kubecost/AjayTripathy-fix-gpujobs

fix no billing on missing container issues
Ajay Tripathy 6 vuotta sitten
vanhempi
sitoutus
e58a69b5e8
2 muutettua tiedostoa jossa 18 lisäystä ja 19 poistoa
  1. 4 4
      costmodel/costmodel.go
  2. 14 15
      costmodel/router.go

+ 4 - 4
costmodel/costmodel.go

@@ -398,7 +398,7 @@ func (cm *CostModel) ComputeCostData(cli prometheusClient.Client, clientset kube
 		return nil, fmt.Errorf("Error parsing normalization values: " + err.Error())
 		return nil, fmt.Errorf("Error parsing normalization values: " + err.Error())
 	}
 	}
 
 
-	nodes, err := getNodeCost(cm.Cache, cp)
+	nodes, err := cm.GetNodeCost(cp)
 	if err != nil {
 	if err != nil {
 		klog.V(1).Infof("Warning, no Node cost model available: " + err.Error())
 		klog.V(1).Infof("Warning, no Node cost model available: " + err.Error())
 		return nil, err
 		return nil, err
@@ -922,13 +922,13 @@ func GetPVCost(pv *costAnalyzerCloud.PV, kpv *v1.PersistentVolume, cp costAnalyz
 	return nil
 	return nil
 }
 }
 
 
-func getNodeCost(cache clustercache.ClusterCache, cp costAnalyzerCloud.Provider) (map[string]*costAnalyzerCloud.Node, error) {
+func (cm *CostModel) GetNodeCost(cp costAnalyzerCloud.Provider) (map[string]*costAnalyzerCloud.Node, error) {
 	cfg, err := cp.GetConfig()
 	cfg, err := cp.GetConfig()
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
 
 
-	nodeList := cache.GetAllNodes()
+	nodeList := cm.Cache.GetAllNodes()
 	nodes := make(map[string]*costAnalyzerCloud.Node)
 	nodes := make(map[string]*costAnalyzerCloud.Node)
 
 
 	for _, n := range nodeList {
 	for _, n := range nodeList {
@@ -1503,7 +1503,7 @@ func (cm *CostModel) costDataRange(cli prometheusClient.Client, clientset kubern
 			start, end, window, err.Error())
 			start, end, window, err.Error())
 	}
 	}
 
 
-	nodes, err := getNodeCost(cm.Cache, cp)
+	nodes, err := cm.GetNodeCost(cp)
 	if err != nil {
 	if err != nil {
 		klog.V(1).Infof("Warning, no cost model available: " + err.Error())
 		klog.V(1).Infof("Warning, no cost model available: " + err.Error())
 		return nil, err
 		return nil, err

+ 14 - 15
costmodel/router.go

@@ -632,13 +632,8 @@ func (a *Accesses) recordPrices() {
 				data = map[string]*CostData{}
 				data = map[string]*CostData{}
 			}
 			}
 
 
-			for _, costs := range data {
-				nodeName := costs.NodeName
-				node := costs.NodeData
-				if node == nil {
-					klog.V(4).Infof("Skipping Node \"%s\" due to missing Node Data costs", nodeName)
-					continue
-				}
+			nodes, err := a.Model.GetNodeCost(a.Cloud)
+			for nodeName, node := range nodes {
 				cpuCost, _ := strconv.ParseFloat(node.VCPUCost, 64)
 				cpuCost, _ := strconv.ParseFloat(node.VCPUCost, 64)
 				cpu, _ := strconv.ParseFloat(node.VCPU, 64)
 				cpu, _ := strconv.ParseFloat(node.VCPU, 64)
 				ramCost, _ := strconv.ParseFloat(node.RAMCost, 64)
 				ramCost, _ := strconv.ParseFloat(node.RAMCost, 64)
@@ -648,6 +643,17 @@ func (a *Accesses) recordPrices() {
 
 
 				totalCost := cpu*cpuCost + ramCost*(ram/1024/1024/1024) + gpu*gpuCost
 				totalCost := cpu*cpuCost + ramCost*(ram/1024/1024/1024) + gpu*gpuCost
 
 
+				a.CPUPriceRecorder.WithLabelValues(nodeName, nodeName).Set(cpuCost)
+				a.RAMPriceRecorder.WithLabelValues(nodeName, nodeName).Set(ramCost)
+				a.GPUPriceRecorder.WithLabelValues(nodeName, nodeName).Set(gpuCost)
+				a.NodeTotalPriceRecorder.WithLabelValues(nodeName, nodeName).Set(totalCost)
+				labelKey := getKeyFromLabelStrings(nodeName, nodeName)
+				nodeSeen[labelKey] = true
+			}
+
+			for _, costs := range data {
+				nodeName := costs.NodeName
+
 				namespace := costs.Namespace
 				namespace := costs.Namespace
 				podName := costs.PodName
 				podName := costs.PodName
 				containerName := costs.Name
 				containerName := costs.Name
@@ -662,13 +668,6 @@ func (a *Accesses) recordPrices() {
 					}
 					}
 				}
 				}
 
 
-				a.CPUPriceRecorder.WithLabelValues(nodeName, nodeName).Set(cpuCost)
-				a.RAMPriceRecorder.WithLabelValues(nodeName, nodeName).Set(ramCost)
-				a.GPUPriceRecorder.WithLabelValues(nodeName, nodeName).Set(gpuCost)
-				a.NodeTotalPriceRecorder.WithLabelValues(nodeName, nodeName).Set(totalCost)
-				labelKey := getKeyFromLabelStrings(nodeName, nodeName)
-				nodeSeen[labelKey] = true
-
 				if len(costs.RAMAllocation) > 0 {
 				if len(costs.RAMAllocation) > 0 {
 					a.RAMAllocationRecorder.WithLabelValues(namespace, podName, containerName, nodeName, nodeName).Set(costs.RAMAllocation[0].Value)
 					a.RAMAllocationRecorder.WithLabelValues(namespace, podName, containerName, nodeName, nodeName).Set(costs.RAMAllocation[0].Value)
 				}
 				}
@@ -679,7 +678,7 @@ func (a *Accesses) recordPrices() {
 					// allocation here is set to the request because shared GPU usage not yet supported.
 					// allocation here is set to the request because shared GPU usage not yet supported.
 					a.GPUAllocationRecorder.WithLabelValues(namespace, podName, containerName, nodeName, nodeName).Set(costs.GPUReq[0].Value)
 					a.GPUAllocationRecorder.WithLabelValues(namespace, podName, containerName, nodeName, nodeName).Set(costs.GPUReq[0].Value)
 				}
 				}
-				labelKey = getKeyFromLabelStrings(namespace, podName, containerName, nodeName, nodeName)
+				labelKey := getKeyFromLabelStrings(namespace, podName, containerName, nodeName, nodeName)
 				if podStatus[podName] == v1.PodRunning { // Only report data for current pods
 				if podStatus[podName] == v1.PodRunning { // Only report data for current pods
 					containerSeen[labelKey] = true
 					containerSeen[labelKey] = true
 				} else {
 				} else {