dcg

kubectl

kubernetes.kubectl

Protects against destructive kubectl operations like delete namespace, drain, and mass deletion

  • 19blocking rules
  • 9safe patterns
  • 7Critical
  • 11High
  • 1Medium
~/.config/dcg/config.toml
[packs]
enabled = ["kubernetes.kubectl"]

Evaluated when a command contains kubectl, delete, drain, cordon, taint.

Recorded verdicts

  • kubectl delete namespace productionBlockedkubernetes.kubectl:delete-namespace
  • kubectl get pods -n productionAllowed

Blocking rules

Each rule ID is kubernetes.kubectl:<name>. Use it in allowlists and [policy.rules].

  1. api-delete-namespace

    Critical

    DELETE to /api/v1/namespaces/<name> removes the namespace and ALL resources in it.

    Explanation
    This is `kubectl delete namespace` spelled as an API call, and it destroys everything inside the namespace: - All deployments, pods, services - All configmaps and secrets - All persistent volume claims (data may be lost) It is irreversible, and the API server applies it without the CLI's confirmation or dry-run affordances. Preview what would be deleted: kubectl get all -n <namespace>
  2. api-delete-persistent-storage

    Critical

    DELETE to a PVC or PV path can permanently destroy the underlying storage.

    Explanation
    This is `kubectl delete pvc` / `kubectl delete pv` as an API call: - Cloud disks (EBS, GCE PD, Azure Disk) may be deleted - Data is not recoverable once the volume is released - Even with a Retain policy, deleting the PV may trigger cleanup Check what is bound to it first: kubectl get pvc -A
  3. delete-all-namespaces

    Critical

    kubectl delete with -A/--all-namespaces affects ALL namespaces. Very dangerous!

    Explanation
    The -A/--all-namespaces flag expands deletion to EVERY namespace in the cluster. This can take down your entire cluster: - Production, staging, and dev environments affected - System namespaces (kube-system) may be impacted - Cross-namespace resources and dependencies break This is almost never what you want. Always specify a namespace: kubectl delete <resource> -n <namespace> Preview cluster-wide resources: kubectl get <resource> -A
  4. delete-force

    Critical

    kubectl delete --force --grace-period=0 immediately removes resources without graceful shutdown.

    Explanation
    Force deletion with zero grace period is dangerous: - Pods are killed immediately (no SIGTERM, just gone) - In-flight requests fail - Data corruption risk if writes in progress - Finalizers may be skipped (resource leak) Kubernetes warns against this. Use only for stuck pods that won't terminate. Try graceful deletion first: kubectl delete pod <name> # Default 30s grace kubectl delete pod <name> --grace-period=60 # Extended grace Check why pod is stuck: kubectl describe pod <name> | grep -A5 Status
  5. delete-namespace

    Critical

    kubectl delete namespace removes the entire namespace and ALL resources within it.

    Explanation
    Deleting a namespace destroys EVERYTHING inside it: - All deployments, pods, services - All configmaps and secrets - All persistent volume claims (data may be lost) - All ingresses and network policies - All RBAC resources scoped to the namespace This is irreversible. Even if you recreate the namespace, all resources are gone. Preview what would be deleted: kubectl get all -n <namespace> kubectl get pvc -n <namespace> Safer approach: kubectl delete deployment <name> -n <namespace> # Delete specific resources
  6. delete-pv

    Critical

    kubectl delete pv may permanently delete the underlying storage.

    Explanation
    Deleting a PersistentVolume can permanently destroy the underlying storage: - Cloud disks (EBS, GCE PD, Azure Disk) may be deleted - NFS mounts become orphaned - Local storage data is lost Even with Retain policy, deleting the PV may trigger storage cleanup. Check what's using the PV: kubectl get pvc -A | grep <pv-name> Check storage class policy: kubectl get storageclass <class> -o yaml Preview: kubectl delete pv <name> --dry-run=client
  7. delete-pvc

    Critical

    kubectl delete pvc may permanently delete data if ReclaimPolicy is Delete.

    Explanation
    Deleting a PVC can cause permanent data loss depending on the PV's reclaimPolicy: - Delete: Underlying storage is deleted (DATA LOST) - Retain: PV is kept but becomes 'Released' (manual recovery needed) - Recycle: Deprecated, data scrubbed Check the reclaim policy: kubectl get pv <pv-name> -o jsonpath='{.spec.persistentVolumeReclaimPolicy}' Backup first: kubectl exec <pod> -- tar czf - /data > backup.tar.gz Preview: kubectl delete pvc <name> --dry-run=client
  8. api-delete-collection

    High

    DELETE to a collection path removes every resource of that type in the namespace.

    Explanation
    A DELETE to a path that ends at the resource type, with no /<name> after it, is the API's deleteCollection — the equivalent of `kubectl delete <type> --all`: - .../pods kills every pod in the namespace - .../services removes all services (networking breaks) - .../persistentvolumeclaims may delete all persistent data Name the single resource instead, or use a label selector: kubectl delete <resource> -l app=myapp
  9. api-delete-workload

    High

    DELETE to a workload path removes the controller and the pods it manages.

    Explanation
    This is `kubectl delete deployment/statefulset/daemonset/replicaset` as an API call. The controller is removed and its pods terminate; anything not stored outside the pod is gone. Check what it manages first: kubectl get all -n <namespace> -l app=<name>
  10. apply-force

    High

    kubectl apply --force deletes and recreates resources, causing downtime.

    Explanation
    kubectl apply --force deletes the resource and recreates it from the manifest. This causes: - Downtime as pods are terminated before new ones start - Loss of any runtime modifications - Potential data loss for stateful workloads - Disruption to in-flight requests Use this only when you cannot update resources normally due to immutable field changes. Preview changes first: kubectl diff -f <file> Try server-side apply for safer updates: kubectl apply --server-side -f <file>
  11. apply-prune

    High

    kubectl apply --prune deletes live resources that are missing from the applied manifests.

    Explanation
    With --prune, apply also DELETES objects in the selected scope that the manifests no longer contain. A partial manifest directory, a wrong -l selector, or --all can remove far more than intended, and deleted workloads, services and their data-bearing claims do not come back. Preview exactly what would be pruned: kubectl apply --prune --dry-run=server -f <dir> kubectl diff -f <dir>
  12. delete-all

    High

    kubectl delete --all removes ALL resources of that type. Use --dry-run=client first.

    Explanation
    The --all flag deletes EVERY resource of the specified type in the namespace. For example: - kubectl delete pods --all: Kills all pods (services go down) - kubectl delete svc --all: Removes all services (networking breaks) - kubectl delete pvc --all: May delete all persistent data Always preview first: kubectl delete <resource> --all --dry-run=client Safer alternative: kubectl delete <resource> -l app=myapp # Use label selectors
  13. delete-from-directory

    High

    kubectl delete -f with directories or --recursive deletes many resources at once.

    Explanation
    Deleting from a directory or recursively removes ALL resources defined in those files: - Multiple deployments, services, configmaps deleted at once - Hard to recover if wrong directory - No confirmation or preview by default Always preview first: kubectl diff -f <directory> ls -la <directory>/*.yaml Delete specific files instead: kubectl delete -f <specific-file.yaml>
  14. delete-from-stdin

    High

    kubectl delete -f - deletes every resource described by stdin without a reviewable manifest path.

    Explanation
    Materialize the manifest, inspect it with kubectl diff, and run kubectl delete --dry-run=client before deleting the resources.
  15. delete-workload

    High

    kubectl delete deployment/statefulset/daemonset removes the workload. Use --dry-run first.

    Explanation
    Deleting a workload terminates all its pods: - Deployment: All replicas terminated, service goes down - StatefulSet: Ordered shutdown, PVCs may be orphaned - DaemonSet: Removed from all nodes - ReplicaSet: Pods terminated (usually managed by Deployment) Preview first: kubectl delete <type> <name> --dry-run=client kubectl get pods -l app=<name> # Check affected pods Consider scaling down first: kubectl scale deployment <name> --replicas=0
  16. drain-node

    High

    kubectl drain evicts all pods from a node. Ensure proper pod disruption budgets.

    Explanation
    kubectl drain evicts ALL pods from a node, typically for maintenance. This can cause service disruption: - All pods are evicted (respecting PodDisruptionBudgets) - DaemonSet pods remain unless --ignore-daemonsets is used - Pods with local storage fail unless --delete-emptydir-data is used - Without replicas elsewhere, services go down Before draining: kubectl get pods -o wide | grep <node> # Check what's running kubectl get pdb -A # Check disruption budgets Safer approach: kubectl cordon <node> # Prevent new pods first, then drain gradually
  17. scale-to-zero

    High

    kubectl scale --replicas=0 stops all pods for the workload.

    Explanation
    Scaling to zero replicas terminates ALL pods for the workload: - Service becomes unavailable - Endpoints are removed from Service - In-flight requests are dropped - StatefulSets: Ordered shutdown from highest ordinal This is often intentional but can cause outages if done accidentally. Check current replicas: kubectl get deployment <name> --replicas=0 To restore: kubectl scale deployment <name> --replicas=<N>
  18. taint-noexecute

    High

    kubectl taint with NoExecute evicts existing pods that don't tolerate the taint.

    Explanation
    A NoExecute taint immediately evicts pods that don't have a matching toleration. This is more aggressive than NoSchedule: - Existing pods are evicted (not just new scheduling blocked) - Can cause immediate service disruption - Pods may not have time for graceful shutdown Check current taints: kubectl describe node <node> | grep Taints Consider NoSchedule first: kubectl taint nodes <node> key=value:NoSchedule Remove taint: kubectl taint nodes <node> key=value:NoExecute-
  19. cordon-node

    Medium

    kubectl cordon marks a node unschedulable. Existing pods continue running.

    Explanation
    kubectl cordon marks a node as unschedulable. Existing pods continue running, but no new pods will be scheduled to this node. Use cases: - Preparing for maintenance - Investigating node issues - Gradual migration To reverse: kubectl uncordon <node> Check node status: kubectl get nodes kubectl describe node <node> | grep Taints

Safe patterns

Checked first. A command that matches one is never blocked by this pack.

  • kubectl-get
  • kubectl-describe
  • kubectl-logs
  • kubectl-diff
  • kubectl-explain
  • kubectl-top
  • kubectl-config
  • kubectl-api
  • kubectl-version