Skip to content

cleanup-orphan-hpa.sh #2

@latermonk

Description

@latermonk
#!/bin/bash
# 获取所有 HPA,检查其 ScaleTargetRef 是否存在
kubectl get hpa -A -o json | jq -r '.items[] | "\(.metadata.namespace) \(.metadata.name) \(.spec.scaleTargetRef.kind) \(.spec.scaleTargetRef.name)"' | while read ns hpa kind target; do
  if ! kubectl -n $ns get $kind $target &>/dev/null; then
    echo "Deleting orphan HPA $hpa in namespace $ns (target $kind/$target not found)"
    kubectl -n $ns delete hpa $hpa
  fi
done

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions