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 sidecar containers requests and limits #2848

Merged
merged 2 commits into from
Oct 13, 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
18 changes: 16 additions & 2 deletions internal/render/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (p Pod) Render(o interface{}, ns string, row *model1.Row) error {
if pwm.MX != nil {
ccmx = pwm.MX.Containers
}
c, r := gatherCoMX(po.Spec.Containers, ccmx)
derailed marked this conversation as resolved.
Show resolved Hide resolved
c, r := gatherCoMX(&po.Spec, ccmx)
phase := p.Phase(&po)
row.ID = client.MetaFQN(po.ObjectMeta)

Expand Down Expand Up @@ -223,7 +223,11 @@ func (p *PodWithMetrics) DeepCopyObject() runtime.Object {
return p
}

func gatherCoMX(cc []v1.Container, ccmx []mv1beta1.ContainerMetrics) (c, r metric) {
func gatherCoMX(spec *v1.PodSpec, ccmx []mv1beta1.ContainerMetrics) (c, r metric) {
cc := make([]v1.Container, 0, len(spec.InitContainers)+len(spec.Containers))
cc = append(cc, filterRestartableInitCO(spec.InitContainers)...)
cc = append(cc, spec.Containers...)

rcpu, rmem := cosRequests(cc)
r.cpu, r.mem = rcpu.MilliValue(), rmem.Value()

Expand Down Expand Up @@ -490,3 +494,13 @@ func hasPodReadyCondition(conditions []v1.PodCondition) bool {
func restartableInitCO(p *v1.ContainerRestartPolicy) bool {
derailed marked this conversation as resolved.
Show resolved Hide resolved
return p != nil && *p == v1.ContainerRestartPolicyAlways
}

func filterRestartableInitCO(cc []v1.Container) []v1.Container {
rcc := make([]v1.Container, 0, len(cc))
for _, c := range cc {
if c.RestartPolicy != nil && *c.RestartPolicy == v1.ContainerRestartPolicyAlways {
rcc = append(rcc, c)
}
}
return rcc
}
124 changes: 107 additions & 17 deletions internal/render/pod_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,78 @@ func Test_restartableInitCO(t *testing.T) {
}
}

func Test_filterRestartableInitCO(t *testing.T) {
always := v1.ContainerRestartPolicyAlways

uu := map[string]struct {
cc []v1.Container
ecc []v1.Container
}{
"empty": {
cc: []v1.Container{},
ecc: []v1.Container{},
},
"restartable": {
cc: []v1.Container{
{
Name: "c1",
RestartPolicy: &always,
},
},
ecc: []v1.Container{
{
Name: "c1",
RestartPolicy: &always,
},
},
},
"not-restartable": {
cc: []v1.Container{
{
Name: "c1",
},
},
ecc: []v1.Container{},
},
"mixed": {
cc: []v1.Container{
{
Name: "c1",
},
{
Name: "c2",
RestartPolicy: &always,
},
},
ecc: []v1.Container{
{
Name: "c2",
RestartPolicy: &always,
},
},
},
}

for k := range uu {
u := uu[k]
t.Run(k, func(t *testing.T) {
assert.Equal(t, u.ecc, filterRestartableInitCO(u.cc))
})
}
}

func Test_gatherPodMx(t *testing.T) {
uu := map[string]struct {
cc []v1.Container
spec *v1.PodSpec
mx []mv1beta1.ContainerMetrics
c, r metric
perc string
}{
"single": {
cc: []v1.Container{
makeContainer("c1", false, "10m", "1Mi", "20m", "2Mi"),
spec: &v1.PodSpec{
Containers: []v1.Container{
makeContainer("c1", false, "10m", "1Mi", "20m", "2Mi"),
},
},
mx: []mv1beta1.ContainerMetrics{
makeCoMX("c1", "1m", "22Mi"),
Expand All @@ -322,10 +384,12 @@ func Test_gatherPodMx(t *testing.T) {
perc: "10",
},
"multi": {
cc: []v1.Container{
makeContainer("c1", false, "11m", "22Mi", "111m", "44Mi"),
makeContainer("c2", false, "93m", "1402Mi", "0m", "2804Mi"),
makeContainer("c3", false, "11m", "34Mi", "0m", "69Mi"),
spec: &v1.PodSpec{
Containers: []v1.Container{
makeContainer("c1", false, "11m", "22Mi", "111m", "44Mi"),
makeContainer("c2", false, "93m", "1402Mi", "0m", "2804Mi"),
makeContainer("c3", false, "11m", "34Mi", "0m", "69Mi"),
},
},
r: metric{
cpu: 11 + 93 + 11,
Expand All @@ -344,12 +408,37 @@ func Test_gatherPodMx(t *testing.T) {
},
perc: "46",
},
"sidecar": {
spec: &v1.PodSpec{
Containers: []v1.Container{
makeContainer("c1", false, "11m", "22Mi", "111m", "44Mi"),
},
InitContainers: []v1.Container{
makeContainer("c2", true, "93m", "1402Mi", "0m", "2804Mi"),
},
},
r: metric{
cpu: 11 + 93,
mem: (22 + 1402) * client.MegaByte,
lcpu: 111 + 0,
lmem: (44 + 2804) * client.MegaByte,
},
mx: []mv1beta1.ContainerMetrics{
makeCoMX("c1", "1m", "22Mi"),
makeCoMX("c2", "51m", "1275Mi"),
},
c: metric{
cpu: 1 + 51,
mem: (22 + 1275) * client.MegaByte,
},
perc: "50",
},
}

for k := range uu {
u := uu[k]
t.Run(k, func(t *testing.T) {
c, r := gatherCoMX(u.cc, u.mx)
c, r := gatherCoMX(u.spec, u.mx)
assert.Equal(t, u.c.cpu, c.cpu)
assert.Equal(t, u.c.mem, c.mem)
assert.Equal(t, u.c.lcpu, c.lcpu)
Expand Down Expand Up @@ -427,18 +516,19 @@ func Test_podRequests(t *testing.T) {

// Helpers...

func makeContainer(n string, init bool, rc, rm, lc, lm string) v1.Container {
func makeContainer(n string, restartable bool, rc, rm, lc, lm string) v1.Container {
always := v1.ContainerRestartPolicyAlways
var res v1.ResourceRequirements
if init {
res = v1.ResourceRequirements{}
} else {
res = v1.ResourceRequirements{
Requests: makeRes(rc, rm),
Limits: makeRes(lc, lm),
}
var rp *v1.ContainerRestartPolicy
res = v1.ResourceRequirements{
Requests: makeRes(rc, rm),
Limits: makeRes(lc, lm),
}
if restartable {
rp = &always
}

return v1.Container{Name: n, Resources: res}
return v1.Container{Name: n, Resources: res, RestartPolicy: rp}
}

func makeRes(c, m string) v1.ResourceList {
Expand Down
16 changes: 16 additions & 0 deletions internal/render/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,22 @@ func TestPodInitRender(t *testing.T) {
assert.Equal(t, e, r.Fields[:19])
}

func TestPodSidecarRender(t *testing.T) {
pom := render.PodWithMetrics{
Raw: load(t, "po_sidecar"),
MX: makePodMX("sleep", "100m", "40Mi"),
}

var po render.Pod
r := model1.NewRow(14)
err := po.Render(&pom, "", &r)
assert.Nil(t, err)

assert.Equal(t, "default/sleep", r.ID)
e := model1.Fields{"default", "sleep", "0", "●", "1/1", "Running", "0", "100", "40", "50:250", "50:80", "200", "40", "80", "50", "10.244.0.8", "kind-control-plane", "<none>", "<none>"}
assert.Equal(t, e, r.Fields[:19])
}

func TestCheckPodStatus(t *testing.T) {
uu := map[string]struct {
pod v1.Pod
Expand Down
Loading