/
scheduler.go
158 lines (138 loc) · 5.21 KB
/
scheduler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright ©2020. The virtual-kubelet authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package scheduler
import (
"context"
"fmt"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/clientcmd"
componentbaseconfig "k8s.io/component-base/config"
"k8s.io/klog/v2"
schedulerappconfig "k8s.io/kubernetes/cmd/kube-scheduler/app/config"
"k8s.io/kubernetes/pkg/scheduler/framework"
frameworkruntime "k8s.io/kubernetes/pkg/scheduler/framework/runtime"
"github.com/cwdsuzhou/super-scheduling/pkg/apis/config"
clusterconfig "github.com/cwdsuzhou/super-scheduling/pkg/multicluster/config"
"github.com/cwdsuzhou/super-scheduling/pkg/util"
)
// Name is MultiClusterScheduling plugin name, will use in configuration file
const Name = "MultiClusterScheduling"
// MultiSchedulingPlugin is plugin implemented scheduling framework
type MultiSchedulingPlugin struct {
frameworkHandler framework.Handle
schedulers map[string]*Scheduler
}
// Name returns the plugin name
func (m MultiSchedulingPlugin) Name() string {
return Name
}
var _ framework.FilterPlugin = &MultiSchedulingPlugin{}
// Filter check if a pod can run on node
func (m MultiSchedulingPlugin) Filter(pc context.Context, state *framework.CycleState,
pod *v1.Pod, nodeInfo *framework.NodeInfo) *framework.Status {
snapshot := m.frameworkHandler.SnapshotSharedLister().NodeInfos()
if snapshot == nil {
return framework.NewStatus(framework.Success, "")
}
if nodeInfo == nil || nodeInfo.Node() == nil {
return framework.NewStatus(framework.Unschedulable, "node not exit")
}
if !util.IsVirtualNode(nodeInfo.Node()) {
klog.V(5).Infof("node %v is not virtual node", nodeInfo.Node().Name)
return framework.NewStatus(framework.Success, "")
}
schedulerName := util.GetClusterID(nodeInfo.Node())
if len(schedulerName) == 0 {
klog.V(5).Infof("Can not found multicluster %v", schedulerName)
return framework.NewStatus(framework.Success, "")
}
scheduler := m.schedulers[schedulerName]
if scheduler == nil {
klog.V(5).Infof("Can not found scheduling %v", schedulerName)
return framework.NewStatus(framework.Success, "")
}
podCopy := pod.DeepCopy()
klog.V(5).Infof("Matching node for pod: %v", pod.Name)
cns := util.ConvertAnnotations(podCopy.Annotations)
// remove selector
if cns != nil {
podCopy.Spec.NodeSelector = cns.NodeSelector
podCopy.Spec.Affinity = cns.Affinity
podCopy.Spec.Tolerations = cns.Tolerations
} else {
podCopy.Spec.NodeSelector = nil
podCopy.Spec.Affinity = nil
podCopy.Spec.Tolerations = nil
}
result, err := scheduler.Algorithm.Filter(pc, scheduler.Profiles["default-scheduler"], state, podCopy)
klog.V(5).Infof("%v Nodes, Node %s can be scheduled to run pod", result.FeasibleNodes, result.SuggestedHost)
if err != nil {
klog.Infof("Pod selector: %+v, affinity: %+v", pod.Spec.NodeSelector, pod.Spec.Affinity)
return framework.NewStatus(framework.Unschedulable, fmt.Sprintf("Can not found nodes: %s", err))
}
if result.FeasibleNodes == 0 || result.SuggestedHost == "" {
return framework.NewStatus(framework.Unschedulable, fmt.Sprintf("Can not found feasible nodes from cluster: %v/%v",
result.FeasibleNodes, result.EvaluatedNodes))
}
return framework.NewStatus(framework.Success, "")
}
// New initializes a new plugin and returns it.
func New(obj runtime.Object, f framework.Handle) (framework.Plugin, error) {
var configs config.SchedulingArgs
err := frameworkruntime.DecodeInto(obj, &configs)
if err != nil {
return nil, fmt.Errorf("want args to be of type SchedulingArgs, got %T\n", obj)
}
ctx := context.TODO()
schedulers := make(map[string]*Scheduler)
for name, config := range configs.ClusterConfiguration {
klog.V(4).Infof("cluster %s's config: master(%s), kube-config(%s)", name, config.KubeMaster, config.KubeConfig)
// Init client and Informer
c, err := clientcmd.BuildConfigFromFlags(config.KubeMaster, config.KubeConfig)
if err != nil {
return nil, fmt.Errorf("failed to init rest.Config: %v", err)
}
c.QPS = 10
c.Burst = 20
componentConfig, err := clusterconfig.NewDefaultComponentConfig()
if err != nil {
klog.Fatal(err)
}
componentConfig.ClientConnection = componentbaseconfig.ClientConnectionConfiguration{
Kubeconfig: config.KubeConfig,
}
// componentConfig.DisablePreemption = true
cfg := schedulerappconfig.Config{
ComponentConfig: *componentConfig,
}
newConfig, err := clusterconfig.Config(cfg, config.KubeMaster)
if err != nil {
klog.Fatal(err)
}
scheduler, err := NewScheduler(ctx, *newConfig, ctx.Done())
if err != nil {
klog.Fatal(err)
}
go scheduler.Run(ctx)
schedulers[name] = scheduler
}
klog.Infof("MultiScheduling start")
return MultiSchedulingPlugin{
frameworkHandler: f,
schedulers: schedulers,
}, nil
}