Sfoglia il codice sorgente

provide mechanism to control LB PARC behavior

Signed-off-by: Alex Meijer <ameijer@kubecost.com>
Alex Meijer 2 anni fa
parent
commit
2dd0c8bf97
2 ha cambiato i file con 18 aggiunte e 6 eliminazioni
  1. 13 6
      pkg/costmodel/costmodel.go
  2. 5 0
      pkg/env/costmodelenv.go

+ 13 - 6
pkg/costmodel/costmodel.go

@@ -2383,7 +2383,7 @@ func (cm *CostModel) QueryAllocation(window kubecost.Window, resolution, step ti
 	// appending each to the response.
 	// appending each to the response.
 	stepStart := *window.Start()
 	stepStart := *window.Start()
 	stepEnd := stepStart.Add(step)
 	stepEnd := stepStart.Add(step)
-	var isAzure bool
+	var isAKS bool
 	for window.End().After(stepStart) {
 	for window.End().After(stepStart) {
 		allocSet, err := cm.ComputeAllocation(stepStart, stepEnd, resolution)
 		allocSet, err := cm.ComputeAllocation(stepStart, stepEnd, resolution)
 		if err != nil {
 		if err != nil {
@@ -2404,7 +2404,7 @@ func (cm *CostModel) QueryAllocation(window kubecost.Window, resolution, step ti
 				// we must know if this is an AKS cluster
 				// we must know if this is an AKS cluster
 				for _, node := range assetSet.Nodes {
 				for _, node := range assetSet.Nodes {
 					if _, found := node.Labels["label_kubernetes_azure_com_cluster"]; found {
 					if _, found := node.Labels["label_kubernetes_azure_com_cluster"]; found {
-						isAzure = true
+						isAKS = true
 						break
 						break
 					}
 					}
 				}
 				}
@@ -2484,7 +2484,7 @@ func (cm *CostModel) QueryAllocation(window kubecost.Window, resolution, step ti
 			}
 			}
 
 
 			var totalPublicLbCost, totalPrivateLbCost float64
 			var totalPublicLbCost, totalPrivateLbCost float64
-			if isAzure {
+			if isAKS && env.IsSingleLB() {
 				// loop through all assetTotals, adding all load balancer costs by public and private
 				// loop through all assetTotals, adding all load balancer costs by public and private
 				for _, tot := range totalStoreByNode {
 				for _, tot := range totalStoreByNode {
 					if tot.PrivateLoadBalancer {
 					if tot.PrivateLoadBalancer {
@@ -2499,6 +2499,10 @@ func (cm *CostModel) QueryAllocation(window kubecost.Window, resolution, step ti
 			for _, alloc := range as.Allocations {
 			for _, alloc := range as.Allocations {
 				for rawKey, parc := range alloc.ProportionalAssetResourceCosts {
 				for rawKey, parc := range alloc.ProportionalAssetResourceCosts {
 
 
+					if parc.Type == "LoadBalancer" {
+						log.Debug("brk")
+					}
+
 					key := strings.TrimSuffix(strings.ReplaceAll(rawKey, ",", "/"), "/")
 					key := strings.TrimSuffix(strings.ReplaceAll(rawKey, ",", "/"), "/")
 					// for each parc , check the totals store for each
 					// for each parc , check the totals store for each
 					// on a totals hit, set the corresponding total and calculate percentage
 					// on a totals hit, set the corresponding total and calculate percentage
@@ -2521,9 +2525,7 @@ func (cm *CostModel) QueryAllocation(window kubecost.Window, resolution, step ti
 					parc.GPUTotalCost = totals.GPUCost
 					parc.GPUTotalCost = totals.GPUCost
 					parc.RAMTotalCost = totals.RAMCost
 					parc.RAMTotalCost = totals.RAMCost
 					parc.PVTotalCost = totals.PersistentVolumeCost
 					parc.PVTotalCost = totals.PersistentVolumeCost
-					if !isAzure {
-						parc.LoadBalancerTotalCost = totals.LoadBalancerCost
-					} else if len(alloc.LoadBalancers) > 0 {
+					if isAKS && env.IsSingleLB() && len(alloc.LoadBalancers) > 0 {
 						// Azure is a special case - use computed totals above
 						// Azure is a special case - use computed totals above
 						// use the lbAllocations in the object to determine if
 						// use the lbAllocations in the object to determine if
 						// this PARC is a public or private load balancer
 						// this PARC is a public or private load balancer
@@ -2538,8 +2540,13 @@ func (cm *CostModel) QueryAllocation(window kubecost.Window, resolution, step ti
 								parc.LoadBalancerTotalCost = totalPublicLbCost
 								parc.LoadBalancerTotalCost = totalPublicLbCost
 							}
 							}
 						}
 						}
+					} else {
+						parc.LoadBalancerTotalCost = totals.LoadBalancerCost
 					}
 					}
 
 
+					if parc.LoadBalancerProportionalCost > 0 {
+						log.Debug("brk")
+					}
 					kubecost.ComputePercentages(&parc)
 					kubecost.ComputePercentages(&parc)
 					alloc.ProportionalAssetResourceCosts[rawKey] = parc
 					alloc.ProportionalAssetResourceCosts[rawKey] = parc
 				}
 				}

+ 5 - 0
pkg/env/costmodelenv.go

@@ -86,6 +86,7 @@ const (
 	KubecostConfigBucketEnvVar    = "KUBECOST_CONFIG_BUCKET"
 	KubecostConfigBucketEnvVar    = "KUBECOST_CONFIG_BUCKET"
 	ClusterInfoFileEnabledEnvVar  = "CLUSTER_INFO_FILE_ENABLED"
 	ClusterInfoFileEnabledEnvVar  = "CLUSTER_INFO_FILE_ENABLED"
 	ClusterCacheFileEnabledEnvVar = "CLUSTER_CACHE_FILE_ENABLED"
 	ClusterCacheFileEnabledEnvVar = "CLUSTER_CACHE_FILE_ENABLED"
+	SingleLoadBalancerEnabled     = "SINGLE_LOAD_BALANCER"
 
 
 	PrometheusQueryOffsetEnvVar                 = "PROMETHEUS_QUERY_OFFSET"
 	PrometheusQueryOffsetEnvVar                 = "PROMETHEUS_QUERY_OFFSET"
 	PrometheusRetryOnRateLimitResponseEnvVar    = "PROMETHEUS_RETRY_ON_RATE_LIMIT"
 	PrometheusRetryOnRateLimitResponseEnvVar    = "PROMETHEUS_RETRY_ON_RATE_LIMIT"
@@ -112,6 +113,10 @@ const DefaultConfigMountPath = "/var/configs"
 
 
 var offsetRegex = regexp.MustCompile(`^(\+|-)(\d\d):(\d\d)$`)
 var offsetRegex = regexp.MustCompile(`^(\+|-)(\d\d):(\d\d)$`)
 
 
+func IsSingleLB() bool {
+	return GetBool(SingleLoadBalancerEnabled, false)
+}
+
 func IsETLReadOnlyMode() bool {
 func IsETLReadOnlyMode() bool {
 	return GetBool(ETLReadOnlyMode, false)
 	return GetBool(ETLReadOnlyMode, false)
 }
 }