Browse Source

Get Azure billing account and offer ID from environment

They're not sensitive information so it's much simpler than passing
them in through the service key secret.

Signed-off-by: Christian Muirhead <christian.muirhead@microsoft.com>
Christian Muirhead 3 năm trước cách đây
mục cha
commit
be55226ce4
2 tập tin đã thay đổi với 32 bổ sung13 xóa
  1. 14 13
      pkg/cloud/azureprovider.go
  2. 18 0
      pkg/env/costmodelenv.go

+ 14 - 13
pkg/cloud/azureprovider.go

@@ -517,7 +517,6 @@ type AzureAppKey struct {
 // Azure service key for a specific subscription
 type AzureServiceKey struct {
 	SubscriptionID string       `json:"subscriptionId"`
-	BillingAccount string       `json:"billingAccount"`
 	ServiceKey     *AzureAppKey `json:"serviceKey"`
 }
 
@@ -531,12 +530,11 @@ func (ask *AzureServiceKey) IsValid() bool {
 }
 
 // Loads the azure authentication via configuration or a secret set at install time.
-func (az *Azure) getAzureRateCardAuth(forceReload bool, cp *CustomPricing) (subscriptionID, billingAccount, clientID, clientSecret, tenantID string) {
+func (az *Azure) getAzureRateCardAuth(forceReload bool, cp *CustomPricing) (subscriptionID, clientID, clientSecret, tenantID string) {
 	// 1. Check for secret (secret values will always be used if they are present)
 	s, _ := az.loadAzureAuthSecret(forceReload)
 	if s != nil && s.IsValid() {
 		subscriptionID = s.SubscriptionID
-		billingAccount = s.BillingAccount
 		clientID = s.ServiceKey.AppID
 		clientSecret = s.ServiceKey.Password
 		tenantID = s.ServiceKey.Tenant
@@ -545,7 +543,6 @@ func (az *Azure) getAzureRateCardAuth(forceReload bool, cp *CustomPricing) (subs
 	// 2. Check config values (set though endpoint)
 	if cp.AzureSubscriptionID != "" && cp.AzureClientID != "" && cp.AzureClientSecret != "" && cp.AzureTenantID != "" {
 		subscriptionID = cp.AzureSubscriptionID
-		billingAccount = cp.AzureBillingAccount
 		clientID = cp.AzureClientID
 		clientSecret = cp.AzureClientSecret
 		tenantID = cp.AzureTenantID
@@ -558,7 +555,7 @@ func (az *Azure) getAzureRateCardAuth(forceReload bool, cp *CustomPricing) (subs
 		return
 	}
 	// 4. Empty values
-	return "", "", "", "", ""
+	return "", "", "", ""
 
 }
 
@@ -789,10 +786,18 @@ func (az *Azure) DownloadPricingData() error {
 		return err
 	}
 
+	envBillingAccount := env.GetAzureBillingAccount()
+	if envBillingAccount != "" {
+		config.AzureBillingAccount = envBillingAccount
+	}
+	envOfferID := env.GetAzureOfferID()
+	if envOfferID != "" {
+		config.AzureOfferDurableID = envOfferID
+	}
+
 	// Load the service provider keys
-	subscriptionID, billingAccount, clientID, clientSecret, tenantID := az.getAzureRateCardAuth(false, config)
+	subscriptionID, clientID, clientSecret, tenantID := az.getAzureRateCardAuth(false, config)
 	config.AzureSubscriptionID = subscriptionID
-	config.AzureBillingAccount = billingAccount
 	config.AzureClientID = clientID
 	config.AzureClientSecret = clientSecret
 	config.AzureTenantID = tenantID
@@ -1251,7 +1256,7 @@ func (az *Azure) getDisks() ([]*compute.Disk, error) {
 	}
 
 	// Load the service provider keys
-	subscriptionID, _, clientID, clientSecret, tenantID := az.getAzureRateCardAuth(false, config)
+	subscriptionID, clientID, clientSecret, tenantID := az.getAzureRateCardAuth(false, config)
 	config.AzureSubscriptionID = subscriptionID
 	config.AzureClientID = clientID
 	config.AzureClientSecret = clientSecret
@@ -1494,11 +1499,7 @@ func (az *Azure) GetConfig() (*CustomPricing, error) {
 	}
 	// Default to pay-as-you-go Durable offer id
 	if c.AzureOfferDurableID == "" {
-		c.AzureOfferDurableID = "MS-AZR-0017P"
-		// TODO: what should this be? How can I override it for
-		// testing - the default value below doesn't appear in the
-		// downloaded pricesheet.
-		// c.AzureOfferDurableID = "MS-AZR-0003p"
+		c.AzureOfferDurableID = "MS-AZR-0003p"
 	}
 	if c.ShareTenancyCosts == "" {
 		c.ShareTenancyCosts = defaultShareTenancyCost

+ 18 - 0
pkg/env/costmodelenv.go

@@ -18,6 +18,9 @@ const (
 	AlibabaAccessKeyIDEnvVar     = "ALIBABA_ACCESS_KEY_ID"
 	AlibabaAccessKeySecretEnvVar = "ALIBABA_SECRET_ACCESS_KEY"
 
+	AzureOfferIDEnvVar        = "AZURE_OFFER_ID"
+	AzureBillingAccountEnvVar = "AZURE_BILLING_ACCOUNT"
+
 	KubecostNamespaceEnvVar        = "KUBECOST_NAMESPACE"
 	PodNameEnvVar                  = "POD_NAME"
 	ClusterIDEnvVar                = "CLUSTER_ID"
@@ -228,6 +231,21 @@ func GetAlibabaAccessKeySecret() string {
 	return Get(AlibabaAccessKeySecretEnvVar, "")
 }
 
+// GetAzureOfferID returns the environment variable value for AzureOfferIDEnvVar which represents
+// the Azure offer ID for determining prices.
+func GetAzureOfferID() string {
+	return Get(AzureOfferIDEnvVar, "")
+}
+
+// GetAzureBillingAccount returns the environment variable value for
+// AzureBillingAccountEnvVar which represents the Azure billing
+// account for determining prices. If this is specified
+// customer-specific prices will be downloaded from the consumption
+// price sheet API.
+func GetAzureBillingAccount() string {
+	return Get(AzureBillingAccountEnvVar, "")
+}
+
 // GetKubecostNamespace returns the environment variable value for KubecostNamespaceEnvVar which
 // represents the namespace the cost model exists in.
 func GetKubecostNamespace() string {