2018-02-05 16:05:59 -05:00
|
|
|
package cluster // import "github.com/docker/docker/daemon/cluster"
|
2017-02-11 13:45:00 -05:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
|
|
|
|
2017-02-11 13:45:00 -05:00
|
|
|
apitypes "github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
types "github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/daemon/cluster/convert"
|
2022-04-21 17:33:07 -04:00
|
|
|
swarmapi "github.com/moby/swarmkit/v2/api"
|
2018-10-29 20:44:11 -04:00
|
|
|
"google.golang.org/grpc"
|
2017-02-11 13:45:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetTasks returns a list of tasks matching the filter options.
|
|
|
|
func (c *Cluster) GetTasks(options apitypes.TaskListOptions) ([]types.Task, error) {
|
2017-07-24 12:52:31 -04:00
|
|
|
var r *swarmapi.ListTasksResponse
|
2017-02-11 13:45:00 -05:00
|
|
|
|
2017-07-24 12:52:31 -04:00
|
|
|
if err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
|
|
|
|
filterTransform := func(filter filters.Args) error {
|
2017-09-26 07:59:45 -04:00
|
|
|
if filter.Contains("service") {
|
2017-07-24 12:52:31 -04:00
|
|
|
serviceFilters := filter.Get("service")
|
|
|
|
for _, serviceFilter := range serviceFilters {
|
|
|
|
service, err := getService(ctx, state.controlClient, serviceFilter, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
filter.Del("service", serviceFilter)
|
|
|
|
filter.Add("service", service.ID)
|
2017-02-11 13:45:00 -05:00
|
|
|
}
|
|
|
|
}
|
2017-09-26 07:59:45 -04:00
|
|
|
if filter.Contains("node") {
|
2017-07-24 12:52:31 -04:00
|
|
|
nodeFilters := filter.Get("node")
|
|
|
|
for _, nodeFilter := range nodeFilters {
|
|
|
|
node, err := getNode(ctx, state.controlClient, nodeFilter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
filter.Del("node", nodeFilter)
|
|
|
|
filter.Add("node", node.ID)
|
2017-02-11 13:45:00 -05:00
|
|
|
}
|
|
|
|
}
|
2017-09-26 07:59:45 -04:00
|
|
|
if !filter.Contains("runtime") {
|
2017-07-24 12:52:31 -04:00
|
|
|
// default to only showing container tasks
|
|
|
|
filter.Add("runtime", "container")
|
|
|
|
filter.Add("runtime", "")
|
|
|
|
}
|
|
|
|
return nil
|
2017-02-11 13:45:00 -05:00
|
|
|
}
|
|
|
|
|
2017-07-24 12:52:31 -04:00
|
|
|
filters, err := newListTasksFilters(options.Filters, filterTransform)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-11 13:45:00 -05:00
|
|
|
|
2017-07-24 12:52:31 -04:00
|
|
|
r, err = state.controlClient.ListTasks(
|
|
|
|
ctx,
|
2018-10-29 20:44:11 -04:00
|
|
|
&swarmapi.ListTasksRequest{Filters: filters},
|
|
|
|
grpc.MaxCallRecvMsgSize(defaultRecvSizeForListResponse),
|
|
|
|
)
|
2017-07-24 12:52:31 -04:00
|
|
|
return err
|
|
|
|
}); err != nil {
|
2017-02-11 13:45:00 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-13 23:02:28 -04:00
|
|
|
tasks := make([]types.Task, 0, len(r.Tasks))
|
2017-02-11 13:45:00 -05:00
|
|
|
for _, task := range r.Tasks {
|
2017-06-07 13:07:01 -04:00
|
|
|
t, err := convert.TaskFromGRPC(*task)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-02-11 13:45:00 -05:00
|
|
|
}
|
2017-06-07 13:07:01 -04:00
|
|
|
tasks = append(tasks, t)
|
2017-02-11 13:45:00 -05:00
|
|
|
}
|
|
|
|
return tasks, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTask returns a task by an ID.
|
|
|
|
func (c *Cluster) GetTask(input string) (types.Task, error) {
|
2017-02-28 05:12:11 -05:00
|
|
|
var task *swarmapi.Task
|
|
|
|
if err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
|
|
|
|
t, err := getTask(ctx, state.controlClient, input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
task = t
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2017-02-11 13:45:00 -05:00
|
|
|
return types.Task{}, err
|
|
|
|
}
|
2017-06-07 13:07:01 -04:00
|
|
|
return convert.TaskFromGRPC(*task)
|
2017-02-11 13:45:00 -05:00
|
|
|
}
|