Skip to content

Commit 0374223

Browse files
nelchaeltzneal
authored andcommitted
Support pod-level resources.
1 parent 9edea05 commit 0374223

4 files changed

Lines changed: 57 additions & 20 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
k8s.io/api v0.36.0
1717
k8s.io/apimachinery v0.36.0
1818
k8s.io/client-go v0.36.0
19+
k8s.io/component-helpers v0.36.0
1920
sigs.k8s.io/karpenter v1.12.0
2021
)
2122

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ k8s.io/client-go v0.36.0 h1:pOYi7C4RHChYjMiHpZSpSbIM6ZxVbRXBy7CuiIwqA3c=
270270
k8s.io/client-go v0.36.0/go.mod h1:ZKKcpwF0aLYfkHFCjillCKaTK/yBkEDHTDXCFY6AS9Y=
271271
k8s.io/component-base v0.36.0 h1:hFjEktssxiJhrK1zfybkH4kJOi8iZuF+mIDCqS5+jRo=
272272
k8s.io/component-base v0.36.0/go.mod h1:JZvIfcNHk+uck+8LhJzhSBtydWXaZNQwX2OdL+Mnwsk=
273-
k8s.io/component-helpers v0.35.0 h1:wcXv7HJRksgVjM4VlXJ1CNFBpyDHruRI99RrBtrJceA=
274-
k8s.io/component-helpers v0.35.0/go.mod h1:ahX0m/LTYmu7fL3W8zYiIwnQ/5gT28Ex4o2pymF63Co=
273+
k8s.io/component-helpers v0.36.0 h1:KznLAOD7oPxjaeheW4SOQijz9UtMO8Nvp89+lR8FYks=
274+
k8s.io/component-helpers v0.36.0/go.mod h1:BqZG+01Z97KR8GN9Stb8SiRmtn/EpZogriuQtpMCsLg=
275275
k8s.io/klog/v2 v2.140.0 h1:Tf+J3AH7xnUzZyVVXhTgGhEKnFqye14aadWv7bzXdzc=
276276
k8s.io/klog/v2 v2.140.0/go.mod h1:o+/RWfJ6PwpnFn7OyAG3QnO47BFsymfEfrz6XyYSSp0=
277277
k8s.io/kube-openapi v0.0.0-20260317180543-43fb72c5454a h1:xCeOEAOoGYl2jnJoHkC3hkbPJgdATINPMAxaynU2Ovg=

pkg/model/pod.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
v1 "k8s.io/api/core/v1"
2424
"k8s.io/apimachinery/pkg/api/resource"
25+
helperResource "k8s.io/component-helpers/resource"
2526
)
2627

2728
// Pod is our pod model used for internal storage and display
@@ -85,24 +86,10 @@ func (p *Pod) Phase() v1.PodPhase {
8586
func (p *Pod) Requested() v1.ResourceList {
8687
p.mu.RLock()
8788
defer p.mu.RUnlock()
88-
requested := v1.ResourceList{}
89-
for _, c := range p.pod.Spec.InitContainers {
90-
if c.RestartPolicy == nil || *c.RestartPolicy != v1.ContainerRestartPolicyAlways {
91-
continue
92-
}
93-
for rn, q := range c.Resources.Requests {
94-
existing := requested[rn]
95-
existing.Add(q)
96-
requested[rn] = existing
97-
}
98-
}
99-
for _, c := range p.pod.Spec.Containers {
100-
for rn, q := range c.Resources.Requests {
101-
existing := requested[rn]
102-
existing.Add(q)
103-
requested[rn] = existing
104-
}
105-
}
89+
requested := helperResource.PodRequests(&p.pod, helperResource.PodResourcesOptions{
90+
SkipPodLevelResources: false,
91+
SkipContainerLevelResources: false,
92+
})
10693
requested[v1.ResourcePods] = resource.MustParse("1")
10794
return requested
10895
}

pkg/model/pod_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,46 @@ func testPod(namespace, name string) *v1.Pod {
7474
}
7575
return p
7676
}
77+
78+
func testPodWithPodLevelResources(namespace, name string) *v1.Pod {
79+
restartAlways := v1.ContainerRestartPolicyAlways
80+
p := &v1.Pod{
81+
ObjectMeta: metav1.ObjectMeta{
82+
Namespace: namespace,
83+
Name: name,
84+
},
85+
Status: v1.PodStatus{
86+
Phase: v1.PodPending,
87+
},
88+
Spec: v1.PodSpec{
89+
Resources: &v1.ResourceRequirements{
90+
Requests: v1.ResourceList{
91+
v1.ResourceCPU: resource.MustParse("10"),
92+
v1.ResourceMemory: resource.MustParse("12Gi"),
93+
},
94+
},
95+
InitContainers: []v1.Container{
96+
{
97+
Image: "normalinit",
98+
Name: "container",
99+
},
100+
{
101+
Image: "sidecar",
102+
Name: "container",
103+
RestartPolicy: &restartAlways,
104+
},
105+
},
106+
Containers: []v1.Container{
107+
{
108+
Image: "test-image",
109+
Name: "container",
110+
},
111+
},
112+
},
113+
}
114+
return p
115+
}
116+
77117
func TestNewPod(t *testing.T) {
78118
pod := testPod("default", "mypod")
79119
pod.Spec.NodeName = "mynode"
@@ -100,6 +140,15 @@ func TestNewPod(t *testing.T) {
100140
if exp, got := resource.MustParse("2Gi"), p.Requested()[v1.ResourceMemory]; exp.Cmp(got) != 0 {
101141
t.Errorf("expected Memory = %s, got %s", exp.String(), got.String())
102142
}
143+
144+
podWithPodLevelResources := testPodWithPodLevelResources("default", "mypod")
145+
pWithPodLevelResources := model.NewPod(podWithPodLevelResources)
146+
if exp, got := resource.MustParse("10"), pWithPodLevelResources.Requested()[v1.ResourceCPU]; exp.Cmp(got) != 0 {
147+
t.Errorf("expected CPU = %s, got %s", exp.String(), got.String())
148+
}
149+
if exp, got := resource.MustParse("12Gi"), pWithPodLevelResources.Requested()[v1.ResourceMemory]; exp.Cmp(got) != 0 {
150+
t.Errorf("expected Memory = %s, got %s", exp.String(), got.String())
151+
}
103152
}
104153

105154
func TestPodUpdate(t *testing.T) {

0 commit comments

Comments
 (0)