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

AllocationProperties to string and other fixes

Sean Holcomb 5 лет назад
Родитель
Сommit
57bdb775f5

+ 5 - 5
pkg/costmodel/aggregation.go

@@ -2118,20 +2118,20 @@ func (a *Accesses) AggregateCostModelHandler(w http.ResponseWriter, r *http.Requ
 // encoded under the given key. If none exist, or if parsing fails, an error
 // is returned with empty AllocationProperties.
 func ParseAggregationProperties(qp util.QueryParams, key string) ([]string, error) {
-	AggregateBy := []string{}
+	aggregateBy := []string{}
 	for _, agg := range qp.GetList(key, ",") {
 		aggregate := strings.TrimSpace(agg)
 		if aggregate != "" {
 			if prop, err := kubecost.ParseProperty(aggregate); err == nil {
-				AggregateBy = append(AggregateBy, string(prop))
+				aggregateBy = append(aggregateBy, string(prop))
 			} else if strings.HasPrefix(aggregate, "label:") {
-				AggregateBy = append(AggregateBy, aggregate)
+				aggregateBy = append(aggregateBy, aggregate)
 			} else if strings.HasPrefix(aggregate, "annotation:") {
-				AggregateBy = append(AggregateBy, aggregate)
+				aggregateBy = append(aggregateBy, aggregate)
 			}
 		}
 	}
-	return AggregateBy, nil
+	return aggregateBy, nil
 }
 
 // ComputeAllocationHandler computes an AllocationSetRange from the CostModel.

+ 13 - 14
pkg/kubecost/allocation.go

@@ -1177,23 +1177,24 @@ func (a *Allocation) generateKey(aggregateBy []string) string {
 	// identifies allocations.
 	names := []string{}
 
-	// Search for special case label for ETL conversion
+	// 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 strings.HasPrefix(agg, "controllerKind:") {
-			aggControllerKind = strings.Split(agg, ":")[1]
+		if agg == "deployment" || agg == "daemonset" || agg == "statefulset" {
+			aggControllerKind = agg
 		}
 	}
 
 	for _, agg := range aggregateBy {
 		switch true {
-		case agg == string(AllocationClusterProp):
+		case agg == AllocationClusterProp:
 			names = append(names, a.Properties.Cluster)
-		case agg == string(AllocationNodeProp):
+		case agg == AllocationNodeProp:
 			names = append(names, a.Properties.Node)
-		case agg == string(AllocationNamespaceProp):
+		case agg == AllocationNamespaceProp:
 			names = append(names, a.Properties.Namespace)
-		case agg == string(AllocationControllerKindProp):
+		case agg == AllocationControllerKindProp:
 			controllerKind := a.Properties.ControllerKind
 			if controllerKind == "" {
 				// Indicate that allocation has no controller
@@ -1204,8 +1205,8 @@ func (a *Allocation) generateKey(aggregateBy []string) string {
 				controllerKind = UnallocatedSuffix
 			}
 			names = append(names, controllerKind)
-		case agg == string(AllocationControllerProp):
-			if indexOf(string(AllocationControllerKindProp), aggregateBy) == -1 &&
+		case agg == AllocationControllerProp:
+			if indexOf(AllocationControllerKindProp, aggregateBy) == -1 &&
 				a.Properties.ControllerKind != "" {
 				names = append(names, a.Properties.ControllerKind)
 			}
@@ -1215,11 +1216,11 @@ func (a *Allocation) generateKey(aggregateBy []string) string {
 				controller = UnallocatedSuffix
 			}
 			names = append(names, controller)
-		case agg == string(AllocationPodProp):
+		case agg == AllocationPodProp:
 			names = append(names, a.Properties.Pod)
-		case agg == string(AllocationContainerProp):
+		case agg == AllocationContainerProp:
 			names = append(names, a.Properties.Container)
-		case agg == string(AllocationServiceProp):
+		case agg == AllocationServiceProp:
 			services := a.Properties.Services
 			if services == nil || len(services) == 0  {
 				// Indicate that allocation has no services
@@ -1231,8 +1232,6 @@ func (a *Allocation) generateKey(aggregateBy []string) string {
 					break
 				}
 			}
-		case strings.HasPrefix(agg, "controllerKind:"):
-			continue
 		case strings.HasPrefix(agg, "label:"):
 			labels := a.Properties.Labels
 			if labels == nil {

+ 31 - 31
pkg/kubecost/allocation_test.go

@@ -465,7 +465,7 @@ func TestAllocationSet_generateKey(t *testing.T) {
 	var key string
 
 	props := []string{
-		AllocationClusterProp.String(),
+		AllocationClusterProp,
 	}
 
 	key = alloc.generateKey(props)
@@ -488,8 +488,8 @@ func TestAllocationSet_generateKey(t *testing.T) {
 	}
 
 	props = []string{
-		AllocationClusterProp.String(),
-		AllocationNamespaceProp.String(),
+		AllocationClusterProp,
+		AllocationNamespaceProp,
 		"label:app",
 	}
 
@@ -864,7 +864,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 
 	// 1a AggregationProperties=(Cluster)
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationClusterProp)}, nil)
+	err = as.AggregateBy([]string{AllocationClusterProp}, nil)
 	assertAllocationSetTotals(t, as, "1a", err, numClusters+numIdle, activeTotalCost+idleTotalCost)
 	assertAllocationTotals(t, as, "1a", map[string]float64{
 		"cluster1": 46.00,
@@ -875,7 +875,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 
 	// 1b AggregationProperties=(Namespace)
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationNamespaceProp)}, nil)
+	err = as.AggregateBy([]string{AllocationNamespaceProp}, nil)
 	assertAllocationSetTotals(t, as, "1b", err, numNamespaces+numIdle, activeTotalCost+idleTotalCost)
 	assertAllocationTotals(t, as, "1b", map[string]float64{
 		"namespace1": 28.00,
@@ -887,7 +887,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 
 	// 1c AggregationProperties=(Pod)
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationPodProp)}, nil)
+	err = as.AggregateBy([]string{AllocationPodProp}, nil)
 	assertAllocationSetTotals(t, as, "1c", err, numPods+numIdle, activeTotalCost+idleTotalCost)
 	assertAllocationTotals(t, as, "1c", map[string]float64{
 		"pod-jkl":  6.00,
@@ -905,7 +905,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 
 	// 1d AggregationProperties=(Container)
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationContainerProp)}, nil)
+	err = as.AggregateBy([]string{AllocationContainerProp}, nil)
 	assertAllocationSetTotals(t, as, "1d", err, numContainers+numIdle, activeTotalCost+idleTotalCost)
 	assertAllocationTotals(t, as, "1d", map[string]float64{
 		"container2": 6.00,
@@ -923,7 +923,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 
 	// 1e AggregationProperties=(ControllerKind)
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationControllerKindProp)}, nil)
+	err = as.AggregateBy([]string{AllocationControllerKindProp}, nil)
 	assertAllocationSetTotals(t, as, "1e", err, numControllerKinds+numIdle+numUnallocated, activeTotalCost+idleTotalCost)
 	assertAllocationTotals(t, as, "1e", map[string]float64{
 		"daemonset":       12.00,
@@ -936,7 +936,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 
 	// 1f AggregationProperties=(Controller)
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationControllerProp)}, nil)
+	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,
@@ -951,7 +951,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 
 	// 1g AggregationProperties=(Service)
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationServiceProp)}, nil)
+	err = as.AggregateBy([]string{AllocationServiceProp}, nil)
 	assertAllocationSetTotals(t, as, "1g", err, numServices+numIdle+numUnallocated, activeTotalCost+idleTotalCost)
 	assertAllocationTotals(t, as, "1g", map[string]float64{
 		"service1":        12.00,
@@ -974,7 +974,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 
 	// 1i AggregationProperties=(ControllerKind:deployment)
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationControllerKindProp), "controllerKind:deployment" }, nil)
+	err = as.AggregateBy([]string{AllocationControllerKindProp, "deployment" }, nil)
 	assertAllocationSetTotals(t, as, "1i", err, 1+numIdle+numUnallocated, activeTotalCost+idleTotalCost)
 	assertAllocationTotals(t, as, "1i", map[string]float64{
 		"deployment":      42.00,
@@ -1016,7 +1016,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 
 	// 2e AggregationProperties=(Cluster, Label:app, Label:environment)
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationClusterProp), "label:app;env"}, nil)
+	err = as.AggregateBy([]string{AllocationClusterProp, "label:app;env"}, nil)
 	assertAllocationSetTotals(t, as, "2e", err, 6, activeTotalCost+idleTotalCost)
 	assertAllocationTotals(t, as, "2e", map[string]float64{
 		"cluster1/app=app2/env=env2":             12.00,
@@ -1029,7 +1029,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 
 	// 2f AggregationProperties=(annotation:team, pod)
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationPodProp), "annotation:team"}, nil)
+	err = as.AggregateBy([]string{AllocationPodProp, "annotation:team"}, nil)
 	assertAllocationSetTotals(t, as, "2f", err, 11, activeTotalCost+idleTotalCost)
 	assertAllocationTotals(t, as, "2f", map[string]float64{
 		"pod-jkl/" + UnallocatedSuffix: 6.00,
@@ -1054,7 +1054,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 	// namespace2: 46.3125 = 36.00 + 5.0*(3.0/6.0) + 15.0*(3.0/16.0) + 5.0*(3.0/6.0) + 5.0*(3.0/6.0)
 	// namespace3: 23.0000 = 18.00 + 5.0*(3.0/6.0) + 5.0*(3.0/6.0)
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationNamespaceProp)}, &AllocationAggregationOptions{ShareIdle: ShareWeighted})
+	err = as.AggregateBy([]string{AllocationNamespaceProp}, &AllocationAggregationOptions{ShareIdle: ShareWeighted})
 	assertAllocationSetTotals(t, as, "3a", err, numNamespaces, activeTotalCost+idleTotalCost)
 	assertAllocationTotals(t, as, "3a", map[string]float64{
 		"namespace1": 42.69,
@@ -1068,7 +1068,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 	// namespace2: 51.0000 = 36.00 + 5.0*(1.0/2.0) + 15.0*(1.0/2.0) + 5.0*(1.0/2.0) + 5.0*(1.0/2.0)
 	// namespace3: 23.0000 = 18.00 + 5.0*(1.0/2.0) + 5.0*(1.0/2.0)
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationNamespaceProp)}, &AllocationAggregationOptions{ShareIdle: ShareEven})
+	err = as.AggregateBy([]string{AllocationNamespaceProp}, &AllocationAggregationOptions{ShareIdle: ShareEven})
 	assertAllocationSetTotals(t, as, "3a", err, numNamespaces, activeTotalCost+idleTotalCost)
 	assertAllocationTotals(t, as, "3a", map[string]float64{
 		"namespace1": 38.00,
@@ -1084,7 +1084,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 	// namespace2: 45.5000 = 36.00 + 18.00*(1.0/2.0)
 	// idle:       30.0000
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationNamespaceProp)}, &AllocationAggregationOptions{
+	err = as.AggregateBy([]string{AllocationNamespaceProp}, &AllocationAggregationOptions{
 		ShareFuncs: []AllocationMatchFunc{isNamespace3},
 		ShareSplit: ShareEven,
 	})
@@ -1101,7 +1101,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 	// namespace2: 37.5000 =
 	// idle:       30.0000
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationNamespaceProp)}, &AllocationAggregationOptions{
+	err = as.AggregateBy([]string{AllocationNamespaceProp}, &AllocationAggregationOptions{
 		ShareFuncs: []AllocationMatchFunc{isNamespace3},
 		ShareSplit: ShareWeighted,
 	})
@@ -1119,7 +1119,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 	// namespace3: 23.3333 = 18.00 + 16.00*(1.0/3.0)
 	// idle:       30.0000
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationNamespaceProp)}, &AllocationAggregationOptions{
+	err = as.AggregateBy([]string{AllocationNamespaceProp}, &AllocationAggregationOptions{
 		ShareFuncs: []AllocationMatchFunc{isApp1},
 		ShareSplit: ShareEven,
 	})
@@ -1138,7 +1138,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 	// namespace3: 54.878 = 18.00 + (7.0*24.0)*(18.00/82.00)
 	// idle:       30.0000
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationNamespaceProp)}, &AllocationAggregationOptions{
+	err = as.AggregateBy([]string{AllocationNamespaceProp}, &AllocationAggregationOptions{
 		SharedHourlyCosts: map[string]float64{"total": sharedOverheadHourlyCost},
 		ShareSplit:        ShareWeighted,
 	})
@@ -1169,7 +1169,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 
 	// 5a Filter by cluster with separate idle
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationClusterProp)}, &AllocationAggregationOptions{
+	err = as.AggregateBy([]string{AllocationClusterProp}, &AllocationAggregationOptions{
 		FilterFuncs: []AllocationMatchFunc{isCluster("cluster1")},
 		ShareIdle:   ShareNone,
 	})
@@ -1182,7 +1182,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 
 	// 5b Filter by cluster with shared idle
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationClusterProp)}, &AllocationAggregationOptions{
+	err = as.AggregateBy([]string{AllocationClusterProp}, &AllocationAggregationOptions{
 		FilterFuncs: []AllocationMatchFunc{isCluster("cluster1")},
 		ShareIdle:   ShareWeighted,
 	})
@@ -1194,7 +1194,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 
 	// 5c Filter by cluster, agg by namespace, with separate idle
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationNamespaceProp)}, &AllocationAggregationOptions{
+	err = as.AggregateBy([]string{AllocationNamespaceProp}, &AllocationAggregationOptions{
 		FilterFuncs: []AllocationMatchFunc{isCluster("cluster1")},
 		ShareIdle:   ShareNone,
 	})
@@ -1208,7 +1208,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 
 	// 5d Filter by namespace, agg by cluster, with separate idle
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationClusterProp)}, &AllocationAggregationOptions{
+	err = as.AggregateBy([]string{AllocationClusterProp}, &AllocationAggregationOptions{
 		FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
 		ShareIdle:   ShareNone,
 	})
@@ -1224,7 +1224,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 
 	// 6a SplitIdle
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationNamespaceProp)}, &AllocationAggregationOptions{SplitIdle: true})
+	err = as.AggregateBy([]string{AllocationNamespaceProp}, &AllocationAggregationOptions{SplitIdle: true})
 	assertAllocationSetTotals(t, as, "6a", err, numNamespaces+numSplitIdle, activeTotalCost+idleTotalCost)
 	assertAllocationTotals(t, as, "6a", map[string]float64{
 		"namespace1":                           28.00,
@@ -1239,7 +1239,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 	// Should match values from unfiltered aggregation (3a)
 	// namespace2: 46.3125 = 36.00 + 5.0*(3.0/6.0) + 15.0*(3.0/16.0) + 5.0*(3.0/6.0) + 5.0*(3.0/6.0)
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationNamespaceProp)}, &AllocationAggregationOptions{
+	err = as.AggregateBy([]string{AllocationNamespaceProp}, &AllocationAggregationOptions{
 		FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
 		ShareIdle:   ShareWeighted,
 	})
@@ -1253,7 +1253,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 	// Should match values from unfiltered aggregation (3b)
 	// namespace2: 51.0000 = 36.00 + 5.0*(1.0/2.0) + 15.0*(1.0/2.0) + 5.0*(1.0/2.0) + 5.0*(1.0/2.0)
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationNamespaceProp)}, &AllocationAggregationOptions{
+	err = as.AggregateBy([]string{AllocationNamespaceProp}, &AllocationAggregationOptions{
 		FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
 		ShareIdle:   ShareEven,
 	})
@@ -1270,7 +1270,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 	// idle:       30.0000
 	// Then namespace 2 is filtered.
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationNamespaceProp)}, &AllocationAggregationOptions{
+	err = as.AggregateBy([]string{AllocationNamespaceProp}, &AllocationAggregationOptions{
 		FilterFuncs:       []AllocationMatchFunc{isNamespace("namespace2")},
 		SharedHourlyCosts: map[string]float64{"total": sharedOverheadHourlyCost},
 		ShareSplit:        ShareWeighted,
@@ -1291,7 +1291,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 	// namespace2: 54.667 = 36.00 + (28.00)*(36.00/54.00)
 	// idle:       30.0000
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationNamespaceProp)}, &AllocationAggregationOptions{
+	err = as.AggregateBy([]string{AllocationNamespaceProp}, &AllocationAggregationOptions{
 		FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
 		ShareFuncs:  []AllocationMatchFunc{isNamespace("namespace1")},
 		ShareSplit:  ShareWeighted,
@@ -1339,7 +1339,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 	//   shared cost    14.2292 = (42.6875)*(18.0/54.0)
 	//
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationNamespaceProp)}, &AllocationAggregationOptions{
+	err = as.AggregateBy([]string{AllocationNamespaceProp}, &AllocationAggregationOptions{
 		ShareFuncs: []AllocationMatchFunc{isNamespace("namespace1")},
 		ShareSplit: ShareWeighted,
 		ShareIdle:  ShareWeighted,
@@ -1389,7 +1389,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 	// Then, filter for namespace2: 74.7708
 	//
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationNamespaceProp)}, &AllocationAggregationOptions{
+	err = as.AggregateBy([]string{AllocationNamespaceProp}, &AllocationAggregationOptions{
 		FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
 		ShareFuncs:  []AllocationMatchFunc{isNamespace("namespace1")},
 		ShareSplit:  ShareWeighted,
@@ -1430,7 +1430,7 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 	//
 	// Then namespace 2 is filtered.
 	as = generateAllocationSet(start)
-	err = as.AggregateBy([]string{string(AllocationNamespaceProp)}, &AllocationAggregationOptions{
+	err = as.AggregateBy([]string{AllocationNamespaceProp}, &AllocationAggregationOptions{
 		FilterFuncs:       []AllocationMatchFunc{isNamespace("namespace2")},
 		ShareSplit:        ShareWeighted,
 		ShareIdle:         ShareWeighted,

+ 18 - 87
pkg/kubecost/allocationprops.go

@@ -6,24 +6,25 @@ import (
 	"strings"
 )
 
-type AllocationProperty string
+
+
 
 const (
-	AllocationNilProp            AllocationProperty = ""
-	AllocationClusterProp        AllocationProperty = "cluster"
-	AllocationNodeProp           AllocationProperty = "node"
-	AllocationContainerProp      AllocationProperty = "container"
-	AllocationControllerProp     AllocationProperty = "controller"
-	AllocationControllerKindProp AllocationProperty = "controllerKind"
-	AllocationNamespaceProp      AllocationProperty = "namespace"
-	AllocationPodProp            AllocationProperty = "pod"
-	AllocationProviderIDProp     AllocationProperty = "providerID"
-	AllocationServiceProp        AllocationProperty = "service"
-	AllocationLabelProp          AllocationProperty = "label"
-	AllocationAnnotationProp     AllocationProperty = "annotation"
+	AllocationNilProp            string = ""
+	AllocationClusterProp        string = "cluster"
+	AllocationNodeProp           string = "node"
+	AllocationContainerProp      string = "container"
+	AllocationControllerProp     string = "controller"
+	AllocationControllerKindProp string = "controllerKind"
+	AllocationNamespaceProp      string = "namespace"
+	AllocationPodProp            string = "pod"
+	AllocationProviderIDProp     string = "providerID"
+	AllocationServiceProp        string = "service"
+	AllocationLabelProp          string = "label"
+	AllocationAnnotationProp     string = "annotation"
 )
 
-func ParseProperty(text string) (AllocationProperty, error) {
+func ParseProperty(text string) (string, error) {
 	switch strings.TrimSpace(strings.ToLower(text)) {
 	case "cluster":
 		return AllocationClusterProp, nil
@@ -51,10 +52,6 @@ func ParseProperty(text string) (AllocationProperty, error) {
 	return AllocationNilProp, fmt.Errorf("invalid allocation property: %s", text)
 }
 
-func (p AllocationProperty) String() string {
-	return string(p)
-}
-
 // AllocationProperties describes a set of Kubernetes objects.
 type AllocationProperties struct {
 	Cluster        string                `json:"cluster,omitempty"`
@@ -160,6 +157,8 @@ func (p *AllocationProperties) Equal(that *AllocationProperties) bool {
 				return false
 			}
 		}
+	} else {
+		return false
 	}
 
 	pAnnotations := p.Annotations
@@ -189,6 +188,7 @@ func (p *AllocationProperties) Equal(that *AllocationProperties) bool {
 	} else {
 		return false
 	}
+
 	return true
 }
 
@@ -283,72 +283,3 @@ func (p *AllocationProperties) String() string {
 
 	return fmt.Sprintf("{%s}", strings.Join(strs, "; "))
 }
-
-// AggregationStrings converts a AllocationProperties object into a slice of strings
-// representing a request to aggregate by certain properties.
-// NOTE: today, the ordering of the properties *has to match the ordering
-// of the allocation function generateKey*
-func (p *AllocationProperties) AggregationStrings() []string {
-	if p == nil {
-		return []string{}
-	}
-
-	aggStrs := []string{}
-	if p.Cluster != "" {
-		aggStrs = append(aggStrs, AllocationClusterProp.String())
-	}
-	if p.Node != "" {
-		aggStrs = append(aggStrs, AllocationNodeProp.String())
-	}
-	if p.Container != "" {
-		aggStrs = append(aggStrs, AllocationContainerProp.String())
-	}
-	if p.Controller != "" {
-		aggStrs = append(aggStrs, AllocationControllerProp.String())
-	}
-	if p.ControllerKind != "" {
-		aggStrs = append(aggStrs, AllocationControllerKindProp.String())
-	}
-	if p.Namespace != "" {
-		aggStrs = append(aggStrs, AllocationNamespaceProp.String())
-	}
-	if p.Pod != "" {
-		aggStrs = append(aggStrs, AllocationPodProp.String())
-	}
-	if p.ProviderID != "" {
-		aggStrs = append(aggStrs, AllocationProviderIDProp.String())
-	}
-	if len(p.Services) > 0 {
-		aggStrs = append(aggStrs, AllocationServiceProp.String())
-	}
-	
-	if len(p.Labels) > 0  {
-		// e.g. expect format map[string]string{
-		// 	 "env":""
-		// 	 "app":"",
-		// }
-		// for aggregating by "label:app,label:env"
-		labels := p.Labels
-		labelAggStrs := []string{}
-		for labelName := range labels {
-			labelAggStrs = append(labelAggStrs, fmt.Sprintf("label:%s", labelName))
-		}
-		if len(labelAggStrs) > 0 {
-			// Enforce alphabetical ordering, then append to aggStrs
-			sort.Strings(labelAggStrs)
-			for _, labelName := range labelAggStrs {
-				aggStrs = append(aggStrs, labelName)
-			}
-		}
-	}
-
-
-	return aggStrs
-}
-
-func (p *AllocationProperties) IsEmpty() bool {
-	if p == nil {
-		return true
-	}
-	return p.Equal(&AllocationProperties{})
-}

+ 0 - 54
pkg/kubecost/allocationprops_test.go

@@ -1,65 +1,11 @@
 package kubecost
 
-import "testing"
-
 // TODO niko/etl
 // func TestParseProperty(t *testing.T) {}
 
 // TODO niko/etl
 // func TestProperty_String(t *testing.T) {}
 
-func TestProperties_AggregationString(t *testing.T) {
-	var props *AllocationProperties
-	var aggStrs []string
-
-	// nil AllocationProperties should produce and empty slice
-	aggStrs = props.AggregationStrings()
-	if aggStrs == nil || len(aggStrs) > 0 {
-		t.Fatalf("expected empty slice; got %v", aggStrs)
-	}
-
-	// empty AllocationProperties should product an empty slice
-	props = &AllocationProperties{}
-	aggStrs = props.AggregationStrings()
-	if aggStrs == nil || len(aggStrs) > 0 {
-		t.Fatalf("expected empty slice; got %v", aggStrs)
-	}
-
-	// AllocationProperties with single, simple property set
-	props = &AllocationProperties{}
-	props.Namespace = "true"
-	aggStrs = props.AggregationStrings()
-	if len(aggStrs) != 1 || aggStrs[0] != "namespace" {
-		t.Fatalf("expected [\"namespace\"]; got %v", aggStrs)
-	}
-
-	// AllocationProperties with mutiple properties, including labels
-	// Note: order matters!
-	props = &AllocationProperties{}
-	props.Namespace = "true"
-	props.Labels = map[string]string{
-		"env": "",
-		"app": "",
-	}
-	props.Cluster = "true"
-	aggStrs = props.AggregationStrings()
-	if len(aggStrs) != 4 {
-		t.Fatalf("expected length %d; got lenfth %d", 4, len(aggStrs))
-	}
-	if aggStrs[0] != "cluster" {
-		t.Fatalf("expected aggStrs[0] == \"%s\"; got \"%s\"", "cluster", aggStrs[0])
-	}
-	if aggStrs[1] != "namespace" {
-		t.Fatalf("expected aggStrs[1] == \"%s\"; got \"%s\"", "namespace", aggStrs[1])
-	}
-	if aggStrs[2] != "label:app" {
-		t.Fatalf("expected aggStrs[2] == \"%s\"; got \"%s\"", "label:app", aggStrs[2])
-	}
-	if aggStrs[3] != "label:env" {
-		t.Fatalf("expected aggStrs[3] == \"%s\"; got \"%s\"", "label:env", aggStrs[3])
-	}
-}
-
 // TODO niko/etl
 // func TestProperties_Clone(t *testing.T) {}
 

+ 8 - 8
pkg/kubecost/asset.go

@@ -184,21 +184,21 @@ func AssetToExternalAllocation(asset Asset, aggregateBy []string, externalLabels
 
 				// Set the corresponding property on props
 				switch aggBy {
-				case AllocationClusterProp.String():
+				case AllocationClusterProp:
 					props.Cluster = value
-				case AllocationNodeProp.String():
+				case AllocationNodeProp:
 					props.Node = value
-				case AllocationNamespaceProp.String():
+				case AllocationNamespaceProp:
 					props.Namespace = value
-				case AllocationControllerKindProp.String():
+				case AllocationControllerKindProp:
 					props.ControllerKind = value
-				case AllocationControllerProp.String():
+				case AllocationControllerProp:
 					props.Controller = value
-				case AllocationPodProp.String():
+				case AllocationPodProp:
 					props.Pod = value
-				case AllocationContainerProp.String():
+				case AllocationContainerProp:
 					props.Container = value
-				case AllocationServiceProp.String():
+				case AllocationServiceProp:
 					// TODO: external allocation: how to do this? multi-service?
 					props.Services = []string{value}
 				}