2019-11-06 21:20:49 +05:30
|
|
|
/*
|
|
|
|
|
Copyright © 2019 The OpenEBS 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-12-04 18:00:08 +05:30
|
|
|
package scheduler
|
2019-11-06 21:20:49 +05:30
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"math"
|
|
|
|
|
|
|
|
|
|
"github.com/container-storage-interface/spec/lib/go/csi"
|
2020-04-30 14:13:29 +05:30
|
|
|
k8sapi "github.com/openebs/zfs-localpv/pkg/client/k8s/v1alpha1"
|
2019-11-06 21:20:49 +05:30
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2020-06-29 12:18:33 +05:30
|
|
|
"k8s.io/klog"
|
2019-11-06 21:20:49 +05:30
|
|
|
)
|
|
|
|
|
|
2020-12-04 18:00:08 +05:30
|
|
|
// getNodeList gets the nodelist which satisfies the topology info
|
|
|
|
|
func getNodeList(topo *csi.TopologyRequirement) ([]string, error) {
|
2020-04-30 14:13:29 +05:30
|
|
|
|
|
|
|
|
var nodelist []string
|
|
|
|
|
|
|
|
|
|
list, err := k8sapi.ListNodes(metav1.ListOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, node := range list.Items {
|
|
|
|
|
for _, prf := range topo.Preferred {
|
|
|
|
|
nodeFiltered := false
|
|
|
|
|
for key, value := range prf.Segments {
|
|
|
|
|
if node.Labels[key] != value {
|
|
|
|
|
nodeFiltered = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if nodeFiltered == false {
|
|
|
|
|
nodelist = append(nodelist, node.Name)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nodelist, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-04 18:00:08 +05:30
|
|
|
// runScheduler goes through the node mapping
|
|
|
|
|
// in the topology and picks the node which is less weighted
|
|
|
|
|
func runScheduler(nodelist []string, nmap map[string]int64) string {
|
2019-11-06 21:20:49 +05:30
|
|
|
var selected string
|
|
|
|
|
|
2020-12-04 18:00:08 +05:30
|
|
|
var weight int64 = math.MaxInt64
|
2019-11-06 21:20:49 +05:30
|
|
|
|
2020-12-04 18:00:08 +05:30
|
|
|
// schedule it on the node which has less weight
|
2020-04-30 14:13:29 +05:30
|
|
|
for _, node := range nodelist {
|
2020-12-04 18:00:08 +05:30
|
|
|
if nmap[node] < weight {
|
2019-11-06 21:20:49 +05:30
|
|
|
selected = node
|
2020-12-04 18:00:08 +05:30
|
|
|
weight = nmap[node]
|
2019-11-06 21:20:49 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return selected
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-04 18:00:08 +05:30
|
|
|
// Scheduler schedules the PV as per topology constraints for
|
|
|
|
|
// the given node weight.
|
|
|
|
|
func Scheduler(req *csi.CreateVolumeRequest, nmap map[string]int64) string {
|
|
|
|
|
topo := req.AccessibilityRequirements
|
2019-11-21 19:00:15 +05:30
|
|
|
if topo == nil ||
|
|
|
|
|
len(topo.Preferred) == 0 {
|
2020-06-29 12:18:33 +05:30
|
|
|
klog.Errorf("scheduler: topology information not provided")
|
2020-04-30 14:13:29 +05:30
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-04 18:00:08 +05:30
|
|
|
nodelist, err := getNodeList(topo)
|
2020-04-30 14:13:29 +05:30
|
|
|
if err != nil {
|
2020-06-29 12:18:33 +05:30
|
|
|
klog.Errorf("scheduler: can not get the nodelist err : %v", err.Error())
|
2020-04-30 14:13:29 +05:30
|
|
|
return ""
|
|
|
|
|
} else if len(nodelist) == 0 {
|
2020-06-29 12:18:33 +05:30
|
|
|
klog.Errorf("scheduler: nodelist is empty")
|
2019-11-06 21:20:49 +05:30
|
|
|
return ""
|
|
|
|
|
}
|
2020-04-30 14:13:29 +05:30
|
|
|
|
2019-11-06 21:20:49 +05:30
|
|
|
// if there is a single node, schedule it on that
|
2020-04-30 14:13:29 +05:30
|
|
|
if len(nodelist) == 1 {
|
|
|
|
|
return nodelist[0]
|
2019-11-06 21:20:49 +05:30
|
|
|
}
|
|
|
|
|
|
2020-12-04 18:00:08 +05:30
|
|
|
return runScheduler(nodelist, nmap)
|
2019-11-06 21:20:49 +05:30
|
|
|
}
|