Skip to content
Permalink
initial-random…
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
package minisched
import (
"context"
"math/rand"
"time"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"k8s.io/apimachinery/pkg/util/wait"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func (sched *Scheduler) Run(ctx context.Context) {
wait.UntilWithContext(ctx, sched.scheduleOne, 0)
}
func (sched *Scheduler) scheduleOne(ctx context.Context) {
klog.Info("minischeduler: Try to get pod from queue....")
pod := sched.SchedulingQueue.NextPod()
klog.Info("minischeduler: Start schedule(" + pod.Name + ")")
// get nodes
nodes, err := sched.client.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
klog.Error(err)
return
}
klog.Info("minischeduler: Got Nodes successfully")
// select node randomly
selectedNode := nodes.Items[rand.Intn(len(nodes.Items))]
if err := sched.Bind(ctx, pod, selectedNode.Name); err != nil {
klog.Error(err)
return
}
klog.Info("minischeduler: Bind Pod successfully")
}
func (sched *Scheduler) Bind(ctx context.Context, p *v1.Pod, nodeName string) error {
binding := &v1.Binding{
ObjectMeta: metav1.ObjectMeta{Namespace: p.Namespace, Name: p.Name, UID: p.UID},
Target: v1.ObjectReference{Kind: "Node", Name: nodeName},
}
err := sched.client.CoreV1().Pods(binding.Namespace).Bind(ctx, binding, metav1.CreateOptions{})
if err != nil {
return err
}
return nil
}