#!/bin/bash

set -e

if [ $# -lt 1 ]; then
  echo "Error: Namespace not provided"
  echo "Usage: $0 <NAMESPACE>"
  exit 1
fi

NAMESPACE=$1

mkdir logs

set -x

# List all the pods
kubectl get pods -n $NAMESPACE -o wide > logs/pods.$NAMESPACE.txt
kubectl get pods -n kube-system -o wide > logs/pods.kube-system.txt

# List all
kubectl get all -n $NAMESPACE -o wide > logs/all.$NAMESPACE.txt
kubectl get all -n kube-system -o wide > logs/all.$NAMESPACE.txt

# Save events
kubectl get events -n $NAMESPACE --field-selector involvedObject.kind=Pod | grep -E 'Pulling|Pulled' > logs/events.$NAMESPACE.txt || echo "Failed to get events" >&2
kubectl get events -n kube-system --field-selector involvedObject.kind=Pod | grep -E 'Pulling|Pulled' > logs/events.$NAMESPACE.txt || echo "Failed to get events" >&2

# Describe top
kubectl top pods -n $NAMESPACE > logs/top.$NAMESPACE.txt
kubectl top pods -n kube-system > logs/top.kube-system.txt

# Get Nodes
kubectl describe nodes > logs/nodes.txt
kubectl top nodes >> logs/nodes.txt

# Describe Deployments
mkdir -p logs/deployments
function log_deployments() {
  local namespace=$1
  mkdir -p logs/deployments/$namespace
  for dep in $(kubectl get deployments -n $namespace -o jsonpath='{.items[*].metadata.name}'); do
    # Send to a file
    kubectl describe deployment -n $namespace $dep > logs/deployments/$namespace/$dep.txt || echo "Failed to log Deployment $dep" >&2
  done
}

log_deployments $NAMESPACE
log_deployments kube-system

# Describe StatefulSets
mkdir -p logs/statefulsets

function log_statefulsets() {
  local namespace=$1
  mkdir -p logs/statefulsets/$namespace
  for sts in $(kubectl get statefulsets -n $namespace -o jsonpath='{.items[*].metadata.name}'); do
    kubectl describe statefulset -n $namespace $sts > logs/statefulsets/$namespace/$sts.txt || echo "Failed to log StatefulSet $sts" >&2
  done
}

log_statefulsets $NAMESPACE
log_statefulsets kube-system

# Get Logs
# Collect the logs for each pod
mkdir -p logs/pods/logs
function get_logs(){

  local namespace=$1
  mkdir -p logs/pods/logs/$namespace
  for pod in $(kubectl get pods -n $namespace -o jsonpath='{.items[*].metadata.name}'); do
    mkdir -p logs/pods/logs/$namespace/$pod
    for container in $(kubectl get pods -n $namespace $pod -o jsonpath='{.spec.containers[*].name}'); do
      kubectl logs -n $namespace $pod $container > logs/pods/logs/$namespace/$pod/$container.log || echo "Failed to get container logs of $pod/$container" >&2
      kubectl logs -n $namespace $pod $container --previous > logs/pods/logs/$namespace/$pod/$container.previous.log || echo "Failed to get previous container logs of $pod/$container" >&2
    done
    for container in $(kubectl get pods -n $namespace $pod -o jsonpath='{.spec.initContainers[*].name}'); do
      kubectl logs -n $namespace $pod $container > logs/pods/logs/$namespace/$pod/$container.log || echo "Failed to get container logs of $pod/$container" >&2
      kubectl logs -n $namespace $pod $container --previous > logs/pods/logs/$namespace/$pod/$container.previous.log || echo "Failed to get previous container logs of $pod/$container" >&2
    done
  done
}

get_logs $NAMESPACE
get_logs kube-system

# Describe Pods
# Collect the logs for each pod
mkdir -p logs/pods/describe

function describe_pod(){
  local namespace=$1
  mkdir -p logs/pods/describe/$namespace
  for pod in $(kubectl get pods -n $namespace -o jsonpath='{.items[*].metadata.name}'); do
    # Send to a file
    kubectl describe pod -n $namespace $pod > logs/pods/describe/$namespace/$pod.txt || echo "Failed to describe Pod $pod" >&2
  done
}

describe_pod $NAMESPACE
describe_pod kube-system

# Describe PVC
function describe_pvc(){
  namespace=$1
  mkdir -p logs/pvc/$namespace
  for p in $(kubectl get pvc -n $namespace -o jsonpath='{.items[*].metadata.name}'); do
    # send to file
    kubectl describe pvc -n $namespace $p > logs/pvc/$namespace/$p.txt || echo "Failed to describe PVC $p" >&2
  done

  mkdir -p logs/pv/$namespace
  for p in $(kubectl get pv -n $namespace -o jsonpath='{.items[*].metadata.name}'); do
    # send to file
    kubectl describe pv -n $namespace $p > logs/pv/$namespace/$p.txt || echo "Failed to describe PV $p" >&2
  done

}

describe_pvc $NAMESPACE
describe_pvc kube-system

# CRD Definition
kubectl get crd -A > logs/crds.txt
