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

Add support for SidecarContainers #10

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 2 additions & 18 deletions pkg/resourcelist/resourcelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
39 changes: 38 additions & 1 deletion pkg/resourcelist/resourcelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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() {
Expand Down
Loading