Przeglądaj źródła

Replace SharedCosts with SharedOverhead in CustomPricing

Kaelan Patel 4 lat temu
rodzic
commit
27123b518f

+ 1 - 6
pkg/cloud/awsprovider.go

@@ -439,12 +439,7 @@ func (aws *AWS) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, er
 						return err
 					}
 				} else {
-					sci := v.(map[string]interface{})
-					sc := make(map[string]string)
-					for k, val := range sci {
-						sc[k] = val.(string)
-					}
-					c.SharedCosts = sc //todo: support reflection/multiple map fields
+					return fmt.Errorf("type error while updating config for %s", kUpper)
 				}
 			}
 		}

+ 1 - 6
pkg/cloud/azureprovider.go

@@ -922,12 +922,7 @@ func (az *Azure) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, e
 					return err
 				}
 			} else {
-				sci := v.(map[string]interface{})
-				sc := make(map[string]string)
-				for k, val := range sci {
-					sc[k] = val.(string)
-				}
-				c.SharedCosts = sc //todo: support reflection/multiple map fields
+				return fmt.Errorf("type error while updating config for %s", kUpper)
 			}
 		}
 

+ 2 - 6
pkg/cloud/customprovider.go

@@ -1,6 +1,7 @@
 package cloud
 
 import (
+	"fmt"
 	"io"
 	"strconv"
 	"strings"
@@ -82,12 +83,7 @@ func (cp *CustomProvider) UpdateConfig(r io.Reader, updateType string) (*CustomP
 					return err
 				}
 			} else {
-				sci := v.(map[string]interface{})
-				sc := make(map[string]string)
-				for k, val := range sci {
-					sc[k] = val.(string)
-				}
-				c.SharedCosts = sc //todo: support reflection/multiple map fields
+				return fmt.Errorf("type error while updating config for %s", kUpper)
 			}
 		}
 

+ 1 - 6
pkg/cloud/gcpprovider.go

@@ -284,12 +284,7 @@ func (gcp *GCP) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, er
 						return err
 					}
 				} else {
-					sci := v.(map[string]interface{})
-					sc := make(map[string]string)
-					for k, val := range sci {
-						sc[k] = val.(string)
-					}
-					c.SharedCosts = sc //todo: support reflection/multiple map fields
+					return fmt.Errorf("type error while updating config for %s", kUpper)
 				}
 			}
 		}

+ 50 - 50
pkg/cloud/provider.go

