Преглед изворни кода

Added NetworkTransferBytes and NetworkReceiveBytes to Allocation.

Matt Bolt пре 4 година
родитељ
комит
aeae08cf08
4 измењених фајлова са 98 додато и 42 уклоњено
  1. 46 0
      pkg/costmodel/allocation.go
  2. 3 1
      pkg/kubecost/allocation.go
  3. 1 1
      pkg/kubecost/bingen.go
  4. 48 40
      pkg/kubecost/kubecost_codecs.go

+ 46 - 0
pkg/costmodel/allocation.go

@@ -51,6 +51,8 @@ const (
 	queryFmtNetRegionCostPerGiB   = `avg(avg_over_time(kubecost_network_region_egress_cost{}[%s]%s)) by (%s)`
 	queryFmtNetInternetGiB        = `sum(increase(kubecost_pod_network_egress_bytes_total{internet="true"}[%s]%s)) by (pod_name, namespace, %s) / 1024 / 1024 / 1024`
 	queryFmtNetInternetCostPerGiB = `avg(avg_over_time(kubecost_network_internet_egress_cost{}[%s]%s)) by (%s)`
+	queryFmtNetReceiveBytes       = `sum(increase(container_network_receive_bytes_total{pod_name!=""}[%s]%s)) by (pod_name, namespace, %s)`
+	queryFmtNetTransferBytes      = `sum(increase(container_network_transmit_bytes_total{pod_name!=""}[%s]%s)) by (pod_name, namespace, %s)`
 	queryFmtNamespaceLabels       = `avg_over_time(kube_namespace_labels[%s]%s)`
 	queryFmtNamespaceAnnotations  = `avg_over_time(kube_namespace_annotations[%s]%s)`
 	queryFmtPodLabels             = `avg_over_time(kube_pod_labels[%s]%s)`
@@ -180,6 +182,12 @@ func (cm *CostModel) ComputeAllocation(start, end time.Time, resolution time.Dur
 	queryPVCostPerGiBHour := fmt.Sprintf(queryFmtPVCostPerGiBHour, durStr, offStr, env.GetPromClusterLabel())
 	resChPVCostPerGiBHour := ctx.Query(queryPVCostPerGiBHour)
 
+	queryNetTransferBytes := fmt.Sprintf(queryFmtNetTransferBytes, durStr, offStr, env.GetPromClusterLabel())
+	resChNetTransferBytes := ctx.Query(queryNetTransferBytes)
+
+	queryNetReceiveBytes := fmt.Sprintf(queryFmtNetReceiveBytes, durStr, offStr, env.GetPromClusterLabel())
+	resChNetReceiveBytes := ctx.Query(queryNetReceiveBytes)
+
 	queryNetZoneGiB := fmt.Sprintf(queryFmtNetZoneGiB, durStr, offStr, env.GetPromClusterLabel())
 	resChNetZoneGiB := ctx.Query(queryNetZoneGiB)
 
@@ -253,6 +261,8 @@ func (cm *CostModel) ComputeAllocation(start, end time.Time, resolution time.Dur
 	resPVCBytesRequested, _ := resChPVCBytesRequested.Await()
 	resPodPVCAllocation, _ := resChPodPVCAllocation.Await()
 
+	resNetTransferBytes, _ := resChNetTransferBytes.Await()
+	resNetReceiveBytes, _ := resChNetReceiveBytes.Await()
 	resNetZoneGiB, _ := resChNetZoneGiB.Await()
 	resNetZoneCostPerGiB, _ := resChNetZoneCostPerGiB.Await()
 	resNetRegionGiB, _ := resChNetRegionGiB.Await()
@@ -292,6 +302,7 @@ func (cm *CostModel) ComputeAllocation(start, end time.Time, resolution time.Dur
 	applyRAMBytesUsedAvg(podMap, resRAMUsageAvg)
 	applyRAMBytesUsedMax(podMap, resRAMUsageMax)
 	applyGPUsRequested(podMap, resGPUsRequested)
+	applyNetworkTotals(podMap, resNetTransferBytes, resNetReceiveBytes)
 	applyNetworkAllocation(podMap, resNetZoneGiB, resNetZoneCostPerGiB)
 	applyNetworkAllocation(podMap, resNetRegionGiB, resNetRegionCostPerGiB)
 	applyNetworkAllocation(podMap, resNetInternetGiB, resNetInternetCostPerGiB)
@@ -906,6 +917,41 @@ func applyGPUsRequested(podMap map[podKey]*Pod, resGPUsRequested []*prom.QueryRe
 	}
 }
 
+func applyNetworkTotals(podMap map[podKey]*Pod, resNetworkTransferBytes []*prom.QueryResult, resNetworkReceiveBytes []*prom.QueryResult) {
+	for _, res := range resNetworkTransferBytes {
+		podKey, err := resultPodKey(res, env.GetPromClusterLabel(), "namespace", "pod_name")
+		if err != nil {
+			log.DedupedWarningf(10, "CostModel.ComputeAllocation: Network Transfer Bytes query result missing field: %s", err)
+			continue
+		}
+
+		pod, ok := podMap[podKey]
+		if !ok {
+			continue
+		}
+
+		for _, alloc := range pod.Allocations {
+			alloc.NetworkTransferBytes = res.Values[0].Value / float64(len(pod.Allocations))
+		}
+	}
+	for _, res := range resNetworkReceiveBytes {
+		podKey, err := resultPodKey(res, env.GetPromClusterLabel(), "namespace", "pod_name")
+		if err != nil {
+			log.DedupedWarningf(10, "CostModel.ComputeAllocation: Network Receive Bytes query result missing field: %s", err)
+			continue
+		}
+
+		pod, ok := podMap[podKey]
+		if !ok {
+			continue
+		}
+
+		for _, alloc := range pod.Allocations {
+			alloc.NetworkReceiveBytes = res.Values[0].Value / float64(len(pod.Allocations))
+		}
+	}
+}
+
 func applyNetworkAllocation(podMap map[podKey]*Pod, resNetworkGiB []*prom.QueryResult, resNetworkCostPerGiB []*prom.QueryResult) {
 	costPerGiBByCluster := map[string]float64{}
 

+ 3 - 1
pkg/kubecost/allocation.go

@@ -63,6 +63,8 @@ type Allocation struct {
 	GPUHours                   float64               `json:"gpuHours"`
 	GPUCost                    float64               `json:"gpuCost"`
 	GPUCostAdjustment          float64               `json:"gpuCostAdjustment"`
+	NetworkTransferBytes       float64               `json:"networkTransferBytes"`
+	NetworkReceiveBytes        float64               `json:"networkReceiveBytes"`
 	NetworkCost                float64               `json:"networkCost"`
 	NetworkCostAdjustment      float64               `json:"networkCostAdjustment"`
 	LoadBalancerCost           float64               `json:"loadBalancerCost"`
@@ -934,7 +936,7 @@ func (as *AllocationSet) AggregateBy(aggregateBy []string, options *AllocationAg
 	for _, alloc := range as.allocations {
 		idleId, err := alloc.getIdleId(options)
 		if err != nil {
-			log.DedupedWarningf(3,"AllocationSet.AggregateBy: missing idleId for allocation: %s", alloc.Name)
+			log.DedupedWarningf(3, "AllocationSet.AggregateBy: missing idleId for allocation: %s", alloc.Name)
 		}
 
 		skip := false

+ 1 - 1
pkg/kubecost/bingen.go

@@ -29,4 +29,4 @@ package kubecost
 // @bingen:generate:PVKey
 // @bingen:generate:PVAllocation
 
-//go:generate bingen -package=kubecost -version=14 -buffer=github.com/kubecost/cost-model/pkg/util
+//go:generate bingen -package=kubecost -version=15 -buffer=github.com/kubecost/cost-model/pkg/util

+ 48 - 40
pkg/kubecost/kubecost_codecs.go

@@ -26,7 +26,7 @@ const (
 	GeneratorPackageName string = "kubecost"
 
 	// CodecVersion is the version passed into the generator
-	CodecVersion uint8 = 14
+	CodecVersion uint8 = 15
 )
 
 //--------------------------------------------------------------------------
@@ -171,6 +171,8 @@ func (target *Allocation) MarshalBinary() (data []byte, err error) {
 	buff.WriteFloat64(target.GPUHours)                   // write float64
 	buff.WriteFloat64(target.GPUCost)                    // write float64
 	buff.WriteFloat64(target.GPUCostAdjustment)          // write float64
+	buff.WriteFloat64(target.NetworkTransferBytes)       // write float64
+	buff.WriteFloat64(target.NetworkReceiveBytes)        // write float64
 	buff.WriteFloat64(target.NetworkCost)                // write float64
 	buff.WriteFloat64(target.NetworkCostAdjustment)      // write float64
 	buff.WriteFloat64(target.LoadBalancerCost)           // write float64
@@ -340,35 +342,41 @@ func (target *Allocation) UnmarshalBinary(data []byte) (err error) {
 	target.GPUCostAdjustment = x
 
 	y := buff.ReadFloat64() // read float64
-	target.NetworkCost = y
+	target.NetworkTransferBytes = y
 
 	aa := buff.ReadFloat64() // read float64
-	target.NetworkCostAdjustment = aa
+	target.NetworkReceiveBytes = aa
 
 	bb := buff.ReadFloat64() // read float64
-	target.LoadBalancerCost = bb
+	target.NetworkCost = bb
 
 	cc := buff.ReadFloat64() // read float64
-	target.LoadBalancerCostAdjustment = cc
+	target.NetworkCostAdjustment = cc
+
+	dd := buff.ReadFloat64() // read float64
+	target.LoadBalancerCost = dd
+
+	ee := buff.ReadFloat64() // read float64
+	target.LoadBalancerCostAdjustment = ee
 
 	// --- [begin][read][alias](PVAllocations) ---
-	var dd map[PVKey]*PVAllocation
+	var ff map[PVKey]*PVAllocation
 	if buff.ReadUInt8() == uint8(0) {
-		dd = nil
+		ff = nil
 	} else {
 		// --- [begin][read][map](map[PVKey]*PVAllocation) ---
-		ff := buff.ReadInt() // map len
-		ee := make(map[PVKey]*PVAllocation, ff)
-		for i := 0; i < ff; i++ {
+		hh := buff.ReadInt() // map len
+		gg := make(map[PVKey]*PVAllocation, hh)
+		for i := 0; i < hh; i++ {
 			// --- [begin][read][struct](PVKey) ---
-			gg := &PVKey{}
-			hh := buff.ReadInt()     // byte array length
-			kk := buff.ReadBytes(hh) // byte array
-			errE := gg.UnmarshalBinary(kk)
+			kk := &PVKey{}
+			ll := buff.ReadInt()     // byte array length
+			mm := buff.ReadBytes(ll) // byte array
+			errE := kk.UnmarshalBinary(mm)
 			if errE != nil {
 				return errE
 			}
-			v := *gg
+			v := *kk
 			// --- [end][read][struct](PVKey) ---
 
 			var z *PVAllocation
@@ -376,62 +384,62 @@ func (target *Allocation) UnmarshalBinary(data []byte) (err error) {
 				z = nil
 			} else {
 				// --- [begin][read][struct](PVAllocation) ---
-				ll := &PVAllocation{}
-				mm := buff.ReadInt()     // byte array length
-				nn := buff.ReadBytes(mm) // byte array
-				errF := ll.UnmarshalBinary(nn)
+				nn := &PVAllocation{}
+				oo := buff.ReadInt()     // byte array length
+				pp := buff.ReadBytes(oo) // byte array
+				errF := nn.UnmarshalBinary(pp)
 				if errF != nil {
 					return errF
 				}
-				z = ll
+				z = nn
 				// --- [end][read][struct](PVAllocation) ---
 
 			}
-			ee[v] = z
+			gg[v] = z
 		}
-		dd = ee
+		ff = gg
 		// --- [end][read][map](map[PVKey]*PVAllocation) ---
 
 	}
-	target.PVs = PVAllocations(dd)
+	target.PVs = PVAllocations(ff)
 	// --- [end][read][alias](PVAllocations) ---
 
-	oo := buff.ReadFloat64() // read float64
-	target.PVCostAdjustment = oo
-
-	pp := buff.ReadFloat64() // read float64
-	target.RAMByteHours = pp
-
 	qq := buff.ReadFloat64() // read float64
-	target.RAMBytesRequestAverage = qq
+	target.PVCostAdjustment = qq
 
 	rr := buff.ReadFloat64() // read float64
-	target.RAMBytesUsageAverage = rr
+	target.RAMByteHours = rr
 
 	ss := buff.ReadFloat64() // read float64
-	target.RAMCost = ss
+	target.RAMBytesRequestAverage = ss
 
 	tt := buff.ReadFloat64() // read float64
-	target.RAMCostAdjustment = tt
+	target.RAMBytesUsageAverage = tt
 
 	uu := buff.ReadFloat64() // read float64
-	target.SharedCost = uu
+	target.RAMCost = uu
 
 	ww := buff.ReadFloat64() // read float64
-	target.ExternalCost = ww
+	target.RAMCostAdjustment = ww
+
+	xx := buff.ReadFloat64() // read float64
+	target.SharedCost = xx
+
+	yy := buff.ReadFloat64() // read float64
+	target.ExternalCost = yy
 
 	if buff.ReadUInt8() == uint8(0) {
 		target.RawAllocationOnly = nil
 	} else {
 		// --- [begin][read][struct](RawAllocationOnlyData) ---
-		xx := &RawAllocationOnlyData{}
-		yy := buff.ReadInt()      // byte array length
-		aaa := buff.ReadBytes(yy) // byte array
-		errG := xx.UnmarshalBinary(aaa)
+		aaa := &RawAllocationOnlyData{}
+		bbb := buff.ReadInt()      // byte array length
+		ccc := buff.ReadBytes(bbb) // byte array
+		errG := aaa.UnmarshalBinary(ccc)
 		if errG != nil {
 			return errG
 		}
-		target.RawAllocationOnly = xx
+		target.RawAllocationOnly = aaa
 		// --- [end][read][struct](RawAllocationOnlyData) ---
 
 	}