move SidecarContainers featureGate checking

to PreFilter

Signed-off-by: KunWuLuan <kunwuluan@gmail.com>
This commit is contained in:
kunwuluan 2023-10-20 17:54:16 +08:00 committed by KunWuLuan
parent 04e38b2de8
commit a00a610d15
No known key found for this signature in database
GPG Key ID: 2907D905509505F8
2 changed files with 19 additions and 17 deletions

View File

@ -211,6 +211,14 @@ func computePodResourceRequest(pod *v1.Pod) *preFilterState {
// PreFilter invoked at the prefilter extension point.
func (f *Fit) PreFilter(ctx context.Context, cycleState *framework.CycleState, pod *v1.Pod) (*framework.PreFilterResult, *framework.Status) {
if !f.enableSidecarContainers && hasRestartableInitContainer(pod) {
// Scheduler will calculate resources usage for a Pod containing
// restartable init containers that will be equal or more than kubelet will
// require to run the Pod. So there will be no overbooking. However, to
// avoid the inconsistency in resource calculation between the scheduler
// and the older (before v1.28) kubelet, make the Pod unschedulable.
return nil, framework.NewStatus(framework.UnschedulableAndUnresolvable, "Pod has a restartable init container and the SidecarContainers feature is disabled")
}
cycleState.Write(preFilterStateKey, computePodResourceRequest(pod))
return nil, nil
}
@ -253,15 +261,6 @@ func (f *Fit) EventsToRegister() []framework.ClusterEventWithHint {
// Checks if a node has sufficient resources, such as cpu, memory, gpu, opaque int resources etc to run a pod.
// It returns a list of insufficient resources, if empty, then the node has all the resources requested by the pod.
func (f *Fit) Filter(ctx context.Context, cycleState *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) *framework.Status {
if !f.enableSidecarContainers && hasRestartableInitContainer(pod) {
// Scheduler will calculate resources usage for a Pod containing
// restartable init containers that will be equal or more than kubelet will
// require to run the Pod. So there will be no overbooking. However, to
// avoid the inconsistency in resource calculation between the scheduler
// and the older (before v1.28) kubelet, make the Pod unschedulable.
return framework.NewStatus(framework.UnschedulableAndUnresolvable, "Pod has a restartable init container and the SidecarContainers feature is disabled")
}
s, err := getPreFilterState(cycleState)
if err != nil {
return framework.AsStatus(err)

View File

@ -694,16 +694,16 @@ func TestRestartableInitContainers(t *testing.T) {
name string
pod *v1.Pod
enableSidecarContainers bool
wantStatus *framework.Status
wantPreFilterStatus *framework.Status
}{
{
name: "allow pod without restartable init containers if sidecar containers is disabled",
pod: newPod(),
},
{
name: "not allow pod with restartable init containers if sidecar containers is disabled",
pod: newPodWithRestartableInitContainers(),
wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, "Pod has a restartable init container and the SidecarContainers feature is disabled"),
name: "not allow pod with restartable init containers if sidecar containers is disabled",
pod: newPodWithRestartableInitContainers(),
wantPreFilterStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, "Pod has a restartable init container and the SidecarContainers feature is disabled"),
},
{
name: "allow pod without restartable init containers if sidecar containers is enabled",
@ -732,13 +732,16 @@ func TestRestartableInitContainers(t *testing.T) {
}
cycleState := framework.NewCycleState()
_, preFilterStatus := p.(framework.PreFilterPlugin).PreFilter(context.Background(), cycleState, test.pod)
if diff := cmp.Diff(test.wantPreFilterStatus, preFilterStatus); diff != "" {
t.Error("status does not match (-expected +actual):\n", diff)
}
if !preFilterStatus.IsSuccess() {
t.Errorf("prefilter failed with status: %v", preFilterStatus)
return
}
gotStatus := p.(framework.FilterPlugin).Filter(context.Background(), cycleState, test.pod, nodeInfo)
if diff := cmp.Diff(gotStatus, test.wantStatus); diff != "" {
t.Errorf("status does not match: %v, want: %v", gotStatus, test.wantStatus)
filterStatus := p.(framework.FilterPlugin).Filter(ctx, cycleState, test.pod, nodeInfo)
if !filterStatus.IsSuccess() {
t.Error("status does not match (-expected +actual):\n- Success\n +\n", filterStatus.Code())
}
})
}