-
Notifications
You must be signed in to change notification settings - Fork 0
/
carbon-cronjob.yaml
141 lines (140 loc) · 3.64 KB
/
carbon-cronjob.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# This cronjob queries an existing Prometheus for `entsoe_generation_co2`
# and makes decission how many resourcequotas are allowed for the workload in the running namespace.
---
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
app: carbon-cronjob
name: carbon-cronjob
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
labels:
app: carbon-cronjob
name: carbon-cronjob
rules:
- apiGroups:
- ""
resources:
- resourcequotas
verbs:
- create
- get
- list
- watch
- patch
- update
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
labels:
app: carbon-cronjob
name: carbon-cronjob
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: carbon-cronjob
subjects:
- kind: ServiceAccount
name: carbon-cronjob
---
apiVersion: v1
data:
run.sh: |
#!/bin/bash
# set 3 carbon emission limits to operate resource quotas
highlimit=80
middlelimit=50
smalllimit=40
quota=10
# get the current carbon emission from entsoe
carbon=$(curl -s http://project-monitoring-prometheus.cattle-monitoring-system:9090/api/v1/query?query=entsoe_generation_co2 | jq -r '.data.result[]|select(.metric.job=="caas-carbon-footprint")|.value[-1]')
if [ "${#carbon}" -lt 1 ]; then
echo "no carbon emission data at this time"
exit
fi
# unit is g/s, multiplicate and round because bash can't handle floating numbers
carbon=$(printf "%.0f\n" $( bc -l <<<"10000*$carbon" ))
echo "carbon factor is "$carbon" , operate the quota now"
# set the cpu resource limit based on the current carbon emission factor
if [[ $carbon -gt $highlimit ]]; then
quota=10
elif [[ $carbon -gt $middlelimit ]] && [[ $carbon -lt $highlimit ]]; then
quota=1
elif [[ $carbon -lt $middlelimit ]] && [[ $carbon -gt $smalllimit ]]; then
quota=500m
elif [[ $carbon -lt $smalllimit ]] && [[ $carbon -gt 0 ]]; then
quota=100m
fi
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ResourceQuota
metadata:
labels:
resourcequota.management.cattle.io/default-resource-quota: "true"
name: co2-quota
spec:
hard:
limits.cpu: $quota
EOF
kind: ConfigMap
metadata:
labels:
app: carbon-cronjob
name: carbon-cronjob
---
apiVersion: batch/v1
kind: CronJob
metadata:
labels:
job-name: carbon-cronjob
name: carbon-cronjob
spec:
concurrencyPolicy: Forbid
failedJobsHistoryLimit: 1
successfulJobsHistoryLimit: 1
suspend: false
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
restartPolicy: Never
containers:
- image: mtr.devops.telekom.de/caas/k8s-tools:latest
imagePullPolicy: Always
name: carbon-cronjob
command: ["sh","-c"]
args: ["/sidecar/run.sh"]
resources:
limits:
cpu: 400m
memory: 512Mi
requests:
cpu: 100m
memory: 128Mi
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
privileged: false
readOnlyRootFilesystem: true
runAsUser: 1000
runAsGroup: 1000
volumeMounts:
- name: carbon-cronjob
mountPath: /sidecar
securityContext:
fsGroup: 1000
supplementalGroups:
- 1000
serviceAccountName: carbon-cronjob
volumes:
- name: carbon-cronjob
configMap:
defaultMode: 0755
name: carbon-cronjob