Created
July 6, 2018 08:59
-
-
Save frankh/050943c72273cf639886b43e98bc3caa to your computer and use it in GitHub Desktop.
HPA Downscale Limiter
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
apiVersion: batch/v1beta1 | |
kind: CronJob | |
metadata: | |
name: hpa-downscale-limiter | |
namespace: kube-system | |
spec: | |
schedule: "*/3 * * * *" | |
concurrencyPolicy: Forbid | |
jobTemplate: | |
spec: | |
template: | |
spec: | |
serviceAccountName: hpa-downscale-limiter | |
containers: | |
- name: kubectl | |
image: frankh/k8s-kubectl:1.10.3 | |
command: ["/bin/bash", "-c"] | |
args: | |
- | | |
set -xeuo pipefail | |
namespaces=$(kubectl get hpa --no-headers --all-namespaces | cut -d' ' -f1 | uniq) | |
for namespace in $namespaces; do | |
hpas=$(kubectl get hpa --namespace=$namespace --no-headers | cut -d' ' -f1) | |
for hpa in $hpas; do | |
echo "$(kubectl get hpa --namespace=$namespace $hpa -o jsonpath="{.spec.minReplicas} {.status.desiredReplicas} {.metadata.annotations.originalMinimum} {.metadata.annotations.downscaleLimit}")" > tmpfile | |
read -r minReplicas desiredReplicas originalMinimum downscaleLimit < tmpfile | |
if [ -z "$originalMinimum" ]; then | |
kubectl annotate hpa --namespace=$namespace $hpa originalMinimum="$minReplicas" | |
originalMinimum=$minReplicas | |
fi | |
if [ -z "$downscaleLimit" ]; then | |
downscaleLimit=1 | |
fi | |
target=$(( $desiredReplicas - $downscaleLimit )) | |
target=$(( $target > $originalMinimum ? $target : $originalMinimum )) | |
if [ "$minReplicas" -ne "$target" ]; then | |
kubectl patch hpa --namespace=$namespace $hpa --patch="{\"spec\": {\"minReplicas\": "$target"}}" | |
fi | |
done | |
done | |
restartPolicy: OnFailure | |
--- | |
apiVersion: v1 | |
kind: ServiceAccount | |
metadata: | |
name: hpa-downscale-limiter | |
namespace: kube-system | |
--- | |
apiVersion: rbac.authorization.k8s.io/v1beta1 | |
kind: ClusterRoleBinding | |
metadata: | |
name: hpa-downscale-limiter-admin | |
roleRef: | |
apiGroup: rbac.authorization.k8s.io | |
kind: ClusterRole | |
name: cluster-admin | |
subjects: | |
- name: hpa-downscale-limiter | |
kind: ServiceAccount | |
namespace: kube-system | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment