Skip to content

Instantly share code, notes, and snippets.

@gerritjvv
Created September 21, 2023 09:56
Show Gist options
  • Save gerritjvv/a8369a50e7b93e94221673340a70a446 to your computer and use it in GitHub Desktop.
Save gerritjvv/a8369a50e7b93e94221673340a70a446 to your computer and use it in GitHub Desktop.
Download yaml resources from kubernetes recursively using bash point free style
#!/usr/bin/env bash
echo "ingress deployments services statefulset" | \
xargs -n 1 -I ktype \
sh -c 'kubectl get ktype | tail -n+2 | awk '\''{print $1}'\'' | xargs -t -P 2 -I resource sh -c '\''kubectl get ktype resource -o yaml > resource.yml'\'''
# echo "ingress deployments services statefulset" | xargs -n 1 -I ktype sh -c
# ^^ runs a command for each ingress deployments etc.. the -I parameter is string substitution, i.e where ever it sees ktype it will put the ingress deployments etc read from stdin.
# kubectl get ktype | tail -n+2 | awk '{print $1}' | xargs -t -P 2 -I resource sh -c 'kubectl get ktype resource -o yaml > resource.yml'
# ^^ this part is run for each ingress/deployments etc, and does a kubectl get ingress for example, then tail n+2 gets the second line onwards, to avoid the “NAME” heading from kubectl
# k8s prints the first column which is the resource name
# then xargs , -t prints the command it will run, -P 2 uses 2 processes, -I resource will substitute the word resource with the stdin values read, and then run kubectl get -o yaml outputs the yaml content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment