Kaynağa Gözat

Merge commit 'a0e5fcb6ee499e590e96f9eea245bd392069a66c' into sean/gpu-count-bugs

Sean Holcomb 5 yıl önce
ebeveyn
işleme
7ec70fa554

+ 18 - 12
pkg/costmodel/allocation.go

@@ -970,10 +970,12 @@ 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)
+		if strings.Contains(key.Service, "_") {
+			duplicateService := strings.Replace(key.Service, "_", "-", -1)
+			duplicateKey := newServiceKey(key.Cluster, key.Namespace, duplicateService)
+			if _, ok := serviceLabels[duplicateKey]; ok {
+				delete(serviceLabels, key)
+			}
 		}
 	}
 
@@ -1001,10 +1003,12 @@ 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)
+		if strings.Contains(key.Controller, "_") {
+			duplicateController := strings.Replace(key.Controller, "_", "-", -1)
+			duplicateKey := newControllerKey(key.Cluster, key.Namespace, key.ControllerKind, duplicateController)
+			if _, ok := deploymentLabels[duplicateKey]; ok {
+				delete(deploymentLabels, key)
+			}
 		}
 	}
 
@@ -1032,10 +1036,12 @@ 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)
+		if strings.Contains(key.Controller, "_") {
+			duplicateController := strings.Replace(key.Controller, "_", "-", -1)
+			duplicateKey := newControllerKey(key.Cluster, key.Namespace, key.ControllerKind, duplicateController)
+			if _, ok := statefulSetLabels[duplicateKey]; ok {
+				delete(statefulSetLabels, key)
+			}
 		}
 	}
 

+ 1 - 1
pkg/env/costmodelenv.go

@@ -70,7 +70,7 @@ const (
 // GetAWSAccessKeyID returns the environment variable value for AWSAccessKeyIDEnvVar which represents
 // the AWS access key for authentication
 func GetAppVersion() string {
-	return Get(AppVersionEnvVar, "1.75.1")
+	return Get(AppVersionEnvVar, "1.76.0")
 }
 
 // IsEmitNamespaceAnnotationsMetric returns true if cost-model is configured to emit the kube_namespace_annotations metric

+ 1 - 0
pkg/kubecost/allocation.go

@@ -684,6 +684,7 @@ func (as *AllocationSet) AggregateBy(properties Properties, options *AllocationA
 				Start:      as.Start(),
 				End:        as.End(),
 				SharedCost: totalSharedCost,
+				Properties: Properties{ClusterProp: SharedSuffix}, // The allocation needs to belong to a cluster,but it really doesn't matter which one, so just make it clear.
 			})
 		}
 	}

+ 41 - 3
pkg/kubecost/allocation_test.go

@@ -430,9 +430,6 @@ func TestAllocation_MarshalJSON(t *testing.T) {
 	// In the meantime, just set the Window so that we can test the rest.
 	after.Window = before.Window.Clone()
 
-	fmt.Println(*before)
-	fmt.Println(*after)
-
 	if !after.Equal(before) {
 		t.Fatalf("Allocation.MarshalJSON: before and after are not equal")
 	}
@@ -1327,6 +1324,47 @@ func TestAllocationSet_AggregateBy(t *testing.T) {
 	})
 	assertAllocationWindow(t, as, "6g", startYesterday, endYesterday, 1440.0)
 
+	// 6h Share idle, share resources, share overhead
+	//
+	// Share idle weighted:
+	//
+	// namespace1:      39.6875
+	//   initial cost   25.0000
+	//   cluster1.cpu    2.5000 = 5.00*(3.00/6.00)
+	//   cluster1.ram   12.1875 = 15.00*(13.0/16.0)
+	//
+	// namespace2:      40.3125
+	//   initial cost   30.0000
+	//   cluster1.cpu    2.5000 = 5.00*(3.0/6.0)
+	//   cluster1.ram    2.8125 = 15.00*(3.0/16.0)
+	//   cluster2.cpu    2.5000 = 5.00*(3.0/6.0)
+	//   cluster2.ram    2.5000 = 5.00*(3.0/6.0)
+	//
+	// namespace3:      20.0000
+	//   initial cost   15.0000
+	//   cluster2.cpu    2.5000 = 5.00*(3.0/6.0)
+	//   cluster2.ram    2.5000 = 5.00*(3.0/6.0)
+	//
+	// Then share overhead:
+	//
+	// namespace1:      99.6875 = 39.6875 + (7.0*24.0)*(25.00/70.00)
+	// namespace2:     112.3125 = 40.3125 + (7.0*24.0)*(30.00/70.00)
+	// namespace3:      56.0000 = 20.0000 + (7.0*24.0)*(15.00/70.00)
+	//
+	// Then namespace 2 is filtered.
+	as = generateAllocationSet(start)
+	err = as.AggregateBy(Properties{NamespaceProp: ""}, &AllocationAggregationOptions{
+		FilterFuncs:       []AllocationMatchFunc{isNamespace("namespace2")},
+		ShareSplit:        ShareWeighted,
+		ShareIdle:         ShareWeighted,
+		SharedHourlyCosts: map[string]float64{"total": sharedOverheadHourlyCost},
+	})
+	assertAllocationSetTotals(t, as, "6h", err, 1, 112.31)
+	assertAllocationTotals(t, as, "6h", map[string]float64{
+		"namespace2": 112.31,
+	})
+	assertAllocationWindow(t, as, "6h", startYesterday, endYesterday, 1440.0)
+
 	// 7  Edge cases and errors
 
 	// 7a Empty AggregationProperties

+ 2 - 0
pkg/kubecost/status.go

@@ -8,6 +8,8 @@ type ETLStatus struct {
 	LastRun     time.Time        `json:"lastRun"`
 	Progress    float64          `json:"progress"`
 	RefreshRate string           `json:"refreshRate"`
+	Resolution  string           `json:"resolution"`
+	MaxBatch    string           `json:"maxBatch"`
 	StartTime   time.Time        `json:"startTime"`
 	UTCOffset   string           `json:"utcOffset"`
 	Backup      *DirectoryStatus `json:"backup,omitempty"`