Przeglądaj źródła

revert provider.go

AjayTripathy 6 lat temu
rodzic
commit
7210305792
2 zmienionych plików z 57 dodań i 7 usunięć
  1. 2 2
      pkg/cloud/awsprovider.go
  2. 55 5
      pkg/cloud/provider.go

+ 2 - 2
pkg/cloud/awsprovider.go

@@ -1813,7 +1813,7 @@ func parseSpotData(bucket string, prefix string, projectID string, region string
 		return nil, err
 	}
 	lsoLen := len(lso.Contents)
-	klog.V(2).Infof("Found %d spot data files from yesterday", lsoLen)
+	klog.V(2).Infof("Found %d spot data files from yesterday in \"s3://%s/%s\"", lsoLen, *ls.Bucket, *ls.Prefix)
 	if lsoLen == 0 {
 		klog.V(5).Infof("ListObjects \"s3://%s/%s\" produced no keys", *ls.Bucket, *ls.Prefix)
 	}
@@ -1822,7 +1822,7 @@ func parseSpotData(bucket string, prefix string, projectID string, region string
 		return nil, err
 	}
 	lso2Len := len(lso2.Contents)
-	klog.V(2).Infof("Found %d spot data files from today", lso2Len)
+	klog.V(2).Infof("Found %d spot data files from today in \"s3://%s/%s\"", lso2Len, *ls.Bucket, *ls.Prefix)
 	if lso2Len == 0 {
 		klog.V(5).Infof("ListObjects \"s3://%s/%s\" produced no keys", *ls2.Bucket, *ls2.Prefix)
 	}

+ 55 - 5
pkg/cloud/provider.go

@@ -2,6 +2,7 @@ package cloud
 
 import (
 	"database/sql"
+	"errors"
 	"fmt"
 	"io"
 	"os"
@@ -9,6 +10,7 @@ import (
 
 	"k8s.io/klog"
 
+	"cloud.google.com/go/compute/metadata"
 	"github.com/kubecost/cost-model/pkg/clustercache"
 
 	v1 "k8s.io/api/core/v1"
@@ -233,12 +235,60 @@ func NewProvider(cache clustercache.ClusterCache, apiKey string) (Provider, erro
 		return nil, fmt.Errorf("Could not locate any nodes for cluster.")
 	}
 
-	klog.V(2).Info("Unsupported provider, falling back to default")
-	return &CustomProvider{
-		Clientset: cache,
-		Config:    NewProviderConfig("default.json"),
-	}, nil
+	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
+	}
 
+	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 {