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
Changes from 1 commit
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,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\"}}}]"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the output here reveal if the deployment was changed or not? If so - you could skip the wait, if the resources had already been set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is a good point, will have to look into that

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: namespace-lister
rules:
- apiGroups: [""]
resources: ["namespaces"]
verbs: ["list"]
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "list", "patch"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does more than just listing namespaces. Perhaps name the resource for what uses them instead. This will be easier if you convert it to a helm chart.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah... sorry about that. I started out with a logging functionality to test it out and get a quick feedback loop. Will fix

---
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: namespace-lister
apiGroup: rbac.authorization.k8s.io
Loading