π¬ Command line utility for filtering collections of Kubernetes resources
This repository provides krf, a tool that can be used to easily sift through large collections of Kubernetes resources using a number of powerful filters.
The original impetus for this was the need to search through a stream of Kubernetes yaml documents for resources that contain some value.
The immediate next problem is that tools like e.g. grep are only filename/line number aware at best; so you end up with a list of matching lines, but you are no closer to knowing which specific resources contain those lines.
To address this, krf was build as a Kubernetes resource aware grep, but can be utilized with both more power and precision.
Binaries for various architectures are published on the releases page.
The latest release can be installed by running:
OS=$(uname | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m | sed 's/x86_64/amd64/; s/aarch64/arm64/')
wget -O krf.tar.gz https://github.com/joshdk/krf/releases/latest/download/krf-${OS}-${ARCH}.tar.gz
tar -xf krf.tar.gz
sudo install krf /usr/local/bin/krfRelease binaries are also available via Brew.
The latest release can be installed by running:
brew tap joshdk/tap
brew install joshdk/tap/krfInstallation can also be done directly from this repository.
The latest commit can be installed by running:
go install github.com/joshdk/krf@masterTo begin, a corpus of input resources can be provided in a number of ways.
A single file:
krf resources.yamlAll yaml files contained within a directory:
krf ./manifests/The output of kustomize build:
kustomize build β¦ | krfThe output of kubectl in yaml or json format:
kubectl get β¦ -o=yaml | krf
kubectl get β¦ -o=json | krfThe input corpus can then be filtered using a set of individual matchers that you can mix and match.
Include resources that have the name backend:
β¦ | krf --name backendInclude resources that have the name backend and the namespace default:
β¦ | krf --name backend --namespace defaultInclude resources that have the name backend and are of kind Deployment or Service:
β¦ | krf --name backend --kind deploy,svcInclude resources that have the name backend or frontend and are not of kind Ingress:
β¦ | krf --name backend,frontend --not-kind ingFinally, simply output the same set of resources that were provided as input:
β¦ | krfWhen krf is invoked, each input resource is evaluated against various categories of positive and negative matchers in order to reject, or ultimately accept the resource.
At a high level, in order to be accepted, each resource:
- Must not match any of the negative matcher values from each provided category.
- Must match any of the positive matcher values from each provided category.
As a concrete example, consider an invocation like so:
β¦ | krf --not-kind job,po --name backend,cache --not-label role=secondary --namespace client-a,client-b,client-cThe following checks are then performed against each resource in the input stream:
- If the resource is of kind of
Job, then the resource is rejected. - If the resource is of kind of
Pod, then the resource is rejected. - If the resource has the label
role=secondary, then the resource is rejected. - If the resource does not have a name of
backendorcache, then the resource is rejected. - If the resource does not have a namespace of
client-a,client-b, orclient-c, then the resource is rejected. - Otherwise, the resource is finally accepted!
A resource such as this would be accepted by the above rules:
apiVersion: apps/v1
kind: Deployment
metadata:
name: cache
namespace: client-b
labels:
role: primaryBy default, resources will be output in a table format:
β¦ | krf
Namespace API Version Kind Name
βββββββββ βββββββββββ ββββ ββββ
default apps/v1 Deployment backendIf resources are sourced from a file (opposed to stdin) then the associated paths will also be included:
krf ./manifests
Namespace API Version Kind Name Path
βββββββββ βββββββββββ ββββ ββββ ββββ
default apps/v1 Deployment backend ./manifests/deployment.yamlIf output is being redirected (to a file or another command) then the resources will be output in a yaml format:
β¦ | krf | cat
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: default
name: backendYou can also explicitly choose to output resources in yaml format:
β¦ | krf -o=yaml
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: default
name: backendConversely, you must explicitly choose to output resources in table format if you want to redirect that output:
β¦ | krf -o=table | tee summary.txt
Namespace API Version Kind Name
βββββββββ βββββββββββ ββββ ββββ
default apps/v1 Deployment backendYou can also output only the names of filtered resources (similar to kubectl -o=name):
β¦ | krf -o=name
Deployment/backendOr output only the names of resources that are referenced by filtered resources:
β¦ | krf -o=references
ConfigMap/configuration
PersistentVolumeClaim/database
Secret/api-credentialsHere is a collection of some useful ways to utilize krf.
Get a quick overview of all resources:
krf ./kustomizeShow resource patches (files ending in .patch.yaml) in the production overlay:
krf ./kustomize/environments/production --patchIdentify deployments that would roll if the credentials ConfigMap (which may have a generated name like credentials-8mbdf7882g) were updated:
kubectl get deploy -o=yaml | krf --references cm/credentials- Identify pods that do not have a security context configured:
kubectl get pod -o=yaml | krf --not-jsonpath '..securityContext'Iteratively verify the impact of refactors on resources:
# Render out the current resources:
kustomize build ./kustomize/environments/production > original.yaml
# Perform your refactors on the various resources or manifests:
vim ./kustomize/applications/kustomization.yaml
# Render out the (now refactored) resources:
kustomize build ./kustomize/environments/production > modified.yaml
# Display a list of any of the modified resources which differences from their
# original counterparts. Verify that your changes had the desired impact, or
# even to verify that no resources were ultimately impacted.
krf --diff original.yaml modified.yamlSelectively apply certain resources:
kustomize build β¦ | krf --kind svc,ing | kubectl apply -f -Perform some additional processing on resources:
krf β¦ -o=json | while IFS= read -r line; do
echo $line | jq -r '. | "processing \(.kind)/\(.metadata.name)..."'
doneThis code is distributed under the MIT License, see LICENSE.txt for more information.
Created by Josh Komoroske β