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

Less computationally expensive logic

(in theory, hasn't been benchmarked properly)
Michael Dresser 4 лет назад
Родитель
Сommit
bddecb5911
1 измененных файлов с 15 добавлено и 15 удалено
  1. 15 15
      pkg/kubecost/allocation.go

+ 15 - 15
pkg/kubecost/allocation.go

@@ -603,28 +603,28 @@ func (a *Allocation) add(that *Allocation) {
 		return
 		return
 	}
 	}
 
 
-	// Save the original properties so we can set a controller name in the event of
-	// equality in all fields except the controller name
-	leftProperties := a.Properties
+	// Generate keys for each allocation to allow for special logic to set the controller
+	// in the case of keys matching but controllers not matching.
+	aggByForKey := []string{"cluster", "node", "namespace", "pod", "container"}
+	leftKey := a.generateKey(aggByForKey, nil)
+	rightKey := a.generateKey(aggByForKey, nil)
+	leftProperties := a.Properties.Clone()
 	rightProperties := that.Properties
 	rightProperties := that.Properties
 
 
 	// Preserve string properties that are matching between the two allocations
 	// Preserve string properties that are matching between the two allocations
 	a.Properties = a.Properties.Intersection(that.Properties)
 	a.Properties = a.Properties.Intersection(that.Properties)
 
 
 	// Overwrite regular intersection logic for the controller name property in the
 	// Overwrite regular intersection logic for the controller name property in the
-	// case that all properties except controller name are the same.
-	if leftProperties != nil &&
+	// case that the Allocation keys are the same but the controllers are not.
+	if leftKey == rightKey &&
+		leftProperties != nil &&
 		rightProperties != nil &&
 		rightProperties != nil &&
-		leftProperties.Controller != rightProperties.Controller &&
-		leftProperties.Controller != "" &&
-		rightProperties.Controller != "" {
-
-		lpNoController := leftProperties.Clone()
-		lpNoController.Controller = ""
-		rpNoController := rightProperties.Clone()
-		rpNoController.Controller = ""
-
-		if lpNoController.Equal(rpNoController) {
+		leftProperties.Controller != rightProperties.Controller {
+		if leftProperties.Controller == "" {
+			a.Properties.Controller = rightProperties.Controller
+		} else if rightProperties.Controller == "" {
+			a.Properties.Controller = leftProperties.Controller
+		} else {
 			controllers := []string{
 			controllers := []string{
 				leftProperties.Controller,
 				leftProperties.Controller,
 				rightProperties.Controller,
 				rightProperties.Controller,