Ajay Tripathy vor 5 Jahren
Ursprung
Commit
e444252722

+ 2 - 0
configs/pricing_schema_case.csv

@@ -0,0 +1,2 @@
+EndTimestamp,InstanceID,Region,AssetClass,InstanceIDField,InstanceType,MarketPriceHourly,Version
+2020-07-20,/subscriptions/123a7sd-asd-1234-578a9-123abcdef/resourceGroups/CASE_12_STAGE_TEST7/providers/Microsoft.Compute/virtualMachineScaleSets/vmss-agent-worker0-12stagetest7-ezggnore/virtualMachines/7,eastus2,node,spec.providerID,Standard_F32s_v2,0.13370357,

+ 1 - 0
pkg/cloud/provider.go

@@ -58,6 +58,7 @@ type Node struct {
 	InstanceType     string                `json:"instanceType,omitempty"`
 	Region           string                `json:"region,omitempty"`
 	Reserved         *ReservedInstanceData `json:"reserved,omitempty"`
+	ProviderID       string                `json:"providerID,omitempty"`
 }
 
 // IsSpot determines whether or not a Node uses spot by usage type

+ 1 - 0
pkg/costmodel/costmodel.go

@@ -1154,6 +1154,7 @@ func (cm *CostModel) GetNodeCost(cp costAnalyzerCloud.Provider) (map[string]*cos
 		if newCnode.Region == "" {
 			newCnode.Region = n.Labels[v1.LabelZoneRegion]
 		}
+		newCnode.ProviderID = n.Spec.ProviderID
 
 		var cpu float64
 		if newCnode.VCPU == "" {

+ 42 - 13
pkg/costmodel/router.go

@@ -50,6 +50,7 @@ var (
 	productAnalyticsEnabled         bool   = env.IsProductAnalyticsEnabled()
 	errorReportingEnabled           bool   = env.IsErrorReportingEnabled()
 	valuesReportingEnabled          bool   = env.IsValuesReportingEnabled()
+	clusterProfile                  string = env.GetClusterProfile()
 	multiclusterDBBasicAuthUsername string = env.GetMultiClusterBasicAuthUsername()
 	multiclusterDBBasicAuthPW       string = env.GetMultiClusterBasicAuthPassword()
 )
@@ -625,6 +626,13 @@ func (p *Accesses) ClusterInfo(w http.ResponseWriter, r *http.Request, ps httpro
 		klog.Infof("Could not get k8s version info: %s", err.Error())
 	}
 
+	// Ensure we create the info object if it doesn't exist
+	if data == nil {
+		data = make(map[string]string)
+	}
+
+	data["clusterProfile"] = clusterProfile
+
 	// Include Product Reporting Flags with Cluster Info
 	writeReportingFlags(data)
 
@@ -732,11 +740,11 @@ func (a *Accesses) recordPrices() {
 
 				totalCost := cpu*cpuCost + ramCost*(ram/1024/1024/1024) + gpu*gpuCost
 
-				a.CPUPriceRecorder.WithLabelValues(nodeName, nodeName, nodeType, nodeRegion).Set(cpuCost)
-				a.RAMPriceRecorder.WithLabelValues(nodeName, nodeName, nodeType, nodeRegion).Set(ramCost)
-				a.GPUPriceRecorder.WithLabelValues(nodeName, nodeName, nodeType, nodeRegion).Set(gpuCost)
-				a.NodeTotalPriceRecorder.WithLabelValues(nodeName, nodeName, nodeType, nodeRegion).Set(totalCost)
-				labelKey := getKeyFromLabelStrings(nodeName, nodeName)
+				a.CPUPriceRecorder.WithLabelValues(nodeName, nodeName, nodeType, nodeRegion, node.ProviderID).Set(cpuCost)
+				a.RAMPriceRecorder.WithLabelValues(nodeName, nodeName, nodeType, nodeRegion, node.ProviderID).Set(ramCost)
+				a.GPUPriceRecorder.WithLabelValues(nodeName, nodeName, nodeType, nodeRegion, node.ProviderID).Set(gpuCost)
+				a.NodeTotalPriceRecorder.WithLabelValues(nodeName, nodeName, nodeType, nodeRegion, node.ProviderID).Set(totalCost)
+				labelKey := getKeyFromLabelStrings(nodeName, nodeName, nodeType, nodeRegion, node.ProviderID)
 				nodeSeen[labelKey] = true
 			}
 
@@ -811,11 +819,32 @@ func (a *Accesses) recordPrices() {
 			}
 			for labelString, seen := range nodeSeen {
 				if !seen {
+					klog.Infof("Removing %s from nodes", labelString)
 					labels := getLabelStringsFromKey(labelString)
-					a.NodeTotalPriceRecorder.DeleteLabelValues(labels...)
-					a.CPUPriceRecorder.DeleteLabelValues(labels...)
-					a.GPUPriceRecorder.DeleteLabelValues(labels...)
-					a.RAMPriceRecorder.DeleteLabelValues(labels...)
+					ok := a.NodeTotalPriceRecorder.DeleteLabelValues(labels...)
+					if ok {
+						klog.Infof("removed %s from totalprice", labelString)
+					} else {
+						klog.Infof("FAILURE TO REMOVE %s from totalprice", labelString)
+					}
+					ok = a.CPUPriceRecorder.DeleteLabelValues(labels...)
+					if ok {
+						klog.Infof("removed %s from cpuprice", labelString)
+					} else {
+						klog.Infof("FAILURE TO REMOVE %s from cpuprice", labelString)
+					}
+					ok = a.GPUPriceRecorder.DeleteLabelValues(labels...)
+					if ok {
+						klog.Infof("removed %s from gpuprice", labelString)
+					} else {
+						klog.Infof("FAILURE TO REMOVE %s from gpuprice", labelString)
+					}
+					ok = a.RAMPriceRecorder.DeleteLabelValues(labels...)
+					if ok {
+						klog.Infof("removed %s from ramprice", labelString)
+					} else {
+						klog.Infof("FAILURE TO REMOVE %s from ramprice", labelString)
+					}
 					delete(nodeSeen, labelString)
 				}
 				nodeSeen[labelString] = false
@@ -1040,22 +1069,22 @@ func Initialize(additionalConfigWatchers ...ConfigWatchers) {
 	cpuGv := prometheus.NewGaugeVec(prometheus.GaugeOpts{
 		Name: "node_cpu_hourly_cost",
 		Help: "node_cpu_hourly_cost hourly cost for each cpu on this node",
-	}, []string{"instance", "node", "instance_type", "region"})
+	}, []string{"instance", "node", "instance_type", "region", "provider_id"})
 
 	ramGv := prometheus.NewGaugeVec(prometheus.GaugeOpts{
 		Name: "node_ram_hourly_cost",
 		Help: "node_ram_hourly_cost hourly cost for each gb of ram on this node",
-	}, []string{"instance", "node", "instance_type", "region"})
+	}, []string{"instance", "node", "instance_type", "region", "provider_id"})
 
 	gpuGv := prometheus.NewGaugeVec(prometheus.GaugeOpts{
 		Name: "node_gpu_hourly_cost",
 		Help: "node_gpu_hourly_cost hourly cost for each gpu on this node",
-	}, []string{"instance", "node", "instance_type", "region"})
+	}, []string{"instance", "node", "instance_type", "region", "provider_id"})
 
 	totalGv := prometheus.NewGaugeVec(prometheus.GaugeOpts{
 		Name: "node_total_hourly_cost",
 		Help: "node_total_hourly_cost Total node cost per hour",
-	}, []string{"instance", "node", "instance_type", "region"})
+	}, []string{"instance", "node", "instance_type", "region", "provider_id"})
 
 	pvGv := prometheus.NewGaugeVec(prometheus.GaugeOpts{
 		Name: "pv_hourly_cost",

+ 7 - 0
pkg/env/costmodelenv.go

@@ -7,6 +7,7 @@ const (
 
 	KubecostNamespaceEnvVar        = "KUBECOST_NAMESPACE"
 	ClusterIDEnvVar                = "CLUSTER_ID"
+	ClusterProfileEnvVar           = "CLUSTER_PROFILE"
 	PrometheusServerEndpointEnvVar = "PROMETHEUS_SERVER_ENDPOINT"
 	MaxQueryConcurrencyEnvVar      = "MAX_QUERY_CONCURRENCY"
 	RemoteEnabledEnvVar            = "REMOTE_WRITE_ENABLED"
@@ -55,6 +56,12 @@ func GetKubecostNamespace() string {
 	return Get(KubecostNamespaceEnvVar, "kubecost")
 }
 
+// GetClusterProfile returns the environment variable value for ClusterProfileEnvVar which
+// represents the cluster profile configured for
+func GetClusterProfile() string {
+	return Get(ClusterProfileEnvVar, "development")
+}
+
 // GetClusterID returns the environment variable value for ClusterIDEnvVar which represents the
 // configurable identifier used for multi-cluster metric emission.
 func GetClusterID() string {

+ 7 - 0
test/cloud_test.go

@@ -231,10 +231,17 @@ func TestNodePriceFromCSVWithRegion(t *testing.T) {
 
 func TestNodePriceFromCSVWithCase(t *testing.T) {
 	n := &v1.Node{}
+<<<<<<< HEAD
 	n.Spec.ProviderID = "azure:///subscriptions/75d6a706-70ad-4689-9ca4-4659a44f0ba8/resourceGroups/ethos_11_stage_va7/providers/Microsoft.Compute/virtualMachineScaleSets/vmss-agent-worker0-11stageva7-ezpwh/virtualMachines/7"
 	n.Labels = make(map[string]string)
 	n.Labels[v1.LabelZoneRegion] = "eastus2"
 	wantPrice := "0.443952693121295"
+=======
+	n.Spec.ProviderID = "azure:///subscriptions/123a7sd-asd-1234-578a9-123abcdef/resourceGroups/case_12_STaGe_TeSt7/providers/Microsoft.Compute/virtualMachineScaleSets/vmss-agent-worker0-12stagetest7-ezggnore/virtualMachines/7"
+	n.Labels = make(map[string]string)
+	n.Labels[v1.LabelZoneRegion] = "eastus2"
+	wantPrice := "0.13370357"
+>>>>>>> 2ceed3650993693345cd3222ceab14547daebb4b
 
 	c := &cloud.CSVProvider{
 		CSVLocation: "../configs/pricing_schema_case.csv",