2018-02-05 16:05:59 -05:00
|
|
|
package cluster // import "github.com/docker/docker/daemon/cluster"
|
2017-02-11 13:36:09 -05:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
|
|
|
|
2017-02-11 13:36:09 -05:00
|
|
|
apitypes "github.com/docker/docker/api/types"
|
|
|
|
types "github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/daemon/cluster/convert"
|
2018-01-11 14:53:06 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
2022-04-21 17:33:07 -04:00
|
|
|
swarmapi "github.com/moby/swarmkit/v2/api"
|
2019-06-03 12:33:01 -04:00
|
|
|
"google.golang.org/grpc"
|
2017-02-11 13:36:09 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetNodes returns a list of all nodes known to a cluster.
|
|
|
|
func (c *Cluster) GetNodes(options apitypes.NodeListOptions) ([]types.Node, error) {
|
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
|
|
|
|
|
|
|
state := c.currentNodeState()
|
|
|
|
if !state.IsActiveManager() {
|
|
|
|
return nil, c.errNoManager(state)
|
|
|
|
}
|
|
|
|
|
|
|
|
filters, err := newListNodesFilters(options.Filters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := c.getRequestContext()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
r, err := state.controlClient.ListNodes(
|
|
|
|
ctx,
|
2019-06-03 12:33:01 -04:00
|
|
|
&swarmapi.ListNodesRequest{Filters: filters},
|
|
|
|
grpc.MaxCallRecvMsgSize(defaultRecvSizeForListResponse),
|
|
|
|
)
|
2017-02-11 13:36:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-13 23:02:28 -04:00
|
|
|
nodes := make([]types.Node, 0, len(r.Nodes))
|
2017-02-11 13:36:09 -05:00
|
|
|
|
|
|
|
for _, node := range r.Nodes {
|
|
|
|
nodes = append(nodes, convert.NodeFromGRPC(*node))
|
|
|
|
}
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNode returns a node based on an ID.
|
|
|
|
func (c *Cluster) GetNode(input string) (types.Node, error) {
|
2017-02-28 05:12:11 -05:00
|
|
|
var node *swarmapi.Node
|
|
|
|
|
|
|
|
if err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
|
|
|
|
n, err := getNode(ctx, state.controlClient, input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
node = n
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2017-02-11 13:36:09 -05:00
|
|
|
return types.Node{}, err
|
|
|
|
}
|
2017-02-28 05:12:11 -05:00
|
|
|
|
2017-02-11 13:36:09 -05:00
|
|
|
return convert.NodeFromGRPC(*node), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateNode updates existing nodes properties.
|
|
|
|
func (c *Cluster) UpdateNode(input string, version uint64, spec types.NodeSpec) error {
|
2019-08-05 09:22:28 -04:00
|
|
|
return c.lockedManagerAction(func(_ context.Context, state nodeState) error {
|
2017-02-28 05:12:11 -05:00
|
|
|
nodeSpec, err := convert.NodeSpecToGRPC(spec)
|
|
|
|
if err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return errdefs.InvalidParameter(err)
|
2017-02-28 05:12:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := c.getRequestContext()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
currentNode, err := getNode(ctx, state.controlClient, input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = state.controlClient.UpdateNode(
|
|
|
|
ctx,
|
|
|
|
&swarmapi.UpdateNodeRequest{
|
|
|
|
NodeID: currentNode.ID,
|
|
|
|
Spec: &nodeSpec,
|
|
|
|
NodeVersion: &swarmapi.Version{
|
|
|
|
Index: version,
|
|
|
|
},
|
2017-02-11 13:36:09 -05:00
|
|
|
},
|
2017-02-28 05:12:11 -05:00
|
|
|
)
|
|
|
|
return err
|
|
|
|
})
|
2017-02-11 13:36:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveNode removes a node from a cluster
|
|
|
|
func (c *Cluster) RemoveNode(input string, force bool) error {
|
2017-02-28 05:12:11 -05:00
|
|
|
return c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
|
|
|
|
node, err := getNode(ctx, state.controlClient, input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-11 13:36:09 -05:00
|
|
|
|
2017-02-28 05:12:11 -05:00
|
|
|
_, err = state.controlClient.RemoveNode(ctx, &swarmapi.RemoveNodeRequest{NodeID: node.ID, Force: force})
|
2017-02-11 13:36:09 -05:00
|
|
|
return err
|
2017-02-28 05:12:11 -05:00
|
|
|
})
|
2017-02-11 13:36:09 -05:00
|
|
|
}
|