Last active
March 2, 2020 12:28
-
-
Save dplusic/b48396022c42df7ccbeb3c9521abbfc6 to your computer and use it in GitHub Desktop.
Kubernetes ConfigMap Compare and Swap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
# | |
# Usage Example: ./k8s_configmap_cas.sh -n namespace configmap-name '{"key":"value1"}' '{"key":"value2"}' | |
# | |
NAMESPACE= | |
while getopts n: o; do | |
case $o in | |
(n) NAMESPACE=$OPTARG;; | |
(*) exit 1 | |
esac | |
done | |
shift "$((OPTIND - 1))" | |
CONFIGMAP_NAME=$1 | |
CONFIGMAP_DATA_OLD=$2 | |
CONFIGMAP_DATA_NEW=$3 | |
if [ -z "$NAMESPACE" ] | |
then | |
NAMESPACE_OPTION= | |
else | |
NAMESPACE_OPTION="--namespace $NAMESPACE" | |
fi | |
CONFIGMAP_DESC=$(kubectl get configmap "$CONFIGMAP_NAME" --no-headers --output json $NAMESPACE_OPTION) | |
CONFIGMAP_DATA_EXIST=$(echo "$CONFIGMAP_DESC" | jq -c .data) | |
RESOURCE_VERSION="$(echo "$CONFIGMAP_DESC" | jq .metadata.resourceVersion)" | |
if [ "$CONFIGMAP_DATA_EXIST" != "$CONFIGMAP_DATA_OLD" ] | |
then | |
exit 2 | |
fi | |
echo " | |
apiVersion: v1 | |
kind: ConfigMap | |
metadata: | |
name: $CONFIGMAP_NAME | |
resourceVersion: $RESOURCE_VERSION | |
data: $CONFIGMAP_DATA_NEW | |
" | kubectl replace --filename - $NAMESPACE_OPTION |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
for VARIABLE in {1..10} | |
do | |
for (( ; ; )) | |
do | |
NUM_OLD=$(kubectl get configmap my-config -o json | jq -r .data.num) | |
NUM_NEW=$(echo "$NUM_OLD + 1" | bc) | |
if ./cas-configmap.sh my-config "{\"num\":\"$NUM_OLD\"}" "{\"num\":\"$NUM_NEW\"}" | |
then | |
break | |
fi | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment