Writing a Kube Macro

When to Write Your Own Kube Macro

Create a shell script file

#!/bin/bashfunction get-pod-containers {
:
}
kubectl macro get-pod-containers

Implement the Macro

#!/bin/bashfunction get-pod-containers {
local args=()
local ns=''
# Parse the arguments input from command line
while [[ $# -gt 0 ]]; do
case "$1" in
-n|--namespace|-n=*|--namespace=*)
ns=$2
args+=("$1 $2")
shift
shift ;;
-A|--all-namespaces)
ns="all namespaces"
args+=("$1")
shift ;;
*)
args+=("$1")
shift ;;
esac
done
# Run kubectl get and define custom columns to include pod container names
local custom_columns="NAME:.metadata.name,CONTAINERS:.spec.containers[*].name"
if [[ $ns == "all namespaces" ]]; then
custom_columns="NAMESPACE:.metadata.namespace,$custom_columns"
fi
kubectl get pods ${args[@]} -o custom-columns=$custom_columns
}
  • Parse the arguments input from command line.
  • Run kubectl get and define custom columns to include the pod container names extracted from pod definition by specifying the JSONPath.
kubectl macro get-pod-containers

Add Comment to The Macro

##
# @Name: <Input your single-line macro name here>
# @Description: <Input your single-line macro description here>
# <Input detailed description for your macro started from here>
# <It supports multiple lines and markdown syntax>
# @Author: <Input single-line author name here, support markdown syntax>
# @Usage: <Input your single-line macro usage information here>
# @Options:
# <Input help information for all your options started from here>
# <It supports multiple lines>
# @Examples:
# <Input detailed information for all examples started from here>
# <It supports multiple lines>
# @Dependencies: <Input single-line comma-separated dependencies of your macro here>
##
##
# @Name: get-pod-containers
# @Description: List the pods with their containers.
#
# This is a sample macro to demonstrate how to write a macro by your own.
# It can be used to list pods and their containers. For example, to list
# all pods and their containers in `kube-system` namespace:
# ```shell
# kubectl macro get-pod-containers -n kube-system
# NAME CONTAINERS
# coredns-6955765f44-gtx2q coredns
# coredns-6955765f44-tz96m coredns
# etcd-kind-control-plane etcd
# kindnet-4pzm7 kindnet-cni
# kube-apiserver-kind-control-plane kube-apiserver
# kube-controller-manager-kind-control-plane kube-controller-manager
# kube-proxy-b6wn8 kube-proxy
# kube-scheduler-kind-control-plane kube-scheduler
# ```
#
# @Author: [morningspace](https://github.com/morningspace/)
# @Usage: kubectl macro get-pod-containers [options]
# @Options:
# -A, --all-namespaces=false: If present, list the requested object(s) across all namespaces. Namespace in current
# context is ignored even if specified with --namespace.
# -h, --help: Print the help information.
# -n, --namespace='': If present, the namespace scope for this CLI request.
# -l, --selector='': Selector (label query) to filter on, not including uninitialized ones.
# --version: Print the version information.
# -w, --watch=false: After listing/getting the requested object, watch for changes. Uninitialized objects are excluded
# if no object name is provided.
# --watch-only=false: Watch for changes to the requested object(s), without listing/getting first.
# @Examples:
# # To list all pods with their containers in a namespace.
# kubectl macro get-pod-containers -n foo
# # To list all pods with their containers in all namespaces.
# kubectl macro get-pod-containers -A
# @Dependencies: kubectl
##
function cluster {
...
}
kubectl macro --help
kubectl macro get-pod-containers --help
get-pod-containers: List the pods with their containers.Usage: kubectl macro get-pod-containers [options]Options:
-A, --all-namespaces=false: If present, list the requested object(s) across all namespaces. Namespace in current
context is ignored even if specified with --namespace.
-h, --help: Print the help information.
-n, --namespace='': If present, the namespace scope for this CLI request.
-l, --selector='': Selector (label query) to filter on, not including uninitialized ones.
--version: Print the version information.
-w, --watch=false: After listing/getting the requested object, watch for changes. Uninitialized objects are excluded
if no object name is provided.
--watch-only=false: Watch for changes to the requested object(s), without listing/getting first.
Examples:
# To list all pods with their containers in a namespace.
kubectl macro get-pod-containers -n foo
# To list all pods with their containers in all namespaces.
kubectl macro get-pod-containers -A

Macro Dependencies

Summary

--

--

--

Life is coding and writing! I am a software engineer who have been in IT field for 10+ years. I would love to write beautiful code and story for people.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Booming February with 30 days — 7 challenges — 166,666 $HRZ in Horizonland

An inner look into AsyncTask

How to Select the Correct Machine Vision Lens for Your Application

How to login to your Space Pool farm settings page?

A way of testing views in ClojureScript Apps

Daily Devotion Web Scraping Using Python

Creating a RESTful currency exchange rate calculator with Flutter

Comprehensive Analysis of the Case of the Killer Robot

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
MorningSpace

MorningSpace

Life is coding and writing! I am a software engineer who have been in IT field for 10+ years. I would love to write beautiful code and story for people.

More from Medium

Implementing a vPLC solution over 5G network

No! You shall not pass! Kyverno’s here!

Complying with the Executive Order is easy with RKVST SBOM Hub

Log4Hell — Just the beginning…