#!/bin/bash # Script to find and follow logs of pods based on pattern matching # Usage: ./kubectl-logs [part3] ... # Example: ./kubectl-logs shl staging # This will find pods matching: sieservice-shl-*-staging-* NAMESPACE="sieservice" # Check if at least one argument is provided if [ $# -eq 0 ]; then echo "Usage: $0 [part3] ..." echo "Example: $0 shl staging" echo "This will find pods matching pattern: sieservice-shl-*-staging-*" exit 1 fi # Construct the grep pattern from arguments # Start with "sieservice-" PATTERN="sieservice" for arg in "$@"; do PATTERN="${PATTERN}-.*${arg}" done echo "Getting pods in namespace: $NAMESPACE" echo "Looking for pod matching pattern: ${PATTERN}-" echo "Arguments provided: $@" echo "----------------------------------------" # Get the pod name that matches the pattern POD_NAME=$(kubectl get pods -n $NAMESPACE --no-headers | grep -E "^${PATTERN}-" | awk '{print $1}') if [ -z "$POD_NAME" ]; then echo "Error: No pod found matching pattern '${PATTERN}-' in namespace '$NAMESPACE'" echo "" echo "Available pods in namespace '$NAMESPACE':" kubectl get pods -n $NAMESPACE exit 1 fi # Check if multiple pods match the pattern POD_COUNT=$(echo "$POD_NAME" | wc -l) if [ $POD_COUNT -gt 1 ]; then echo "Multiple pods found matching the pattern:" echo "$POD_NAME" echo "" echo "Please be more specific with your search terms." exit 1 fi echo "Found pod: $POD_NAME" echo "Following logs..." echo "----------------------------------------" # Follow the logs of the found pod kubectl logs -n $NAMESPACE -f $POD_NAME