Skip to content

Commit

Permalink
Remove podinternodeaffinity for now due to snapshot cache issue, add …
Browse files Browse the repository at this point in the history
…a small smoke test for the resources requirements check
  • Loading branch information
burmanm committed Nov 8, 2024
1 parent df740b6 commit 4912ba3
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 11 deletions.
36 changes: 25 additions & 11 deletions pkg/scheduler/fit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"errors"

"k8s.io/kubernetes/pkg/scheduler/apis/config"
"k8s.io/kubernetes/pkg/scheduler/framework"
plfeature "k8s.io/kubernetes/pkg/scheduler/framework/plugins/feature"
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/interpodaffinity"

// "k8s.io/kubernetes/pkg/scheduler/framework/plugins/interpodaffinity"
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/nodeaffinity"
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/noderesources"
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/nodeunschedulable"
Expand Down Expand Up @@ -57,46 +59,50 @@ func WillTheyFit(ctx context.Context, cli client.Client, proposedPods []*corev1.
return err
}

noderesourcesPlugin, err := noderesources.NewFit(ctx, nil, nil, plfeature.Features{})
noderesourcesPlugin, err := noderesources.NewFit(ctx, &config.NodeResourcesFitArgs{ScoringStrategy: defaultScoringStrategy}, nil, plfeature.Features{})
if err != nil {
return err
}

nodeaffinityPlugin, err := nodeaffinity.New(ctx, nil, nil)
nodeaffinityPlugin, err := nodeaffinity.New(ctx, &config.NodeAffinityArgs{}, nil)
if err != nil {
return err
}

interpodaffinityPlugin, err := interpodaffinity.New(ctx, nil, nil)
if err != nil {
return err
}
// interpodaffinityPlugin, err := interpodaffinity.New(ctx, &config.InterPodAffinityArgs{}, snapshot)
// if err != nil {
// return err
// }

tainttolerationPlugin, err := tainttoleration.New(ctx, nil, nil)
if err != nil {
return err
}

plugins := []framework.FilterPlugin{
schedulablePlugin.(framework.FilterPlugin),
noderesourcesPlugin.(framework.FilterPlugin),
schedulablePlugin.(framework.FilterPlugin),
nodeaffinityPlugin.(framework.FilterPlugin),
interpodaffinityPlugin.(framework.FilterPlugin),
// interpodaffinityPlugin.(framework.FilterPlugin),
tainttolerationPlugin.(framework.FilterPlugin),
}

NextPod:
for _, pod := range proposedPods {
NextNode:
for _, node := range usableNodes {
podInfo, err := framework.NewPodInfo(pod)
if err != nil {
return err
}

for _, plugin := range plugins {
if prefilterPlugin, ok := plugin.(framework.PreFilterPlugin); ok {
prefilterPlugin.PreFilter(ctx, state, podInfo.Pod)
}
status := plugin.Filter(ctx, state, podInfo.Pod, node)
if status.Code() == framework.Unschedulable {
continue
if status.Code() != framework.Success {
continue NextNode
}
}

Expand All @@ -108,3 +114,11 @@ NextPod:
}
return nil
}

var defaultScoringStrategy = &config.ScoringStrategy{
Type: config.LeastAllocated,
Resources: []config.ResourceSpec{
{Name: "cpu", Weight: 1},
{Name: "memory", Weight: 1},
},
}
88 changes: 88 additions & 0 deletions pkg/scheduler/fit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package scheduler

import (
"context"
"strconv"
"testing"

"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestResources(t *testing.T) {
require := require.New(t)
ctx := context.TODO()
cli := createClient()
require.NoError(cli.Create(ctx, makeNode("node1")))

pods := []*corev1.Pod{
makePod("pod1"),
}
require.NoError(WillTheyFit(ctx, cli, pods))

// Lets add more pods than the node can handle
for i := 2; i < 20; i++ {
pods = append(pods, makePod("pod"+strconv.Itoa(i)))
}
require.Error(WillTheyFit(ctx, cli, pods))
}

func createClient() client.Client {
s := runtime.NewScheme()
s.AddKnownTypes(corev1.SchemeGroupVersion, &corev1.Node{})

nodeNameIndexer := client.IndexerFunc(func(obj client.Object) []string {
pod, ok := obj.(*corev1.Pod)
if !ok {
return nil
}
return []string{pod.Spec.NodeName}
})

fakeClient := fake.NewClientBuilder().WithIndex(&corev1.Pod{}, "spec.nodeName", nodeNameIndexer).Build()
return fakeClient
}

func makeResources(milliCPU, memory, pods int64) corev1.ResourceList {
return corev1.ResourceList{
corev1.ResourceCPU: *resource.NewMilliQuantity(milliCPU, resource.DecimalSI),
corev1.ResourceMemory: *resource.NewQuantity(memory, resource.BinarySI),
corev1.ResourcePods: *resource.NewQuantity(pods, resource.DecimalSI),
}
}

func makeNode(name string) *corev1.Node {
n := &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: corev1.NodeSpec{
Unschedulable: false,
},
}
n.Status.Capacity, n.Status.Allocatable = makeResources(1000, 1000, 100), makeResources(1000, 1000, 100)
return n
}

func makePod(name string) *corev1.Pod {
return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Resources: corev1.ResourceRequirements{
Requests: makeResources(100, 100, 1),
Limits: makeResources(200, 200, 1),
},
},
},
},
}
}

0 comments on commit 4912ba3

Please sign in to comment.