| 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 chooserscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/chooser`/ |
| 31 | |
| 32 | import ( |
| 33 | "contextscip-go gomod github.com/golang/go/src go1.21 context/" |
| 34 | |
| 35 | "github.com/purpleidea/mgmt/etcd/interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/interfaces`/" |
| 36 | |
| 37 | etcdtypesscip-go gomod go.etcd.io/etcd/client/pkg/v3 v3.5.13 `go.etcd.io/etcd/client/pkg/v3/types`/ "go.etcd.io/etcd/client/pkg/v3/typesscip-go gomod go.etcd.io/etcd/client/pkg/v3 v3.5.13 `go.etcd.io/etcd/client/pkg/v3/types`/" |
| 38 | ) |
| 39 | |
| 40 | // Data represents the input data that is passed to the chooser. |
| 41 | type Datascip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/chooser`/Data# struct { |
| 42 | // Hostname is the hostname running this chooser instance. It can be |
| 43 | // used as a unique key in the cluster. |
| 44 | Hostnamescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/chooser`/Data#Hostname. string // ourself |
| 45 | |
| 46 | Debugscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/chooser`/Data#Debug. bool |
| 47 | Logfscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/chooser`/Data#Logf. func(format string, v ...interface{}) |
| 48 | } |
| 49 | |
| 50 | // Chooser represents the interface you must implement if you want to be able to |
| 51 | // control which cluster members are added and removed. Remember that this can |
| 52 | // get run from any peer (server) machine in the cluster, and that this may |
| 53 | // change as different leaders are elected! Do not assume any state will remain |
| 54 | // between invocations. If you want to maintain hysteresis or state, make sure |
| 55 | // to synchronize it in etcd. |
| 56 | type Chooserscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/chooser`/Chooser# interface { |
| 57 | // Validate validates the chooser implementation to ensure the params |
| 58 | // represent a valid instantiation. |
| 59 | Validatescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/chooser`/Chooser#Validate.() error |
| 60 | |
| 61 | // Init initializes the chooser and passes in some useful data and |
| 62 | // handles. |
| 63 | Initscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/chooser`/Chooser#Init.(*Datascip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/chooser`/Data#) error |
| 64 | |
| 65 | // Connect will be called with a client interfaces.Client that you can |
| 66 | // use if necessary to store some shared state between instances of this |
| 67 | // and watch for external changes. Sharing state between members should |
| 68 | // be avoided if possible, and there is no guarantee that your data |
| 69 | // won't be deleted in a disaster. There are no backups for this, |
| 70 | // regenerate anything you might need. Additionally, this may only be |
| 71 | // used inside the Chooser method, since Connect is only called after |
| 72 | // Init. This is however very useful for implementing special choosers. |
| 73 | // Since some operations can run on connect, it gets a context. If you |
| 74 | // cancel this context, then you might expect that Watch could die too. |
| 75 | // Both of these should get cancelled if you call Disconnect. |
| 76 | Connectscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/chooser`/Chooser#Connect.(contextscip-go gomod github.com/golang/go/src go1.21 context/.Contextscip-go gomod github.com/golang/go/src go1.21 context/Context#, interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/interfaces`/.Clientscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/interfaces`/Client#) error // we get given a namespaced client |
| 77 | |
| 78 | // Disconnect tells us to cancel our use of the client interface that we |
| 79 | // got from the Connect method. We must not return until we're done. |
| 80 | Disconnectscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/chooser`/Chooser#Disconnect.() error |
| 81 | |
| 82 | // Watch is called by the engine to allow us to Watch for changes that |
| 83 | // might cause us to want to re-evaluate our nomination decision. It |
| 84 | // should error if it cannot startup. Once it is running, it should send |
| 85 | // a nil error on every event, and an error if things go wrong. When |
| 86 | // Disconnect is shutdown, then that should cause this to exit. When |
| 87 | // this sends events, Choose will usually eventually get called in |
| 88 | // response. |
| 89 | Watchscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/chooser`/Chooser#Watch.() (chan error, error) |
| 90 | |
| 91 | // Choose takes the current peer membership state, and the available |
| 92 | // volunteers, and produces a list of who we should add and who should |
| 93 | // quit. In general, it's best to only remove one member at a time, in |
| 94 | // particular because this will get called iteratively on future events, |
| 95 | // and it can remove subsequent members on the next iteration. One |
| 96 | // important note: when building a new cluster, we do assume that out of |
| 97 | // one available volunteer, and no members, that this first volunteer is |
| 98 | // selected. Make sure that any implementations of this function do this |
| 99 | // as well, since otherwise the hardcoded initial assumption would be |
| 100 | // proven wrong here! |
| 101 | // TODO: we could pass in two lists of hostnames instead of the full |
| 102 | // URLsMap here, but let's keep it more complicated now in case, and |
| 103 | // reduce it down later if needed... |
| 104 | // TODO: should we add a step arg here ? |
| 105 | Choosescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/chooser`/Chooser#Choose.(membership, volunteers etcdtypesscip-go gomod go.etcd.io/etcd/client/pkg/v3 v3.5.13 `go.etcd.io/etcd/client/pkg/v3/types`/.URLsMapscip-go gomod go.etcd.io/etcd/client/pkg/v3 v3.5.13 `go.etcd.io/etcd/client/pkg/v3/types`/URLsMap#) (nominees, quitters []string, err error) |
| 106 | |
| 107 | // Close runs some cleanup routines in case there is anything that you'd |
| 108 | // like to free after we're done. |
| 109 | Closescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/etcd/chooser`/Chooser#Close.() error |
| 110 | } |