Kaynağa Gözat

Parse ProviderIDs

Niko Kovacevic 5 yıl önce
ebeveyn
işleme
f63428f135

+ 12 - 0
pkg/cloud/awsprovider.go

@@ -2399,3 +2399,15 @@ func (a *AWS) ServiceAccountStatus() *ServiceAccountStatus {
 func (aws *AWS) CombinedDiscountForNode(instanceType string, isPreemptible bool, defaultDiscount, negotiatedDiscount float64) float64 {
 	return 1.0 - ((1.0 - defaultDiscount) * (1.0 - negotiatedDiscount))
 }
+
+func (aws *AWS) ParseID(id string) string {
+	// It's of the form aws:///us-east-2a/i-0fea4fd46592d050b and we want i-0fea4fd46592d050b, if it exists
+	rx := regexp.MustCompile("aws:///[^/]+/([^/]+)")
+	match := rx.FindStringSubmatch(id)
+	if len(match) < 2 {
+		log.Infof("awsprovider.ParseID: failed to parse %s", id)
+		return id
+	}
+
+	return match[1]
+}

+ 4 - 0
pkg/cloud/azureprovider.go

@@ -784,3 +784,7 @@ func (az *Azure) ServiceAccountStatus() *ServiceAccountStatus {
 func (az *Azure) CombinedDiscountForNode(instanceType string, isPreemptible bool, defaultDiscount, negotiatedDiscount float64) float64 {
 	return 1.0 - ((1.0 - defaultDiscount) * (1.0 - negotiatedDiscount))
 }
+
+func (az *Azure) ParseID(id string) string {
+	return id
+}

+ 4 - 0
pkg/cloud/csvprovider.go

@@ -298,3 +298,7 @@ func (c *CSVProvider) ServiceAccountStatus() *ServiceAccountStatus {
 func (c *CSVProvider) CombinedDiscountForNode(instanceType string, isPreemptible bool, defaultDiscount, negotiatedDiscount float64) float64 {
 	return 1.0 - ((1.0 - defaultDiscount) * (1.0 - negotiatedDiscount))
 }
+
+func (c *CSVProvider) ParseID(id string) string {
+	return id
+}

+ 4 - 0
pkg/cloud/customprovider.go

@@ -272,3 +272,7 @@ func (cp *CustomProvider) ServiceAccountStatus() *ServiceAccountStatus {
 func (cp *CustomProvider) CombinedDiscountForNode(instanceType string, isPreemptible bool, defaultDiscount, negotiatedDiscount float64) float64 {
 	return 1.0 - ((1.0 - defaultDiscount) * (1.0 - negotiatedDiscount))
 }
+
+func (cp *CustomProvider) ParseID(id string) string {
+	return id
+}

+ 14 - 0
pkg/cloud/gcpprovider.go

@@ -21,6 +21,7 @@ import (
 
 	"github.com/kubecost/cost-model/pkg/clustercache"
 	"github.com/kubecost/cost-model/pkg/env"
+	"github.com/kubecost/cost-model/pkg/log"
 	"github.com/kubecost/cost-model/pkg/util"
 
 	"golang.org/x/oauth2"
@@ -1401,3 +1402,16 @@ func sustainedUseDiscount(class string, defaultDiscount float64) float64 {
 	}
 	return discount
 }
+
+func (gcp *GCP) ParseID(id string) string {
+	// gce://guestbook-227502/us-central1-a/gke-niko-n1-standard-2-wljla-8df8e58a-hfy7
+	//  => gke-niko-n1-standard-2-wljla-8df8e58a-hfy7
+	rx := regexp.MustCompile("gce://[^/]+/[^/]+/([^/]+)")
+	match := rx.FindStringSubmatch(id)
+	if len(match) < 2 {
+		log.Infof("gcpprovider.ParseID: failed to parse %s", id)
+		return id
+	}
+
+	return match[1]
+}

+ 1 - 0
pkg/cloud/provider.go

@@ -189,6 +189,7 @@ type Provider interface {
 	ApplyReservedInstancePricing(map[string]*Node)
 	ServiceAccountStatus() *ServiceAccountStatus
 	CombinedDiscountForNode(string, bool, float64, float64) float64
+	ParseID(string) string
 }
 
 // ClusterName returns the name defined in cluster info, defaulting to the

+ 3 - 3
pkg/costmodel/cluster.go

@@ -340,7 +340,7 @@ func ClusterNodes(cp cloud.Provider, client prometheus.Client, duration, offset
 				Cluster:    cluster,
 				Name:       name,
 				NodeType:   nodeType,
-				ProviderID: providerID,
+				ProviderID: cp.ParseID(providerID),
 			}
 		}
 		nodeMap[key].CPUCost += cpuCost
@@ -394,7 +394,7 @@ func ClusterNodes(cp cloud.Provider, client prometheus.Client, duration, offset
 				Cluster:    cluster,
 				Name:       name,
 				NodeType:   nodeType,
-				ProviderID: providerID,
+				ProviderID: cp.ParseID(providerID),
 			}
 		}
 		nodeMap[key].RAMCost += ramCost
@@ -448,7 +448,7 @@ func ClusterNodes(cp cloud.Provider, client prometheus.Client, duration, offset
 				Cluster:    cluster,
 				Name:       name,
 				NodeType:   nodeType,
-				ProviderID: providerID,
+				ProviderID: cp.ParseID(providerID),
 			}
 		}
 		nodeMap[key].GPUCost += gpuCost