alias k='kubectl '
alias kshell='kubectl run -it shell --image bash --restart Never --rm -- sh'

kube-ssh() {
  sh -c "$(curl -sSL https://raw.githubusercontent.com/AverageMarcus/kube-ssh/master/ssh.sh)"
}

kube-forward() {
  if [ -n "$ZSH_VERSION" ]; then
    setopt LOCAL_OPTIONS NO_NOTIFY NO_MONITOR
  fi
  IFS=$'\n'
  SERVICES=( $(kubectl get service --no-headers -o json | jq '[.items[] | select(.metadata.annotations."kube-forward" != "false")] | (.[] | .metadata.name + "\t" + ([.spec.ports[].port] | join(",")))' -r | column -t) )
  unset IFS

  TO_KILL=()

  cleanup() {
    echo "\nClosing connections..."
    for pid in "${TO_KILL[@]}"
    do
      (kill -2 $pid) &> /dev/null
    done

    trap - INT TERM
  }
  trap 'cleanup' INT TERM

  HOST_PORT=9001

  echo "Forwarding..."

  for s in "${SERVICES[@]}"
  do
    SERVICE=( $(echo $s) )
    if [ -n "$ZSH_VERSION" ]; then
      NAME=${SERVICE[1]}
      PORT=${SERVICE[2]}
    else
      NAME=${SERVICE[0]}
      PORT=${SERVICE[1]}
    fi
    PORTS=($(echo $PORT | tr "," "\n"))
    for PORT in "${PORTS[@]}"
    do
      (kubectl port-forward svc/$NAME $HOST_PORT:$PORT > /dev/null 2>&1) &
      BG_PID=$!

      if `curl -s -o /dev/null --retry 5 --retry-delay 0 --retry-connrefused -m 3 http://localhost:$HOST_PORT`
      then
        echo "\e[1m$NAME:$PORT\e[0m ➡ \e[34mhttp://localhost:$HOST_PORT\e[0m"
        TO_KILL+=($BG_PID)
        ((HOST_PORT=HOST_PORT+1))
      else
        (kill -2 $BG_PID) &> /dev/null
      fi
    done
  done

  echo "\n\e[2m(Use [Ctl + C] to exit)"
  cat
  if [ -n "$BASH_VERSION" ]; then
    cleanup
  fi
}

source <(kubectl completion zsh)

## Kubectl exec
kx () {
	local pod=($(kubectl get pods --all-namespaces -owide | fzf | awk '{print $1, $2}'))
	local cmd=${@:-"sh"}

	echo kubectl exec -it --namespace $pod[1] $pod[2] $cmd
	kubectl exec -it --namespace $pod[1] $pod[2] $cmd
}

## Kubectl logs
kl () {
	local pod=($(kubectl get pods --all-namespaces -owide | fzf | awk '{print $1, $2}'))
	local attr=${@:-""}

	echo kubectl logs -f $attr --namespace $pod[1] $pod[2]
	kubectl logs -f $attr --namespace $pod[1] $pod[2]
}

## Display everything
kall() {
  NAMESPACE=""
  LABEL=""
  ALL_NAMESPACES=false
  while test $# -gt 0; do
    case "$1" in
      -n|--namespace)
        shift
        NAMESPACE=$1
        shift
        ;;
      -l|--selector)
        shift
        LABEL=$1
        shift
        ;;
      -A|--all-namespaces)
        ALL_NAMESPACES=true
        shift
        ;;
      -h|--help)
        echo "kall - get all Kubernetes resources matching a given label selector"
        echo " "
        echo "kall [options]"
        echo " "
        echo "Options:"
        echo "-h, --help            show this help text"
        echo "-n, --namespace       the namespace to check against"
        echo "-l, --selector        the label selector to match on"
        echo "-A, --all-namespaces  search all namespaces"
        return 0
        ;;
      *)
        break
        ;;
    esac
  done

  if [[ "${LABEL}" == "" ]]; then
    echo "Please provide a label selector to match on"
    return 1
  fi

  NAMES="$(kubectl api-resources --namespaced --verbs list -o name | tr '\n' ,)"

  if [ $ALL_NAMESPACES ]; then
    kubectl get "${NAMES:0:-1}" --show-kind --ignore-not-found -l ${LABEL} -A
  else
    kubectl get "${NAMES:0:-1}" --show-kind --ignore-not-found -l ${LABEL} -n ${NAMESPACE}
  fi
}

k-version-test() {
  VERSION=""
  FILES=""
  while test $# -gt 0; do
    case "$1" in
      -v|--version)
        shift
        VERSION=$1
        shift
        ;;
      -f|--files)
        shift
        FILES=$1
        shift
        ;;
      -h|--help)
        echo "k-version-test - test Kubernetes manifest files against different versions of Kubernetes"
        echo " "
        echo "k-version-test [options]"
        echo " "
        echo "Options:"
        echo "-h, --help      show this help text"
        echo "-v, --version   the version of kubernetes to test against"
        echo "-f, --files     the manifest file(s) to test against"
        return 0
        ;;
      *)
        break
        ;;
    esac
  done

  which kind &> /dev/null || (echo "'kind' not installed. Follow install instructions here: https://github.com/kubernetes-sigs/kind/"; return 1)

  if [ ! -z $VERSION ];
  then
    TAG=$(curl -s 'https://registry.hub.docker.com/v2/repositories/kindest/node/tags/' | jq '."results" | map(.name | select(startswith("v'$VERSION'"))) | .[0]' | xargs)
    kind create cluster --image kindest/node:$TAG
  else
    kind create cluster
  fi

  echo

  if kubectl apply --dry-run -f $FILES ; then
    echo "\n☸️  Kubernetes $VERSION. Result: ✅\n"
  else
    echo "\n☸️  Kubernetes $VERSION. Result: ❌\n"
  fi

  kind delete cluster
}


fix-broken-replicasets() {
  kubectl get replicasets --all-namespaces -o jsonpath='{range .items[?(@.status.replicas==0)]}--namespace {@.metadata.namespace} {@.metadata.name};{end}'  | tr ";" "\n" | xargs -I {} sh -c "kubectl delete rs {}"
}

source <(tkn completion zsh)