Sean Holcomb 3 лет назад
Родитель
Сommit
e1faaeb79c
4 измененных файлов с 29 добавлено и 9 удалено
  1. 1 1
      pkg/costmodel/router.go
  2. 8 6
      pkg/kubecost/audit.go
  3. 12 2
      pkg/kubecost/etlrange.go
  4. 8 0
      pkg/kubecost/window.go

+ 1 - 1
pkg/costmodel/router.go

@@ -510,7 +510,7 @@ func (a *Accesses) CostDataModelRange(w http.ResponseWriter, r *http.Request, ps
 	}
 
 	window := kubecost.NewWindow(&start, &end)
-	if window.IsOpen() || window.IsEmpty() || window.IsNegative() {
+	if window.IsOpen() || !window.HasDuration() || window.IsNegative() {
 		w.Write(WrapDataWithMessage(nil, fmt.Errorf("invalid date range: %s", window), fmt.Sprintf("invalid date range: %s", window)))
 		return
 	}

+ 8 - 6
pkg/kubecost/audit.go

@@ -37,8 +37,8 @@ func ToAuditType(check string) AuditType {
 		return AuditAssetTotalStore
 	case string(AuditAssetAggStore):
 		return AuditAssetAggStore
-	case string(AuditClusterEquality):
-		return AuditClusterEquality
+	//case string(AuditClusterEquality):
+	//	return AuditClusterEquality
 	case string(AuditAll):
 		return AuditAll
 	default:
@@ -61,13 +61,14 @@ type AuditMissingValue struct {
 	Key         string
 }
 
-// AuditFloatResult structure for holding the results of a failed audit on a float value, Expected should be the Audit generated value
-// while actual is what is contained in the relevant store
+// AuditFloatResult structure for holding the results of a failed audit on a float value, Expected should be the value
+// calculated by the Audit func while Actual is what is contained in the relevant store.
 type AuditFloatResult struct {
 	Expected float64
 	Actual   float64
 }
 
+// Clone returns a deep copy of the caller
 func (afr *AuditFloatResult) Clone() *AuditFloatResult {
 	return &AuditFloatResult{
 		Expected: afr.Expected,
@@ -75,8 +76,8 @@ func (afr *AuditFloatResult) Clone() *AuditFloatResult {
 	}
 }
 
-// AllocationReconciliationAudit records the differences of between compute resource costs between allocations by nodes
-// and node assets keyed on node name and resource
+// AllocationReconciliationAudit records the differences of between compute resources (cpu, ram, gpu) costs between
+// allocations by nodes and node assets keyed on node name and compute resource
 type AllocationReconciliationAudit struct {
 	Status        AuditStatus
 	Description   string
@@ -85,6 +86,7 @@ type AllocationReconciliationAudit struct {
 	MissingValues []*AuditMissingValue
 }
 
+// Clone returns a deep copy of the caller
 func (ara *AllocationReconciliationAudit) Clone() *AllocationReconciliationAudit {
 	if ara == nil {
 		return nil

+ 12 - 2
pkg/kubecost/etlrange.go

@@ -16,6 +16,9 @@ type SetRange[T ETLSet] struct {
 // Append attaches the given ETLSet to the end of the sets slice.
 // currently does not check that the window is correct.
 func (r *SetRange[T]) Append(that T) {
+	if r == nil {
+		return
+	}
 	r.lock.Lock()
 	defer r.lock.Unlock()
 	r.sets = append(r.sets, that)
@@ -34,9 +37,13 @@ func (r *SetRange[T]) Each(f func(int, T)) {
 
 // Get retrieves the given index from the sets slice
 func (r *SetRange[T]) Get(i int) (T, error) {
+	var set T
+	if r == nil {
+		return set, fmt.Errorf("SetRange: Get: is nil")
+	}
 	if i < 0 || i >= len(r.sets) {
-		var set T
-		return set, fmt.Errorf("range: index out of range: %d", i)
+
+		return set, fmt.Errorf("SetRange: Get: index out of range: %d", i)
 	}
 
 	r.lock.RLock()
@@ -73,6 +80,9 @@ func (r *SetRange[T]) IsEmpty() bool {
 
 // MarshalJSON converts SetRange to JSON
 func (r *SetRange[T]) MarshalJSON() ([]byte, error) {
+	if r == nil {
+		return nil, fmt.Errorf("SetRange: MarshalJSON: is nil")
+	}
 	r.lock.RLock()
 	defer r.lock.RUnlock()
 	return json.Marshal(r.sets)

+ 8 - 0
pkg/kubecost/window.go

@@ -455,14 +455,22 @@ func (w Window) Hours() float64 {
 	return w.end.Sub(*w.start).Hours()
 }
 
+//IsEmpty a Window is empty if it does not have a start and an end
 func (w Window) IsEmpty() bool {
 	return w.start == nil && w.end == nil
 }
 
+//HasDuration a Window has duration if neither start and end are not nil and not equal
+func (w Window) HasDuration() bool {
+	return !w.IsOpen() && !w.end.Equal(*w.Start())
+}
+
+//IsNegative a Window is negative if start and end are not null and end is before start
 func (w Window) IsNegative() bool {
 	return !w.IsOpen() && w.end.Before(*w.Start())
 }
 
+//IsOpen a Window is open if it has a nil start or end
 func (w Window) IsOpen() bool {
 	return w.start == nil || w.end == nil
 }