kubectl-grep icon indicating copy to clipboard operation
kubectl-grep copied to clipboard

Filter Kubernetes resources by matching their names

Kubectl Grep

CircleCI GoDoc Go Report Card GitHub release GitHub go.mod Go version

Filter Kubernetes resources by matching their names

Requirements

  • Kubernetes 1.20.0+
  • Kubectl 1.20.0+
  • Krew 0.4.0+

Compatibility

please refer to Kubernetes version policy and CHANGELOG for supported version matrix.

Example: kubectl-grep build with Kubernetes-1.21.x should be compatable with Kubernetes cluster version 1.20, 1.21, 1.22.

Why we need it?

playing with Kubernetes is my daily job, and I normally search pods by pipe, grep, --label, --field-selector, etc. while hunting abnormal pods, but typing such long commands is quite annoying.

Before change, we usually filter pods by the following commands,

$ kubectl get pods                           | grep "keyword"
$ kubectl get pods -o wide                   | grep "keyword"
$ kubectl get pods -n "my-ns"                | grep "keyword"
$ kubectl get pods -A                        | grep "keyword"
$ kubectl get pods -l "key=value"            | grep "keyword"
$ kubectl get pods -l "key=value" -n "my-ns" | grep "keyword"
$ kubectl get pods -l "key=value" -A         | grep "keyword"

With this plugin installed, you can filter pod with kubectl grep easily

$ kubectl grep pods "keyword"
$ kubectl grep pods "keyword" -o wide
$ kubectl grep pods "keyword" -n "my-ns"
$ kubectl grep pods "keyword" -A
$ kubectl grep pods "keyword" -l "key=value"
$ kubectl grep pods "keyword" -l "key=value" -n "my-ns"
$ kubectl grep pods "keyword" -l "key=value" -A

Supported APIs

apps/v1

  • [X] DaemonSet
  • [X] Deployment
  • [X] ReplicaSet
  • [X] StatefulSet

autoscaling/v1

  • [X] HPA

batch/v1

  • [X] CronJob
  • [X] Job

discovery.k8s.io/v1

  • [ ] EndpointSlice

networking.k8s.io/v1

  • [X] Ingress
  • [ ] IngressClass
  • [ ] NetworkPolicy

policy/v1

  • [ ] PodDisruptionBudget

rbac.authorization.k8s.io/v1

  • [ ] ClusterRole
  • [ ] ClusterRoleBinding
  • [ ] Role
  • [ ] RoleBinding

scheduling.k8s.io/v1

  • [ ] PriorityClass

storage.k8s.io/v1

  • [X] CSIDriver
  • [X] StorageClass

v1

  • [X] ConfigMap
  • [ ] Endpoints
  • [ ] LimitRange
  • [ ] Namespace
  • [X] Node
  • [ ] PersistentVolume
  • [ ] PersistentVolumeClaim
  • [X] Pod
  • [ ] PriorityClass
  • [ ] ReplicationController
  • [ ] ResourceQuota
  • [X] Secret
  • [X] Service
  • [ ] ServiceAccount

Installation

Installation via krew

$ kubectl krew version # make sure you are running 0.4.0+
$ kubectl krew install grep
$ kubectl krew upgrade

Manual Installation

$ export KUBECTL_GREP_VERSION=$(curl -s https://api.github.com/repos/guessi/kubectl-grep/releases/latest | jq -r .tag_name)
$ curl -L -O https://github.com/guessi/kubectl-grep/releases/download/${KUBECTL_GREP_VERSION}/kubectl-grep-$(uname -s)-$(uname -m).tar.gz
$ tar zxvf kubectl-grep-$(uname -s)-$(uname -m).tar.gz
$ mv kubectl-grep /usr/local/bin
$ chmod +x /usr/local/bin/kubectl-grep

Examples

List all pods in default namespace,

$ kubectl grep pods

List all pods in all namespaces,

$ kubectl grep pods -A

List all pods with specific keyword, under specific namespace,

$ kubectl grep pods -n star-lab flash

How to get developer build?

$ go get -u github.com/guessi/kubectl-grep

$ cd ${GOPATH}/src/github.com/guessi/kubectl-grep

$ make all

FAQ

How do I check the tool's version?

$ kubectl grep version

Can I run Kubernetes 1.20.0 with kubectl 1.21.0?

sure, no problem

Reference

License

Apache-2.0