Skip to content

Commit b5951a1

Browse files
committed
webhook: enforce minimum memory limit
If memory limit is set and less than minimum, set it to minimum. This is to to account for kata-containers@0ec3403 Signed-off-by: Saul Paredes <saulparedes@microsoft.com>
1 parent 5982b67 commit b5951a1

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

tools/testing/kata-webhook/deploy/webhook.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ spec:
2020
spec:
2121
containers:
2222
- name: pod-annotate-webhook
23-
image: quay.io/kata-containers/kata-webhook-example:latest
23+
image: marineraks.azurecr.io/kata-containers/kata-webhook:min_memory_limit
2424
imagePullPolicy: Always
2525
env:
2626
- name: RUNTIME_CLASS
@@ -29,6 +29,12 @@ spec:
2929
name: kata-webhook
3030
key: runtime_class
3131
optional: true
32+
- name: MIN_MEMORY_LIMIT
33+
valueFrom:
34+
configMapKeyRef:
35+
name: kata-webhook
36+
key: min_memory_limit
37+
optional: true
3238
args:
3339
- -tls-cert-file=/etc/webhook/certs/cert.pem
3440
- -tls-key-file=/etc/webhook/certs/key.pem
@@ -74,3 +80,4 @@ metadata:
7480
name: kata-webhook
7581
data:
7682
runtime_class: kata
83+
min_memory_limit: "128Mi"

tools/testing/kata-webhook/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515

1616
corev1 "k8s.io/api/core/v1"
17+
"k8s.io/apimachinery/pkg/api/resource"
1718
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1819

1920
"github.com/sirupsen/logrus"
@@ -75,6 +76,23 @@ func annotatePodMutator(_ context.Context, ar *kwhmodel.AdmissionReview, obj met
7576
kataRuntimeClassName := getRuntimeClass(runtimeClassEnvKey, "kata")
7677
pod.Spec.RuntimeClassName = &kataRuntimeClassName
7778

79+
minMemoryLimit, foundMinMemoryLimit := os.LookupEnv("MIN_MEMORY_LIMIT")
80+
81+
if foundMinMemoryLimit {
82+
minMemoryLimitVal := resource.MustParse(minMemoryLimit)
83+
for i := range pod.Spec.Containers {
84+
if pod.Spec.Containers[i].Resources.Limits == nil {
85+
continue
86+
} else {
87+
currentMemoryLimit := pod.Spec.Containers[i].Resources.Limits.Memory().Value()
88+
if currentMemoryLimit < minMemoryLimitVal.Value() {
89+
pod.Spec.Containers[i].Resources.Limits["memory"] = resource.MustParse(minMemoryLimit)
90+
fmt.Println("memory limit too low. Updating to : ", pod.Spec.Containers[i].Resources.Limits)
91+
}
92+
}
93+
}
94+
}
95+
7896
return &kwhmutating.MutatorResult{
7997
MutatedObject: pod,
8098
}, nil

0 commit comments

Comments
 (0)