#!/bin/bash

NS="$1"
PODS="`mktemp`"
kubectl -n "$NS" --output=json get pods \
  | jq -r .items[].metadata.name \
  > "$PODS"
for POD in $(cat "$PODS"); do
  CONTAINERS="`mktemp`"
  kubectl -n "$NS" --output=json get pod "$POD" \
    | jq -r .spec.containers[].name \
    > "$CONTAINERS"
  for CONTAINER in $(cat "$CONTAINERS"); do
    # echo -e "$POD:\t$CONTAINER"
    if [ "$CONTAINER" = "$2" ]; then
      kubectl -n "$NS" exec -ti "$POD" -c "$CONTAINER" -- "$3"
    fi
  done
  rm "$CONTAINERS"
done
rm "$PODS"
