#!/bin/bash

namespace=$1
unset KUBECONFIG
pprof_collection_path="/must-gather/memory-profiles/${namespace}"
pprof_http_endpoint=mig-controller-metrics.${namespace}.svc.cluster.local:2112/debug/pprof/heap
mkdir -p ${pprof_collection_path}

echo "Gathering mig-controller heap profile in ns ${namespace}"
/usr/bin/curl -s ${pprof_http_endpoint} --output ${pprof_collection_path}/pprof_raw_payload &> /dev/null || true
echo "Exporting mig-controller heap profile as PNG file in ns ${namespace}"
/usr/bin/pprof -png ${pprof_collection_path}/pprof_raw_payload 1> ${pprof_collection_path}/profile.png 2> /dev/null || true
