Last active
August 9, 2024 23:50
-
-
Save steezeburger/0204aa3475fcc9eda67e1d5f0db26edf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function display_help() { | |
echo "Usage: $0 [POD_NAME] [-c CONTAINER] [-n NAMESPACE] " | |
echo | |
echo "Fetch logs for a Kubernetes pod or a specific container within a pod." | |
echo | |
echo "POD_NAME is the name of the pod to fetch logs for. This argument is required." | |
echo | |
echo "Options:" | |
echo " -n NAMESPACE Specify the namespace to search in. If not provided, defaults to 'astria-dev-cluster'." | |
echo " -c CONTAINER Specify the container to fetch logs for. If not provided, fetches logs for the entire pod." | |
echo " -h Display this help message." | |
} | |
# Set default namespace | |
NAMESPACE="astria-dev-cluster" | |
CONTAINER="" | |
POD_NAME=$1 | |
shift | |
# Ensure pod name is provided | |
if [ -z "$POD_NAME" ]; then | |
echo "Pod name must be provided as the first argument." | |
display_help | |
exit 1 | |
fi | |
# Parse command line options. | |
while getopts ":n:c:h" option; do | |
case "${option}" in | |
n) | |
NAMESPACE=${OPTARG} | |
;; | |
c) | |
CONTAINER=${OPTARG} | |
;; | |
h) | |
display_help | |
exit 0 | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
display_help | |
exit 1 | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2 | |
display_help | |
exit 1 | |
;; | |
esac | |
done | |
# Run the command with the provided pod name and namespace | |
if [ -z "$CONTAINER" ]; then | |
kubectl get -n "$NAMESPACE" pods --no-headers=true \ | |
| awk '/'$POD_NAME'/{print $1}' \ | |
| xargs kubectl logs --namespace "$NAMESPACE" -f | |
else | |
kubectl get -n "$NAMESPACE" pods --no-headers=true \ | |
| awk '/'$POD_NAME'/{print $1}' \ | |
| xargs kubectl logs --namespace "$NAMESPACE" -f -c "$CONTAINER" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment