2018-02-05 21:05:59 +00:00
|
|
|
package cluster // import "github.com/docker/docker/daemon/cluster"
|
2016-06-14 02:52:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2016-06-14 02:52:49 +00:00
|
|
|
runconfigopts "github.com/docker/docker/runconfig/opts"
|
2022-04-21 21:33:07 +00:00
|
|
|
swarmapi "github.com/moby/swarmkit/v2/api"
|
2016-06-14 02:52:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func newListNodesFilters(filter filters.Args) (*swarmapi.ListNodesRequest_Filters, error) {
|
|
|
|
accepted := map[string]bool{
|
|
|
|
"name": true,
|
|
|
|
"id": true,
|
|
|
|
"label": true,
|
|
|
|
"role": true,
|
|
|
|
"membership": true,
|
2018-08-16 23:29:33 +00:00
|
|
|
"node.label": true,
|
2016-06-14 02:52:49 +00:00
|
|
|
}
|
|
|
|
if err := filter.Validate(accepted); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f := &swarmapi.ListNodesRequest_Filters{
|
2016-07-17 20:21:29 +00:00
|
|
|
NamePrefixes: filter.Get("name"),
|
|
|
|
IDPrefixes: filter.Get("id"),
|
|
|
|
Labels: runconfigopts.ConvertKVStringsToMap(filter.Get("label")),
|
2018-08-16 23:29:33 +00:00
|
|
|
NodeLabels: runconfigopts.ConvertKVStringsToMap(filter.Get("node.label")),
|
2016-06-14 02:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range filter.Get("role") {
|
|
|
|
if role, ok := swarmapi.NodeRole_value[strings.ToUpper(r)]; ok {
|
|
|
|
f.Roles = append(f.Roles, swarmapi.NodeRole(role))
|
|
|
|
} else if r != "" {
|
|
|
|
return nil, fmt.Errorf("Invalid role filter: '%s'", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, a := range filter.Get("membership") {
|
|
|
|
if membership, ok := swarmapi.NodeSpec_Membership_value[strings.ToUpper(a)]; ok {
|
|
|
|
f.Memberships = append(f.Memberships, swarmapi.NodeSpec_Membership(membership))
|
|
|
|
} else if a != "" {
|
|
|
|
return nil, fmt.Errorf("Invalid membership filter: '%s'", a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
2016-06-30 13:09:03 +00:00
|
|
|
func newListTasksFilters(filter filters.Args, transformFunc func(filters.Args) error) (*swarmapi.ListTasksRequest_Filters, error) {
|
2016-06-14 02:52:49 +00:00
|
|
|
accepted := map[string]bool{
|
|
|
|
"name": true,
|
|
|
|
"id": true,
|
|
|
|
"label": true,
|
|
|
|
"service": true,
|
|
|
|
"node": true,
|
2016-06-29 16:20:32 +00:00
|
|
|
"desired-state": true,
|
2017-03-03 02:26:41 +00:00
|
|
|
// UpToDate is not meant to be exposed to users. It's for
|
|
|
|
// internal use in checking create/update progress. Therefore,
|
|
|
|
// we prefix it with a '_'.
|
|
|
|
"_up-to-date": true,
|
2017-06-07 17:07:01 +00:00
|
|
|
"runtime": true,
|
2016-06-14 02:52:49 +00:00
|
|
|
}
|
|
|
|
if err := filter.Validate(accepted); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-30 13:09:03 +00:00
|
|
|
if transformFunc != nil {
|
|
|
|
if err := transformFunc(filter); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2016-06-14 02:52:49 +00:00
|
|
|
f := &swarmapi.ListTasksRequest_Filters{
|
2016-07-17 20:21:29 +00:00
|
|
|
NamePrefixes: filter.Get("name"),
|
|
|
|
IDPrefixes: filter.Get("id"),
|
|
|
|
Labels: runconfigopts.ConvertKVStringsToMap(filter.Get("label")),
|
|
|
|
ServiceIDs: filter.Get("service"),
|
|
|
|
NodeIDs: filter.Get("node"),
|
2017-03-03 02:26:41 +00:00
|
|
|
UpToDate: len(filter.Get("_up-to-date")) != 0,
|
2017-06-07 17:07:01 +00:00
|
|
|
Runtimes: filter.Get("runtime"),
|
2016-06-14 02:52:49 +00:00
|
|
|
}
|
|
|
|
|
2016-06-29 16:20:32 +00:00
|
|
|
for _, s := range filter.Get("desired-state") {
|
2016-06-14 02:52:49 +00:00
|
|
|
if state, ok := swarmapi.TaskState_value[strings.ToUpper(s)]; ok {
|
|
|
|
f.DesiredStates = append(f.DesiredStates, swarmapi.TaskState(state))
|
|
|
|
} else if s != "" {
|
2016-06-29 16:20:32 +00:00
|
|
|
return nil, fmt.Errorf("Invalid desired-state filter: '%s'", s)
|
2016-06-14 02:52:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return f, nil
|
|
|
|
}
|
2016-10-19 16:22:02 +00:00
|
|
|
|
|
|
|
func newListSecretsFilters(filter filters.Args) (*swarmapi.ListSecretsRequest_Filters, error) {
|
|
|
|
accepted := map[string]bool{
|
|
|
|
"names": true,
|
|
|
|
"name": true,
|
|
|
|
"id": true,
|
|
|
|
"label": true,
|
|
|
|
}
|
|
|
|
if err := filter.Validate(accepted); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &swarmapi.ListSecretsRequest_Filters{
|
|
|
|
Names: filter.Get("names"),
|
|
|
|
NamePrefixes: filter.Get("name"),
|
|
|
|
IDPrefixes: filter.Get("id"),
|
|
|
|
Labels: runconfigopts.ConvertKVStringsToMap(filter.Get("label")),
|
|
|
|
}, nil
|
|
|
|
}
|
2017-03-15 21:52:17 +00:00
|
|
|
|
|
|
|
func newListConfigsFilters(filter filters.Args) (*swarmapi.ListConfigsRequest_Filters, error) {
|
|
|
|
accepted := map[string]bool{
|
|
|
|
"name": true,
|
|
|
|
"id": true,
|
|
|
|
"label": true,
|
|
|
|
}
|
|
|
|
if err := filter.Validate(accepted); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &swarmapi.ListConfigsRequest_Filters{
|
|
|
|
NamePrefixes: filter.Get("name"),
|
|
|
|
IDPrefixes: filter.Get("id"),
|
|
|
|
Labels: runconfigopts.ConvertKVStringsToMap(filter.Get("label")),
|
|
|
|
}, nil
|
|
|
|
}
|