Sean Holcomb 5 лет назад
Родитель
Сommit
3aa610bcb0
3 измененных файлов с 58 добавлено и 32 удалено
  1. 8 15
      pkg/kubecost/allocation.go
  2. 38 14
      pkg/kubecost/allocation_test.go
  3. 12 3
      pkg/kubecost/allocationprops.go

+ 8 - 15
pkg/kubecost/allocation.go

@@ -1177,14 +1177,6 @@ func (a *Allocation) generateKey(aggregateBy []string) string {
 	// identifies allocations.
 	names := []string{}
 
-	// Search for special case label for ETL conversion. valid values for this are DaemonSet, StatefulSet and Deployment
-	// and are relevant when the result of the ETL is being converted to the format of the Aggregation api.
-	aggControllerKind := ""
-	for _, agg := range aggregateBy {
-		if agg == "deployment" || agg == "daemonset" || agg == "statefulset" {
-			aggControllerKind = agg
-		}
-	}
 
 	for _, agg := range aggregateBy {
 		switch true {
@@ -1200,20 +1192,21 @@ func (a *Allocation) generateKey(aggregateBy []string) string {
 				// Indicate that allocation has no controller
 				controllerKind = UnallocatedSuffix
 			}
-			if aggControllerKind != "" && aggControllerKind != controllerKind {
+			names = append(names, controllerKind)
+	case agg == AllocationDaemonSetProp || agg == AllocationStatefulSetProp || agg == AllocationDeploymentProp || agg == AllocationJobProp:
+			controller := a.Properties.Controller
+			if agg != a.Properties.ControllerKind || controller == ""{
 				// The allocation does not have the specified controller kind
-				controllerKind = UnallocatedSuffix
+				controller = UnallocatedSuffix
 			}
-			names = append(names, controllerKind)
+			names = append(names, controller)
 		case agg == AllocationControllerProp:
-			if indexOf(AllocationControllerKindProp, aggregateBy) == -1 &&
-				a.Properties.ControllerKind != "" {
-				names = append(names, a.Properties.ControllerKind)
-			}
 			controller := a.Properties.Controller
 			if controller == "" {
 				// Indicate that allocation has no controller
 				controller = UnallocatedSuffix
+			} else if a.Properties.ControllerKind != "" {
+				controller = fmt.Sprintf("%s:%s", a.Properties.ControllerKind, controller)
 			}
 			names = append(names, controller)
 		case agg == AllocationPodProp:

+ 38 - 14
pkg/kubecost/allocation_test.go

@@ -107,7 +107,7 @@ func TestAllocation_Add(t *testing.T) {
 	a1 := &Allocation{
 		Start:                  s1,
 		End:                    e1,
-		Properties: &AllocationProperties{},
+		Properties:             &AllocationProperties{},
 		CPUCoreHours:           2.0 * hrs1,
 		CPUCoreRequestAverage:  2.0,
 		CPUCoreUsageAverage:    1.0,
@@ -132,7 +132,7 @@ func TestAllocation_Add(t *testing.T) {
 	a2 := &Allocation{
 		Start:                  s2,
 		End:                    e2,
-		Properties: &AllocationProperties{},
+		Properties:             &AllocationProperties{},
 		CPUCoreHours:           1.0 * hrs2,
 		CPUCoreRequestAverage:  1.0,
 		CPUCoreUsageAverage:    1.0,
@@ -264,7 +264,7 @@ func TestAllocation_Share(t *testing.T) {
 	a1 := &Allocation{
 		Start:                  s1,
 		End:                    e1,
-		Properties: &AllocationProperties{},
+		Properties:             &AllocationProperties{},
 		CPUCoreHours:           2.0 * hrs1,
 		CPUCoreRequestAverage:  2.0,
 		CPUCoreUsageAverage:    1.0,
@@ -288,7 +288,7 @@ func TestAllocation_Share(t *testing.T) {
 	a2 := &Allocation{
 		Start:                  s2,
 		End:                    e2,
-		Properties: &AllocationProperties{},
+		Properties:             &AllocationProperties{},
 		CPUCoreHours:           1.0 * hrs2,
 		CPUCoreRequestAverage:  1.0,
 		CPUCoreUsageAverage:    1.0,
@@ -939,11 +939,11 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 	err = as.AggregateBy([]string{AllocationControllerProp}, nil)
 	assertAllocationSetTotals(t, as, "1f", err, numControllers+numIdle+numUnallocated, activeTotalCost+idleTotalCost)
 	assertAllocationTotals(t, as, "1f", map[string]float64{
-		"deployment/deployment2":   24.00,
-		"daemonset/daemonset1":     12.00,
-		"deployment/deployment3":   6.00,
-		"statefulset/statefulset1": 12.00,
-		"deployment/deployment1":   12.00,
+		"deployment:deployment2":   24.00,
+		"daemonset:daemonset1":     12.00,
+		"deployment:deployment3":   6.00,
+		"statefulset:statefulset1": 12.00,
+		"deployment:deployment1":   12.00,
 		IdleSuffix:                 30.00,
 		UnallocatedSuffix:          16.00,
 	})
@@ -972,12 +972,14 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 	})
 	assertAllocationWindow(t, as, "1h", startYesterday, endYesterday, 1440.0)
 
-	// 1i AggregationProperties=(ControllerKind:deployment)
+	// 1i AggregationProperties=(deployment)
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{AllocationControllerKindProp, "deployment" }, nil)
-	assertAllocationSetTotals(t, as, "1i", err, 1+numIdle+numUnallocated, activeTotalCost+idleTotalCost)
+	err = as.AggregateBy([]string{AllocationDeploymentProp}, nil)
+	assertAllocationSetTotals(t, as, "1i", err, 3+numIdle+numUnallocated, activeTotalCost+idleTotalCost)
 	assertAllocationTotals(t, as, "1i", map[string]float64{
-		"deployment":      42.00,
+		"deployment1":     12.00,
+		"deployment2":     24.00,
+		"deployment3":     6.00,
 		IdleSuffix:        30.00,
 		UnallocatedSuffix: 40.00,
 	})
@@ -993,7 +995,29 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 		IdleSuffix:        30.00,
 		UnallocatedSuffix: 64.00,
 	})
-	assertAllocationWindow(t, as, "1i", startYesterday, endYesterday, 1440.0)
+	assertAllocationWindow(t, as, "1j", startYesterday, endYesterday, 1440.0)
+
+	// 1k AggregationProperties=(daemonSet)
+	as = generateAllocationSet(start)
+	err = as.AggregateBy([]string{AllocationDaemonSetProp}, nil)
+	assertAllocationSetTotals(t, as, "1k", err, 1+numIdle+numUnallocated, activeTotalCost+idleTotalCost)
+	assertAllocationTotals(t, as, "1k", map[string]float64{
+		"daemonset1":      12.00,
+		IdleSuffix:        30.00,
+		UnallocatedSuffix: 70.00,
+	})
+	assertAllocationWindow(t, as, "1k", startYesterday, endYesterday, 1440.0)
+
+	// 1l AggregationProperties=(statefulSet)
+	as = generateAllocationSet(start)
+	err = as.AggregateBy([]string{AllocationStatefulSetProp}, nil)
+	assertAllocationSetTotals(t, as, "1l", err, 1+numIdle+numUnallocated, activeTotalCost+idleTotalCost)
+	assertAllocationTotals(t, as, "1l", map[string]float64{
+		"statefulset1":    12.00,
+		IdleSuffix:        30.00,
+		UnallocatedSuffix: 70.00,
+	})
+	assertAllocationWindow(t, as, "1l", startYesterday, endYesterday, 1440.0)
 
 	// 2  Multi-aggregation
 

+ 12 - 3
pkg/kubecost/allocationprops.go

@@ -6,9 +6,6 @@ import (
 	"strings"
 )
 
-
-
-
 const (
 	AllocationNilProp            string = ""
 	AllocationClusterProp        string = "cluster"
@@ -22,6 +19,10 @@ const (
 	AllocationServiceProp        string = "service"
 	AllocationLabelProp          string = "label"
 	AllocationAnnotationProp     string = "annotation"
+	AllocationDeploymentProp     string = "deployment"
+	AllocationStatefulSetProp    string = "statefulset"
+	AllocationDaemonSetProp      string = "daemonset"
+	AllocationJobProp            string = "job"
 )
 
 func ParseProperty(text string) (string, error) {
@@ -48,6 +49,14 @@ func ParseProperty(text string) (string, error) {
 		return AllocationLabelProp, nil
 	case "annotation":
 		return AllocationAnnotationProp, nil
+	case "deployment":
+		return AllocationDeploymentProp, nil
+	case "daemonset":
+		return AllocationDaemonSetProp, nil
+	case "statefulset":
+		return AllocationStatefulSetProp, nil
+	case "job":
+		return AllocationJobProp, nil
 	}
 	return AllocationNilProp, fmt.Errorf("invalid allocation property: %s", text)
 }