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

include resources for sidecar init containers #340

Merged
merged 1 commit into from
Nov 27, 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
8 changes: 4 additions & 4 deletions pkg/model/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ func TestClusterAddPod(t *testing.T) {
t.Errorf("expected 0 pods, got %d", got)
}

if got := cluster.Stats().UsedResources["cpu"]; got.Cmp(resource.MustParse("1")) != 0 {
t.Errorf("expected 1 CPU used, got %s", got.String())
if got := cluster.Stats().UsedResources["cpu"]; got.Cmp(resource.MustParse("2")) != 0 {
t.Errorf("expected 2 CPU used, got %s", got.String())
}

// deleting the pod should remove the usage
Expand Down Expand Up @@ -194,8 +194,8 @@ func TestClusterDeleteNodeDeletesPods(t *testing.T) {
t.Errorf("expected 0 pods, got %d", got)
}

if got := cluster.Stats().UsedResources["cpu"]; got.Cmp(resource.MustParse("1")) != 0 {
t.Errorf("expected 1 CPU used, got %s", got.String())
if got := cluster.Stats().UsedResources["cpu"]; got.Cmp(resource.MustParse("2")) != 0 {
t.Errorf("expected 2 CPU used, got %s", got.String())
}

// deleting the node should clear all of the usage of pods that were bound to the node
Expand Down
15 changes: 13 additions & 2 deletions pkg/model/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,23 @@ func (p *Pod) Phase() v1.PodPhase {
return p.pod.Status.Phase
}

// Requested returns the sum of the resources requested by the pod. This doesn't include any init containers as we
// are interested in the steady state usage of the pod
// Requested returns the sum of the resources requested by the pod.
// Also include resources for init containers that are sidecars as described in
// https://kubernetes.io/blog/2023/08/25/native-sidecar-containers .
func (p *Pod) Requested() v1.ResourceList {
p.mu.RLock()
defer p.mu.RUnlock()
requested := v1.ResourceList{}
for _, c := range p.pod.Spec.InitContainers {
if c.RestartPolicy == nil || *c.RestartPolicy != v1.ContainerRestartPolicyAlways {
continue
}
for rn, q := range c.Resources.Requests {
existing := requested[rn]
existing.Add(q)
requested[rn] = existing
}
}
for _, c := range p.pod.Spec.Containers {
for rn, q := range c.Resources.Requests {
existing := requested[rn]
Expand Down
28 changes: 26 additions & 2 deletions pkg/model/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
)

func testPod(namespace, name string) *v1.Pod {
restartAlways := v1.ContainerRestartPolicyAlways
p := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Expand All @@ -34,6 +35,29 @@ func testPod(namespace, name string) *v1.Pod {
Phase: v1.PodPending,
},
Spec: v1.PodSpec{
InitContainers: []v1.Container{
{
Image: "normalinit",
Name: "container",
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceCPU: resource.MustParse("1"),
v1.ResourceMemory: resource.MustParse("1Gi"),
},
},
},
{
Image: "sidecar",
Name: "container",
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceCPU: resource.MustParse("1"),
v1.ResourceMemory: resource.MustParse("1Gi"),
},
},
RestartPolicy: &restartAlways,
},
},
Containers: []v1.Container{
{
Image: "test-image",
Expand Down Expand Up @@ -70,10 +94,10 @@ func TestNewPod(t *testing.T) {
t.Errorf("expected Phase = %v, got %v", exp, got)
}

if exp, got := resource.MustParse("1"), p.Requested()[v1.ResourceCPU]; exp.Cmp(got) != 0 {
if exp, got := resource.MustParse("2"), p.Requested()[v1.ResourceCPU]; exp.Cmp(got) != 0 {
t.Errorf("expected CPU = %s, got %s", exp.String(), got.String())
}
if exp, got := resource.MustParse("1Gi"), p.Requested()[v1.ResourceMemory]; exp.Cmp(got) != 0 {
if exp, got := resource.MustParse("2Gi"), p.Requested()[v1.ResourceMemory]; exp.Cmp(got) != 0 {
t.Errorf("expected Memory = %s, got %s", exp.String(), got.String())
}
}
Expand Down
Loading