K8s cheatsheet

Get all namespaces with Rancher project ID:

kubectl get namespaces -o custom-columns=NAME:.metadata.name,PROJECTID:.metadata.labels."field\.cattle\.io/projectId" --sort-by .metadata.labels."field\.cattle\.io/projectId"

Get all resources connected to certain namespace

kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get --show-kind --ignore-not-found -n nsone

Remove NodeAffinity pods

kubectl get pods -n NameTheSpace -o json | jq '.items[] | select(.status.reason!=null) | select(.status.reason | contains("NodeAffinity")) | "kubectl delete pods \(.metadata.name) -n \(.metadata.namespace)"' | xargs -n 1 bash -c

Get NS descriptions

kubectl get ns -o custom-columns=NAME:.metadata.name,DESC:.metadata.annotations."field\.cattle\.io/description"

Update all clusterrepo's

for i in $(kubectl get clusterrepo -o=jsonpath="{.items[*]['metadata.name']}"); do kubectl patch clusterrepo $i --type='json' -p="[{'op': 'replace', 'path': '/spec/forceUpdate', 'value':'$(date +'%Y-%m-%dT%H:%M:%SZ' -d '5 seconds ago')'}]"; done

Find pod by IP

kubectl get --all-namespaces  --output json  pods | jq '.items[] | select(.status.podIP=="10.22.19.69")' | jq .metadata.name

Delete all evicted pods

kubectl get pods -A -o json | jq '.items[] | select(.status.reason!=null) | select(.status.reason | contains("Evicted")) | "kubectl delete pods \(.metadata.name) -n \(.metadata.namespace)"' | xargs -n 1 bash -c

Login to GHCR.io from CLI

gh auth refresh --scopes write:packages
gh auth token | docker login ghcr.io --username fe-ax --password-stdin