forked from moby/swarmkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
165 lines (136 loc) · 3.37 KB
/
common.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
package node
import (
"context"
"errors"
"fmt"
"reflect"
"strings"
"github.com/moby/swarmkit/swarmd/cmd/swarmctl/common"
"github.com/moby/swarmkit/v2/api"
"github.com/spf13/cobra"
)
var (
errNoChange = errors.New("node attribute was already set to the requested value")
flagLabel = "label"
)
func changeNodeAvailability(cmd *cobra.Command, args []string, availability api.NodeSpec_Availability) error {
if len(args) == 0 {
return errors.New("missing node ID")
}
if len(args) > 1 {
return errors.New("command takes exactly 1 argument")
}
c, err := common.Dial(cmd)
if err != nil {
return err
}
node, err := getNode(common.Context(cmd), c, args[0])
if err != nil {
return err
}
spec := &node.Spec
if spec.Availability == availability {
return errNoChange
}
spec.Availability = availability
_, err = c.UpdateNode(common.Context(cmd), &api.UpdateNodeRequest{
NodeID: node.ID,
NodeVersion: &node.Meta.Version,
Spec: spec,
})
return err
}
func changeNodeRole(cmd *cobra.Command, args []string, role api.NodeRole) error {
if len(args) == 0 {
return errors.New("missing node ID")
}
if len(args) > 1 {
return errors.New("command takes exactly 1 argument")
}
c, err := common.Dial(cmd)
if err != nil {
return err
}
node, err := getNode(common.Context(cmd), c, args[0])
if err != nil {
return err
}
spec := &node.Spec
if spec.DesiredRole == role {
return errNoChange
}
spec.DesiredRole = role
_, err = c.UpdateNode(common.Context(cmd), &api.UpdateNodeRequest{
NodeID: node.ID,
NodeVersion: &node.Meta.Version,
Spec: spec,
})
return err
}
func getNode(ctx context.Context, c api.ControlClient, input string) (*api.Node, error) {
// GetNode to match via full ID.
rg, err := c.GetNode(ctx, &api.GetNodeRequest{NodeID: input})
if err != nil {
// If any error (including NotFound), ListServices to match via full name.
rl, err := c.ListNodes(ctx,
&api.ListNodesRequest{
Filters: &api.ListNodesRequest_Filters{
Names: []string{input},
},
},
)
if err != nil {
return nil, err
}
if len(rl.Nodes) == 0 {
return nil, fmt.Errorf("node %s not found", input)
}
if l := len(rl.Nodes); l > 1 {
return nil, fmt.Errorf("node %s is ambiguous (%d matches found)", input, l)
}
return rl.Nodes[0], nil
}
return rg.Node, nil
}
func updateNode(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("node ID missing")
}
if len(args) > 1 {
return errors.New("command takes exactly 1 argument")
}
c, err := common.Dial(cmd)
if err != nil {
return err
}
node, err := getNode(common.Context(cmd), c, args[0])
if err != nil {
return err
}
spec := node.Spec.Copy()
flags := cmd.Flags()
if flags.Changed(flagLabel) {
labels, err := flags.GetStringSlice(flagLabel)
if err != nil {
return err
}
// overwrite existing labels
spec.Annotations.Labels = map[string]string{}
for _, l := range labels {
parts := strings.SplitN(l, "=", 2)
if len(parts) != 2 {
return fmt.Errorf("malformed label for node %s", l)
}
spec.Annotations.Labels[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
}
}
if reflect.DeepEqual(spec, &node.Spec) {
return errNoChange
}
_, err = c.UpdateNode(common.Context(cmd), &api.UpdateNodeRequest{
NodeID: node.ID,
NodeVersion: &node.Meta.Version,
Spec: spec,
})
return err
}