Bladeren bron

Add IsEmpty() methods, fix awsProvider RIPricingError

Sean Holcomb 4 jaren geleden
bovenliggende
commit
4b3f80128a
3 gewijzigde bestanden met toevoegingen van 45 en 10 verwijderingen
  1. 26 6
      pkg/cloud/awsprovider.go
  2. 12 4
      pkg/cloud/azureprovider.go
  3. 7 0
      pkg/cloud/gcpprovider.go

+ 26 - 6
pkg/cloud/awsprovider.go

@@ -400,6 +400,18 @@ type AwsAthenaInfo struct {
 	MasterPayerARN   string `json:"masterPayerARN"`
 }
 
+// IsEmpty returns true if all fields in config are empty, false if not.
+func (aai *AwsAthenaInfo) IsEmpty() bool {
+	return aai.AthenaBucketName == "" &&
+		aai.AthenaRegion == "" &&
+		aai.AthenaDatabase == "" &&
+		aai.AthenaTable == "" &&
+		aai.ServiceKeyName == "" &&
+		aai.ServiceKeySecret == "" &&
+		aai.AccountID == "" &&
+		aai.MasterPayerARN == ""
+}
+
 // CreateConfig creates an AWS SDK V2 Config for the credentials that it contains
 func (aai *AwsAthenaInfo) CreateConfig() (awsSDK.Config, error) {
 	keyProvider := AWSAccessKey{AccessKeyID: aai.ServiceKeyName, SecretAccessKey: aai.ServiceKeySecret}
@@ -807,7 +819,7 @@ func (aws *AWS) DownloadPricingData() error {
 
 	// RIDataRunning establishes the existance of the goroutine. Since it's possible we
 	// run multiple downloads, we don't want to create multiple go routines if one already exists
-	if !aws.RIDataRunning && c.AthenaBucketName != "" {
+	if !aws.RIDataRunning {
 		err = aws.GetReservationDataFromAthena() // Block until one run has completed.
 		if err != nil {
 			klog.V(1).Infof("Failed to lookup reserved instance data: %s", err.Error())
@@ -827,7 +839,7 @@ func (aws *AWS) DownloadPricingData() error {
 			}()
 		}
 	}
-	if !aws.SavingsPlanDataRunning && c.AthenaBucketName != "" {
+	if !aws.SavingsPlanDataRunning {
 		err = aws.GetSavingsPlanDataFromAthena()
 		if err != nil {
 			klog.V(1).Infof("Failed to lookup savings plan data: %s", err.Error())
@@ -1636,10 +1648,13 @@ type SavingsPlanData struct {
 func (aws *AWS) GetSavingsPlanDataFromAthena() error {
 	cfg, err := aws.GetConfig()
 	if err != nil {
+		aws.RIPricingError = err
 		return err
 	}
 	if cfg.AthenaBucketName == "" {
-		return fmt.Errorf("No Athena Bucket configured")
+		err = fmt.Errorf("No Athena Bucket configured")
+		aws.RIPricingError = err
+		return err
 	}
 	if aws.SavingsPlanDataByInstanceID == nil {
 		aws.SavingsPlanDataByInstanceID = make(map[string]*SavingsPlanData)
@@ -1710,6 +1725,7 @@ func (aws *AWS) GetSavingsPlanDataFromAthena() error {
 
 	err = aws.QueryAthenaPaginated(context.TODO(), query, processResults)
 	if err != nil {
+		aws.RIPricingError = err
 		return fmt.Errorf("Error fetching Savings Plan Data: %s", err)
 	}
 
@@ -1726,10 +1742,13 @@ type RIData struct {
 func (aws *AWS) GetReservationDataFromAthena() error {
 	cfg, err := aws.GetConfig()
 	if err != nil {
+		aws.RIPricingError = err
 		return err
 	}
 	if cfg.AthenaBucketName == "" {
-		return fmt.Errorf("No Athena Bucket configured")
+		err = fmt.Errorf("No Athena Bucket configured")
+		aws.RIPricingError = err
+		return err
 	}
 
 	// Query for all column names in advance in order to validate configured
@@ -1737,8 +1756,9 @@ func (aws *AWS) GetReservationDataFromAthena() error {
 	columns, _ := aws.fetchColumns()
 
 	if !columns["reservation_reservation_a_r_n"] || !columns["reservation_effective_cost"] {
-		klog.Infof("No reserved data available in Athena")
-		aws.RIPricingError = nil
+		err = fmt.Errorf("No reservation data available in Athena")
+		aws.RIPricingError = err
+		log.Infof(err.Error())
 	}
 	if aws.RIPricingByInstanceID == nil {
 		aws.RIPricingByInstanceID = make(map[string]*RIData)

+ 12 - 4
pkg/cloud/azureprovider.go

@@ -327,7 +327,7 @@ func toRegionID(meterRegion string, regions map[string]string) (string, error) {
 var regionIdByDisplayName = map[string]string{
 	"US Gov AZ": "usgovarizona",
 	"US Gov TX": "usgovtexas",
-	"US Gov": "usgovvirginia",
+	"US Gov":    "usgovvirginia",
 }
 
 func checkRegionID(regionID string, regions map[string]string) bool {
@@ -462,7 +462,7 @@ func (k *azureKey) GetGPUCount() string {
 	return "0"
 }
 
-// Represents an azure storage config
+// AzureStorageConfig Represents an azure storage config
 type AzureStorageConfig struct {
 	SubscriptionId string `json:"azureSubscriptionID"`
 	AccountName    string `json:"azureStorageAccount"`
@@ -471,6 +471,15 @@ type AzureStorageConfig struct {
 	AzureCloud     string `json:"azureCloud"`
 }
 
+// IsEmpty returns true if all fields in config are empty, false if not.
+func (asc *AzureStorageConfig) IsEmpty() bool {
+	return asc.SubscriptionId == "" &&
+		asc.AccountName == "" &&
+		asc.AccessKey == "" &&
+		asc.ContainerName == "" &&
+		asc.AzureCloud == ""
+}
+
 // Represents an azure app key
 type AzureAppKey struct {
 	AppID       string `json:"appId"`
@@ -530,7 +539,6 @@ func (az *Azure) GetAzureStorageConfig(forceReload bool) (*AzureStorageConfig, e
 		defaultSubscriptionID = config.AzureSubscriptionID
 	}
 
-
 	// 1. Check for secret
 	s, err := az.loadAzureStorageConfig(forceReload)
 	if err != nil {
@@ -545,7 +553,7 @@ func (az *Azure) GetAzureStorageConfig(forceReload bool) (*AzureStorageConfig, e
 		// To support already configured users, subscriptionID may not be set in secret in which case, the subscriptionID
 		// for the rate card API is used
 		if s.SubscriptionId == "" {
-			s.SubscriptionId  = defaultSubscriptionID
+			s.SubscriptionId = defaultSubscriptionID
 		}
 		return s, nil
 	}

+ 7 - 0
pkg/cloud/gcpprovider.go

@@ -161,12 +161,19 @@ func (gcp *GCP) GetConfig() (*CustomPricing, error) {
 	return c, nil
 }
 
+// BigQueryConfig contain the required config and credentials to access OOC resources for GCP
 type BigQueryConfig struct {
 	ProjectID          string            `json:"projectID"`
 	BillingDataDataset string            `json:"billingDataDataset"`
 	Key                map[string]string `json:"key"`
 }
 
+// IsEmpty returns true if all fields in config are empty, false if not.
+func (bqc *BigQueryConfig) IsEmpty() bool {
+	return bqc.ProjectID == "" &&
+		bqc.BillingDataDataset == "" &&
+		(bqc.Key == nil || len(bqc.Key) == 0)
+}
 func (gcp *GCP) GetManagementPlatform() (string, error) {
 	nodes := gcp.Clientset.GetAllNodes()