Преглед изворни кода

feat(pricing): Add a pricingSourceSummary endpoint

It's helpful to be able to debug OpenCost by viewing what the current
pricing schema looks like. This change adds a `/pricingSourceSummary`
api endpoint that, if implemented, will write out in json the providers
`Pricing` object.

Signed-off-by: pokom <mark.poko@grafana.com>
pokom пре 3 година
родитељ
комит
fe0bccde09

+ 4 - 0
pkg/cloud/aliyunprovider.go

@@ -1331,3 +1331,7 @@ func determinePVRegion(pv *v1.PersistentVolume) string {
 	}
 	return ""
 }
+
+func (a *Alibaba) PricingSourceSummary() (interface{}, error) {
+	return a.Pricing, nil
+}

+ 1 - 0
pkg/cloud/awsprovider.go

@@ -2300,6 +2300,7 @@ func (aws *AWS) Regions() []string {
 	return awsRegions
 }
 
+// PricingSourceSummary returns the Pricing field from the AWS struct.
 func (aws *AWS) PricingSourceSummary() (interface{}, error) {
 	// encode the pricing source summary as a JSON string
 	return aws.Pricing, nil

+ 1 - 2
pkg/cloud/azureprovider.go

@@ -405,8 +405,7 @@ type Azure struct {
 }
 
 func (az *Azure) PricingSourceSummary() (interface{}, error) {
-	//TODO implement me
-	panic("implement me")
+	return az.Pricing, nil
 }
 
 type azureKey struct {

+ 1 - 2
pkg/cloud/customprovider.go

@@ -34,8 +34,7 @@ type CustomProvider struct {
 }
 
 func (cp *CustomProvider) PricingSourceSummary() (interface{}, error) {
-	//TODO implement me
-	panic("implement me")
+	return cp.Pricing, nil
 }
 
 type customProviderKey struct {

+ 5 - 1
pkg/cloud/gcpprovider.go

@@ -28,7 +28,7 @@ import (
 	"cloud.google.com/go/bigquery"
 	"cloud.google.com/go/compute/metadata"
 	"golang.org/x/oauth2/google"
-	compute "google.golang.org/api/compute/v1"
+	"google.golang.org/api/compute/v1"
 	v1 "k8s.io/api/core/v1"
 )
 
@@ -1577,3 +1577,7 @@ func getUsageType(labels map[string]string) string {
 	}
 	return "ondemand"
 }
+
+func (gcp *GCP) PricingSourceSummary() (interface{}, error) {
+	return gcp.Pricing, nil
+}

+ 3 - 2
pkg/cloud/provider.go

@@ -479,7 +479,7 @@ func NewProvider(cache clustercache.ClusterCache, apiKey string, config *config.
 			},
 		}, nil
 	case kubecost.GCPProvider:
-		log.Info("metadata reports we are in GCE")
+		log.Info("Found ProviderID starting with \"gce\", using GCP Provider")
 		if apiKey == "" {
 			return nil, errors.New("Supply a GCP Key to start getting data")
 		}
@@ -554,7 +554,8 @@ func getClusterProperties(node *v1.Node) clusterProperties {
 		accountID:      "",
 		projectID:      "",
 	}
-	if metadata.OnGCE() {
+	// The second conditional is mainly if you're running opencost outside of GCE, say in a local environment.
+	if metadata.OnGCE() || strings.HasPrefix(providerID, "gce") {
 		cp.provider = kubecost.GCPProvider
 		cp.configFileName = "gcp.json"
 		cp.projectID = parseGCPProjectID(providerID)