Quellcode durchsuchen

CostModel.ComputeAllocation: prune duplicate services and controllers

Niko Kovacevic vor 5 Jahren
Ursprung
Commit
8ab05a3272
1 geänderte Dateien mit 31 neuen und 0 gelöschten Zeilen
  1. 31 0
      pkg/costmodel/allocation.go

+ 31 - 0
pkg/costmodel/allocation.go

@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"math"
 	"strconv"
+	"strings"
 	"time"
 
 	"github.com/kubecost/cost-model/pkg/cloud"
@@ -984,6 +985,16 @@ func getServiceLabels(resServiceLabels []*prom.QueryResult) map[serviceKey]map[s
 		}
 	}
 
+	// Prune duplicate services. That is, if the same service exists with
+	// hyphens instead of underscores, keep the one that uses hyphens.
+	for key := range serviceLabels {
+		duplicateService := strings.Replace(key.Service, "_", "-", -1)
+		duplicateKey := newServiceKey(key.Cluster, key.Namespace, duplicateService)
+		if _, ok := serviceLabels[duplicateKey]; ok {
+			delete(serviceLabels, key)
+		}
+	}
+
 	return serviceLabels
 }
 
@@ -1005,6 +1016,16 @@ func resToDeploymentLabels(resDeploymentLabels []*prom.QueryResult) map[controll
 		}
 	}
 
+	// Prune duplicate deployments. That is, if the same deployment exists with
+	// hyphens instead of underscores, keep the one that uses hyphens.
+	for key := range deploymentLabels {
+		duplicateController := strings.Replace(key.Controller, "_", "-", -1)
+		duplicateKey := newControllerKey(key.Cluster, key.Namespace, key.ControllerKind, duplicateController)
+		if _, ok := deploymentLabels[duplicateKey]; ok {
+			delete(deploymentLabels, key)
+		}
+	}
+
 	return deploymentLabels
 }
 
@@ -1026,6 +1047,16 @@ func resToStatefulSetLabels(resStatefulSetLabels []*prom.QueryResult) map[contro
 		}
 	}
 
+	// Prune duplicate stateful sets. That is, if the same stateful set exists
+	// with hyphens instead of underscores, keep the one that uses hyphens.
+	for key := range statefulSetLabels {
+		duplicateController := strings.Replace(key.Controller, "_", "-", -1)
+		duplicateKey := newControllerKey(key.Cluster, key.Namespace, key.ControllerKind, duplicateController)
+		if _, ok := statefulSetLabels[duplicateKey]; ok {
+			delete(statefulSetLabels, key)
+		}
+	}
+
 	return statefulSetLabels
 }