#!/bin/bash
source pwait

# Cluster passed in from gather_logs
cluster=$1
ns=$2
logs_since=$3
max_parallelism=$4

for pod in $(/usr/bin/oc get pods --no-headers --namespace $ns | awk '{print $1}'); do
    object_collection_path="/must-gather/clusters/${cluster}/namespaces/${ns}/logs/${pod}"
    mkdir -p ${object_collection_path}

    containers="--all-containers"

    echo "[cluster=${cluster}][ns=${ns}][pod=${pod}] Collecting Pod logs..."
    /usr/bin/oc logs ${containers} --namespace ${ns} ${pod} --since ${logs_since} &> "${object_collection_path}/current.log" &
    echo "[cluster=${cluster}][ns=${ns}][pod=${pod}] Collecting previous Pod logs..."
    /usr/bin/oc logs --previous ${containers} --namespace ${ns} ${pod} --since ${logs_since} &> "${object_collection_path}/previous.log" & 
    pwait $max_parallelism
done

wait