https://github.com/ryane/kfilt
kfilt can filter Kubernetes resources.
https://github.com/ryane/kfilt
helm-charts kubectl kubernetes kustomize
Last synced: 5 months ago
JSON representation
kfilt can filter Kubernetes resources.
- Host: GitHub
- URL: https://github.com/ryane/kfilt
- Owner: ryane
- License: apache-2.0
- Created: 2019-06-25T02:28:24.000Z (about 7 years ago)
- Default Branch: main
- Last Pushed: 2025-12-03T03:16:05.000Z (7 months ago)
- Last Synced: 2025-12-03T06:23:43.715Z (7 months ago)
- Topics: helm-charts, kubectl, kubernetes, kustomize
- Language: Go
- Homepage:
- Size: 172 KB
- Stars: 109
- Watchers: 6
- Forks: 9
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# kfilt
[](https://github.com/ryane/kfilt/actions)
[](https://codecov.io/gh/ryane/kfilt)
[](https://goreportcard.com/report/ryane/kfilt)
[](https://github.com/ryane/kfilt/blob/main/LICENSE)
[](https://github.com/ryane/kfilt/releases)
kfilt can filter Kubernetes resources.
## What is kfilt?
kfilt is a tool that lets you filter specific resources from a stream of Kubernetes YAML manifests. It can read manifests from a file, URL, or from stdin.
kfilt was primarily created to assist developers who are creating [Helm charts](https://helm.sh/docs/developing_charts/) or [Kustomize](https://github.com/kubernetes-sigs/kustomize) bases. Often, when making changes, it is helpful to narrow down focus to a specific resource or set of resources in the output. Without kfilt, you might redirect output to a file for inspection in your text editor or to write complicated grep commands. kfilt makes it easy to filter the output to see just the resources you are currently interested in. Or, to exclude specific resources.
You can also use kfilt to selectively apply (or delete) resources with kubectl.
It is easiest to understand with some examples.
### Examples
#### Working with Files or URLs
Only output a ServiceAccount named "test":
```
kfilt -f ./pkg/decoder/test.yaml -i kind=ServiceAccount,name=test
```
Output all Service Accounts:
```
kfilt -f http://bit.ly/2xSiCJL -i kind=ServiceAccount
```
#### Working with Helm charts
kfilt can be used as a more flexible alternative to the `-x` option of [`helm template`](https://helm.sh/docs/helm/#helm-template). In this example, we will only output rendered Service resources from a Helm chart:
```
helm template chart | kfilt -i kind=service
```
We also have the ability to exclude resources. Here we will exclude all Secrets before applying a Chart to a cluster.
```
helm template chart | kfilt -x kind=secret | kubectl apply -f -
```
#### Working with Kustomize
Only output the ConfigMaps in a Kustomize base.
```
kustomize build github.com/kubernetes-sigs/kustomize//examples/helloWorld | kfilt -i kind=ConfigMap
```
Output only the resources named "the-deployment".
```
kustomize build github.com/kubernetes-sigs/kustomize//examples/helloWorld | kfilt -i name=the-deployment
```
#### Working with kubectl
Find all resources named "nginx-ingress-controller" regardless of kind.
```
kubectl get all -A -oyaml | kfilt -i name=nginx-ingress-controller
```
When used as a kubectl plugin:
```
kubectl get all -A -oyaml | kubectl kfilt -i name=nginx-ingress-controller
```
## Installation
kfilt is available on Linux, Mac, and Windows 1 and binaries are available on the [releases](https://github.com/ryane/kfilt/releases) page.
### kubectl Plugin
kfilt can be used as a kubectl plugin. Install via [Krew](https://krew.sigs.k8s.io/):
```bash
kubectl krew install kfilt
```
See [kubectl plugin documentation](./plugin/kubectl/README.md) for detailed installation and usage instructions.
### Docker
You can also run kfilt as a Docker container. Make sure you include `-i` in your `docker run` command.
```
kustomize build base | docker run --rm -i ryane/kfilt -k ConfigMap
```
### AUR
[kfilt](https://aur.archlinux.org/packages/kfilt-bin) is on the [Arch User
Repository (AUR)](https://aur.archlinux.org/).
### asdf Plugin
[@feniix](https://github.com/feniix) created an
[asdf](https://github.com/asdf-vm/asdf)
[plugin](https://github.com/feniix/asdf-kfilt/).
### nixpkgs
[kfilt](https://search.nixos.org/packages?show=kfilt) is on [nixpkgs](https://github.com/NixOS/nixpkgs).
### nix flake
```shell
nix run github:ryane/kfilt#kfilt -- -f ./pkg/decoder/test.yaml -k serviceaccount
```
### Running as a Kustomize Plugin (experimental)
See [plugin/kustomize](./plugin/kustomize) for an experimental Kustomize plugin.
## Usage
### Including Resources
You can use `--include` or `-i` to control which resources to include in the kfilt output. This argument takes a list of simple key value pairs that make up your query. The following keys are currently supported:
| Key | Field | Example |
| ------------- | ------------------ | ------------------------- |
| kind, k | kind | ServiceAccount |
| name, n | metadata.name | my-app |
| group, g | apiVersion | rbac.authorization.k8s.io |
| version, v | apiVersion | v1 |
| namespace, ns | metadata.namespace | kube-system |
| labels, l | metadata.labels | app=my-app |
Note that it is possible to use wildcards (`*`, and `?`) when filtering by name.
#### Examples
##### Filter by kind
```
kfilt -f ./pkg/decoder/test.yaml -i kind=configmap
```
##### Filter by group
```
kfilt -f ./pkg/decoder/test.yaml -i g=config.istio.io
```
##### Filter by name and kind
You can combine keys in a single `--include` by separating them with a comma. In this example, we are filtering to match ServiceAccount resources named "test":
```
kfilt -f ./pkg/decoder/test.yaml -i k=ServiceAccount,n=test
```
##### Filter by name using wildcards
```
kfilt -f ./pkg/decoder/test.yaml -n "test*"
```
You can use `*` and `?` wildcard characters.
##### Filter by multiple kinds
You can use multiple `--include` flags. kfilt will output resources that match any one of the includes. For example, to output ServiceAccounts and ConfigMaps you could use:
```
kfilt -f ./pkg/decoder/test.yaml -i k=serviceaccount -i k=configmap
```
##### Filter with Label Selectors
```
kfilt -f ./pkg/decoder/test.yaml -i labels=app=test
```
### Excluding Resources
The `--exclude` or `-x` flag will allow you to exclude resources. This supports the same key value pairs as the `--include` flag.
#### Examples
##### Exclude by kind
```
kfilt -f ./pkg/decoder/test.yaml -x kind=configmap
```
##### Exclude by name
```
kfilt -f ./pkg/decoder/test.yaml -x name=test
```
##### Exclude multiple kinds
```
kfilt -f ./pkg/decoder/test.yaml -x kind=configmap -x k=serviceaccount
```
##### Exclude with Label Selectors
```
kfilt -f ./pkg/decoder/test.yaml -x labels=app=test
```
### Shortcuts
Because "kind", "name", and "labels" are the most commonly used fields to filter by, kfilt has special flags allowing you to save some typing.
You can include by "kind" by using the `--kind` (or `-k`) flag with just the name of the kind you want to filter by. You can use `--exclude-kind` (or `-K`) for exclusions.
The corresponding flags for "name" queries are `--name` (`-n`) and `--exclude-name` (`-N`).
Finally, you can use label selectors with the `--labels` (`-l`) and `--exclude-labels` (`L`) flags.
#### Include ConfigMaps and Service Accounts
```
kfilt -f ./pkg/decoder/test.yaml -k configmap -k serviceaccount
```
#### Exclude resources named "test"
```
kfilt -f ./pkg/decoder/test.yaml -N test
```
#### Include resources labeled with "app=test"
```
kfilt -f ./pkg/decoder/test.yaml -l app=test
```
---
1 _note_: kfilt has not been tested extensively on Windows. Please file an issue if you run into any problems.