#!/bin/bash
#
# THIS SCRIPT PROVIDES TARGETED GATHERING BASED ON NS, PLAN AND VM NAME
#
# NOTICE: THIS FILE IS NOT INCLUDED IN THE DEFAULT GATHER SCRIPT
#
# Can be executed by: oc adm must-gather --image quay.io/konveyor/forklift-must-gather:latest -- NS=foo PLAN=bar VM=baz MIGRATION_NS=openshift-migration /usr/bin/targeted
#

unset KUBECONFIG
source pwait
max_parallelism=10

# Namespaces passed in from main gather
namespaces=$1
targeted_query="$(cat /tmp/targeted_logs_grep_query)"
target_vms="$(touch /tmp/target_vms; cat /tmp/target_vms)"
target_dvs="$(touch cat /tmp/dvs; cat /tmp/dvs)"
target_job_pods="$(touch cat /tmp/job_pods; cat /tmp/job_pods)"

# Collect all Pod logs from namespaces where Forklift is installed
for ns in ${namespaces[@]}; do
  for pod in $(/usr/bin/oc get pods --no-headers --namespace $ns | awk '{print $1}'); do
    object_collection_path="/must-gather/namespaces/${ns}/logs/${pod}"
    mkdir -p ${object_collection_path}
    echo "[ns=${ns}][pod=${pod}] Collecting Pod logs..."
    /usr/bin/oc logs --all-containers --namespace ${ns} ${pod} | grep -E $targeted_query &> "${object_collection_path}/current.log" &
    pwait $max_parallelism
  done
done


# Collect related CNV components logs: CDI and vm-import
for component in cdi vm-import; do
  for pod in $(/usr/bin/oc get pods --no-headers --namespace openshift-cnv | grep $component | awk '{print $1}'); do
    object_collection_path="/must-gather/namespaces/openshift-cnv/logs/${pod}"
    mkdir -p ${object_collection_path}
    echo "[ns=openshift-cnv][pod=${pod}] Collecting Pod logs..."
    /usr/bin/oc logs --all-containers --namespace openshift-cnv ${pod} | grep -E $targeted_query &> "${object_collection_path}/current.log" &
    pwait $max_parallelism
  done
done

# Collect virt-launcher and virt-v2v pod logs for migrated VMs (from VM's target namespace)
for nsvm in ${target_vms[@]}; do
  IFS="," read ns vm vmid <<< $nsvm
  virtLauncherPod=$(oc get pods --no-headers -n $ns --selector kubevirt.io=virt-launcher -o go-template='{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' | grep $vm | head -1)
  virtV2VPod=$(oc get pods --no-headers -n $ns --selector vmID=$vmid -o go-template='{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' | grep $vmid | head -1)

  if [[ -z $virtLauncherPod ]]; then
    echo "Virt-launcher Pod for $nsvm doesn't exist, skipping."
  else
    object_collection_path="/must-gather/namespaces/${ns}/logs/${virtLauncherPod}"
    mkdir -p ${object_collection_path}
    echo "[ns=${ns}][pod=${virtLauncherPod}] Collecting virt-launcher Pod logs..."
    /usr/bin/oc logs --all-containers --namespace ${ns} ${virtLauncherPod} &> "${object_collection_path}/current.log" &
    pwait $max_parallelism
  fi

  if [[ -z $virtV2VPod ]]; then
    echo "Virt-V2V Pod for $nsvm doesn't exist, skipping."
  else
    object_collection_path="/must-gather/namespaces/${ns}/logs/${virtV2VPod}"
    mkdir -p ${object_collection_path}
    echo "[ns=${ns}][pod=${virtV2VPod}] Collecting virt-v2v Pod logs..."
    /usr/bin/oc logs --all-containers --namespace ${ns} ${virtV2VPod} &> "${object_collection_path}/current.log" &
    pwait $max_parallelism
  fi
done

# Collect CDI/importer pod logs for imported DVs
for nsdv in ${target_dvs[@]}; do
  IFS="," read ns dv <<< $nsdv
  pod=$(oc get pods --no-headers -n $ns --selector cdi.kubevirt.io=importer -o go-template='{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' | grep $dv | head -1)

  if [[ -z $pod ]]; then
    echo "Importer Pod for $nsdv doesn't exist, skipping."
  else
    object_collection_path="/must-gather/namespaces/${ns}/logs/${pod}"
    mkdir -p ${object_collection_path}
    echo "[ns=${ns}][pod=${pod}] Collecting CDI Importer Pod logs..."
    /usr/bin/oc logs --all-containers --namespace ${ns} ${pod} &> "${object_collection_path}/current.log" &
    pwait $max_parallelism
  fi
done

# Collect migration hook jobs logs
for nsjobpod in ${target_job_pods[@]}; do
  IFS="," read ns job_pod <<< $nsjobpod
  object_collection_path="/must-gather/namespaces/${ns}/logs/${job_pod}"
  mkdir -p ${object_collection_path}
  echo "[ns=${ns}][job=${job_pod}] Collecting migration hook job's pod logs..."
  /usr/bin/oc logs --namespace ${ns} ${job_pod} &> "${object_collection_path}/current.log" &
  pwait $max_parallelism
done

wait
