#!/bin/sh

start_date=$1
end_date=$2

start_date=$start_date"T00:00:00Z"
end_date=$end_date"T00:00:00Z"

kubectl port-forward svc/kubecost-cost-analyzer 9090:9090 -n kubecost >/dev/null 2>&1 &
pid=$!

echo $( curl --output /dev/null --silent --head http://127.0.0.1:9090/ )
# Use netcat to wait for port forward to take effect (or just sleep for a second if you want)
until $(curl --output /dev/null --silent --head http://127.0.0.1:9090/); do
    printf '.'
    sleep 5
done

curl "http://localhost:9090/model/allocation?window=${start_date},${end_date}&aggregate=namespace&external=true&accumulate=false&shareIdle=false&idleByNode=false&shareTenancyCosts=true&shareNamespaces=&shareLabels=&shareCost=0&shareSplit=weighted" \
    -o kubecost_namespace_daily_asset_snapshot.json

kill $pid
