Writing Custom Assertion for KubeAssert

When to Write Custom Assertion

Create a Shell Script File

#!/bin/bashfunction cluster {
:
}
kubectl assert cluster
ASSERT PASS

Implement the Assertion

  • Validate input arguments. In our case, it is the cluster name that will be input by user from the command line. If the validation is failed, you can call the utility function logger::error to print error message and exit the assertion with a non-zero code.
  • Print assertion message. This will print a message, by calling the utility function logger::assert, to tell people what you are going to assert.
  • Run some kubectl commands. This will query the cluster using kubectl.
  • Validate results. By parsing the results returned from kubectl, either fail the assertion or let it pass. If the validation is failed, you can call the utility function logger::fail to print a failure message. Otherwise, you can simply do nothing or call logger::info to print some normal logs to give user a bit more background on what is going on.
#!/bin/bashfunction cluster {
# Validate input arguments
[[ -z $1 ]] && logger::error "You must specify a cluster name." && exit 1
# Print assertion message
logger::assert "Cluster with name $1 should be included in kubeconfig."
# Run some kubectl commands
kubectl config get-clusters
# Validate results
if cat $HOME/.kubeassert/result.txt | grep -q ^$1$; then
# Print normal logs
logger::info "Found $1 in kubeconfig."
else
# Print failure message
logger::fail "$1 not found."
fi
}
kubectl assert cluster
ERROR You must specify a cluster name.
kubectl assert cluster kind
ASSERT Cluster with name kind should be included in kubeconfig.
ASSERT FAIL kind not found.
kubectl assert cluster kind-foo
ASSERT Cluster with name kind-foo should be included in kubeconfig.
INFO Found kind-foo in kubeconfig.
ASSERT PASS
kubectl assert cluster kind-foo -v
ASSERT Cluster with name kind-foo should be included in kubeconfig.
INFO kubectl config get-clusters
NAME
kind-foo
kind-bar
INFO Found kind-foo in kubeconfig.
ASSERT PASS

Add Comment to the Assertion

##
# @Name: <Input your single-line assertion name here>
# @Description: <Input your single-line assertion description here>
# @Usage: <Input your single-line assertion 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>
##
  • ${GLOBAL_OPTIONS}: This variable represents global options that should be applied to all assertions, e.g. -h/--help to print the help information.
  • ${SELECT_OPTIONS}: This variable represents options that are used to filter on resources in cluster, e.g. -l/--selector to filter resource by labels, and -n/--namespace to limit resource to a specific namespace.
  • ${OP_VAL_OPTIONS}: This variable represents comparison operators, e.g. -eq, -lt, -gt, -ge, -le.
##
# @Name: cluster
# @Description: Assert specified cluster included in kubeconfig
# @Usage: kubectl assert cluster (NAME) [options]
# @Options:
# ${GLOBAL_OPTIONS}
# @Examples:
# # To assert a cluster is included in kubeconfig
# kubectl assert cluster kind-foo
##
function cluster {
...
}
kubectl assert --help
kubectl assert cluster --help
Assert cluster with specified name included in kubeconfig fileUsage: kubectl assert cluster (NAME) [options]Options:
-h, --help: Print the help information.
-v, --verbose: Enable the verbose log.
-V, --version: Print the version information.

Summary

#!/bin/bash##
# @Name: cluster
# @Description: Assert specified cluster included in kubeconfig
# @Usage: kubectl assert cluster (NAME) [options]
# @Options:
# ${GLOBAL_OPTIONS}
# @Examples:
# # To assert a cluster is included in kubeconfig
# kubectl assert cluster kind-foo
##
function cluster {
# Validate input arguments
[[ -z $1 ]] && logger::error "You must specify a cluster name." && exit 1
# Print assertion message
logger::assert "Cluster with name $1 should be included in kubeconfig."
# Run some kubectl commands
kubectl config get-clusters
# Validate results
if cat $HOME/.kubeassert/result.txt | grep -q ^$1$; then
# Print normal logs
logger::info "Found $1 in kubeconfig."
else
# Print failure message
logger::fail "$1 not found."
fi
}

--

--

--

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

Can’t be f#@!’d

Associated Domains

Kent Beck — Explore, Expand, Extract; TCR and Limbo!

Merge Sort

You Should Normalize Your CSS

Kubernetes Canary Deployment #2 Argo Rollouts

I Code But I’m Not An Engineer

My very old 13 inch Macbook 2009 on the right bought by my older sister and 13 inch MacBook Air 2015 bought by my mom

Leetcode

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

HashiCorp Vault — Authentication Options

Safely handle secrets on your GitOps flow using ArgoCD and Helm

Announcing Our $25 Million Series A from Coatue to Grow Developer-Centric Community Around…

Autoscaling with Keda