Просмотр исходного кода

Merge branch 'master' of github.com:kubecost/cost-model into AjayTripathy-pricesplit

AjayTripathy 6 лет назад
Родитель
Сommit
d310dd02e4
4 измененных файлов с 40 добавлено и 8 удалено
  1. 1 1
      cloud/gcpprovider.go
  2. 1 0
      cloud/provider.go
  3. 5 1
      costmodel/promparsers.go
  4. 33 6
      costmodel/router.go

+ 1 - 1
cloud/gcpprovider.go

@@ -279,7 +279,7 @@ func (gcp *GCP) ClusterInfo() (map[string]string, error) {
 
 	attribute, err := metadataClient.InstanceAttributeValue("cluster-name")
 	if err != nil {
-		return nil, err
+		klog.Infof("Error loading metadata cluster-name: %s", err.Error())
 	}
 
 	c, err := gcp.GetConfig()

+ 1 - 0
cloud/provider.go

@@ -154,6 +154,7 @@ type CustomPricing struct {
 	SharedCosts           map[string]string `json:"sharedCost"`
 	ClusterName           string            `json:"clusterName"`
 	SharedNamespaces      string            `json:"sharedNamespaces"`
+	ReadOnly              string            `json:"readOnly"`
 }
 
 // Provider represents a k8s provider.

+ 5 - 1
costmodel/promparsers.go

@@ -151,7 +151,11 @@ func parseDataPoint(dataPoint interface{}, labels func() string) (*Vector, error
 		return nil, err
 	}
 
-	if math.IsNaN(v) {
+	// Test for +Inf and -Inf (sign: 0), Test for NaN
+	if math.IsInf(v, 0) {
+		klog.V(1).Infof("[Warning] Found Inf value parsing vector data point for metric: %s", labels())
+		v = 0.0
+	} else if math.IsNaN(v) {
 		klog.V(1).Infof("[Warning] Found NaN value parsing vector data point for metric: %s", labels())
 		v = 0.0
 	}

+ 33 - 6
costmodel/router.go

@@ -5,6 +5,7 @@ import (
 	"encoding/json"
 	"flag"
 	"fmt"
+	"math"
 	"net"
 	"net/http"
 	"os"
@@ -617,6 +618,8 @@ func (a *Accesses) recordPrices() {
 				podStatus[pod.Name] = pod.Status.Phase
 			}
 
+			cfg, _ := a.Cloud.GetConfig()
+
 			// Record network pricing at global scope
 			networkCosts, err := a.Cloud.NetworkPricing()
 			if err != nil {
@@ -636,12 +639,40 @@ func (a *Accesses) recordPrices() {
 
 			nodes, err := a.Model.GetNodeCost(a.Cloud)
 			for nodeName, node := range nodes {
+				// Emit costs, guarding against NaN inputs for custom pricing.
 				cpuCost, _ := strconv.ParseFloat(node.VCPUCost, 64)
+				if math.IsNaN(cpuCost) || math.IsInf(cpuCost, 0) {
+					cpuCost, _ = strconv.ParseFloat(cfg.CPU, 64)
+					if math.IsNaN(cpuCost) || math.IsInf(cpuCost, 0) {
+						cpuCost = 0
+					}
+				}
 				cpu, _ := strconv.ParseFloat(node.VCPU, 64)
+				if math.IsNaN(cpu) || math.IsInf(cpu, 0) {
+					cpu = 1 // Assume 1 CPU
+				}
 				ramCost, _ := strconv.ParseFloat(node.RAMCost, 64)
+				if math.IsNaN(ramCost) || math.IsInf(ramCost, 0) {
+					ramCost, _ = strconv.ParseFloat(cfg.RAM, 64)
+					if math.IsNaN(ramCost) || math.IsInf(ramCost, 0) {
+						ramCost = 0
+					}
+				}
 				ram, _ := strconv.ParseFloat(node.RAMBytes, 64)
+				if math.IsNaN(ram) || math.IsInf(ram, 0) {
+					ram = 0
+				}
 				gpu, _ := strconv.ParseFloat(node.GPU, 64)
+				if math.IsNaN(gpu) || math.IsInf(gpu, 0) {
+					gpu = 0
+				}
 				gpuCost, _ := strconv.ParseFloat(node.GPUCost, 64)
+				if math.IsNaN(gpuCost) || math.IsInf(gpuCost, 0) {
+					gpuCost, _ = strconv.ParseFloat(cfg.GPU, 64)
+					if math.IsNaN(gpuCost) || math.IsInf(gpuCost, 0) {
+						gpuCost = 0
+					}
+				}
 
 				totalCost := cpu*cpuCost + ramCost*(ram/1024/1024/1024) + gpu*gpuCost
 
@@ -837,8 +868,9 @@ func Initialize() {
 	configs, err := kubeClientset.CoreV1().ConfigMaps(kubecostNamespace).Get("pricing-configs", metav1.GetOptions{})
 	if err != nil {
 		klog.Infof("ERROR FETCHING configmap: %s", err.Error())
+	} else {
+		watchConfigFunc(configs)
 	}
-	watchConfigFunc(configs)
 
 	k8sCache.SetConfigMapUpdateFunc(watchConfigFunc)
 
@@ -1008,12 +1040,7 @@ func Initialize() {
 	Router.GET("/costDataModelRangeLarge", A.CostDataModelRangeLarge)
 	Router.GET("/outOfClusterCosts", A.OutOfClusterCostsWithCache)
 	Router.GET("/allNodePricing", A.GetAllNodePricing)
-	Router.GET("/getConfigs", A.GetConfigs)
 	Router.POST("/refreshPricing", A.RefreshPricingData)
-	Router.POST("/updateSpotInfoConfigs", A.UpdateSpotInfoConfigs)
-	Router.POST("/updateAthenaInfoConfigs", A.UpdateAthenaInfoConfigs)
-	Router.POST("/updateBigQueryInfoConfigs", A.UpdateBigQueryInfoConfigs)
-	Router.POST("/updateConfigByKey", A.UpdateConfigByKey)
 	Router.GET("/clusterCostsOverTime", A.ClusterCostsOverTime)
 	Router.GET("/clusterCosts", A.ClusterCosts)
 	Router.GET("/validatePrometheus", A.GetPrometheusMetadata)