Vultr CLI and JSON output
- 2 minutes read - 411 wordsI’ve begun exploring Vultr after the company announced a managed Kubernetes offering Vultr Kubernetes Engine (VKE).
In my brief experience, it’s a decent platform and its CLI vultr-cli
is mostly (!) good. The CLI has a limitation in that command output is text formatted and this makes it challenging to parse the output when scripting.
NOTE The Vultr developers have a branch
rewrite
that includes a solution to this problem.
Example
ID 12345678-90ab-cdef-1234-567890abcdef
LABEL test
DATE CREATED 2022-01-01T00:00:00+00:00
CLUSTER SUBNET 255.255.255.255/16
SERVICE SUBNET 255.255.255.255/12
IP 255.255.255.255
ENDPOINT 12345678-90ab-cdef-1234-567890abcdef.vultr-k8s.com
VERSION v1.23.5+3
REGION mars
STATUS pending
NODE POOLS
ID 12345678-90ab-cdef-1234-567890abcdef
DATE CREATED 2022-01-01T00:00:00+00:00
DATE UPDATED 2022-01-01T00:00:00+00:00
LABEL nodepool
TAG foo
PLAN vc2-1c-2gb
STATUS pending
NODE QUANTITY 1
AUTO SCALER false
MIN NODES 1
MAX NODES 1
NODES
ID DATE CREATED LABEL STATUS
12345678-
Until that’s available, I’m lazy writing very simple bash scripts to parse vultr-cli
command output as JSON. The repo is vultr-cli-format
.
Ideally, the scripts would invoke the underlying vultr-cli
command in order to be able to ‘determine’ which command was being invoked and apply the correct formatting. But, for simplicity, the user must choose the correct command.
NOTE I’m considering restructuring the scripts so that they follow the
vultr-cli
command format, i.e.vultr-cli kubernetes get |\ ./ vultr-cli-output kubernetes get
There are multiple issues with this approach but being able to systematise converting vultr-cli
commands into a more structured form so that the output may be used in scripting, outweighs many of these.
Issues
vultr-cli
command output may change- Repeated sections in the command output (e.g.
NODE POOLS
andNODES
in the above example) are not converted - Table-oriented output (e.g.
NODES
in the above example) are not being converted. - Using
read
, output field names that are space separated (e.g.CLUSTER SUBNET
) require extra care - Likely others
Benefit
FORMAT="/path/to/script"
# Get latest Kubernetes version
VERSION=$(\
vultr-cli kubernetes versions \
| ${FORMAT}/k8s_version.sh \
| jq -r .version)
# Create a cluster and return its ID
ID=$(\
vultr-cli kubernetes create \
--label=${LABEL} \
--region=${REGION} \
--version=${VERSION} \
--node-pools=quantity:1,plan:vc2-1c-2gb,label:nodepool,tag:pool \
| ${FORMAT}/k8s.sh \
| jq -r .id)
# Wait until the cluster is active
until [ "active" = $(\
vultr-cli kubernetes get ${ID} \
| ${FORMAT}/k8s.sh \
| jq -r .status) ]
do
sleep 15s
done
# Create temporarily VKE config
local CONFIG="${PWD}/tmp/vke.$(date +%y%m%d).yaml"
vultr-cli kubernetes config ${ID} \
| base64 --decode > ${CONFIG}
# Create a namespace
kubectl create namespace ${NAMESPACE} \
--kubeconfig=${CONFIG}
# Deploy something
kubectl apply --filename=${DEPLOYMENT} \
--namespace=${NAMESPACE} \
--kubeconfig=${CONFIG}