From e917868f6a561089b8477dd93a99228e69cbc5d3 Mon Sep 17 00:00:00 2001 From: Kazuki Suda Date: Mon, 22 Apr 2024 16:36:17 +0900 Subject: [PATCH] Add support for SidecarContainers --- pkg/resourcelist/resourcelist.go | 20 ++------------ pkg/resourcelist/resourcelist_test.go | 39 ++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/pkg/resourcelist/resourcelist.go b/pkg/resourcelist/resourcelist.go index c1b391d..ae89b80 100644 --- a/pkg/resourcelist/resourcelist.go +++ b/pkg/resourcelist/resourcelist.go @@ -20,29 +20,13 @@ package resourcelist import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" + v1resource "k8s.io/kubernetes/pkg/api/v1/resource" ) type ResourceList corev1.ResourceList func PodRequestResourceList(pod *corev1.Pod) ResourceList { - icRes := ResourceList{} - for _, c := range pod.Spec.InitContainers { - icRes.SetMax(ResourceList(c.Resources.Requests)) - } - - cRes := ResourceList{} - for _, c := range pod.Spec.Containers { - cRes.Add(ResourceList(c.Resources.Requests)) - } - - cRes.SetMax(icRes) - - // If Overhead is being utilized, add to the total requests for the pod - if pod.Spec.Overhead != nil { - cRes.Add(ResourceList(pod.Spec.Overhead)) - } - - return cRes + return ResourceList(v1resource.PodRequests(pod, v1resource.PodResourcesOptions{})) } func (lhs ResourceList) Add(rhs ResourceList) { diff --git a/pkg/resourcelist/resourcelist_test.go b/pkg/resourcelist/resourcelist_test.go index 8b9e8a8..8409618 100644 --- a/pkg/resourcelist/resourcelist_test.go +++ b/pkg/resourcelist/resourcelist_test.go @@ -73,7 +73,7 @@ var _ = Describe("PodResourceRequestList", func() { }) }) - Context("with init containers", func() { + Context("with regular init containers", func() { It("sets max(max(resources of initContainers), total resources of containers)", func() { p := &corev1.Pod{ Spec: corev1.PodSpec{ @@ -114,6 +114,43 @@ var _ = Describe("PodResourceRequestList", func() { }))).Should(Equal(true)) }) }) + + Context("with restartable init containers", func() { + It("sets max(max(resources of regular initContainers), total resources of containers and restartable containers)", func() { + restartPolicy := corev1.ContainerRestartPolicyAlways + p := &corev1.Pod{ + Spec: corev1.PodSpec{ + InitContainers: []corev1.Container{{ + Resources: corev1.ResourceRequirements{ + Requests: corev1.ResourceList{ + "n1": one, + }, + }, + RestartPolicy: &restartPolicy, + }, { + Resources: corev1.ResourceRequirements{ + Requests: corev1.ResourceList{ + "n1": one, + }, + }, + }}, + Containers: []corev1.Container{{ + Resources: corev1.ResourceRequirements{ + Requests: corev1.ResourceList{ + "n1": one, + }, + }, + }}, + }, + } + + r := PodRequestResourceList(p) + + Expect(r.EqualTo(ResourceList(corev1.ResourceList{ + "n1": two, + }))).Should(Equal(true)) + }) + }) }) var _ = Describe("ResourceList", func() {