Skip to content

Instantly share code, notes, and snippets.

@ericjster
Created July 6, 2022 16:12
Show Gist options
  • Save ericjster/5155e727521e88cb2f9cce9a5d2d47e4 to your computer and use it in GitHub Desktop.
Save ericjster/5155e727521e88cb2f9cce9a5d2d47e4 to your computer and use it in GitHub Desktop.
Set draino/drain-buffer
#!/bin/bash
# See https://github.com/DataDog/k8s-resources/pull/33140
# See https://datadoghq.atlassian.net/wiki/spaces/K8S/pages/2277966820/NLA+-+FAQ#%5BinlineExtension%5DHow-can-I-slow-down-the-pace-of-node-rotation-for-my-nodegroup-%3F
# We want to set draino/drain-buffer to a fixed value, for all clusters.
# Currently NLA/Draino is only on us3.prod.dog.
BUFFERVAL=9h
FPATCH=/tmp/tmp_patch.yaml
FPATCHJSON=/tmp/tmp_patch.json
set -e
kubectx plain4.us3.prod.dog
update_one() {
ns=$1
ng=$2
echo "$ns $ng"
# Kubelet: Annotations: draino/drain-buffer:
# Outputs all annotations. We just one the one for drain-buffer.
kubectl get ng -n $ns $ng -o=jsonpath="{.spec.kubelet.annotations}" | jq
# kubectl get ng -n $ns $ng -o yaml | grep "draino/drain-buffer"
# https://github.com/kubernetes/kubernetes/issues/37666
# Cannot use annotate because we want a nested element.
# kubectl annotate ng -n $ns $ng --overwrite "draino/drain-buffer=$BUFFERVAL"
# kubectl annotate ng -n $ns $ng --overwrite "draino/drain-buffer=$BUFFERVAL"
# echo '{"spec":{"kubelet":{"annotations":{"draino/drain-buffer": "'${BUFFERVAL}'" }}}}' >$FPATCH
if false; then
# Try using a file.
# Tried lowercase, and tried mixed case, did not seem to make a difference.
echo "Spec:" >$FPATCH
echo " Kubelet:" >>$FPATCH
echo " Annotations:" >>$FPATCH
echo " draino/drain-buffer: \"$BUFFERVAL\"" >>$FPATCH
# echo " draino/drain-buffer: $BUFFERVAL" >>$FPATCH
cat $FPATCH
# kubectl patch -n $ns ng $ng --patch-file $FPATCH --type=merge
# kubectl patch -n $ns ng $ng --patch-file $FPATCH # Gives error
# --type=merge does not give error, but did not save either
# kubectl patch -n $ns ng $ng --patch-file $FPATCH --type strategic
# Error from server (UnsupportedMediaType): the body of the request was in an unknown format - accepted media types include: application/json-patch+json, application/merge-patch+json, application/apply-patch+yaml
# If I understand right from one of the bugs, we cannot use strategic merge for an internal nested resource or something like that.
# kubectl patch -n $ns ng $ng --patch-file $FPATCH --type merge
kubectl patch -n $ns ng $ng --patch-file $FPATCH --dry-run=server
# Error from server (UnsupportedMediaType): the body of the request was in an unknown format - accepted media types include: application/json-patch+json, application/merge-patch+json, application/apply-patch+yaml
# Maybe this problem:
# https://github.com/redhat-cop/resource-locker-operator/issues/63
# https://github.com/argoproj/argo-rollouts/issues/161
fi
if false; then
# Use a json file. Note that we give directions for what to do in the json.
echo "- op: replace" >$FPATCH
echo " path: \"/spec/kubelet/annotations/draino/drain-buffer\"" >>$FPATCH
# echo " path: \"/spec/kubelet/annotations/draino//drain-buffer\"" >>$FPATCH
# echo " path: \"/spec/kubelet/annotations/draino\\/drain-buffer\"" >>$FPATCH
# echo " value: \"$BUFFERVAL\"" >>$FPATCH
echo " value: $BUFFERVAL" >>$FPATCH
cat $FPATCH
kubectl patch -n $ns ng $ng --patch-file $FPATCH --type=json --dry-run=server
# The request is invalid.
# kubectl patch -n $ns ng $ng --patch-file $FPATCH --type=json
# The request is invalid.
fi
if false; then
echo '{"spec":{"kubelet":{"annotations":{"draino/drain-buffer": "'${BUFFERVAL}'" }}}}' >$FPATCH
cat $FPATCH
kubectl patch -n $ns ng $ng --patch-file $FPATCH --type=json
# Error from server (BadRequest): json: cannot unmarshal object into Go value of type jsonpatch.Patch
# echo '{"kubelet":{"annotations":{"draino/drain-buffer": "'${BUFFERVAL}'" }}}' >$FPATCH
# cat $FPATCH
# kubectl patch -n $ns ng $ng --patch-file $FPATCH --type json
# Error from server (BadRequest): json: cannot unmarshal object into Go value of type jsonpatch.Patch
fi
if false; then
# kubectl patch -n $ns ng $ng --patch '{"spec":{"kubelet":{"annotations":{"draino/drain-buffer": "11h" }}}}'
# Error from server (UnsupportedMediaType): the body of the request was in an unknown format - accepted media types include: application/json-patch+json, application/merge-patch+json, application/apply-patch+yaml
kubectl patch -n $ns ng $ng --type='json' --patch '{"spec":{"kubelet":{"annotations":{"draino/drain-buffer": "11h" }}}}'
# Error from server (UnsupportedMediaType): the body of the request was in an unknown format - accepted media types include: application/json-patch+json, application/merge-patch+json, application/apply-patch+yaml
fi
if false; then
# Try []
kubectl patch -n $ns ng $ng --dry-run=server --type='json' --patch '{"spec":{"kubelet":{"annotations":[{"draino/drain-buffer": "11h" }]}}}'
# Error from server (BadRequest): json: cannot unmarshal object into Go value of type jsonpatch.Patch
fi
if false; then
kubectl patch -n $ns ng $ng --dry-run=server --type='json' --patch '{"spec":{"kubelet":{"annotations":{"draino/drain-buffer": "11h" }}}}'
# Error from server (BadRequest): json: cannot unmarshal object into Go value of type jsonpatch.Patch
fi
if true; then
kubectl patch -n $ns ng $ng --dry-run=server --type=strategic --patch '{"spec":{"kubelet":{"annotations":{"draino/drain-buffer": "11h" }}}}'
# Error from server (UnsupportedMediaType): the body of the request was in an unknown format - accepted media types include: application/json-patch+json, application/merge-patch+json, application/apply-patch+yaml
fi
kubectl get ng -n $ns $ng -o=jsonpath="{.spec.kubelet.annotations}" | jq
# kubectl get ng -n $ns $ng -o yaml | grep "draino/drain-buffer"
}
update_one cassandra-hosts cassandra-hosts
# kubectl get ng -l service=cassandra -A --no-headers | while read -r ns ng junk; do
# update_one $ns $ng
# done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment