Created
April 8, 2025 18:04
-
-
Save venkata-qa/924513b769f451c3f98988321df1b8c8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| # Ensure a namespace is provided | |
| NAMESPACE=$1 | |
| if [[ -z "$NAMESPACE" ]]; then | |
| echo "β Usage: $0 <namespace>" | |
| exit 1 | |
| fi | |
| REPORT_FILE="k8s_namespace_report_${NAMESPACE}_$(date +%Y%m%d).txt" | |
| TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S") | |
| echo "π Generating Kubernetes report for namespace: $NAMESPACE..." | |
| echo "π Report will be saved to: $REPORT_FILE" | |
| # Function to add section headers | |
| add_section() { | |
| echo -e "\n$1" >> $REPORT_FILE | |
| echo "$(printf '%0.s=' $(seq 1 $((${#1}+2))))" >> $REPORT_FILE | |
| } | |
| # Function to add subsection headers | |
| add_subsection() { | |
| echo -e "\n$1" >> $REPORT_FILE | |
| echo "$(printf '%0.s-' $(seq 1 $((${#1}+2))))" >> $REPORT_FILE | |
| } | |
| # Initialize report file | |
| cat <<EOF > $REPORT_FILE | |
| Kubernetes Namespace Report | |
| ========================== | |
| Namespace: $NAMESPACE | |
| Generated: $TIMESTAMP | |
| Cluster: $(kubectl config current-context) | |
| EOF | |
| # π 1. Namespace Overview | |
| add_section "1. Namespace Overview" | |
| kubectl describe namespace $NAMESPACE >> $REPORT_FILE | |
| # π 2. Deployment Summary | |
| add_section "2. Deployment Summary" | |
| add_subsection "2.1 Deployment Status" | |
| kubectl get deployments -n $NAMESPACE -o wide >> $REPORT_FILE | |
| add_subsection "2.2 Deployment Details" | |
| for dep in $(kubectl get deployments -n $NAMESPACE -o jsonpath='{.items[*].metadata.name}'); do | |
| echo -e "\nDeployment: $dep" >> $REPORT_FILE | |
| kubectl describe deployment $dep -n $NAMESPACE | grep -v "Conditions:" | grep -v "Events:" >> $REPORT_FILE | |
| done | |
| # π 3. Pod Status | |
| add_section "3. Pod Status" | |
| add_subsection "3.1 All Pods Summary" | |
| kubectl get pods -n $NAMESPACE -o wide >> $REPORT_FILE | |
| add_subsection "3.2 Healthy Pods (Running)" | |
| kubectl get pods -n $NAMESPACE --field-selector=status.phase=Running -o wide >> $REPORT_FILE | |
| add_subsection "3.3 Failing Pods (Not Running)" | |
| kubectl get pods -n $NAMESPACE --field-selector=status.phase!=Running -o wide >> $REPORT_FILE | |
| add_subsection "3.4 CrashLoopBackOff Pods" | |
| kubectl get pods -n $NAMESPACE | grep CrashLoopBackOff >> $REPORT_FILE | |
| add_subsection "3.5 Pod Details (All)" | |
| for pod in $(kubectl get pods -n $NAMESPACE -o jsonpath='{.items[*].metadata.name}'); do | |
| echo -e "\nPod: $pod" >> $REPORT_FILE | |
| kubectl describe pod $pod -n $NAMESPACE | grep -v "Conditions:" | grep -v "Events:" >> $REPORT_FILE | |
| done | |
| # π 4. Services | |
| add_section "4. Services" | |
| add_subsection "4.1 Service Summary" | |
| kubectl get services -n $NAMESPACE -o wide >> $REPORT_FILE | |
| add_subsection "4.2 Service Details" | |
| for svc in $(kubectl get services -n $NAMESPACE -o jsonpath='{.items[*].metadata.name}'); do | |
| echo -e "\nService: $svc" >> $REPORT_FILE | |
| kubectl describe service $svc -n $NAMESPACE >> $REPORT_FILE | |
| done | |
| # π 5. Persistent Volume Claims | |
| add_section "5. Persistent Volume Claims" | |
| kubectl get pvc -n $NAMESPACE -o wide >> $REPORT_FILE | |
| # π 6. ConfigMaps and Secrets | |
| add_section "6. ConfigMaps and Secrets" | |
| add_subsection "6.1 ConfigMaps" | |
| kubectl get configmaps -n $NAMESPACE >> $REPORT_FILE | |
| add_subsection "6.2 Secrets" | |
| kubectl get secrets -n $NAMESPACE >> $REPORT_FILE | |
| # π 7. Events | |
| add_section "7. Recent Events" | |
| kubectl get events -n $NAMESPACE --sort-by='.lastTimestamp' >> $REPORT_FILE | |
| # π 8. Recommendations | |
| add_section "8. Recommendations" | |
| echo "Issues detected:" >> $REPORT_FILE | |
| kubectl get pods -n $NAMESPACE --field-selector=status.phase!=Running -o name >> $REPORT_FILE | |
| echo -e "\nSuggested actions:" >> $REPORT_FILE | |
| echo "1. For CrashLoopBackOff pods, check logs with:" >> $REPORT_FILE | |
| echo " kubectl logs -n $NAMESPACE <pod-name> -p" >> $REPORT_FILE | |
| echo "2. For ImagePullBackOff pods, verify image name and registry access" >> $REPORT_FILE | |
| echo "3. For pods with high restart counts, investigate application logs" >> $REPORT_FILE | |
| echo "4. Check detailed pod events with:" >> $REPORT_FILE | |
| echo " kubectl describe pod -n $NAMESPACE <pod-name>" >> $REPORT_FILE | |
| echo -e "\nβ Report generation complete!" | |
| echo "π Report saved to: $REPORT_FILE" | |
| # Optional: Print the report to console | |
| # cat $REPORT_FILE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment