/
namespace_endpoint.go
371 lines (319 loc) · 9.99 KB
/
namespace_endpoint.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package nomad
import (
"fmt"
"time"
metrics "github.com/armon/go-metrics"
memdb "github.com/hashicorp/go-memdb"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/nomad/nomad/state"
"github.com/hashicorp/nomad/nomad/structs"
)
// Namespace endpoint is used for manipulating namespaces
type Namespace struct {
srv *Server
}
// UpsertNamespaces is used to upsert a set of namespaces
func (n *Namespace) UpsertNamespaces(args *structs.NamespaceUpsertRequest,
reply *structs.GenericResponse) error {
args.Region = n.srv.config.AuthoritativeRegion
if done, err := n.srv.forward("Namespace.UpsertNamespaces", args, args, reply); done {
return err
}
defer metrics.MeasureSince([]string{"nomad", "namespace", "upsert_namespaces"}, time.Now())
// Check management permissions
if aclObj, err := n.srv.ResolveToken(args.AuthToken); err != nil {
return err
} else if aclObj != nil && !aclObj.IsManagement() {
return structs.ErrPermissionDenied
}
// Validate there is at least one namespace
if len(args.Namespaces) == 0 {
return fmt.Errorf("must specify at least one namespace")
}
// Validate the namespaces and set the hash
for _, ns := range args.Namespaces {
if err := ns.Validate(); err != nil {
return fmt.Errorf("Invalid namespace %q: %v", ns.Name, err)
}
ns.SetHash()
}
// Update via Raft
out, index, err := n.srv.raftApply(structs.NamespaceUpsertRequestType, args)
if err != nil {
return err
}
// Check if there was an error when applying.
if err, ok := out.(error); ok && err != nil {
return err
}
// Update the index
reply.Index = index
return nil
}
// DeleteNamespaces is used to delete a namespace
func (n *Namespace) DeleteNamespaces(args *structs.NamespaceDeleteRequest, reply *structs.GenericResponse) error {
args.Region = n.srv.config.AuthoritativeRegion
if done, err := n.srv.forward("Namespace.DeleteNamespaces", args, args, reply); done {
return err
}
defer metrics.MeasureSince([]string{"nomad", "namespace", "delete_namespaces"}, time.Now())
// Check management permissions
if aclObj, err := n.srv.ResolveToken(args.AuthToken); err != nil {
return err
} else if aclObj != nil && !aclObj.IsManagement() {
return structs.ErrPermissionDenied
}
// Validate at least one namespace
if len(args.Namespaces) == 0 {
return fmt.Errorf("must specify at least one namespace to delete")
}
for _, ns := range args.Namespaces {
if ns == structs.DefaultNamespace {
return fmt.Errorf("can not delete default namespace")
}
}
// Check that the deleting namespaces do not have non-terminal jobs in both
// this region and all federated regions
var mErr multierror.Error
for _, ns := range args.Namespaces {
nonTerminal, err := n.nonTerminalNamespaces(args.AuthToken, ns)
if err != nil {
_ = multierror.Append(&mErr, err)
} else if len(nonTerminal) != 0 {
_ = multierror.Append(&mErr, fmt.Errorf("namespace %q has non-terminal jobs in regions: %v", ns, nonTerminal))
}
}
if err := mErr.ErrorOrNil(); err != nil {
return err
}
// Update via Raft
out, index, err := n.srv.raftApply(structs.NamespaceDeleteRequestType, args)
if err != nil {
return err
}
// Check if there was an error when applying.
if err, ok := out.(error); ok && err != nil {
return err
}
// Update the index
reply.Index = index
return nil
}
// nonTerminalNamespaces returns whether the set of regions in which the
// namespaces contains non-terminal jobs, checking all federated regions
// including this one.
func (n *Namespace) nonTerminalNamespaces(authToken, namespace string) ([]string, error) {
regions := n.srv.Regions()
thisRegion := n.srv.Region()
terminal := make([]string, 0, len(regions))
// Check if this region is terminal
localTerminal, err := n.namespaceTerminalLocally(namespace)
if err != nil {
return nil, err
}
if !localTerminal {
terminal = append(terminal, thisRegion)
}
for _, region := range regions {
if region == thisRegion {
continue
}
remoteTerminal, err := n.namespaceTerminalInRegion(authToken, namespace, region)
if err != nil {
return nil, err
}
if !remoteTerminal {
terminal = append(terminal, region)
}
}
return terminal, nil
}
// namespaceTerminalLocally returns if the namespace contains only terminal jobs
// in the local region .
func (n *Namespace) namespaceTerminalLocally(namespace string) (bool, error) {
snap, err := n.srv.fsm.State().Snapshot()
if err != nil {
return false, err
}
iter, err := snap.JobsByNamespace(nil, namespace)
if err != nil {
return false, err
}
for {
raw := iter.Next()
if raw == nil {
break
}
job := raw.(*structs.Job)
if job.Status != structs.JobStatusDead {
return false, nil
}
}
return true, nil
}
// namespaceTerminalInRegion returns if the namespace contains only terminal
// jobs in the given region .
func (n *Namespace) namespaceTerminalInRegion(authToken, namespace, region string) (bool, error) {
req := &structs.JobListRequest{
QueryOptions: structs.QueryOptions{
Region: region,
Namespace: namespace,
AllowStale: false,
AuthToken: authToken,
},
}
var resp structs.JobListResponse
done, err := n.srv.forward("Job.List", req, req, &resp)
if !done {
return false, fmt.Errorf("unexpectedly did not forward Job.List to region %q", region)
} else if err != nil {
return false, err
}
for _, job := range resp.Jobs {
if job.Status != structs.JobStatusDead {
return false, nil
}
}
return true, nil
}
// ListNamespaces is used to list the namespaces
func (n *Namespace) ListNamespaces(args *structs.NamespaceListRequest, reply *structs.NamespaceListResponse) error {
if done, err := n.srv.forward("Namespace.ListNamespaces", args, args, reply); done {
return err
}
defer metrics.MeasureSince([]string{"nomad", "namespace", "list_namespace"}, time.Now())
// Resolve token to acl to filter namespace list
aclObj, err := n.srv.ResolveToken(args.AuthToken)
if err != nil {
return err
}
// Setup the blocking query
opts := blockingOptions{
queryOpts: &args.QueryOptions,
queryMeta: &reply.QueryMeta,
run: func(ws memdb.WatchSet, s *state.StateStore) error {
// Iterate over all the namespaces
var err error
var iter memdb.ResultIterator
if prefix := args.QueryOptions.Prefix; prefix != "" {
iter, err = s.NamespacesByNamePrefix(ws, prefix)
} else {
iter, err = s.Namespaces(ws)
}
if err != nil {
return err
}
reply.Namespaces = nil
for {
raw := iter.Next()
if raw == nil {
break
}
ns := raw.(*structs.Namespace)
// Only return namespaces allowed by acl
if aclObj == nil || aclObj.AllowNamespace(ns.Name) {
reply.Namespaces = append(reply.Namespaces, ns)
}
}
// Use the last index that affected the namespace table
index, err := s.Index(state.TableNamespaces)
if err != nil {
return err
}
// Ensure we never set the index to zero, otherwise a blocking query cannot be used.
// We floor the index at one, since realistically the first write must have a higher index.
if index == 0 {
index = 1
}
reply.Index = index
return nil
}}
return n.srv.blockingRPC(&opts)
}
// GetNamespace is used to get a specific namespace
func (n *Namespace) GetNamespace(args *structs.NamespaceSpecificRequest, reply *structs.SingleNamespaceResponse) error {
if done, err := n.srv.forward("Namespace.GetNamespace", args, args, reply); done {
return err
}
defer metrics.MeasureSince([]string{"nomad", "namespace", "get_namespace"}, time.Now())
// Check capabilities for the given namespace permissions
if aclObj, err := n.srv.ResolveToken(args.AuthToken); err != nil {
return err
} else if aclObj != nil && !aclObj.AllowNamespace(args.Name) {
return structs.ErrPermissionDenied
}
// Setup the blocking query
opts := blockingOptions{
queryOpts: &args.QueryOptions,
queryMeta: &reply.QueryMeta,
run: func(ws memdb.WatchSet, s *state.StateStore) error {
// Look for the namespace
out, err := s.NamespaceByName(ws, args.Name)
if err != nil {
return err
}
// Setup the output
reply.Namespace = out
if out != nil {
reply.Index = out.ModifyIndex
} else {
// Use the last index that affected the namespace table
index, err := s.Index(state.TableNamespaces)
if err != nil {
return err
}
// Ensure we never set the index to zero, otherwise a blocking query cannot be used.
// We floor the index at one, since realistically the first write must have a higher index.
if index == 0 {
index = 1
}
reply.Index = index
}
return nil
}}
return n.srv.blockingRPC(&opts)
}
// GetNamespaces is used to get a set of namespaces
func (n *Namespace) GetNamespaces(args *structs.NamespaceSetRequest, reply *structs.NamespaceSetResponse) error {
if done, err := n.srv.forward("Namespace.GetNamespaces", args, args, reply); done {
return err
}
defer metrics.MeasureSince([]string{"nomad", "namespace", "get_namespaces"}, time.Now())
// Check management permissions
if aclObj, err := n.srv.ResolveToken(args.AuthToken); err != nil {
return err
} else if aclObj != nil && !aclObj.IsManagement() {
return structs.ErrPermissionDenied
}
// Setup the blocking query
opts := blockingOptions{
queryOpts: &args.QueryOptions,
queryMeta: &reply.QueryMeta,
run: func(ws memdb.WatchSet, s *state.StateStore) error {
// Setup the output
reply.Namespaces = make(map[string]*structs.Namespace, len(args.Namespaces))
// Look for the namespace
for _, namespace := range args.Namespaces {
out, err := s.NamespaceByName(ws, namespace)
if err != nil {
return err
}
if out != nil {
reply.Namespaces[namespace] = out
}
}
// Use the last index that affected the policy table
index, err := s.Index(state.TableNamespaces)
if err != nil {
return err
}
// Ensure we never set the index to zero, otherwise a blocking query cannot be used.
// We floor the index at one, since realistically the first write must have a higher index.
if index == 0 {
index = 1
}
reply.Index = index
return nil
}}
return n.srv.blockingRPC(&opts)
}