Skip to content

Commit a41e4db

Browse files
committed
nrt: filter: log correctly sidecar containers
add utilities to recognize sidecar containers, and log them correctly. Signed-off-by: Francesco Romani <[email protected]>
1 parent bf4d1bd commit a41e4db

File tree

4 files changed

+104
-6
lines changed

4 files changed

+104
-6
lines changed

pkg/noderesourcetopology/filter.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,16 @@ func singleNUMAContainerLevelHandler(lh logr.Logger, pod *v1.Pod, zones topology
5555
// https://kubernetes.io/docs/concepts/workloads/pods/init-containers/#understanding-init-containers
5656
// therefore, we don't need to accumulate their resources together
5757
for _, initContainer := range pod.Spec.InitContainers {
58-
// TODO: handle sidecar explicitely (new kind)
59-
clh := lh.WithValues(logging.KeyContainer, initContainer.Name, logging.KeyContainerKind, logging.KindContainerInit)
58+
cntKind := logging.GetInitContainerKind(&initContainer)
59+
clh := lh.WithValues(logging.KeyContainer, initContainer.Name, logging.KeyContainerKind, cntKind)
6060
clh.V(6).Info("desired resources", stringify.ResourceListToLoggable(initContainer.Resources.Requests)...)
6161

6262
_, match := resourcesAvailableInAnyNUMANodes(clh, nodes, initContainer.Resources.Requests, qos, nodeInfo)
6363
if !match {
64+
msg := "cannot align " + cntKind + " container"
6465
// we can't align init container, so definitely we can't align a pod
65-
clh.V(2).Info("cannot align container")
66-
return framework.NewStatus(framework.Unschedulable, "cannot align init container")
66+
clh.V(2).Info(msg)
67+
return framework.NewStatus(framework.Unschedulable, msg)
6768
}
6869
}
6970

pkg/noderesourcetopology/logging/logging.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"reflect"
2121

2222
corev1 "k8s.io/api/core/v1"
23+
"sigs.k8s.io/scheduler-plugins/pkg/util"
2324
)
2425

2526
// well-known structured log keys
@@ -44,8 +45,9 @@ const (
4445
)
4546

4647
const (
47-
KindContainerInit string = "init"
48-
KindContainerApp string = "app"
48+
KindContainerInit string = "init"
49+
KindContainerSidecar string = "sidecar"
50+
KindContainerApp string = "app"
4951
)
5052

5153
const (
@@ -62,3 +64,10 @@ func PodUID(pod *corev1.Pod) string {
6264
}
6365
return string(pod.GetUID())
6466
}
67+
68+
func GetInitContainerKind(container *corev1.Container) string {
69+
if util.IsSidecarInitContainer(container) {
70+
return KindContainerSidecar
71+
}
72+
return KindContainerInit
73+
}

pkg/util/sidecar.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
v1 "k8s.io/api/core/v1"
21+
)
22+
23+
// IsSidecarInitContainer assumes the given container is a pod init container;
24+
// returns true if that container is a sidecar, false otherwise.
25+
func IsSidecarInitContainer(container *v1.Container) bool {
26+
return container.RestartPolicy != nil && *container.RestartPolicy == v1.ContainerRestartPolicyAlways
27+
}

pkg/util/sidecar_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"testing"
21+
22+
v1 "k8s.io/api/core/v1"
23+
)
24+
25+
func TestIsSidecarInitContainer(t *testing.T) {
26+
always_ := v1.ContainerRestartPolicyAlways
27+
testCases := []struct {
28+
name string
29+
cnt *v1.Container
30+
expected bool
31+
}{
32+
{
33+
name: "zero value",
34+
cnt: &v1.Container{},
35+
expected: false,
36+
},
37+
{
38+
name: "explicit nil",
39+
cnt: &v1.Container{
40+
RestartPolicy: nil,
41+
},
42+
expected: false,
43+
},
44+
{
45+
name: "true sidecar container",
46+
cnt: &v1.Container{
47+
Name: "init-1",
48+
RestartPolicy: &always_,
49+
},
50+
expected: true,
51+
},
52+
}
53+
for _, testCase := range testCases {
54+
t.Run(string(testCase.name), func(t *testing.T) {
55+
got := IsSidecarInitContainer(testCase.cnt)
56+
if got != testCase.expected {
57+
t.Fatalf("expected %t to equal %t", got, testCase.expected)
58+
}
59+
})
60+
}
61+
}

0 commit comments

Comments
 (0)