-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
export.go
260 lines (216 loc) · 7.03 KB
/
export.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package export
import (
"errors"
"flag"
"fmt"
"strings"
"github.com/mitchellh/cli"
"github.com/hashicorp/consul/agent"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/flags"
)
func New(ui cli.Ui) *cmd {
c := &cmd{UI: ui}
c.init()
return c
}
type cmd struct {
UI cli.Ui
flags *flag.FlagSet
http *flags.HTTPFlags
help string
serviceName string
peerNames string
partitionNames string
}
func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.flags.StringVar(&c.serviceName, "name", "", "(Required) Specify the name of the service you want to export.")
c.flags.StringVar(&c.peerNames, "consumer-peers", "", "(Required) A comma-separated list of cluster peers to export the service to. In Consul Enterprise, this flag is optional if -consumer-partitions is specified.")
c.flags.StringVar(&c.partitionNames, "consumer-partitions", "", "(Enterprise only) A comma-separated list of admin partitions within the same datacenter to export the service to. This flag is optional if -consumer-peers is specified.")
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.MultiTenancyFlags())
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {
if err := c.flags.Parse(args); err != nil {
return 1
}
if err := c.validateFlags(); err != nil {
c.UI.Error(err.Error())
return 1
}
peerNames, err := c.getPeerNames()
if err != nil {
c.UI.Error(err.Error())
return 1
}
partitionNames, err := c.getPartitionNames()
if err != nil {
c.UI.Error(err.Error())
return 1
}
client, err := c.http.APIClient()
if err != nil {
c.UI.Error(fmt.Sprintf("Error connect to Consul agent: %s", err))
return 1
}
// Name matches partition, so "default" if none specified
cfgName := "default"
if c.http.Partition() != "" {
cfgName = c.http.Partition()
}
entry, _, err := client.ConfigEntries().Get(api.ExportedServices, cfgName, &api.QueryOptions{Namespace: ""})
if err != nil && !strings.Contains(err.Error(), agent.ConfigEntryNotFoundErr) {
c.UI.Error(fmt.Sprintf("Error reading config entry %s/%s: %v", "exported-services", "default", err))
return 1
}
var cfg *api.ExportedServicesConfigEntry
if entry == nil {
cfg = c.initializeConfigEntry(cfgName, peerNames, partitionNames)
} else {
existingCfg, ok := entry.(*api.ExportedServicesConfigEntry)
if !ok {
c.UI.Error(fmt.Sprintf("Existing config entry has incorrect type: %t", entry))
return 1
}
cfg = c.updateConfigEntry(existingCfg, peerNames, partitionNames)
}
ok, _, err := client.ConfigEntries().CAS(cfg, cfg.GetModifyIndex(), nil)
if err != nil {
c.UI.Error(fmt.Sprintf("Error writing config entry: %s", err))
return 1
} else if !ok {
c.UI.Error(fmt.Sprintf("Config entry was changed during update. Please try again"))
return 1
}
switch {
case len(c.peerNames) > 0 && len(c.partitionNames) > 0:
c.UI.Info(fmt.Sprintf("Successfully exported service %q to cluster peers %q and to partitions %q", c.serviceName, c.peerNames, c.partitionNames))
case len(c.peerNames) > 0:
c.UI.Info(fmt.Sprintf("Successfully exported service %q to cluster peers %q", c.serviceName, c.peerNames))
case len(c.partitionNames) > 0:
c.UI.Info(fmt.Sprintf("Successfully exported service %q to partitions %q", c.serviceName, c.partitionNames))
}
return 0
}
func (c *cmd) validateFlags() error {
if c.serviceName == "" {
return errors.New("Missing the required -name flag")
}
if c.peerNames == "" && c.partitionNames == "" {
return errors.New("Missing the required -consumer-peers or -consumer-partitions flag")
}
return nil
}
func (c *cmd) getPeerNames() ([]string, error) {
var peerNames []string
if c.peerNames != "" {
peerNames = strings.Split(c.peerNames, ",")
for _, peerName := range peerNames {
if peerName == "" {
return nil, fmt.Errorf("Invalid peer %q", peerName)
}
}
}
return peerNames, nil
}
func (c *cmd) getPartitionNames() ([]string, error) {
var partitionNames []string
if c.partitionNames != "" {
partitionNames = strings.Split(c.partitionNames, ",")
for _, partitionName := range partitionNames {
if partitionName == "" {
return nil, fmt.Errorf("Invalid partition %q", partitionName)
}
}
}
return partitionNames, nil
}
func (c *cmd) initializeConfigEntry(cfgName string, peerNames, partitionNames []string) *api.ExportedServicesConfigEntry {
return &api.ExportedServicesConfigEntry{
Name: cfgName,
Services: []api.ExportedService{
{
Name: c.serviceName,
Namespace: c.http.Namespace(),
Consumers: buildConsumers(peerNames, partitionNames),
},
},
}
}
func (c *cmd) updateConfigEntry(cfg *api.ExportedServicesConfigEntry, peerNames, partitionNames []string) *api.ExportedServicesConfigEntry {
serviceExists := false
for i, service := range cfg.Services {
if service.Name == c.serviceName && service.Namespace == c.http.Namespace() {
serviceExists = true
// Add a consumer for each peer where one doesn't already exist
for _, peerName := range peerNames {
peerExists := false
for _, consumer := range service.Consumers {
if consumer.Peer == peerName {
peerExists = true
break
}
}
if !peerExists {
cfg.Services[i].Consumers = append(cfg.Services[i].Consumers, api.ServiceConsumer{Peer: peerName})
}
}
// Add a consumer for each partition where one doesn't already exist
for _, partitionName := range partitionNames {
partitionExists := false
for _, consumer := range service.Consumers {
if consumer.Partition == partitionName {
partitionExists = true
break
}
}
if !partitionExists {
cfg.Services[i].Consumers = append(cfg.Services[i].Consumers, api.ServiceConsumer{Partition: partitionName})
}
}
}
}
if !serviceExists {
cfg.Services = append(cfg.Services, api.ExportedService{
Name: c.serviceName,
Namespace: c.http.Namespace(),
Consumers: buildConsumers(peerNames, partitionNames),
})
}
return cfg
}
func buildConsumers(peerNames []string, partitionNames []string) []api.ServiceConsumer {
var consumers []api.ServiceConsumer
for _, peer := range peerNames {
consumers = append(consumers, api.ServiceConsumer{
Peer: peer,
})
}
for _, partition := range partitionNames {
consumers = append(consumers, api.ServiceConsumer{
Partition: partition,
})
}
return consumers
}
//========
func (c *cmd) Synopsis() string {
return synopsis
}
func (c *cmd) Help() string {
return flags.Usage(c.help, nil)
}
const (
synopsis = "Export a service from one peer or admin partition to another"
help = `
Usage: consul services export [options] -name <service name> -consumer-peers <other cluster name>
Export a service to a peered cluster.
$ consul services export -name=web -consumer-peers=other-cluster
Use the -consumer-partitions flag instead of -consumer-peers to export to a different partition in the same cluster.
$ consul services export -name=web -consumer-partitions=other-partition
Additional flags and more advanced use cases are detailed below.
`
)