Skip to content

Commit 08fc149

Browse files
author
Mattias Andersson
committed
Add possibility to add resource request/limit to initContainer
This commit enables user to configure resource request and limit for cpu and memory on the "setup-ca-certs" initContainer. By not being able to set resources it blocks creation of the Pod if a namespace has a ResourceQuota in place. Signed-off-by: Mattias Andersson <[email protected]>
1 parent 6cdee4a commit 08fc149

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

pkg/certinjectionwebhook/admission_controller.go

+34
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"encoding/json"
1010
"fmt"
11+
"os"
1112

1213
"github.com/pkg/errors"
1314
admissionv1 "k8s.io/api/admission/v1"
@@ -224,6 +225,34 @@ func (ac *admissionController) SetCaCerts(ctx context.Context, obj *corev1.Pod)
224225
})
225226
}
226227

228+
var resources corev1.ResourceRequirements
229+
if cpuRequest, found := os.LookupEnv("INIT_CONTAINER_CPU_REQUEST"); found {
230+
if resources.Requests == nil {
231+
resources.Requests = corev1.ResourceList{}
232+
}
233+
resources.Requests[corev1.ResourceCPU] = resource.MustParse(cpuRequest)
234+
}
235+
236+
if memoryRequest, found := os.LookupEnv("INIT_CONTAINER_MEMORY_REQUEST"); found {
237+
if resources.Requests == nil {
238+
resources.Requests = corev1.ResourceList{}
239+
}
240+
resources.Requests[corev1.ResourceMemory] = resource.MustParse(memoryRequest)
241+
}
242+
243+
if cpuLimit, found := os.LookupEnv("INIT_CONTAINER_CPU_LIMIT"); found {
244+
if resources.Limits == nil {
245+
resources.Limits = corev1.ResourceList{}
246+
}
247+
resources.Limits[corev1.ResourceCPU] = resource.MustParse(cpuLimit)
248+
}
249+
if memoryLimit, found := os.LookupEnv("INIT_CONTAINER_MEMORY_LIMIT"); found {
250+
if resources.Limits == nil {
251+
resources.Limits = corev1.ResourceList{}
252+
}
253+
resources.Limits[corev1.ResourceMemory] = resource.MustParse(memoryLimit)
254+
}
255+
227256
container := corev1.Container{
228257
Name: "setup-ca-certs",
229258
Image: ac.setupCACertsImage,
@@ -244,6 +273,11 @@ func (ac *admissionController) SetCaCerts(ctx context.Context, obj *corev1.Pod)
244273
Capabilities: &corev1.Capabilities{Drop: []corev1.Capability{"ALL"}},
245274
},
246275
}
276+
277+
if len(resources.Requests) > 0 || len(resources.Limits) > 0 {
278+
container.Resources = resources
279+
}
280+
247281
obj.Spec.InitContainers = append([]corev1.Container{container}, obj.Spec.InitContainers...)
248282
}
249283

0 commit comments

Comments
 (0)