Kaynağa Gözat

impl intersection logic, move instantiation to common KCM code to unblock FE

Signed-off-by: Alex Meijer <ameijer@kubecost.com>
Alex Meijer 2 yıl önce
ebeveyn
işleme
5f1a8c7e29
3 değiştirilmiş dosya ile 64 ekleme ve 17 silme
  1. 0 16
      pkg/cmd/costmodel/costmodel.go
  2. 16 0
      pkg/costmodel/router.go
  3. 48 1
      pkg/customcost/types.go

+ 0 - 16
pkg/cmd/costmodel/costmodel.go

@@ -9,7 +9,6 @@ import (
 
 	"github.com/julienschmidt/httprouter"
 	"github.com/opencost/opencost/pkg/cloudcost"
-	"github.com/opencost/opencost/pkg/customcost"
 	"github.com/prometheus/client_golang/prometheus/promhttp"
 	"github.com/rs/cors"
 
@@ -58,21 +57,6 @@ func Execute(opts *CostModelOpts) error {
 		a.CloudCostQueryService = cloudcost.NewQueryService(repoQuerier, repoQuerier)
 	}
 
-	log.Infof("Custom Costs enabled: %t", env.IsCustomCostEnabled())
-	if env.IsCustomCostEnabled() {
-		hourlyRepo := customcost.NewMemoryRepository()
-		dailyRepo := customcost.NewMemoryRepository()
-		ingConfig := customcost.DefaultIngestorConfiguration()
-		var err error
-		a.CustomCostPipelineService, err = customcost.NewPipelineService(hourlyRepo, dailyRepo, ingConfig)
-		if err != nil {
-			return fmt.Errorf("error instantiating custom cost pipeline service: %v", err)
-		}
-
-		customCostQuerier := customcost.NewQuerier(hourlyRepo, dailyRepo, ingConfig.HourlyDuration, ingConfig.DailyDuration)
-		a.CustomCostQueryService = customcost.NewQueryService(customCostQuerier)
-	}
-
 	rootMux := http.NewServeMux()
 	a.Router.GET("/healthz", Healthz)
 

+ 16 - 0
pkg/costmodel/router.go

@@ -1772,6 +1772,22 @@ func Initialize(additionalConfigWatchers ...*watcher.ConfigMapWatcher) *Accesses
 		a.MetricsEmitter.Start()
 	}
 
+	log.Infof("Custom Costs enabled: %t", env.IsCustomCostEnabled())
+	if env.IsCustomCostEnabled() {
+		hourlyRepo := customcost.NewMemoryRepository()
+		dailyRepo := customcost.NewMemoryRepository()
+		ingConfig := customcost.DefaultIngestorConfiguration()
+		var err error
+		a.CustomCostPipelineService, err = customcost.NewPipelineService(hourlyRepo, dailyRepo, ingConfig)
+		if err != nil {
+			log.Errorf("error instantiating custom cost pipeline service: %v", err)
+			return nil
+		}
+
+		customCostQuerier := customcost.NewQuerier(hourlyRepo, dailyRepo, ingConfig.HourlyDuration, ingConfig.DailyDuration)
+		a.CustomCostQueryService = customcost.NewQueryService(customCostQuerier)
+	}
+
 	a.Router.GET("/costDataModel", a.CostDataModel)
 	a.Router.GET("/costDataModelRange", a.CostDataModelRange)
 	a.Router.GET("/aggregatedCostModel", a.AggregateCostModelHandler)

+ 48 - 1
pkg/customcost/types.go

@@ -101,7 +101,54 @@ func (cc *CustomCost) Add(other *CustomCost) {
 	cc.BilledCost += other.BilledCost
 	cc.ListCost += other.ListCost
 	cc.ListUnitPrice += other.ListUnitPrice
-	cc.UsageQuantity += other.UsageQuantity
+
+	if cc.Id != other.Id {
+		cc.Id = ""
+	}
+
+	if cc.Zone != other.Zone {
+		cc.Zone = ""
+	}
+
+	if cc.AccountName != other.AccountName {
+		cc.AccountName = ""
+	}
+
+	if cc.ChargeCategory != other.ChargeCategory {
+		cc.ChargeCategory = ""
+	}
+
+	if cc.Description != other.Description {
+		cc.Description = ""
+	}
+
+	if cc.ResourceName != other.ResourceName {
+		cc.ResourceName = ""
+	}
+
+	if cc.ResourceType != other.ResourceType {
+		cc.ResourceType = ""
+	}
+
+	if cc.ProviderId != other.ProviderId {
+		cc.ProviderId = ""
+	}
+
+	if cc.UsageUnit != other.UsageUnit {
+		cc.UsageUnit = ""
+	} else {
+		// when usage units are the same, then we can sum the usages
+		cc.UsageQuantity += other.UsageQuantity
+	}
+
+	if cc.Domain != other.Domain {
+		cc.Domain = ""
+	}
+
+	if cc.Aggregate != other.Aggregate {
+		cc.Aggregate = ""
+	}
+
 }
 
 type CustomCostSet struct {