Преглед изворни кода

more locking safety, getcustompricing safety

AjayTripathy пре 6 година
родитељ
комит
97cadf4c4f
5 измењених фајлова са 57 додато и 40 уклоњено
  1. 7 5
      cloud/awsprovider.go
  2. 6 4
      cloud/azureprovider.go
  3. 8 6
      cloud/customprovider.go
  4. 9 7
      cloud/gcpprovider.go
  5. 27 18
      cloud/provider.go

+ 7 - 5
cloud/awsprovider.go

@@ -256,7 +256,7 @@ func (aws *AWS) GetManagementPlatform() (string, error) {
 }
 
 func (aws *AWS) GetConfig() (*CustomPricing, error) {
-	c, err := GetDefaultPricingData("aws.json")
+	c, err := GetCustomPricingData("aws.json")
 	if c.Discount == "" {
 		c.Discount = "0%"
 	}
@@ -269,7 +269,7 @@ func (aws *AWS) GetConfig() (*CustomPricing, error) {
 	return c, nil
 }
 func (aws *AWS) UpdateConfigFromConfigMap(a map[string]string) (*CustomPricing, error) {
-	c, err := GetDefaultPricingData("aws.json")
+	c, err := GetCustomPricingData("aws.json")
 	if err != nil {
 		return nil, err
 	}
@@ -281,7 +281,7 @@ func (aws *AWS) UpdateConfigFromConfigMap(a map[string]string) (*CustomPricing,
 	return configmapUpdate(c, configPath, a)
 }
 func (aws *AWS) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error) {
-	c, err := GetDefaultPricingData("aws.json")
+	c, err := GetCustomPricingData("aws.json")
 	if err != nil {
 		return nil, err
 	}
@@ -357,7 +357,9 @@ func (aws *AWS) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, er
 			return nil, err
 		}
 	}
+	configLock.Lock()
 	err = ioutil.WriteFile(path, cj, 0644)
+	defer configLock.Unlock()
 	if err != nil {
 		return nil, err
 	}
