瀏覽代碼

support aws transformation natively

AjayTripathy 6 年之前
父節點
當前提交
345dfb1e6d
共有 3 個文件被更改,包括 22 次插入55 次删除
  1. 7 0
      pkg/cloud/csvprovider.go
  2. 5 55
      pkg/cloud/provider.go
  3. 10 0
      test/cloud_test.go

+ 7 - 0
pkg/cloud/csvprovider.go

@@ -5,6 +5,7 @@ import (
 	"fmt"
 	"io"
 	"os"
+	"regexp"
 	"strings"
 	"sync"
 	"time"
@@ -165,6 +166,12 @@ func (c *CSVProvider) NodePricing(key Key) (*Node, error) {
 func NodeValueFromMapField(m string, n *v1.Node) string {
 	mf := strings.Split(m, ".")
 	if len(mf) == 2 && mf[0] == "spec" && mf[1] == "providerID" {
+		provIdRx := regexp.MustCompile("aws:///([^/]+)/([^/]+)") // It's of the form aws:///us-east-2a/i-0fea4fd46592d050b and we want i-0fea4fd46592d050b, if it exists
+		for matchNum, group := range provIdRx.FindStringSubmatch(n.Spec.ProviderID) {
+			if matchNum == 2 {
+				return group
+			}
+		}
 		return n.Spec.ProviderID
 	} else if len(mf) > 1 && mf[0] == "metadata" {
 		if mf[1] == "name" {

+ 5 - 55
pkg/cloud/provider.go

@@ -2,7 +2,6 @@ package cloud
 
 import (
 	"database/sql"
-	"errors"
 	"fmt"
 	"io"
 	"os"
@@ -10,7 +9,6 @@ import (
 
 	"k8s.io/klog"
 
-	"cloud.google.com/go/compute/metadata"
 	"github.com/kubecost/cost-model/pkg/clustercache"
 
 	v1 "k8s.io/api/core/v1"
@@ -235,60 +233,12 @@ func NewProvider(cache clustercache.ClusterCache, apiKey string) (Provider, erro
 		return nil, fmt.Errorf("Could not locate any nodes for cluster.")
 	}
 
-	provider := strings.ToLower(nodes[0].Spec.ProviderID)
-
-	if os.Getenv("USE_CSV_PROVIDER") == "true" {
-		klog.Infof("Using CSV Provider with CSV at %s", os.Getenv("CSV_PATH"))
-		configFileName := ""
-		if metadata.OnGCE() {
-			configFileName = "gcp.json"
-		} else if strings.HasPrefix(provider, "aws") {
-			configFileName = "aws.json"
-		} else if strings.HasPrefix(provider, "azure") {
-			configFileName = "azure.json"
-
-		} else {
-			configFileName = "default.json"
-		}
-		return &CSVProvider{
-			CSVLocation: os.Getenv("CSV_PATH"),
-			CustomProvider: &CustomProvider{
-				Clientset: cache,
-				Config:    NewProviderConfig(configFileName),
-			},
-		}, nil
-	}
-	if metadata.OnGCE() {
-		klog.V(3).Info("metadata reports we are in GCE")
-		if apiKey == "" {
-			return nil, errors.New("Supply a GCP Key to start getting data")
-		}
-		return &GCP{
-			Clientset: cache,
-			APIKey:    apiKey,
-			Config:    NewProviderConfig("gcp.json"),
-		}, nil
-	}
+	klog.V(2).Info("Unsupported provider, falling back to default")
+	return &CustomProvider{
+		Clientset: cache,
+		Config:    NewProviderConfig("default.json"),
+	}, nil
 
-	if strings.HasPrefix(provider, "aws") {
-		klog.V(2).Info("Found ProviderID starting with \"aws\", using AWS Provider")
-		return &AWS{
-			Clientset: cache,
-			Config:    NewProviderConfig("aws.json"),
-		}, nil
-	} else if strings.HasPrefix(provider, "azure") {
-		klog.V(2).Info("Found ProviderID starting with \"azure\", using Azure Provider")
-		return &Azure{
-			Clientset: cache,
-			Config:    NewProviderConfig("azure.json"),
-		}, nil
-	} else {
-		klog.V(2).Info("Unsupported provider, falling back to default")
-		return &CustomProvider{
-			Clientset: cache,
-			Config:    NewProviderConfig("default.json"),
-		}, nil
-	}
 }
 
 func UpdateClusterMeta(cluster_id, cluster_name string) error {

+ 10 - 0
test/cloud_test.go

@@ -11,6 +11,16 @@ const(
 	nameMap = "metadata.name"
 	labelMapFoo = "metadata.labels.foo"
 )
+func TestTransformedValueFromMapField(t *testing.T) {
+	providerIDWant := "i-05445591e0d182d42"
+	n := &v1.Node{}
+	n.Spec.ProviderID = "aws:///us-east-1a/i-05445591e0d182d42"
+	got := cloud.NodeValueFromMapField(providerIDMap, n)
+	if got != providerIDWant {
+		t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant, got)
+	}
+}
+
 func TestNodeValueFromMapField(t *testing.T) {
 	providerIDWant := "providerid"
 	nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"