Skip to content

Instantly share code, notes, and snippets.

@mathew-fleisch
Last active August 6, 2023 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathew-fleisch/9743d9801cc2ce3bd6aa678b49cd8ea6 to your computer and use it in GitHub Desktop.
Save mathew-fleisch/9743d9801cc2ce3bd6aa678b49cd8ea6 to your computer and use it in GitHub Desktop.
Using a configmap as a simple queue with kubectl
#!/bin/bash
ns=$1
cm=$2
cmkey=$3
# actions: getfirst, getlast, getall, getyaml, shift, pop, prepend, append, delete
action=$4
cmval=$5
k="kubectl -n $ns"
jp="jsonpath='{.data.$cmkey}'"
er=0
usage="Usage: ./cm-queue.sh [namespace] [configmap-name] [queue-name] [getfirst|getlast|getall|getyaml|shift|pop|prepend|append|delete] [value]"
[ -z "$ns" ] && echo "Must define a namespace" && echo $usage && exit 1
[ -z "$cm" ] && echo "Must define a configmap name" && echo $usage && exit 1
[ -z "$cmkey" ] && echo "Must define a queue name (configmap key)" && echo $usage && exit 1
[ -z "$action" ] && echo "Must define an action" && echo $usage && exit 1
# If the queue doesn't exist... make it ¯\_(ツ)_/¯
if [ -z "$($k get cm $cm --ignore-not-found=true)" ]; then
create=$($k create cm $cm)
fi
function getall() {
$k get cm $cm -o $jp | tr ' ' '\n' | sed -e "s/'//g"
}
function trim() {
echo "$1" | sed 's/^ *//' | sed 's/ *$//g'
}
# getfirst: Get item from beginning of list
if [ $action == "getfirst" ]; then
getall | head -1 && er=1
fi
# getlast: Get item from end of list
if [ $action == "getlast" ]; then
getall | tail -1 && er=1
fi
# getall: Get full list
if [ $action == "getall" ]; then
getall && er=1
fi
# getyaml: get yaml
if [ $action == "getyaml" ]; then
$k get cm $cm -o yaml && er=1
fi
# shift: Remove item from beginning of list
if [ $action == "shift" ]; then
first=$(getall | head -1)
queue="$(getall | tail -n +2 | tr '\n' ' ')"
patch=$($k patch cm $cm --patch='{"data": {"'"$cmkey"'": "'"$(trim "$queue")"'"}}')
echo "$first" && er=1
fi
# pop: Remove item from end of list
if [ $action == "pop" ]; then
last=$(getall | tail -1)
len=$(getall | wc -l | awk '{print $1}')
if [ $len -gt 0 ]; then
queue="$(getall | head -n $((len)) | tr '\n' ' ')"
else
queue=""
fi
patch=$($k patch cm $cm --patch='{"data": {"'"$cmkey"'": "'"$(trim "$queue")"'"}}')
echo "$last" && er=1
fi
# prepend: Add item to beginning of list
if [ $action == "prepend" ]; then
[ -z "$cmval" ] && echo "Must define a configmap queue value to prepend" && echo $usage && exit 1
queue="$cmval $(getall | tr '\n' ' ')"
patch=$($k patch cm $cm --patch='{"data": {"'"$cmkey"'": "'"$(trim "$queue")"'"}}')
getall && er=1
fi
# append: Add item to end of list
if [ $action == "append" ]; then
[ -z "$cmval" ] && echo "Must define a configmap queue value to append" && echo $usage && exit 1
queue="$(getall | tr '\n' ' ') $cmval"
patch=$($k patch cm $cm --patch='{"data": {"'"$cmkey"'": "'"$(trim "$queue")"'"}}')
getall && er=1
fi
# delete: delete the whole configmap
if [ $action == "delete" ]; then
$k delete cm $cm && er=1
fi
if [ $er -eq 0 ]; then
echo "Error: action[\"$action\"] failed."
[ -n "$patch" ] && echo "$patch"
echo "$usage"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment