-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A scheduler plugin imitator that tries to verify if proposed pods wil…
…l be schedulable in the cluster
- Loading branch information
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package scheduler | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"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/nodeaffinity" | ||
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/noderesources" | ||
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/nodeunschedulable" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func createNodeInfos(ctx context.Context, cli client.Client) ([]*framework.NodeInfo, error) { | ||
nodes := corev1.NodeList{} | ||
if err := cli.List(ctx, &nodes); err != nil { | ||
return nil, err | ||
} | ||
|
||
nodeInfos := make([]*framework.NodeInfo, 0, len(nodes.Items)) | ||
|
||
for _, node := range nodes.Items { | ||
nodeInfo := framework.NewNodeInfo() | ||
nodeInfo.SetNode(&node) | ||
pods := &corev1.PodList{} | ||
if err := cli.List(ctx, pods, client.MatchingFields{"spec.nodeName": nodeInfo.Node().Name}); err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, pod := range pods.Items { | ||
nodeInfo.AddPod(&pod) | ||
} | ||
|
||
nodeInfos = append(nodeInfos, nodeInfo) | ||
} | ||
|
||
return nodeInfos, nil | ||
} | ||
|
||
// WillTheyFit checks if the proposed pods will be schedulable in the cluster. The ProposedPods must have their | ||
// labels, tolerations, affinities and resource limits set | ||
func WillTheyFit(ctx context.Context, cli client.Client, proposedPods []*corev1.Pod) error { | ||
usableNodes, err := createNodeInfos(ctx, cli) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
state := framework.NewCycleState() | ||
|
||
schedulablePlugin, err := nodeunschedulable.New(ctx, nil, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
noderesourcesPlugin, err := noderesources.NewFit(ctx, nil, nil, plfeature.Features{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
nodeaffinityPlugin, err := nodeaffinity.New(ctx, nil, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
interpodaffinityPlugin, err := interpodaffinity.New(ctx, nil, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
plugins := []framework.FilterPlugin{ | ||
schedulablePlugin.(framework.FilterPlugin), | ||
noderesourcesPlugin.(framework.FilterPlugin), | ||
nodeaffinityPlugin.(framework.FilterPlugin), | ||
interpodaffinityPlugin.(framework.FilterPlugin), | ||
} | ||
|
||
NextPod: | ||
for _, pod := range proposedPods { | ||
for _, node := range usableNodes { | ||
podInfo, err := framework.NewPodInfo(pod) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, plugin := range plugins { | ||
status := plugin.Filter(ctx, state, podInfo.Pod, node) | ||
if status.Code() == framework.Unschedulable { | ||
continue | ||
} | ||
} | ||
|
||
node.AddPod(pod) | ||
continue NextPod | ||
// Now we need a way to check if the pod was never added to any node | ||
// TODO Need more checks like affinities, inter-pod affinities, taints, tolerations | ||
} | ||
// Pod was never added to any node | ||
return errors.New(framework.Unschedulable.String()) | ||
} | ||
|
||
// Check node condition, will it accept something | ||
// Need tolerations, taints, affinities.. | ||
return nil | ||
} |