func estimateCost()

in cost-optimization/gke-shift-left-cost/api/types.go [290:326]


func estimateCost(kind string, r HorizontalScalableResource, rp ResourcePrice) CostRange {
	cost := CostRange{Kind: kind}
	cpuReq, cpuLim, memReq, memLim := totalContainers(r.getContainers())

	var cpuMonthlyPrice = float64(rp.CPUMonthlyPrice())
	var memoryMonthlyPrice = float64(rp.MemoryMonthlyPrice())

	if r.hasHPA() {
		hpa := r.getHPA()
		targetCPUPercentage := hpa.TargetCPUPercentage
		minReplicas := float64(hpa.MinReplicas)
		maxReplicas := float64(hpa.MaxReplicas)

		cost.MinRequested = (minReplicas * cpuReq * cpuMonthlyPrice) + (minReplicas * memReq * memoryMonthlyPrice)
		cost.MaxRequested = (maxReplicas * cpuReq * cpuMonthlyPrice) + (maxReplicas * memReq * memoryMonthlyPrice)

		cpuBuffer := minReplicas
		if targetCPUPercentage > 0 {
			buff := float64(100-targetCPUPercentage) / 100
			cpuBuffer = minReplicas + (buff * minReplicas)
		}
		cost.HPABuffer = (cpuBuffer * cpuReq * cpuMonthlyPrice) + (cpuBuffer * memReq * memoryMonthlyPrice)

		cost.MinLimited = (minReplicas * cpuLim * cpuMonthlyPrice) + (minReplicas * memLim * memoryMonthlyPrice)
		cost.MaxLimited = (maxReplicas * cpuLim * cpuMonthlyPrice) + (maxReplicas * memLim * memoryMonthlyPrice)

	} else {
		replicas := float64(r.getReplicas())
		cost.MinRequested = (replicas * cpuReq * cpuMonthlyPrice) + (replicas * memReq * memoryMonthlyPrice)
		cost.MaxRequested = cost.MinRequested
		cost.HPABuffer = cost.MinRequested
		cost.MinLimited = (replicas * cpuLim * cpuMonthlyPrice) + (replicas * memLim * memoryMonthlyPrice)
		cost.MaxLimited = cost.MinLimited
	}

	return postProcessCost(cost)
}