forked from moby/swarmkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
141 lines (129 loc) · 4.25 KB
/
task.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
package controlapi
import (
"github.com/docker/swarmkit/api"
"github.com/docker/swarmkit/manager/state/store"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
// GetTask returns a Task given a TaskID.
// - Returns `InvalidArgument` if TaskID is not provided.
// - Returns `NotFound` if the Task is not found.
func (s *Server) GetTask(ctx context.Context, request *api.GetTaskRequest) (*api.GetTaskResponse, error) {
if request.TaskID == "" {
return nil, grpc.Errorf(codes.InvalidArgument, errInvalidArgument.Error())
}
var task *api.Task
s.store.View(func(tx store.ReadTx) {
task = store.GetTask(tx, request.TaskID)
})
if task == nil {
return nil, grpc.Errorf(codes.NotFound, "task %s not found", request.TaskID)
}
return &api.GetTaskResponse{
Task: task,
}, nil
}
// RemoveTask removes a Task referenced by TaskID.
// - Returns `InvalidArgument` if TaskID is not provided.
// - Returns `NotFound` if the Task is not found.
// - Returns an error if the deletion fails.
func (s *Server) RemoveTask(ctx context.Context, request *api.RemoveTaskRequest) (*api.RemoveTaskResponse, error) {
if request.TaskID == "" {
return nil, grpc.Errorf(codes.InvalidArgument, errInvalidArgument.Error())
}
err := s.store.Update(func(tx store.Tx) error {
return store.DeleteTask(tx, request.TaskID)
})
if err != nil {
if err == store.ErrNotExist {
return nil, grpc.Errorf(codes.NotFound, "task %s not found", request.TaskID)
}
return nil, err
}
return &api.RemoveTaskResponse{}, nil
}
func filterTasks(candidates []*api.Task, filters ...func(*api.Task) bool) []*api.Task {
result := []*api.Task{}
for _, c := range candidates {
match := true
for _, f := range filters {
if !f(c) {
match = false
break
}
}
if match {
result = append(result, c)
}
}
return result
}
// ListTasks returns a list of all tasks.
func (s *Server) ListTasks(ctx context.Context, request *api.ListTasksRequest) (*api.ListTasksResponse, error) {
var (
tasks []*api.Task
err error
)
s.store.View(func(tx store.ReadTx) {
switch {
case request.Filters != nil && len(request.Filters.Names) > 0:
tasks, err = store.FindTasks(tx, buildFilters(store.ByName, request.Filters.Names))
case request.Filters != nil && len(request.Filters.NamePrefixes) > 0:
tasks, err = store.FindTasks(tx, buildFilters(store.ByNamePrefix, request.Filters.NamePrefixes))
case request.Filters != nil && len(request.Filters.IDPrefixes) > 0:
tasks, err = store.FindTasks(tx, buildFilters(store.ByIDPrefix, request.Filters.IDPrefixes))
case request.Filters != nil && len(request.Filters.ServiceIDs) > 0:
tasks, err = store.FindTasks(tx, buildFilters(store.ByServiceID, request.Filters.ServiceIDs))
case request.Filters != nil && len(request.Filters.NodeIDs) > 0:
tasks, err = store.FindTasks(tx, buildFilters(store.ByNodeID, request.Filters.NodeIDs))
case request.Filters != nil && len(request.Filters.DesiredStates) > 0:
filters := make([]store.By, 0, len(request.Filters.DesiredStates))
for _, v := range request.Filters.DesiredStates {
filters = append(filters, store.ByDesiredState(v))
}
tasks, err = store.FindTasks(tx, store.Or(filters...))
default:
tasks, err = store.FindTasks(tx, store.All)
}
})
if err != nil {
return nil, err
}
if request.Filters != nil {
tasks = filterTasks(tasks,
func(e *api.Task) bool {
return filterContains(e.ServiceAnnotations.Name, request.Filters.Names)
},
func(e *api.Task) bool {
return filterContainsPrefix(e.ServiceAnnotations.Name, request.Filters.NamePrefixes)
},
func(e *api.Task) bool {
return filterContainsPrefix(e.ID, request.Filters.IDPrefixes)
},
func(e *api.Task) bool {
return filterMatchLabels(e.ServiceAnnotations.Labels, request.Filters.Labels)
},
func(e *api.Task) bool {
return filterContains(e.ServiceID, request.Filters.ServiceIDs)
},
func(e *api.Task) bool {
return filterContains(e.NodeID, request.Filters.NodeIDs)
},
func(e *api.Task) bool {
if len(request.Filters.DesiredStates) == 0 {
return true
}
for _, c := range request.Filters.DesiredStates {
if c == e.DesiredState {
return true
}
}
return false
},
)
}
return &api.ListTasksResponse{
Tasks: tasks,
}, nil
}