@@ -17,11 +17,13 @@ limitations under the License.
1717package driver
1818
1919import (
20+ "math"
2021 "regexp"
2122 "strconv"
2223
2324 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2425
26+ "github.com/openebs/lvm-localpv/pkg/builder/nodebuilder"
2527 "github.com/openebs/lvm-localpv/pkg/builder/volbuilder"
2628 "github.com/openebs/lvm-localpv/pkg/lvm"
2729)
@@ -32,8 +34,11 @@ const (
3234 VolumeWeighted = "VolumeWeighted"
3335
3436 // pick the node where total provisioned volumes have occupied less capacity from the given volume group
35- // this will be the default scheduler when none provided
3637 CapacityWeighted = "CapacityWeighted"
38+
39+ // pick the node which is less loaded space wise
40+ // this will be the default scheduler when none provided
41+ SpaceWeightedMap = "SpaceWeighted"
3742)
3843
3944// getVolumeWeightedMap goes through all the volumegroup on all the nodes
@@ -92,14 +97,50 @@ func getCapacityWeightedMap(re *regexp.Regexp) (map[string]int64, error) {
9297 return nmap , nil
9398}
9499
100+ // getSpaceWeightedMap returns how weighted a node is space wise.
101+ // The node which has max free space available is less loaded and
102+ // can accumulate more volumes.
103+ func getSpaceWeightedMap (re * regexp.Regexp ) (map [string ]int64 , error ) {
104+ nmap := map [string ]int64 {}
105+
106+ nodeList , err := nodebuilder .NewKubeclient ().
107+ WithNamespace (lvm .LvmNamespace ).
108+ List (metav1.ListOptions {})
109+
110+ if err != nil {
111+ return nmap , err
112+ }
113+
114+ for _ , node := range nodeList .Items {
115+ var maxFree int64 = 0
116+ for _ , vg := range node .VolumeGroups {
117+ if re .MatchString (vg .Name ) {
118+ freeCapacity := vg .Free .Value ()
119+ if maxFree < freeCapacity {
120+ maxFree = freeCapacity
121+ }
122+ }
123+ }
124+ if maxFree > 0 {
125+ // converting to SpaceWeighted by subtracting it with MaxInt64
126+ // as the node which has max free space available is less loaded.
127+ nmap [node .Name ] = math .MaxInt64 - maxFree
128+ }
129+ }
130+
131+ return nmap , nil
132+ }
133+
95134// getNodeMap returns the node mapping for the given scheduling algorithm
96135func getNodeMap (schd string , vgPattern * regexp.Regexp ) (map [string ]int64 , error ) {
97136 switch schd {
98137 case VolumeWeighted :
99138 return getVolumeWeightedMap (vgPattern )
100139 case CapacityWeighted :
101140 return getCapacityWeightedMap (vgPattern )
141+ case SpaceWeightedMap :
142+ return getSpaceWeightedMap (vgPattern )
102143 }
103- // return CapacityWeighted (default) if not specified
104- return getCapacityWeightedMap (vgPattern )
144+ // return getSpaceWeightedMap (default) if not specified
145+ return getSpaceWeightedMap (vgPattern )
105146}
0 commit comments