Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created rancher-resource-enumerator script #174

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions rancher-crd/enumerate-resources/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# rancher-resource-enumerator

Rancher Custom Resource enumeration script

## Dependencies

* `kubectl`
* Linux, MacOS or WSL2

## How to use

* Download the script and save as: `rancher-resource-enumerator.sh`
* Make sure the script is executable: `chmod u+x ./rancher-resource-enumerator.sh`
* Run the script: `./rancher-resource-enumerator.sh -a`

The script will output all Rancher custom resource data in the `/tmp/enum-cattle-resources-<timestamp>` directory by default. The `totals` file will give the total count for all resources.

## Flags

```
Rancher Resource Enumerator
Usage: ./rancher-resource-enumerator.sh [ -d <directory> -n <namespace> | -c | -a ]
-h Display this help message.
-a Enumerate all custom resources.
-n <namespace> Only enumerate resources in the specified namespace(s).
-c Only enumerate cluster (non-namespaced) resources.
-d <directory> Path to output directory (default: /tmp/enum-cattle-resources-<timestamp>).
```
102 changes: 102 additions & 0 deletions rancher-crd/enumerate-resources/rancher-resource-enumerator.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/bin/sh

datenow="$(date "+%F-%H-%M-%S")"
outputdir="/tmp/enum-cattle-resources-$datenow"
export outputdir

usage() {
printf "Rancher Resource Enumerator \n"
printf "Usage: ./rancher-resource-enumerator.sh [ -d <directory> -n <namespace> | -c | -a ]\n"
printf " -h Display this help message.\n"
printf " -a Enumerate all custom resources.\n"
printf " -n <namespace> Only enumerate resources in the specified namespace(s).\n"
printf " -c Only enumerate cluster (non-namespaced) resources.\n"
printf " -d <directory> Path to output directory (default: /tmp/enum-cattle-resources-<timestamp>).\n"
exit 0
}

# Arguments
optstring="cahd:n:"
while getopts ${optstring} opt; do
case ${opt} in
h) usage
;;
d) path=${OPTARG}
outputdir="$path-$datenow"
export outputdir
;;
a) all=1
export all
;;
n) namespaces=${OPTARG}
export namespaces
;;
c) cluster=1
export cluster
;;
*) printf "Invalid Option: %s.\n" "$1"
usage
;;
esac
done


# Setup
setup() {
# Create output directory
echo "Output directory set to $outputdir"
mkdir -p "$outputdir"
}

# Get cluster resources
non_namespaced() {
kubectl api-resources --verbs=list --namespaced=false -o name | grep cattle.io | xargs -I _ sh -c "echo '(cluster) enumerating _ resources...'; kubectl get _ -o custom-columns=KIND:.kind,NAME:.metadata.name --no-headers=true --ignore-not-found=true >> $outputdir/_"
}

# Get namespaced resources
namespaced() {
ns="$1"
# Select all namespaces if no namespace is specified
if [ -z "$ns" ]; then
ns="$(kubectl get ns --no-headers -o jsonpath='{.items[*].metadata.name}')"
fi
# Get all custom resources for validated namespaces
for n in $ns
do
kubectl get ns "$n" -o name && \
kubectl api-resources --verbs=list --namespaced=true -o name | grep cattle.io | xargs -I _ sh -c "echo '(namespaced) enumerating _ resources in $n...'; kubectl get _ -n $n -o custom-columns=KIND:.kind,NAME:.metadata.name,NAMESPACE:.metadata.namespace --no-headers=true --ignore-not-found=true >> $outputdir/_"
done
}

# Get total counts
totals() {
countfiles="$outputdir/*"
echo 'counting totals...'
for f in $countfiles
do
wc -l "$f" >> "$outputdir"/totals
done
echo "results saved in $outputdir"
exit 0
}

main() {
if [ -n "$all" ]; then
setup
non_namespaced
namespaced
totals
elif [ -n "$cluster" ]; then
setup
non_namespaced
totals
elif [ -n "$namespaces" ]; then
setup
namespaced "$namespaces"
totals
else
usage
fi
}

main