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

Support token hooks and name splitting in Cloud Cost View and Trends (#3407)

Niko Kovacevic 6 месяцев назад
Родитель
Сommit
466ae53d15

+ 6 - 2
core/pkg/opencost/cloudcostprops.go

@@ -259,10 +259,14 @@ func (ccp *CloudCostProperties) Intersection(that *CloudCostProperties) *CloudCo
 // GenerateKey takes a list of properties and creates a "/" seperated key based on the values of the requested properties.
 // Invalid values are ignored with a warning. A nil input returns the default key, while an empty slice  returns the empty string
 func (ccp *CloudCostProperties) GenerateKey(props []string) string {
+	return strings.Join(ccp.GenerateKeys(props), "/")
+}
+
+func (ccp *CloudCostProperties) GenerateKeys(props []string) []string {
 
 	// nil props replaced with default property list
 	if props == nil {
-		return ccp.hashKey()
+		return []string{ccp.hashKey()}
 	}
 
 	values := make([]string, len(props))
@@ -329,7 +333,7 @@ func (ccp *CloudCostProperties) GenerateKey(props []string) string {
 		values[i] = propVal
 	}
 
-	return strings.Join(values, "/")
+	return values
 }
 
 // HashKey creates a key on the entire property set including labels of a uniform length.

+ 12 - 4
pkg/cloudcost/queryservice.go

@@ -144,7 +144,7 @@ func (s *QueryService) GetCloudCostViewTotalsHandler() func(w http.ResponseWrite
 	}
 }
 
-func (s *QueryService) GetCloudCostViewTableHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
+func (s *QueryService) GetCloudCostViewTableHandler(tokenHook func(ViewTableRows) string) func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	// Return valid handler func
 	return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 		tracer := otel.Tracer(tracerName)
@@ -178,20 +178,28 @@ func (s *QueryService) GetCloudCostViewTableHandler() func(w http.ResponseWriter
 			w.Header().Set("Content-Type", "application/json")
 		}
 
-		resp, err := s.ViewQuerier.QueryViewTable(ctx, *request)
+		rows, err := s.ViewQuerier.QueryViewTable(ctx, *request)
 		if err != nil {
 			http.Error(w, fmt.Sprintf("Internal server error: %s", err), http.StatusInternalServerError)
 			return
 		}
 
+		resp := protocol.NewResponse().WithData(rows)
+
+		if tokenHook != nil {
+			resp = resp.WithMeta(map[string]any{
+				"token": tokenHook(rows),
+			})
+		}
+
 		_, spanResp := tracer.Start(ctx, "write response")
 		defer spanResp.End()
 		if format == csvFormat {
 			window := opencost.NewClosedWindow(request.Start, request.End)
-			writeCloudCostViewTableRowsAsCSV(w, resp, window.String())
+			writeCloudCostViewTableRowsAsCSV(w, rows, window.String())
 			return
 		}
 		w.Header().Set("Content-Type", "application/json")
-		protocol.WriteData(w, resp)
+		protocol.WriteResponse(w, resp)
 	}
 }

+ 2 - 0
pkg/cloudcost/view.go

@@ -32,6 +32,8 @@ func (vtrs ViewTableRows) Equal(that ViewTableRows) bool {
 
 type ViewTableRow struct {
 	Name              string            `json:"name"`
+	Names             []string          `json:"names,omitempty"`
+	Token             string            `json:"token,omitempty"`
 	Labels            map[string]string `json:"labels"`
 	KubernetesPercent float64           `json:"kubernetesPercent"`
 	Cost              float64           `json:"cost"`

+ 1 - 1
pkg/costmodel/router.go

@@ -653,7 +653,7 @@ func InitializeCloudCost(router *httprouter.Router, providerConfig models.Provid
 	router.GET("/cloudCost", cloudCostQueryService.GetCloudCostHandler())
 	router.GET("/cloudCost/view/graph", cloudCostQueryService.GetCloudCostViewGraphHandler())
 	router.GET("/cloudCost/view/totals", cloudCostQueryService.GetCloudCostViewTotalsHandler())
-	router.GET("/cloudCost/view/table", cloudCostQueryService.GetCloudCostViewTableHandler())
+	router.GET("/cloudCost/view/table", cloudCostQueryService.GetCloudCostViewTableHandler(nil))
 
 	router.GET("/cloudCost/status", cloudCostPipelineService.GetCloudCostStatusHandler())
 	router.GET("/cloudCost/rebuild", cloudCostPipelineService.GetCloudCostRebuildHandler())