How To Develop Kubernetes CLIs Like a Pro
A short one today. Just wanted you to meet my new favorite Go library to work with Kubernetes - k8s.io/cli-runtime
. It's probably the most underappreciated package in the whole k8s.io/*
family based on its value to the number of stars ratio.
Here is what the README file says about it:
Set of helpers for creating kubectl commands, as well as kubectl plugins. This library is a shared dependency for clients to work with Kubernetes API infrastructure which allows to maintain kubectl compatible behavior.
If the above description didn't sound too impressive, let me try to decipher it for you - with the cli-runtime
library, you can write CLI tools that behave like and are as potent as the mighty kubectl!
Here is what you actually can achieve with just a few lines of code using the cli-runtime
library:
- Register the well-know flags like
--kubeconfig|--context|--namespace|--server|--token|...
and pass their values to one or moreclient-go
instances. - Look up cluster objects by their resources, kinds, and names with the full-blown support of familiar shortcuts like
deploy
fordeployments
orpo
forpods
. - Read and kustomize YAML/JSON Kubernetes manifests into the corresponding Go structs.
- Pretty-print Kubernetes objects as YAML, JSON (with JSONPath support), and even human-readable tables!
Interested? Then have a look at the usage examples below 😉