/
highavailability.go
268 lines (238 loc) · 8.54 KB
/
highavailability.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package highavailability
import (
"fmt"
"sort"
"strconv"
"strings"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/names/v5"
"github.com/juju/juju/apiserver/common"
apiservererrors "github.com/juju/juju/apiserver/errors"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/controller"
"github.com/juju/juju/core/constraints"
"github.com/juju/juju/core/instance"
"github.com/juju/juju/core/network"
"github.com/juju/juju/core/permission"
"github.com/juju/juju/rpc/params"
"github.com/juju/juju/state"
)
var logger = loggo.GetLogger("juju.apiserver.highavailability")
// HighAvailability defines the methods on the highavailability API end point.
type HighAvailability interface {
EnableHA(args params.ControllersSpecs) (params.ControllersChangeResults, error)
}
// HighAvailabilityAPI implements the HighAvailability interface and is the concrete
// implementation of the api end point.
type HighAvailabilityAPI struct {
state *state.State
resources facade.Resources
authorizer facade.Authorizer
}
var _ HighAvailability = (*HighAvailabilityAPI)(nil)
// EnableHA adds controller machines as necessary to ensure the
// controller has the number of machines specified.
func (api *HighAvailabilityAPI) EnableHA(args params.ControllersSpecs) (params.ControllersChangeResults, error) {
results := params.ControllersChangeResults{}
err := api.authorizer.HasPermission(permission.SuperuserAccess, api.state.ControllerTag())
if err != nil {
return results, apiservererrors.ServerError(apiservererrors.ErrPerm)
}
if len(args.Specs) == 0 {
return results, nil
}
if len(args.Specs) > 1 {
return results, errors.New("only one controller spec is supported")
}
result, err := api.enableHASingle(api.state, args.Specs[0])
results.Results = make([]params.ControllersChangeResult, 1)
results.Results[0].Result = result
results.Results[0].Error = apiservererrors.ServerError(err)
return results, nil
}
func (api *HighAvailabilityAPI) enableHASingle(st *state.State, spec params.ControllersSpec) (
params.ControllersChanges, error,
) {
if !st.IsController() {
return params.ControllersChanges{}, errors.New("unsupported with workload models")
}
// Check if changes are allowed and the command may proceed.
blockChecker := common.NewBlockChecker(st)
if err := blockChecker.ChangeAllowed(); err != nil {
return params.ControllersChanges{}, errors.Trace(err)
}
controllerIds, err := st.ControllerIds()
if err != nil {
return params.ControllersChanges{}, err
}
referenceMachine, err := getReferenceController(st, controllerIds)
if err != nil {
return params.ControllersChanges{}, errors.Trace(err)
}
// If there were no supplied constraints, use the original bootstrap
// constraints.
if constraints.IsEmpty(&spec.Constraints) {
if constraints.IsEmpty(&spec.Constraints) {
cons, err := referenceMachine.Constraints()
if err != nil {
return params.ControllersChanges{}, errors.Trace(err)
}
spec.Constraints = cons
}
}
// Retrieve the controller configuration and merge any implied space
// constraints into the spec constraints.
cfg, err := st.ControllerConfig()
if err != nil {
return params.ControllersChanges{}, errors.Annotate(err, "retrieving controller config")
}
if err = validateCurrentControllers(st, cfg, controllerIds); err != nil {
return params.ControllersChanges{}, errors.Trace(err)
}
spec.Constraints.Spaces = cfg.AsSpaceConstraints(spec.Constraints.Spaces)
if err = validatePlacementForSpaces(st, spec.Constraints.Spaces, spec.Placement); err != nil {
return params.ControllersChanges{}, errors.Trace(err)
}
// Might be nicer to pass the spec itself to this method.
changes, err := st.EnableHA(spec.NumControllers, spec.Constraints, referenceMachine.Base(), spec.Placement)
if err != nil {
return params.ControllersChanges{}, err
}
return controllersChanges(changes), nil
}
// getReferenceController looks up the ideal controller to use as a reference for Constraints and Release
func getReferenceController(st *state.State, controllerIds []string) (*state.Machine, error) {
// Sort the controller IDs from low to high and take the first.
// This will typically give the initial bootstrap machine.
var controllerNumbers []int
for _, id := range controllerIds {
idNum, err := strconv.Atoi(id)
if err != nil {
logger.Warningf("ignoring non numeric controller id %v", id)
continue
}
controllerNumbers = append(controllerNumbers, idNum)
}
if len(controllerNumbers) == 0 {
return nil, errors.Errorf("internal error; failed to find any controllers")
}
sort.Ints(controllerNumbers)
controllerId := controllerNumbers[0]
// Load the controller machine and get its constraints.
cm, err := st.Machine(strconv.Itoa(controllerId))
if err != nil {
return nil, errors.Annotatef(err, "reading controller id %v", controllerId)
}
return cm, nil
}
// validateCurrentControllers checks for a scenario where there is no HA space
// in controller configuration and more than one machine-local address on any
// of the controller machines. An error is returned if it is detected.
// When HA space is set, there are other code paths that ensure controllers
// have at least one address in the space.
func validateCurrentControllers(st *state.State, cfg controller.Config, machineIds []string) error {
if cfg.JujuHASpace() != "" {
return nil
}
var badIds []string
for _, id := range machineIds {
cm, err := st.Machine(id)
if err != nil {
return errors.Annotatef(err, "reading controller id %v", id)
}
addresses := cm.Addresses()
if len(addresses) == 0 {
// machines without any address are essentially not started yet
continue
}
internal := addresses.AllMatchingScope(network.ScopeMatchCloudLocal)
if len(internal) != 1 {
badIds = append(badIds, id)
}
}
if len(badIds) > 0 {
return errors.Errorf(
"juju-ha-space is not set and a unique usable address was not found for machines: %s"+
"\nrun \"juju controller-config juju-ha-space=<name>\" to set a space for Mongo peer communication",
strings.Join(badIds, ", "),
)
}
return nil
}
// validatePlacementForSpaces checks whether there are both space constraints
// and machine placement directives.
// If there are, checks are made to ensure that the machines specified have at
// least one address in all of the spaces.
func validatePlacementForSpaces(st *state.State, spaceNames *[]string, placement []string) error {
if spaceNames == nil || len(*spaceNames) == 0 || len(placement) == 0 {
return nil
}
for _, v := range placement {
p, err := instance.ParsePlacement(v)
if err != nil {
if err == instance.ErrPlacementScopeMissing {
// Where an unscoped placement is not parsed as a machine ID,
// such as for a MaaS node name, just allow it through.
// TODO (manadart 2018-03-27): Possible work at the provider
// level to accommodate placement and space constraints during
// instance pre-check may be entertained in the future.
continue
}
return errors.Annotate(err, "parsing placement")
}
if p.Directive == "" {
continue
}
m, err := st.Machine(p.Directive)
if err != nil {
if errors.IsNotFound(err) {
// Don't throw out of here when the machine does not exist.
// Validate others if required and leave it handled downstream.
continue
}
return errors.Annotate(err, "retrieving machine")
}
spaceInfos, err := st.AllSpaceInfos()
if err != nil {
return errors.Trace(err)
}
for _, name := range *spaceNames {
spaceInfo := spaceInfos.GetByName(name)
if spaceInfo == nil {
return errors.NotFoundf("space with name %q", name)
}
inSpace := false
for _, addr := range m.Addresses() {
if addr.SpaceID == spaceInfo.ID {
inSpace = true
break
}
}
if !inSpace {
return fmt.Errorf("machine %q has no addresses in space %q", p.Directive, name)
}
}
}
return nil
}
// controllersChanges generates a new params instance from the state instance.
func controllersChanges(change state.ControllersChanges) params.ControllersChanges {
return params.ControllersChanges{
Added: machineIdsToTags(change.Added...),
Maintained: machineIdsToTags(change.Maintained...),
Removed: machineIdsToTags(change.Removed...),
Converted: machineIdsToTags(change.Converted...),
}
}
// machineIdsToTags returns a slice of machine tag strings created from the
// input machine IDs.
func machineIdsToTags(ids ...string) []string {
var result []string
for _, id := range ids {
result = append(result, names.NewMachineTag(id).String())
}
return result
}