| 1 | // Mgmt |
| 2 | // Copyright (C) 2013-2024+ James Shubin and the project contributors |
| 3 | // Written by James Shubin <[email protected]> and the project contributors |
| 4 | // |
| 5 | // This program is free software: you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU General Public License as published by |
| 7 | // the Free Software Foundation, either version 3 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // This program is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU General Public License |
| 16 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | // |
| 18 | // Additional permission under GNU GPL version 3 section 7 |
| 19 | // |
| 20 | // If you modify this program, or any covered work, by linking or combining it |
| 21 | // with embedded mcl code and modules (and that the embedded mcl code and |
| 22 | // modules which link with this program, contain a copy of their source code in |
| 23 | // the authoritative form) containing parts covered by the terms of any other |
| 24 | // license, the licensors of this program grant you additional permission to |
| 25 | // convey the resulting work. Furthermore, the licensors of this program grant |
| 26 | // the original author, James Shubin, additional permission to update this |
| 27 | // additional permission if he deems it necessary to achieve the goals of this |
| 28 | // additional permission. |
| 29 | |
| 30 | package schedulerscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/ // TODO: i'd like this to be a separate package, but cycles! |
| 31 | |
| 32 | import ( |
| 33 | "fmtscip-go gomod github.com/golang/go/src go1.21 fmt/" |
| 34 | "sortscip-go gomod github.com/golang/go/src go1.21 sort/" |
| 35 | ) |
| 36 | |
| 37 | func initscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/init().() { |
| 38 | Registerscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/Register().("alpha", func() Strategyscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/Strategy# { return &alphaStrategyscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/alphaStrategy#{} }) // must register the func and name |
| 39 | } |
| 40 | |
| 41 | type alphaStrategyscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/alphaStrategy# struct { |
| 42 | // no state to store |
| 43 | } |
| 44 | |
| 45 | // Schedule returns the first host out of a sorted group of available hostnames. |
| 46 | func (obj *alphaStrategyscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/alphaStrategy#) Schedulescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/alphaStrategy#Schedule().(hostnames map[string]string, opts *schedulerOptionsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/schedulerOptions#) ([]string, error) { |
| 47 | if len(hostnames) <= 0 { |
| 48 | return nil, fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("strategy: cannot schedule from zero hosts") |
| 49 | } |
| 50 | if opts.maxCountscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/scheduler`/schedulerOptions#maxCount. <= 0 { |
| 51 | return nil, fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("strategy: cannot schedule with a max of zero") |
| 52 | } |
| 53 | |
| 54 | sortedHosts := []string{} |
| 55 | for key := range hostnames { |
| 56 | sortedHosts = append(sortedHosts, key) |
| 57 | } |
| 58 | sortscip-go gomod github.com/golang/go/src go1.21 sort/.Stringsscip-go gomod github.com/golang/go/src go1.21 sort/Strings().(sortedHosts) |
| 59 | |
| 60 | return []string{sortedHosts[0]}, nil // pick first host |
| 61 | } |