Procházet zdrojové kódy

add status endpoint, further code review requests

Signed-off-by: Alex Meijer <ameijer@kubecost.com>
Alex Meijer před 2 roky
rodič
revize
1d4cdfc5b9

+ 2 - 0
pkg/costmodel/router.go

@@ -1851,6 +1851,8 @@ func Initialize(additionalConfigWatchers ...*watcher.ConfigMapWatcher) *Accesses
 		a.Router.GET("/customCost/timeseries", a.CustomCostQueryService.GetCustomCostTimeseriesHandler())
 	}
 
+	a.Router.GET("/customCost/status", a.CustomCostPipelineService.GetCustomCostStatusHandler())
+
 	a.httpServices.RegisterAll(a.Router)
 
 	return a

+ 7 - 3
pkg/customcost/pipelineservice.go

@@ -199,8 +199,11 @@ func (s *PipelineService) GetCustomCostRebuildHandler() func(w http.ResponseWrit
 func (s *PipelineService) GetCustomCostStatusHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	// If Reporting Service is nil, always return 501
 	if s == nil {
+		resultStatus := Status{
+			Enabled: false,
+		}
 		return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
-			http.Error(w, "Custom cost pipeline Service is nil", http.StatusNotImplemented)
+			protocol.WriteData(w, resultStatus)
 		}
 	}
 	if s.hourlyIngestor == nil || s.dailyIngestor == nil {
@@ -212,7 +215,8 @@ func (s *PipelineService) GetCustomCostStatusHandler() func(w http.ResponseWrite
 	// Return valid handler func
 	return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 		w.Header().Set("Content-Type", "application/json")
-
-		protocol.WriteData(w, s.Status())
+		stat := s.Status()
+		stat.Enabled = true
+		protocol.WriteData(w, stat)
 	}
 }

+ 1 - 1
pkg/customcost/repositoryquerier.go

@@ -48,7 +48,7 @@ func (rq *RepositoryQuerier) QueryTotal(ctx context.Context, request CostTotalRe
 			ccResponse, err := repo.Get(queryStart, domain)
 			if err != nil {
 				return nil, fmt.Errorf("QueryTotal: %w", err)
-			} else if ccResponse == nil {
+			} else if ccResponse == nil || ccResponse.Start == nil || ccResponse.End == nil {
 				continue
 			}
 

+ 15 - 14
pkg/customcost/status.go

@@ -8,18 +8,19 @@ import (
 
 // Status gives the details and metadata of a CustomCost integration
 type Status struct {
-	Key               string                     `json:"key"`
-	Source            string                     `json:"source"`
-	Provider          string                     `json:"provider"`
-	Active            bool                       `json:"active"`
-	Valid             bool                       `json:"valid"`
-	LastRun           time.Time                  `json:"lastRun"`
-	NextRun           time.Time                  `json:"nextRun"`
-	RefreshRateDaily  string                     `json:"RefreshRateDaily"`
-	RefreshRateHourly string                     `json:"RefreshRateHourly"`
-	Created           time.Time                  `json:"created"`
-	Runs              int                        `json:"runs"`
-	CoverageHourly    map[string]opencost.Window `json:"coverageHourly"`
-	CoverageDaily     map[string]opencost.Window `json:"coverageDaily"`
-	ConnectionStatus  string                     `json:"connectionStatus"`
+	Enabled           bool                       `json:"enabled"`
+	Key               string                     `json:"key,omitempty""`
+	Source            string                     `json:"source,omitempty""`
+	Provider          string                     `json:"provider,omitempty""`
+	Active            bool                       `json:"active,omitempty""`
+	Valid             bool                       `json:"valid,omitempty""`
+	LastRun           time.Time                  `json:"lastRun,omitempty""`
+	NextRun           time.Time                  `json:"nextRun,omitempty""`
+	RefreshRateDaily  string                     `json:"RefreshRateDaily,omitempty""`
+	RefreshRateHourly string                     `json:"RefreshRateHourly,omitempty""`
+	Created           time.Time                  `json:"created,omitempty""`
+	Runs              int                        `json:"runs,omitempty""`
+	CoverageHourly    map[string]opencost.Window `json:"coverageHourly,omitempty""`
+	CoverageDaily     map[string]opencost.Window `json:"coverageDaily,omitempty""`
+	ConnectionStatus  string                     `json:"connectionStatus,omitempty""`
 }