/
leadership.go
201 lines (174 loc) · 6.66 KB
/
leadership.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
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package common
import (
"context"
"github.com/juju/errors"
"github.com/juju/names/v5"
apiservererrors "github.com/juju/juju/apiserver/errors"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/core/leadership"
"github.com/juju/juju/core/permission"
"github.com/juju/juju/rpc/params"
"github.com/juju/juju/state"
)
//go:generate go run go.uber.org/mock/mockgen -package mocks -destination mocks/leadership.go github.com/juju/juju/apiserver/common LeadershipPinningBackend,LeadershipMachine
// LeadershipMachine is an indirection for state.machine.
type LeadershipMachine interface {
ApplicationNames() ([]string, error)
}
type leadershipMachine struct {
*state.Machine
}
// LeadershipPinningBackend describes state method wrappers used by this API.
type LeadershipPinningBackend interface {
Machine(string) (LeadershipMachine, error)
}
type leadershipPinningBackend struct {
*state.State
}
// Machine wraps state.Machine to return an implementation
// of the LeadershipMachine indirection.
func (s leadershipPinningBackend) Machine(name string) (LeadershipMachine, error) {
m, err := s.State.Machine(name)
if err != nil {
return nil, err
}
return leadershipMachine{m}, nil
}
// NewLeadershipPinningFromContext creates and returns a new leadership from
// a facade context.
// This signature is suitable for facade registration.
func NewLeadershipPinningFromContext(ctx facade.ModelContext) (*LeadershipPinning, error) {
st := ctx.State()
model, err := st.Model()
if err != nil {
return nil, errors.Trace(err)
}
pinner, err := ctx.LeadershipPinner()
if err != nil {
return nil, errors.Trace(err)
}
return NewLeadershipPinning(leadershipPinningBackend{st}, model.ModelTag(), pinner, ctx.Auth())
}
// NewLeadershipPinning creates and returns a new leadership API from the
// input tag, Pinner implementation and facade Authorizer.
func NewLeadershipPinning(
st LeadershipPinningBackend, modelTag names.ModelTag, pinner leadership.Pinner, authorizer facade.Authorizer,
) (*LeadershipPinning, error) {
return &LeadershipPinning{
st: st,
modelTag: modelTag,
pinner: pinner,
authorizer: authorizer,
}, nil
}
// LeadershipPinning defines a type for pinning and unpinning application
// leaders.
type LeadershipPinning struct {
st LeadershipPinningBackend
modelTag names.ModelTag
pinner leadership.Pinner
authorizer facade.Authorizer
}
// PinnedLeadership returns all pinned applications and the entities that
// require their pinned behaviour, for leadership in the current model.
func (a *LeadershipPinning) PinnedLeadership(ctx context.Context) (params.PinnedLeadershipResult, error) {
result := params.PinnedLeadershipResult{}
err := a.authorizer.HasPermission(permission.ReadAccess, a.modelTag)
if err != nil {
return result, errors.Trace(err)
}
result.Result, err = a.pinner.PinnedLeadership()
if err != nil {
result.Error = apiservererrors.ServerError(err)
}
return result, nil
}
// PinApplicationLeaders pins leadership for applications based on the auth
// tag provided.
func (a *LeadershipPinning) PinApplicationLeaders(ctx context.Context) (params.PinApplicationsResults, error) {
if !a.authorizer.AuthMachineAgent() {
return params.PinApplicationsResults{}, apiservererrors.ErrPerm
}
tag := a.authorizer.GetAuthTag()
switch tag.Kind() {
case names.MachineTagKind:
return a.pinMachineApplications(ctx, tag)
default:
return params.PinApplicationsResults{}, apiservererrors.ErrPerm
}
}
// UnpinApplicationLeaders unpins leadership for applications based on the auth
// tag provided.
func (a *LeadershipPinning) UnpinApplicationLeaders(ctx context.Context) (params.PinApplicationsResults, error) {
if !a.authorizer.AuthMachineAgent() {
return params.PinApplicationsResults{}, apiservererrors.ErrPerm
}
tag := a.authorizer.GetAuthTag()
switch tag.Kind() {
case names.MachineTagKind:
return a.unpinMachineApplications(ctx, tag)
default:
return params.PinApplicationsResults{}, apiservererrors.ErrPerm
}
}
// GetMachineApplicationNames returns the applications associated with a
// machine.
func (a *LeadershipPinning) GetMachineApplicationNames(ctx context.Context, id string) ([]string, error) {
m, err := a.st.Machine(id)
if err != nil {
return nil, errors.Trace(err)
}
apps, err := m.ApplicationNames()
if err != nil {
return nil, errors.Trace(err)
}
return apps, nil
}
// PinApplicationLeadersByName takes a slice of application names and attempts
// to pin them accordingly.
func (a *LeadershipPinning) PinApplicationLeadersByName(ctx context.Context, tag names.Tag, appNames []string) (params.PinApplicationsResults, error) {
return a.pinAppLeadersOps(tag, appNames, a.pinner.PinLeadership)
}
// UnpinApplicationLeadersByName takes a slice of application names and
// attempts to unpin them accordingly.
func (a *LeadershipPinning) UnpinApplicationLeadersByName(ctx context.Context, tag names.Tag, appNames []string) (params.PinApplicationsResults, error) {
return a.pinAppLeadersOps(tag, appNames, a.pinner.UnpinLeadership)
}
// pinMachineApplications pins leadership for applications represented by units
// running on the auth'd machine.
func (a *LeadershipPinning) pinMachineApplications(ctx context.Context, tag names.Tag) (params.PinApplicationsResults, error) {
appNames, err := a.GetMachineApplicationNames(ctx, tag.Id())
if err != nil {
return params.PinApplicationsResults{}, apiservererrors.ErrPerm
}
return a.pinAppLeadersOps(tag, appNames, a.pinner.PinLeadership)
}
// unpinMachineApplications unpins leadership for applications represented by
// units running on the auth'd machine.
func (a *LeadershipPinning) unpinMachineApplications(ctx context.Context, tag names.Tag) (params.PinApplicationsResults, error) {
appNames, err := a.GetMachineApplicationNames(ctx, tag.Id())
if err != nil {
return params.PinApplicationsResults{}, apiservererrors.ErrPerm
}
return a.pinAppLeadersOps(tag, appNames, a.pinner.UnpinLeadership)
}
// pinAppLeadersOps runs the input pin/unpin operation against all
// applications entities.
// An assumption is made that the validity of the auth tag has been verified
// by the caller.
func (a *LeadershipPinning) pinAppLeadersOps(tag names.Tag, appNames []string, op func(string, string) error) (params.PinApplicationsResults, error) {
var result params.PinApplicationsResults
results := make([]params.PinApplicationResult, len(appNames))
for i, app := range appNames {
results[i] = params.PinApplicationResult{
ApplicationName: app,
}
if err := op(app, tag.String()); err != nil {
results[i].Error = apiservererrors.ServerError(err)
}
}
result.Results = results
return result, nil
}