@@ -478,7 +480,7 @@ func (aws *AWS) isPreemptible(key string) bool {
 func (aws *AWS) DownloadPricingData() error {
 	aws.DownloadPricingDataLock.Lock()
 	defer aws.DownloadPricingDataLock.Unlock()
-	c, err := GetDefaultPricingData("aws.json")
+	c, err := GetCustomPricingData("aws.json")
 	if err != nil {
 		klog.V(1).Infof("Error downloading default pricing data: %s", err.Error())
 	}
@@ -702,7 +704,7 @@ func (aws *AWS) DownloadPricingData() error {
 
 // Stubbed NetworkPricing for AWS. Pull directly from aws.json for now
 func (c *AWS) NetworkPricing() (*Network, error) {
-	cpricing, err := GetDefaultPricingData("aws.json")
+	cpricing, err := GetCustomPricingData("aws.json")
 	if err != nil {
 		return nil, err
 	}

+ 6 - 4
cloud/azureprovider.go

@@ -430,7 +430,7 @@ func (az *Azure) NodePricing(key Key) (*Node, error) {
 
 // Stubbed NetworkPricing for Azure. Pull directly from azure.json for now
 func (c *Azure) NetworkPricing() (*Network, error) {
-	cpricing, err := GetDefaultPricingData("azure.json")
+	cpricing, err := GetCustomPricingData("azure.json")
 	if err != nil {
 		return nil, err
 	}
@@ -514,7 +514,7 @@ func (az *Azure) AddServiceKey(url url.Values) error {
 }
 
 func (az *Azure) UpdateConfigFromConfigMap(a map[string]string) (*CustomPricing, error) {
-	c, err := GetDefaultPricingData("azure.json")
+	c, err := GetCustomPricingData("azure.json")
 	if err != nil {
 		return nil, err
 	}
@@ -528,7 +528,7 @@ func (az *Azure) UpdateConfigFromConfigMap(a map[string]string) (*CustomPricing,
 
 func (az *Azure) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error) {
 	defer az.DownloadPricingData()
-	c, err := GetDefaultPricingData("azure.json")
+	c, err := GetCustomPricingData("azure.json")
 	if err != nil {
 		return nil, err
 	}
@@ -571,7 +571,9 @@ func (az *Azure) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, e
 	}
 
 	configPath := path + "azure.json"
+	configLock.Lock()
 	err = ioutil.WriteFile(configPath, cj, 0644)
+	configLock.Unlock()
 	if err != nil {
 		return nil, err
 	}
@@ -579,7 +581,7 @@ func (az *Azure) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, e
 	return c, nil
 }
 func (az *Azure) GetConfig() (*CustomPricing, error) {
-	c, err := GetDefaultPricingData("azure.json")
+	c, err := GetCustomPricingData("azure.json")
 	if c.Discount == "" {
 		c.Discount = "0%"
 	}

+ 8 - 6
cloud/customprovider.go

@@ -43,7 +43,7 @@ func (*CustomProvider) GetLocalStorageQuery(offset string) (string, error) {
 }
 
 func (*CustomProvider) GetConfig() (*CustomPricing, error) {
-	return GetDefaultPricingData("default.json")
+	return GetCustomPricingData("default.json")
 }
 
 func (*CustomProvider) GetManagementPlatform() (string, error) {
@@ -55,7 +55,7 @@ func (*CustomProvider) ApplyReservedInstancePricing(nodes map[string]*Node) {
 }
 
 func (cp *CustomProvider) UpdateConfigFromConfigMap(a map[string]string) (*CustomPricing, error) {
-	c, err := GetDefaultPricingData("default.json")
+	c, err := GetCustomPricingData("default.json")
 	if err != nil {
 		return nil, err
 	}
@@ -68,7 +68,7 @@ func (cp *CustomProvider) UpdateConfigFromConfigMap(a map[string]string) (*Custo
 }
 
 func (cp *CustomProvider) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error) {
-	c, err := GetDefaultPricingData("default.json")
+	c, err := GetCustomPricingData("default.json")
 	if err != nil {
 		return nil, err
 	}
@@ -105,7 +105,9 @@ func (cp *CustomProvider) UpdateConfig(r io.Reader, updateType string) (*CustomP
 	}
 
 	configPath := path + "default.json"
+	configLock.Lock()
 	err = ioutil.WriteFile(configPath, cj, 0644)
+	configLock.Unlock()
 	if err != nil {
 		return nil, err
 	}
@@ -172,7 +174,7 @@ func (cp *CustomProvider) DownloadPricingData() error {
 		m := make(map[string]*NodePrice)
 		cp.Pricing = m
 	}
-	p, err := GetDefaultPricingData("default.json")
+	p, err := GetCustomPricingData("default.json")
 	if err != nil {
 		return err
 	}
@@ -218,7 +220,7 @@ func (*CustomProvider) QuerySQL(query string) ([]byte, error) {
 }
 
 func (*CustomProvider) PVPricing(pvk PVKey) (*PV, error) {
-	cpricing, err := GetDefaultPricingData("default")
+	cpricing, err := GetCustomPricingData("default")
 	if err != nil {
 		return nil, err
 	}
@@ -228,7 +230,7 @@ func (*CustomProvider) PVPricing(pvk PVKey) (*PV, error) {
 }
 
 func (*CustomProvider) NetworkPricing() (*Network, error) {
-	cpricing, err := GetDefaultPricingData("default")
+	cpricing, err := GetCustomPricingData("default")
 	if err != nil {
 		return nil, err
 	}

+ 9 - 7
cloud/gcpprovider.go

@@ -86,7 +86,7 @@ func (gcp *GCP) GetLocalStorageQuery(offset string) (string, error) {
 }
 
 func (gcp *GCP) GetConfig() (*CustomPricing, error) {
-	c, err := GetDefaultPricingData("gcp.json")
+	c, err := GetCustomPricingData("gcp.json")
 	if err != nil {
 		return nil, err
 	}
@@ -119,7 +119,7 @@ func (gcp *GCP) GetManagementPlatform() (string, error) {
 }
 
 func (gcp *GCP) UpdateConfigFromConfigMap(a map[string]string) (*CustomPricing, error) {
-	c, err := GetDefaultPricingData("gcp.json")
+	c, err := GetCustomPricingData("gcp.json")
 	if err != nil {
 		return nil, err
 	}
@@ -132,7 +132,7 @@ func (gcp *GCP) UpdateConfigFromConfigMap(a map[string]string) (*CustomPricing,
 }
 
 func (gcp *GCP) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error) {
-	c, err := GetDefaultPricingData("gcp.json")
+	c, err := GetCustomPricingData("gcp.json")
 	if err != nil {
 		return nil, err
 	}
@@ -197,7 +197,9 @@ func (gcp *GCP) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, er
 	}
 
 	configPath := path + "gcp.json"
+	configLock.Lock()
 	err = ioutil.WriteFile(configPath, cj, 0644)
+	configLock.Unlock()
 	if err != nil {
 		return nil, err
 	}
@@ -210,7 +212,7 @@ func (gcp *GCP) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, er
 // "start" and "end" are dates of the format YYYY-MM-DD
 // "aggregator" is the tag used to determine how to allocate those assets, ie namespace, pod, etc.
 func (gcp *GCP) ExternalAllocations(start string, end string, aggregator string, filterType string, filterValue string) ([]*OutOfClusterAllocation, error) {
-	c, err := GetDefaultPricingData("gcp.json")
+	c, err := GetCustomPricingData("gcp.json")
 	if err != nil {
 		return nil, err
 	}
@@ -235,7 +237,7 @@ func (gcp *GCP) ExternalAllocations(start string, end string, aggregator string,
 
 // QuerySQL should query BigQuery for billing data for out of cluster costs.
 func (gcp *GCP) QuerySQL(query string) ([]*OutOfClusterAllocation, error) {
-	c, err := GetDefaultPricingData("gcp.json")
+	c, err := GetCustomPricingData("gcp.json")
 	if err != nil {
 		return nil, err
 	}
@@ -688,7 +690,7 @@ func (gcp *GCP) parsePages(inputKeys map[string]Key, pvKeys map[string]PVKey) (m
 func (gcp *GCP) DownloadPricingData() error {
 	gcp.DownloadPricingDataLock.Lock()
 	defer gcp.DownloadPricingDataLock.Unlock()
-	c, err := GetDefaultPricingData("gcp.json")
+	c, err := GetCustomPricingData("gcp.json")
 	if err != nil {
 		klog.V(2).Infof("Error downloading default pricing data: %s", err.Error())
 		return err
@@ -762,7 +764,7 @@ func (gcp *GCP) PVPricing(pvk PVKey) (*PV, error) {
 
 // Stubbed NetworkPricing for GCP. Pull directly from gcp.json for now
 func (c *GCP) NetworkPricing() (*Network, error) {
-	cpricing, err := GetDefaultPricingData("gcp.json")
+	cpricing, err := GetCustomPricingData("gcp.json")
 	if err != nil {
 		return nil, err
 	}

+ 27 - 18
cloud/provider.go

@@ -210,8 +210,26 @@ func CustomPricesEnabled(p Provider) bool {
 	return config.CustomPricesEnabled == "true"
 }
 
+// DefaultPricing should be returned so we can do computation even if no file is supplied.
+func DefaultPricing() *CustomPricing {
+	return &CustomPricing{
+		Provider:              "base",
+		Description:           "Default prices based on GCP us-central1",
+		CPU:                   "0.031611",
+		SpotCPU:               "0.006655",
+		RAM:                   "0.004237",
+		SpotRAM:               "0.000892",
+		GPU:                   "0.95",
+		Storage:               "0.00005479452",
+		ZoneNetworkEgress:     "0.01",
+		RegionNetworkEgress:   "0.01",
+		InternetNetworkEgress: "0.12",
+		CustomPricesEnabled:   "false",
+	}
+}
+
 // GetDefaultPricingData will search for a json file representing pricing data in /models/ and use it for base pricing info.
-func GetDefaultPricingData(fname string) (*CustomPricing, error) {
+func GetCustomPricingData(fname string) (*CustomPricing, error) {
 	configLock.Lock()
 	defer configLock.Unlock()
 
@@ -233,36 +251,27 @@ func GetDefaultPricingData(fname string) (*CustomPricing, error) {
 		var customPricing = &CustomPricing{}
 		err = json.Unmarshal([]byte(byteValue), customPricing)
 		if err != nil {
-			return nil, err
+			klog.Infof("Could not decode Custom Pricing file at path %s", path)
+			return DefaultPricing(), err
 		}
 		return customPricing, nil
 	} else if os.IsNotExist(err) {
-		c := &CustomPricing{
-			Provider:              fname,
-			Description:           "Default prices based on GCP us-central1",
-			CPU:                   "0.031611",
-			SpotCPU:               "0.006655",
-			RAM:                   "0.004237",
-			SpotRAM:               "0.000892",
-			GPU:                   "0.95",
-			Storage:               "0.00005479452",
-			ZoneNetworkEgress:     "0.01",
-			RegionNetworkEgress:   "0.01",
-			InternetNetworkEgress: "0.12",
-			CustomPricesEnabled:   "false",
-		}
+		klog.Infof("Could not find Custom Pricing file at path '%s'", path)
+		c := DefaultPricing()
 		cj, err := json.Marshal(c)
 		if err != nil {
-			return nil, err
+			return c, err
 		}
 
 		err = ioutil.WriteFile(path, cj, 0644)
 		if err != nil {
+			klog.Infof("Could not write Custom Pricing file to path '%s'", path)
 			return nil, err
 		}
 		return c, nil
 	} else {
-		return nil, err
+		klog.Infof("Custom Pricing file at path '%s' read error: '%s'", path, err.Error())
+		return DefaultPricing(), err
 	}
 }