-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathroutes.go
More file actions
256 lines (228 loc) · 8.19 KB
/
routes.go
File metadata and controls
256 lines (228 loc) · 8.19 KB
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
package parser
import (
"fmt"
"strconv"
"strings"
"github.com/drycc/controller-sdk-go/api"
"github.com/drycc/workflow-cli/internal/commands"
"github.com/drycc/workflow-cli/internal/completion"
"github.com/drycc/workflow-cli/internal/template"
"github.com/drycc/workflow-cli/pkg/i18n"
"github.com/spf13/cobra"
)
// NewRoutesCommand creates a command for managing application routes.
func NewRoutesCommand(cmdr *commands.DryccCmd) *cobra.Command {
cmd := &cobra.Command{
Use: "routes",
Short: i18n.T("Manage routes for your applications"),
RunE: func(_ *cobra.Command, _ []string) error {
results, _ := commands.ResponseLimit(limit)
return cmdr.RoutesList(app, results)
},
}
cmd.PersistentFlags().StringVarP(&app, "app", "a", "", i18n.T("The uniquely identifiable name for the application"))
cmd.Flags().IntVarP(&limit, "limit", "l", 0, i18n.T("The maximum number of results to display"))
appCompletion := completion.AppCompletion{ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd.RegisterFlagCompletionFunc("app", appCompletion.CompletionFunc)
cmd.AddCommand(routesListCommand(cmdr))
cmd.AddCommand(routesAddCommand(cmdr))
cmd.AddCommand(routesGetCommand(cmdr))
cmd.AddCommand(routesSetCommand(cmdr))
cmd.AddCommand(routesAttachCommand(cmdr))
cmd.AddCommand(routesDetachCommand(cmdr))
cmd.AddCommand(routesRemoveCommand(cmdr))
return cmd
}
func routesAddCommand(cmdr *commands.DryccCmd) *cobra.Command {
routeKindCompletion := completion.RouteKindCompletion{ArgsLen: 1}
cmd := &cobra.Command{
Use: "add <name> <kind> <backend>...",
Args: cobra.MinimumNArgs(3),
Example: template.CustomExample(
`drycc routes add myroute HTTPRoute svc:8080,100`,
map[string]string{
"<name>": i18n.T("The unique name of the route"),
"<kind>": i18n.T("The route kind, range: HTTPRoute,TCPRoute,UDPRoute,GRPCRoute,TLSRoute"),
"<backend>": i18n.T(`The route's backend, pattern: <service>:<port>,<weight>`),
},
),
Short: i18n.T("Create a route for an application"),
Long: i18n.T("Creates routes for an application, provides a way to route requests"),
ValidArgsFunction: routeKindCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
name := args[0]
kind := args[1]
backends := args[2:]
var backendRefs []api.BackendRefRequest
for _, backend := range backends {
params := strings.Split(backend, ",")
if len(params) != 2 {
return fmt.Errorf("invalid backend format %s", backend)
}
servicePort := strings.Split(params[0], ":")
if len(servicePort) != 2 {
return fmt.Errorf("backend must be in 'service:port,weight' format")
}
port, err := strconv.Atoi(servicePort[1])
if err != nil {
return err
}
weight, err := strconv.Atoi(params[1])
if err != nil {
return err
}
backendRefs = append(backendRefs, api.BackendRefRequest{
Kind: "Service",
Name: servicePort[0],
Port: int32(port),
Weight: int32(weight),
})
}
return cmdr.RoutesCreate(app, name, kind, backendRefs...)
},
}
return cmd
}
func routesListCommand(cmdr *commands.DryccCmd) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: i18n.T("List application routes"),
RunE: func(_ *cobra.Command, _ []string) error {
results, _ := commands.ResponseLimit(limit)
return cmdr.RoutesList(app, results)
},
}
cmd.Flags().IntVarP(&limit, "limit", "l", 0, i18n.T("The maximum number of results to display"))
return cmd
}
func routesGetCommand(cmdr *commands.DryccCmd) *cobra.Command {
routeCompletion := completion.RouteCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "get <name>",
Args: cobra.ExactArgs(1),
Example: template.CustomExample(
"drycc routes get myroute",
map[string]string{
"<name>": i18n.T("The unique name of the route"),
},
),
Short: i18n.T("Get route rules"),
Long: i18n.T("Get route rules for an application"),
ValidArgsFunction: routeCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
name := args[0]
return cmdr.RoutesGet(app, name)
},
}
return cmd
}
func routesSetCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
rulesFile string
}
routeCompletion := completion.RouteCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "set <name>",
Args: cobra.ExactArgs(1),
Example: template.CustomExample(
"drycc routes set myroute --rules-file=rules.yaml",
map[string]string{
"<name>": i18n.T("The unique name of the route"),
},
),
Short: i18n.T("Set route rules"),
Long: i18n.T("Set route rules for an application"),
ValidArgsFunction: routeCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
name := args[0]
return cmdr.RoutesSet(app, name, flags.rulesFile)
},
}
cmd.Flags().StringVar(&flags.rulesFile, "rules-file", "", i18n.T("Rules-file is the file name of rule to apply"))
cmd.MarkFlagRequired("rules-file")
return cmd
}
func routesAttachCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
port int
gateway string
}
routeCompletion := completion.RouteCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "attach <name>",
Args: cobra.ExactArgs(1),
Example: template.CustomExample(
"drycc routes attach myroute --gateway=gw1 --port=80",
map[string]string{
"<name>": i18n.T("The unique name of the route"),
},
),
Short: i18n.T("Attach to gateway"),
ValidArgsFunction: routeCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
name := args[0]
return cmdr.RoutesAttach(app, name, flags.port, flags.gateway)
},
}
cmd.Flags().StringVar(&flags.gateway, "gateway", "", i18n.T("The unique name of the gaetway"))
cmd.Flags().IntVarP(&flags.port, "port", "", 0, i18n.T("Port is the network port, the gateway listener expects to receive"))
mustFlags := []string{"gateway", "port"}
for _, mustFlag := range mustFlags {
cmd.MarkFlagRequired(mustFlag)
}
gatewayNameCompletion := completion.GatewayNameCompletion{AppID: &app, ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd.RegisterFlagCompletionFunc("gateway", gatewayNameCompletion.CompletionFunc)
return cmd
}
func routesDetachCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
port int
gateway string
}
routeCompletion := completion.RouteCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "detach <name>",
Args: cobra.ExactArgs(1),
Example: template.CustomExample(
"drycc routes detach myroute --gateway=gw1 --port=80",
map[string]string{
"<name>": i18n.T("The unique name of the route"),
},
),
Short: i18n.T("Dettach to gateway"),
ValidArgsFunction: routeCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
name := args[0]
return cmdr.RoutesDetach(app, name, flags.port, flags.gateway)
},
}
cmd.Flags().StringVar(&flags.gateway, "gateway", "", i18n.T("The unique name of the gaetway"))
cmd.Flags().IntVarP(&flags.port, "port", "", 0, i18n.T("Port is the network port, the gateway listener expects to receive"))
mustFlags := []string{"port", "protocol"}
for _, mustFlag := range mustFlags {
cmd.MarkFlagRequired(mustFlag)
}
gatewayNameCompletion := completion.GatewayNameCompletion{AppID: &app, ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd.RegisterFlagCompletionFunc("gateway", gatewayNameCompletion.CompletionFunc)
return cmd
}
func routesRemoveCommand(cmdr *commands.DryccCmd) *cobra.Command {
routeCompletion := completion.RouteCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "remove <name>",
Args: cobra.ExactArgs(1),
Example: template.CustomExample(
"drycc routes remove myroute",
map[string]string{
"<name>": i18n.T("The unique name of the route"),
},
),
Short: i18n.T("Remove a route from an application"),
ValidArgsFunction: routeCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
name := args[0]
return cmdr.RoutesRemove(app, name)
},
}
return cmd
}