Skip to content

Instantly share code, notes, and snippets.

@venkata-qa
Created April 8, 2025 18:04
Show Gist options
  • Select an option

  • Save venkata-qa/924513b769f451c3f98988321df1b8c8 to your computer and use it in GitHub Desktop.

Select an option

Save venkata-qa/924513b769f451c3f98988321df1b8c8 to your computer and use it in GitHub Desktop.

Revisions

  1. venkata-qa created this gist Apr 8, 2025.
    118 changes: 118 additions & 0 deletions k8s_report_v1.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,118 @@
    #!/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