#!/bin/bash
#
# k8getall
#
# Get all namespaced resources (except events) from a namespace
#
# from: https://www.studytonight.com/post/how-to-list-all-resources-in-a-kubernetes-namespace

namespace=$1
shift &>/dev/null || :

command -v kubectl &>/dev/null || { echo >&2 kubectl not found; exit 127; }

for resource in $(
    kubectl api-resources --verbs=list --namespaced -o name |
    grep -Ev '^(events|events\.events\.k8s\.io)$' |
    sort -u
)
do
    items=$(
        kubectl ${namespace:+-n "$namespace"} get --ignore-not-found "$@" "$resource"
    )
    if [[ $items ]]
    then
        echo
        echo "Resource: $resource"
        echo
        printf "%s\n" "$items"
    fi
done
