فهرست منبع

add clusterAccountID config

Signed-off-by: Sean Holcomb <seanholcomb@gmail.com>
Sean Holcomb 3 سال پیش
والد
کامیت
fc8dbd7325
6فایلهای تغییر یافته به همراه63 افزوده شده و 37 حذف شده
  1. 27 24
      pkg/cloud/awsprovider.go
  2. 2 2
      pkg/cloud/azureprovider.go
  3. 4 0
      pkg/cloud/customprovider.go
  4. 4 2
      pkg/cloud/gcpprovider.go
  5. 22 9
      pkg/cloud/provider.go
  6. 4 0
      pkg/cloud/scalewayprovider.go

+ 27 - 24
pkg/cloud/awsprovider.go

@@ -179,8 +179,8 @@ type AWS struct {
 	Config                      *ProviderConfig
 	serviceAccountChecks        *ServiceAccountChecks
 	clusterManagementPrice      float64
-	clusterAccountId            string
 	clusterRegion               string
+	clusterAccountID            string
 	clusterProvisioner          string
 	*CustomProvider
 }
@@ -1342,38 +1342,41 @@ func (aws *AWS) NodePricing(k Key) (*Node, error) {
 
 // ClusterInfo returns an object that represents the cluster. TODO: actually return the name of the cluster. Blocked on cluster federation.
 func (awsProvider *AWS) ClusterInfo() (map[string]string, error) {
-	defaultClusterName := "AWS Cluster #1"
+
 	c, err := awsProvider.GetConfig()
 	if err != nil {
 		return nil, err
 	}
 
-	remoteEnabled := env.IsRemoteEnabled()
-
-	makeStructure := func(clusterName string) (map[string]string, error) {
-		m := make(map[string]string)
-		m["name"] = clusterName
-		m["provider"] = kubecost.AWSProvider
-		m["account"] = c.AthenaProjectID // this value requires configuration but is unavailable else where
-		m["region"] = awsProvider.clusterRegion
-		m["id"] = env.GetClusterID()
-		m["remoteReadEnabled"] = strconv.FormatBool(remoteEnabled)
-		m["provisioner"] = awsProvider.clusterProvisioner
-		return m, nil
-	}
-
-	if c.ClusterName != "" {
-		return makeStructure(c.ClusterName)
+	// Determine cluster name
+	clusterName := c.ClusterName
+	if clusterName == "" {
+		awsClusterID := env.GetAWSClusterID()
+		if awsClusterID != "" {
+			log.Infof("Returning \"%s\" as ClusterName", awsClusterID)
+			clusterName = awsClusterID
+		} else {
+			log.Infof("Unable to sniff out cluster ID, perhaps set $%s to force one", env.AWSClusterIDEnvVar)
+			clusterName = "AWS Cluster #1"
+		}
 	}
 
-	maybeClusterId := env.GetAWSClusterID()
-	if len(maybeClusterId) != 0 {
-		log.Infof("Returning \"%s\" as ClusterName", maybeClusterId)
-		return makeStructure(maybeClusterId)
+	// this value requires configuration but is unavailable else where
+	clusterAccountID := c.ClusterAccountID
+	// Use AthenaProjectID if Cluster Account is not set to support older configs
+	if clusterAccountID == "" {
+		clusterAccountID = c.AthenaProjectID
 	}
 
-	log.Infof("Unable to sniff out cluster ID, perhaps set $%s to force one", env.AWSClusterIDEnvVar)
-	return makeStructure(defaultClusterName)
+	m := make(map[string]string)
+	m["name"] = clusterName
+	m["provider"] = kubecost.AWSProvider
+	m["account"] = clusterAccountID
+	m["region"] = awsProvider.clusterRegion
+	m["id"] = env.GetClusterID()
+	m["remoteReadEnabled"] = strconv.FormatBool(env.IsRemoteEnabled())
+	m["provisioner"] = awsProvider.clusterProvisioner
+	return m, nil
 }
 
 // updates the authentication to the latest values (via config or secret)

+ 2 - 2
pkg/cloud/azureprovider.go

@@ -396,7 +396,7 @@ type Azure struct {
 	Config                         *ProviderConfig
 	serviceAccountChecks           *ServiceAccountChecks
 	RateCardPricingError           error
-	clusterAccountId               string
+	clusterAccountID               string
 	clusterRegion                  string
 	loadedAzureSecret              bool
 	azureSecret                    *AzureServiceKey
@@ -1345,7 +1345,7 @@ func (az *Azure) ClusterInfo() (map[string]string, error) {
 		m["name"] = c.ClusterName
 	}
 	m["provider"] = kubecost.AzureProvider
-	m["account"] = az.clusterAccountId
+	m["account"] = az.clusterAccountID
 	m["region"] = az.clusterRegion
 	m["remoteReadEnabled"] = strconv.FormatBool(remoteEnabled)
 	m["id"] = env.GetClusterID()

+ 4 - 0
pkg/cloud/customprovider.go

@@ -30,6 +30,8 @@ type CustomProvider struct {
 	SpotLabelValue          string
 	GPULabel                string
 	GPULabelValue           string
+	clusterRegion           string
+	clusterAccountID        string
 	DownloadPricingDataLock sync.RWMutex
 	Config                  *ProviderConfig
 }
@@ -117,6 +119,8 @@ func (cp *CustomProvider) ClusterInfo() (map[string]string, error) {
 		m["name"] = conf.ClusterName
 	}
 	m["provider"] = kubecost.CustomProvider
+	m["region"] = cp.clusterRegion
+	m["account"] = cp.clusterAccountID
 	m["id"] = env.GetClusterID()
 	return m, nil
 }

+ 4 - 2
pkg/cloud/gcpprovider.go

@@ -102,8 +102,9 @@ type GCP struct {
 	ValidPricingKeys        map[string]bool
 	metadataClient          *metadata.Client
 	clusterManagementPrice  float64
-	clusterProjectId        string
 	clusterRegion           string
+	clusterAccountID        string
+	clusterProjectID        string
 	clusterProvisioner      string
 	*CustomProvider
 }
@@ -333,8 +334,9 @@ func (gcp *GCP) ClusterInfo() (map[string]string, error) {
 	m := make(map[string]string)
 	m["name"] = attribute
 	m["provider"] = kubecost.GCPProvider
-	m["project"] = gcp.clusterProjectId
 	m["region"] = gcp.clusterRegion
+	m["account"] = gcp.clusterAccountID
+	m["project"] = gcp.clusterProjectID
 	m["provisioner"] = gcp.clusterProvisioner
 	m["id"] = env.GetClusterID()
 	m["remoteReadEnabled"] = strconv.FormatBool(remoteEnabled)

+ 22 - 9
pkg/cloud/provider.go

@@ -221,6 +221,7 @@ type CustomPricing struct {
 	NegotiatedDiscount           string `json:"negotiatedDiscount"`
 	SharedOverhead               string `json:"sharedOverhead"`
 	ClusterName                  string `json:"clusterName"`
+	ClusterAccountID             string `json:"clusterAccount,omitempty"`
 	SharedNamespaces             string `json:"sharedNamespaces"`
 	SharedLabelNames             string `json:"sharedLabelNames"`
 	SharedLabelValues            string `json:"sharedLabelValues"`
@@ -475,6 +476,11 @@ func NewProvider(cache clustercache.ClusterCache, apiKey string, config *config.
 	}
 
 	cp := getClusterProperties(nodes[0])
+	providerConfig := NewProviderConfig(config, cp.configFileName)
+	// If ClusterAccount is set apply it to the cluster properties
+	if providerConfig.customPricing != nil && providerConfig.customPricing.ClusterAccountID != "" {
+		cp.accountID = providerConfig.customPricing.ClusterAccountID
+	}
 
 	switch cp.provider {
 	case kubecost.CSVProvider:
@@ -482,8 +488,10 @@ func NewProvider(cache clustercache.ClusterCache, apiKey string, config *config.
 		return &CSVProvider{
 			CSVLocation: env.GetCSVPath(),
 			CustomProvider: &CustomProvider{
-				Clientset: cache,
-				Config:    NewProviderConfig(config, cp.configFileName),
+				Clientset:        cache,
+				clusterRegion:    cp.region,
+				clusterAccountID: cp.accountID,
+				Config:           NewProviderConfig(config, cp.configFileName),
 			},
 		}, nil
 	case kubecost.GCPProvider:
@@ -496,7 +504,8 @@ func NewProvider(cache clustercache.ClusterCache, apiKey string, config *config.
 			APIKey:           apiKey,
 			Config:           NewProviderConfig(config, cp.configFileName),
 			clusterRegion:    cp.region,
-			clusterProjectId: cp.projectID,
+			clusterAccountID: cp.accountID,
+			clusterProjectID: cp.projectID,
 			metadataClient: metadata.NewClient(&http.Client{
 				Transport: httputil.NewUserAgentTransport("kubecost", http.DefaultTransport),
 			}),
@@ -507,7 +516,7 @@ func NewProvider(cache clustercache.ClusterCache, apiKey string, config *config.
 			Clientset:            cache,
 			Config:               NewProviderConfig(config, cp.configFileName),
 			clusterRegion:        cp.region,
-			clusterAccountId:     cp.accountID,
+			clusterAccountID:     cp.accountID,
 			serviceAccountChecks: NewServiceAccountChecks(),
 		}, nil
 	case kubecost.AzureProvider:
@@ -516,7 +525,7 @@ func NewProvider(cache clustercache.ClusterCache, apiKey string, config *config.
 			Clientset:            cache,
 			Config:               NewProviderConfig(config, cp.configFileName),
 			clusterRegion:        cp.region,
-			clusterAccountId:     cp.accountID,
+			clusterAccountID:     cp.accountID,
 			serviceAccountChecks: NewServiceAccountChecks(),
 		}, nil
 	case kubecost.AlibabaProvider:
@@ -531,15 +540,19 @@ func NewProvider(cache clustercache.ClusterCache, apiKey string, config *config.
 	case kubecost.ScalewayProvider:
 		log.Info("Found ProviderID starting with \"scaleway\", using Scaleway Provider")
 		return &Scaleway{
-			Clientset: cache,
-			Config:    NewProviderConfig(config, cp.configFileName),
+			Clientset:        cache,
+			clusterRegion:    cp.region,
+			clusterAccountID: cp.accountID,
+			Config:           NewProviderConfig(config, cp.configFileName),
 		}, nil
 
 	default:
 		log.Info("Unsupported provider, falling back to default")
 		return &CustomProvider{
-			Clientset: cache,
-			Config:    NewProviderConfig(config, cp.configFileName),
+			Clientset:        cache,
+			clusterRegion:    cp.region,
+			clusterAccountID: cp.accountID,
+			Config:           NewProviderConfig(config, cp.configFileName),
 		}, nil
 	}
 }

+ 4 - 0
pkg/cloud/scalewayprovider.go

@@ -36,6 +36,8 @@ type Scaleway struct {
 	Clientset               clustercache.ClusterCache
 	Config                  *ProviderConfig
 	Pricing                 map[string]*ScalewayPricing
+	clusterRegion           string
+	clusterAccountID        string
 	DownloadPricingDataLock sync.RWMutex
 }
 
@@ -285,6 +287,8 @@ func (scw *Scaleway) ClusterInfo() (map[string]string, error) {
 		m["name"] = c.ClusterName
 	}
 	m["provider"] = kubecost.ScalewayProvider
+	m["region"] = scw.clusterRegion
+	m["account"] = scw.clusterAccountID
 	m["remoteReadEnabled"] = strconv.FormatBool(remoteEnabled)
 	m["id"] = env.GetClusterID()
 	return m, nil