Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automatically adjust pr envs resource request #560

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apiVersion: v2
name: patch-pr-env-resource-requests
version: 0.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
apiVersion: batch/v1
kind: CronJob
metadata:
name: patch-resource-requests
namespace: default
spec:
schedule: "0 */6 * * *" # Every 6 hours
jobTemplate:
spec:
template:
spec:
containers:
- name: patch-resources
image: bitnami/kubectl:latest
command:
- /bin/sh
- -c
- |
#!/bin/sh
for ns in $(kubectl get ns --no-headers -o custom-columns=":metadata.name" | grep -E "dpl-cms-pr|dpl-bnf-pr"); do
for deploy in nginx redis varnish cli; do
if kubectl get deployment $deploy -n $ns > /dev/null 2>&1; then
echo "Patching deployment $deploy in namespace $ns"
case $deploy in
nginx)
memory_request="160Mi"
memory_limit="256Mi"
;;
redis)
memory_request="60Mi"
memory_limit="256Mi"
;;
varnish)
memory_request="180Mi"
memory_limit="256Mi"
;;
cli)
memory_request="25Mi"
memory_limit="125Mi"
;;
*)
memory_request="150Mi"
memory_limit="256Mi"
;;
esac
kubectl patch deployment $deploy -n $ns \
--type=json \
-p="[{\"op\": \"replace\", \"path\": \"/spec/template/spec/containers/0/resources\", \"value\": {\"requests\": {\"cpu\": \"15m\", \"memory\": \"$memory_request\"}, \"limits\": {\"cpu\": \"200m\", \"memory\": \"$memory_limit\"}}}]"
else
echo "Deployment $deploy not found in namespace $ns"
fi
done
echo "sleeping for a minute to give the deployments time to get back up and not crash the database"
sleep 60
done
restartPolicy: OnFailure
serviceAccountName: pr-env-patcher-cronjob-sa
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: pr-env-patcher-cronjob-sa
namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: deployments-patch-list-cluster-role
rules:
- apiGroups: [""]
resources: ["namespaces"]
verbs: ["list"]
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "list", "patch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: namespace-lister-binding
subjects:
- kind: ServiceAccount
name: pr-env-patcher-cronjob-sa
namespace: default
roleRef:
kind: ClusterRole
name: deployments-patch-list-cluster-role
apiGroup: rbac.authorization.k8s.io
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
helm upgrade patch-pr-env-resource-requests chart \
--install
Loading