Skip to content

Instantly share code, notes, and snippets.

@ddepaoli3
Created July 17, 2024 08:37
Show Gist options
  • Save ddepaoli3/cb9e5e2c804a1123359be9513d305aba to your computer and use it in GitHub Desktop.
Save ddepaoli3/cb9e5e2c804a1123359be9513d305aba to your computer and use it in GitHub Desktop.
Fargate calculator from kubectl for Ireland
#!/usr/bin/env python
# it requires input from: kubectl get pod -A -o json
import sys
import json
COST_CPU_HOUR = 0.04048
COST_MEMORY_HOUR = 0.004445
COST_STORAGE_HOUR = 0.000122
VOLUME_SIZE_PER_POD = 20
def helper():
print("Usage: kubectl get pod -A -o json | python fargate-cost.py")
def process_input(data=None):
# Parse the input JSON
try:
json_data = json.loads(data)
except json.JSONDecodeError:
print("Invalid JSON input.")
return
# Extract the value of CapacityProvisioned
try:
all_capacity = []
cpu_capacity = []
memory_capacity = []
for item in json_data["items"]:
all_capacity.append(item["metadata"]["annotations"]["CapacityProvisioned"])
cpu_capacity.append(
float(
item["metadata"]["annotations"]["CapacityProvisioned"].split(
"vCPU"
)[0]
)
)
memory_capacity.append(
float(
item["metadata"]["annotations"]["CapacityProvisioned"]
.split(" ")[1]
.split("GB")[0]
)
)
except Exception as e:
print(e)
print("Impossible to evaluate cost of fargate")
helper()
return
# Use the capacity_provisioned variable as needed
total_cpu = sum(cpu_capacity)
total_memory = sum(memory_capacity)
cpu_cost = total_cpu * COST_CPU_HOUR * 24 * 30
memory_cost = total_memory * COST_MEMORY_HOUR * 24 * 30
storage_cost = len(cpu_capacity) * VOLUME_SIZE_PER_POD * COST_STORAGE_HOUR * 24 * 30
print(f"Total CPU: {total_cpu} vCPU, Total Memory: {total_memory} GB")
print(
f"Estimated cost for CPU: ${cpu_cost:.2f}, Memory: ${memory_cost:.2f}, Storage: ${storage_cost:.2f}"
)
print(f"Total estimated cost: ${cpu_cost + memory_cost + storage_cost:.2f}")
if __name__ == "__main__":
if sys.stdin.isatty():
print("No standard input provided.")
helper()
else:
data = sys.stdin.read()
process_input(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment