Просмотр исходного кода

Merge pull request #920 from kubecost/niko/fix-acm

Fix AggregateCostModel's parsing of configured SharedOverhead
Niko Kovacevic 4 лет назад
Родитель
Сommit
bd187bed95
2 измененных файлов с 23 добавлено и 9 удалено
  1. 21 0
      pkg/cloud/provider.go
  2. 2 9
      pkg/costmodel/aggregation.go

+ 21 - 0
pkg/cloud/provider.go

@@ -6,6 +6,7 @@ import (
 	"fmt"
 	"fmt"
 	"io"
 	"io"
 	"regexp"
 	"regexp"
+	"strconv"
 	"strings"
 	"strings"
 	"time"
 	"time"
 
 
@@ -15,6 +16,7 @@ import (
 
 
 	"github.com/kubecost/cost-model/pkg/clustercache"
 	"github.com/kubecost/cost-model/pkg/clustercache"
 	"github.com/kubecost/cost-model/pkg/env"
 	"github.com/kubecost/cost-model/pkg/env"
+	"github.com/kubecost/cost-model/pkg/log"
 
 
 	v1 "k8s.io/api/core/v1"
 	v1 "k8s.io/api/core/v1"
 )
 )
@@ -182,6 +184,25 @@ type CustomPricing struct {
 	KubecostToken                string `json:"kubecostToken"`
 	KubecostToken                string `json:"kubecostToken"`
 }
 }
 
 
+// GetSharedOverheadCostPerMonth parses and returns a float64 representation
+// of the configured monthly shared overhead cost. If the string version cannot
+// be parsed into a float, an error is logged and 0.0 is returned.
+func (cp *CustomPricing) GetSharedOverheadCostPerMonth() float64 {
+	// Empty string should be interpreted as "no cost", i.e. 0.0
+	if cp.SharedOverhead == "" {
+		return 0.0
+	}
+
+	// Attempt to parse, but log and return 0.0 if that fails.
+	sharedCostPerMonth, err := strconv.ParseFloat(cp.SharedOverhead, 64)
+	if err != nil {
+		log.Errorf("SharedOverhead: failed to parse shared overhead \"%s\": %s", cp.SharedOverhead, err)
+		return 0.0
+	}
+
+	return sharedCostPerMonth
+}
+
 type ServiceAccountStatus struct {
 type ServiceAccountStatus struct {
 	Checks []*ServiceAccountCheck `json:"checks"`
 	Checks []*ServiceAccountCheck `json:"checks"`
 }
 }

+ 2 - 9
pkg/costmodel/aggregation.go

@@ -1453,19 +1453,12 @@ func (a *Accesses) ComputeAggregateCostModel(promClient prometheusClient.Client,
 
 
 	sc := make(map[string]*SharedCostInfo)
 	sc := make(map[string]*SharedCostInfo)
 	if !disableSharedOverhead {
 	if !disableSharedOverhead {
-
-		overheadVal := c.SharedOverhead
-
-		cost, err := strconv.ParseFloat(overheadVal, 64)
+		costPerMonth := c.GetSharedOverheadCostPerMonth()
 		durationCoefficient := window.Hours() / timeutil.HoursPerMonth
 		durationCoefficient := window.Hours() / timeutil.HoursPerMonth
-		if err != nil {
-			return nil, "", fmt.Errorf("unable to parse shared cost %s: %s", overheadVal, err)
-		}
 		sc["total"] = &SharedCostInfo{
 		sc["total"] = &SharedCostInfo{
 			Name: "total",
 			Name: "total",
-			Cost: cost * durationCoefficient,
+			Cost: costPerMonth * durationCoefficient,
 		}
 		}
-
 	}
 	}
 
 
 	idleCoefficients := make(map[string]float64)
 	idleCoefficients := make(map[string]float64)