@@ -130,56 +130,56 @@ type OutOfClusterAllocation struct {
 }
 
 type CustomPricing struct {
-	Provider                     string            `json:"provider"`
-	Description                  string            `json:"description"`
-	CPU                          string            `json:"CPU"`
-	SpotCPU                      string            `json:"spotCPU"`
-	RAM                          string            `json:"RAM"`
-	SpotRAM                      string            `json:"spotRAM"`
-	GPU                          string            `json:"GPU"`
-	SpotGPU                      string            `json:"spotGPU"`
-	Storage                      string            `json:"storage"`
-	ZoneNetworkEgress            string            `json:"zoneNetworkEgress"`
-	RegionNetworkEgress          string            `json:"regionNetworkEgress"`
-	InternetNetworkEgress        string            `json:"internetNetworkEgress"`
-	FirstFiveForwardingRulesCost string            `json:"firstFiveForwardingRulesCost"`
-	AdditionalForwardingRuleCost string            `json:"additionalForwardingRuleCost"`
-	LBIngressDataCost            string            `json:"LBIngressDataCost"`
-	SpotLabel                    string            `json:"spotLabel,omitempty"`
-	SpotLabelValue               string            `json:"spotLabelValue,omitempty"`
-	GpuLabel                     string            `json:"gpuLabel,omitempty"`
-	GpuLabelValue                string            `json:"gpuLabelValue,omitempty"`
-	ServiceKeyName               string            `json:"awsServiceKeyName,omitempty"`
-	ServiceKeySecret             string            `json:"awsServiceKeySecret,omitempty"`
-	SpotDataRegion               string            `json:"awsSpotDataRegion,omitempty"`
-	SpotDataBucket               string            `json:"awsSpotDataBucket,omitempty"`
-	SpotDataPrefix               string            `json:"awsSpotDataPrefix,omitempty"`
-	ProjectID                    string            `json:"projectID,omitempty"`
-	AthenaProjectID              string            `json:"athenaProjectID,omitempty"`
-	AthenaBucketName             string            `json:"athenaBucketName"`
-	AthenaRegion                 string            `json:"athenaRegion"`
-	AthenaDatabase               string            `json:"athenaDatabase"`
-	AthenaTable                  string            `json:"athenaTable"`
-	MasterPayerARN               string            `json:"masterPayerARN"`
-	BillingDataDataset           string            `json:"billingDataDataset,omitempty"`
-	CustomPricesEnabled          string            `json:"customPricesEnabled"`
-	DefaultIdle                  string            `json:"defaultIdle"`
-	AzureSubscriptionID          string            `json:"azureSubscriptionID"`
-	AzureClientID                string            `json:"azureClientID"`
-	AzureClientSecret            string            `json:"azureClientSecret"`
-	AzureTenantID                string            `json:"azureTenantID"`
-	AzureBillingRegion           string            `json:"azureBillingRegion"`
-	CurrencyCode                 string            `json:"currencyCode"`
-	Discount                     string            `json:"discount"`
-	NegotiatedDiscount           string            `json:"negotiatedDiscount"`
-	SharedCosts                  map[string]string `json:"sharedCost"`
-	ClusterName                  string            `json:"clusterName"`
-	SharedNamespaces             string            `json:"sharedNamespaces"`
-	SharedLabelNames             string            `json:"sharedLabelNames"`
-	SharedLabelValues            string            `json:"sharedLabelValues"`
-	ShareTenancyCosts            string            `json:"shareTenancyCosts"` // TODO clean up configuration so we can use a type other that string (this should be a bool, but the app panics if it's not a string)
-	ReadOnly                     string            `json:"readOnly"`
-	KubecostToken                string            `json:"kubecostToken"`
+	Provider                     string `json:"provider"`
+	Description                  string `json:"description"`
+	CPU                          string `json:"CPU"`
+	SpotCPU                      string `json:"spotCPU"`
+	RAM                          string `json:"RAM"`
+	SpotRAM                      string `json:"spotRAM"`
+	GPU                          string `json:"GPU"`
+	SpotGPU                      string `json:"spotGPU"`
+	Storage                      string `json:"storage"`
+	ZoneNetworkEgress            string `json:"zoneNetworkEgress"`
+	RegionNetworkEgress          string `json:"regionNetworkEgress"`
+	InternetNetworkEgress        string `json:"internetNetworkEgress"`
+	FirstFiveForwardingRulesCost string `json:"firstFiveForwardingRulesCost"`
+	AdditionalForwardingRuleCost string `json:"additionalForwardingRuleCost"`
+	LBIngressDataCost            string `json:"LBIngressDataCost"`
+	SpotLabel                    string `json:"spotLabel,omitempty"`
+	SpotLabelValue               string `json:"spotLabelValue,omitempty"`
+	GpuLabel                     string `json:"gpuLabel,omitempty"`
+	GpuLabelValue                string `json:"gpuLabelValue,omitempty"`
+	ServiceKeyName               string `json:"awsServiceKeyName,omitempty"`
+	ServiceKeySecret             string `json:"awsServiceKeySecret,omitempty"`
+	SpotDataRegion               string `json:"awsSpotDataRegion,omitempty"`
+	SpotDataBucket               string `json:"awsSpotDataBucket,omitempty"`
+	SpotDataPrefix               string `json:"awsSpotDataPrefix,omitempty"`
+	ProjectID                    string `json:"projectID,omitempty"`
+	AthenaProjectID              string `json:"athenaProjectID,omitempty"`
+	AthenaBucketName             string `json:"athenaBucketName"`
+	AthenaRegion                 string `json:"athenaRegion"`
+	AthenaDatabase               string `json:"athenaDatabase"`
+	AthenaTable                  string `json:"athenaTable"`
+	MasterPayerARN               string `json:"masterPayerARN"`
+	BillingDataDataset           string `json:"billingDataDataset,omitempty"`
+	CustomPricesEnabled          string `json:"customPricesEnabled"`
+	DefaultIdle                  string `json:"defaultIdle"`
+	AzureSubscriptionID          string `json:"azureSubscriptionID"`
+	AzureClientID                string `json:"azureClientID"`
+	AzureClientSecret            string `json:"azureClientSecret"`
+	AzureTenantID                string `json:"azureTenantID"`
+	AzureBillingRegion           string `json:"azureBillingRegion"`
+	CurrencyCode                 string `json:"currencyCode"`
+	Discount                     string `json:"discount"`
+	NegotiatedDiscount           string `json:"negotiatedDiscount"`
+	SharedOverhead               string `json:"sharedOverhead"`
+	ClusterName                  string `json:"clusterName"`
+	SharedNamespaces             string `json:"sharedNamespaces"`
+	SharedLabelNames             string `json:"sharedLabelNames"`
+	SharedLabelValues            string `json:"sharedLabelValues"`
+	ShareTenancyCosts            string `json:"shareTenancyCosts"` // TODO clean up configuration so we can use a type other that string (this should be a bool, but the app panics if it's not a string)
+	ReadOnly                     string `json:"readOnly"`
+	KubecostToken                string `json:"kubecostToken"`
 }
 
 type ServiceAccountStatus struct {

+ 0 - 16
pkg/cloud/providerconfig.go

@@ -188,22 +188,6 @@ func DefaultPricing() *CustomPricing {
 
 func SetCustomPricingField(obj *CustomPricing, name string, value string) error {
 
-	// shared overhead costs go into SharedCosts with key "total" in CustomPricing
-	if name == "SharedOverhead" {
-
-		value = sanitizePolicy.Sanitize(value)
-		val := reflect.ValueOf(value)
-
-		if reflect.ValueOf(obj.SharedCosts["total"]).Type() != val.Type() {
-			return fmt.Errorf("cannot insert value into custom pricing shared costs")
-		}
-
-		obj.SharedCosts["total"] = value
-
-		return nil
-
-	}
-
 	structValue := reflect.ValueOf(obj).Elem()
 	structFieldValue := structValue.FieldByName(name)
 

+ 14 - 12
pkg/costmodel/aggregation.go

@@ -2,7 +2,6 @@ package costmodel
 
 import (
 	"fmt"
-	"github.com/kubecost/cost-model/pkg/util/timeutil"
 	"math"
 	"net/http"
 	"regexp"
@@ -11,6 +10,8 @@ import (
 	"strings"
 	"time"
 
+	"github.com/kubecost/cost-model/pkg/util/timeutil"
+
 	"github.com/julienschmidt/httprouter"
 	"github.com/kubecost/cost-model/pkg/cloud"
 	"github.com/kubecost/cost-model/pkg/env"
@@ -1451,17 +1452,19 @@ func (a *Accesses) ComputeAggregateCostModel(promClient prometheusClient.Client,
 
 	sc := make(map[string]*SharedCostInfo)
 	if !disableSharedOverhead {
-		for key, val := range c.SharedCosts {
-			cost, err := strconv.ParseFloat(val, 64)
-			durationCoefficient := window.Hours() / timeutil.HoursPerMonth
-			if err != nil {
-				return nil, "", fmt.Errorf("unable to parse shared cost %s: %s", val, err)
-			}
-			sc[key] = &SharedCostInfo{
-				Name: key,
-				Cost: cost * durationCoefficient,
-			}
+
+		overheadVal := c.SharedOverhead
+
+		cost, err := strconv.ParseFloat(overheadVal, 64)
+		durationCoefficient := window.Hours() / timeutil.HoursPerMonth
+		if err != nil {
+			return nil, "", fmt.Errorf("unable to parse shared cost %s: %s", overheadVal, err)
+		}
+		sc["total"] = &SharedCostInfo{
+			Name: "total",
+			Cost: cost * durationCoefficient,
 		}
+
 	}
 
 	idleCoefficients := make(map[string]float64)
@@ -1789,7 +1792,6 @@ func (a *Accesses) warmAggregateCostModelCache() {
 			log.Infof("Error building cache %s: %s", window, aggErr)
 		}
 
-
 		totals, err := a.ComputeClusterCosts(promClient, a.CloudProvider, duration, offset, cacheEfficiencyData)
 		if err != nil {
 			log.Infof("Error building cluster costs cache %s", key)

+ 2 - 2
pkg/costmodel/settings.go

@@ -89,8 +89,8 @@ func (a *Accesses) customPricingHasChanged() bool {
 	// describe parameters by which we determine whether or not custom
 	// pricing settings have changed
 	encodeCustomPricing := func(cp *cloud.CustomPricing) string {
-		return fmt.Sprintf("%s:%s:%s:%s:%s:%s:%s:%s:%+v", cp.CustomPricesEnabled, cp.CPU, cp.SpotCPU,
-			cp.RAM, cp.SpotRAM, cp.GPU, cp.Storage, cp.CurrencyCode, cp.SharedCosts)
+		return fmt.Sprintf("%s:%s:%s:%s:%s:%s:%s:%s:%s", cp.CustomPricesEnabled, cp.CPU, cp.SpotCPU,
+			cp.RAM, cp.SpotRAM, cp.GPU, cp.Storage, cp.CurrencyCode, cp.SharedOverhead)
 	}
 
 	// compare cached custom pricing parameters with current values