forked from joeholley/supergloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routing_rule.go
277 lines (246 loc) · 8.08 KB
/
routing_rule.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package apply
import (
"context"
"fmt"
"sort"
"github.com/solo-io/supergloo/cli/pkg/helpers/clients"
"github.com/ghodss/yaml"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
gloov1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
"github.com/solo-io/go-utils/errors"
skclients "github.com/solo-io/solo-kit/pkg/api/v1/clients"
"github.com/solo-io/supergloo/cli/pkg/flagutils"
"github.com/solo-io/supergloo/cli/pkg/helpers"
"github.com/solo-io/supergloo/cli/pkg/options"
"github.com/solo-io/supergloo/cli/pkg/surveyutils"
v1 "github.com/solo-io/supergloo/pkg/api/v1"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
var routingRuleTypes = []routingRuleSpecCommand{
{
use: "trafficshifting",
alias: "ts",
short: "apply a traffic shifting rule",
long: `Traffic Shifting rules are used to divert HTTP requests sent within the mesh from their original destinations.
This can be used to force traffic to be sent to a specific subset of a service, a different service entirely, and/or
be load-balanced by weight across a variety of destinations`,
specSurveyFunc: surveyutils.SurveyTrafficShiftingSpec,
addFlagsFunc: flagutils.AddTrafficShiftingFlags,
convertSpecFunc: func(in options.RoutingRuleSpec) (*v1.RoutingRuleSpec, error) {
if in.TrafficShifting.Destinations == nil || len(in.TrafficShifting.Destinations.Destinations) == 0 {
return nil, errors.Errorf("must provide at least 1 destination")
}
return &v1.RoutingRuleSpec{
RuleType: &v1.RoutingRuleSpec_TrafficShifting{
TrafficShifting: &v1.TrafficShifting{
Destinations: in.TrafficShifting.Destinations,
},
},
}, nil
},
},
faultInjectionSpecCommand,
retryCommand,
}
func applyRoutingRuleCmd(opts *options.Options) *cobra.Command {
cmd := &cobra.Command{
Use: "routingrule",
Aliases: []string{"rr"},
Short: "Apply a routing rule to one or more meshes.",
Long: `
Each Routing Rule applies an HTTP routing feature to a mesh.
Routing rules implement the following semantics:
RULE:
FOR all HTTP Requests:
- FROM these **source pods**
- TO these **destination pods**
- MATCHING these **request matchers**
APPLY this rule
`,
}
flagutils.AddMetadataFlags(cmd.PersistentFlags(), &opts.Metadata)
flagutils.AddOutputFlag(cmd.PersistentFlags(), &opts.OutputType)
flagutils.AddInteractiveFlag(cmd.PersistentFlags(), &opts.Interactive)
flagutils.AddCreateRoutingRuleFlags(cmd.PersistentFlags(), &opts.CreateRoutingRule)
flagutils.AddKubeYamlFlag(cmd.PersistentFlags(), opts)
for _, rrType := range routingRuleTypes {
cmd.AddCommand(createRoutingRuleSubcmd(rrType, opts))
}
return cmd
}
type routingRuleSpecCommand struct {
use string
alias string
short string
long string
subCmds []routingRuleSpecCommand
mutateOpts func(opts *options.Options)
specSurveyFunc func(ctx context.Context, in *options.CreateRoutingRule) error
addFlagsFunc func(set *pflag.FlagSet, in *options.RoutingRuleSpec)
convertSpecFunc func(in options.RoutingRuleSpec) (*v1.RoutingRuleSpec, error)
updateExistingFunc func(old, new *v1.RoutingRuleSpec)
}
func createRoutingRuleSubcmd(subCmd routingRuleSpecCommand, opts *options.Options) *cobra.Command {
cmd := &cobra.Command{
Use: subCmd.use,
Aliases: []string{subCmd.alias},
Short: subCmd.alias,
Long: subCmd.long,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if subCmd.mutateOpts != nil {
subCmd.mutateOpts(opts)
}
if opts.Interactive {
if err := surveyutils.SurveyMetadata("Routing Rule", &opts.Metadata); err != nil {
return err
}
if err := surveyutils.SurveyRoutingRule(opts.Ctx, &opts.CreateRoutingRule); err != nil {
return err
}
}
return nil
},
PreRunE: func(cmd *cobra.Command, args []string) error {
if opts.Interactive {
if err := subCmd.specSurveyFunc(opts.Ctx, &opts.CreateRoutingRule); err != nil {
return err
}
}
return nil
},
}
if subCmd.convertSpecFunc != nil {
cmd.RunE = func(cmd *cobra.Command, args []string) error {
spec, err := subCmd.convertSpecFunc(opts.CreateRoutingRule.RoutingRuleSpec)
if err != nil {
return err
}
return applyRoutingRule(opts, spec, subCmd.updateExistingFunc)
}
}
if subCmd.addFlagsFunc != nil {
subCmd.addFlagsFunc(cmd.PersistentFlags(), &opts.CreateRoutingRule.RoutingRuleSpec)
}
for _, v := range subCmd.subCmds {
cmd.AddCommand(createRoutingRuleSubcmd(v, opts))
}
return cmd
}
func applyRoutingRule(opts *options.Options, spec *v1.RoutingRuleSpec, updateExistingFunc func(old, new *v1.RoutingRuleSpec)) error {
in, err := routingRuleFromOpts(opts)
if err != nil {
return err
}
in.Spec = spec
rr := clients.MustRoutingRuleClient()
existing, err := rr.Read(in.Metadata.Namespace, in.Metadata.Name, skclients.ReadOpts{Ctx: opts.Ctx})
if err == nil {
// perform update
in.Metadata.ResourceVersion = existing.Metadata.ResourceVersion
if updateExistingFunc != nil {
updateExistingFunc(existing.Spec, in.Spec)
}
}
if opts.PrintKubeYaml {
raw, err := yaml.Marshal(v1.RoutingRuleCrd.KubeResource(in))
if err != nil {
return err
}
fmt.Println(string(raw))
return nil
}
in, err = rr.Write(in, skclients.WriteOpts{Ctx: opts.Ctx, OverwriteExisting: true})
if err != nil {
return err
}
helpers.PrintRoutingRules(v1.RoutingRuleList{in}, opts.OutputType)
return nil
}
func routingRuleFromOpts(opts *options.Options) (*v1.RoutingRule, error) {
matchers, err := convertMatchers(opts.CreateRoutingRule.RequestMatchers)
if err != nil {
return nil, err
}
ss, err := ConvertSelector(opts.CreateRoutingRule.SourceSelector)
if err != nil {
return nil, errors.Wrapf(err, "invalid source selector")
}
ds, err := ConvertSelector(opts.CreateRoutingRule.DestinationSelector)
if err != nil {
return nil, errors.Wrapf(err, "invalid destination selector")
}
if opts.Metadata.Name == "" {
return nil, errors.Errorf("name cannot be empty, provide with --name flag")
}
if opts.CreateRoutingRule.TargetMesh.Name == "" || opts.CreateRoutingRule.TargetMesh.Namespace == "" {
return nil, errors.Errorf("target mesh must be specified, provide with --target-mesh flag")
}
ref := core.ResourceRef(opts.CreateRoutingRule.TargetMesh)
_, meshNotFoundErr := clients.MustMeshClient().Read(ref.Namespace, ref.Name, skclients.ReadOpts{Ctx: opts.Ctx})
if meshNotFoundErr != nil {
return nil, meshNotFoundErr
}
in := &v1.RoutingRule{
Metadata: opts.Metadata,
TargetMesh: &ref,
SourceSelector: ss,
DestinationSelector: ds,
RequestMatchers: matchers,
}
return in, nil
}
func convertMatchers(in options.RequestMatchersValue) ([]*gloov1.Matcher, error) {
var matchers []*gloov1.Matcher
for _, match := range in {
converted, err := matcherFromInput(match)
if err != nil {
return nil, err
}
matchers = append(matchers, converted)
}
return matchers, nil
}
var invalidPathsErr = errors.Errorf("can only set one of path-regex, path-prefix, or path-exact")
func matcherFromInput(input options.RequestMatcher) (*gloov1.Matcher, error) {
m := &gloov1.Matcher{}
switch {
case input.PathExact != "":
if input.PathRegex != "" || input.PathPrefix != "" {
return nil, invalidPathsErr
}
m.PathSpecifier = &gloov1.Matcher_Exact{
Exact: input.PathExact,
}
case input.PathRegex != "":
if input.PathExact != "" || input.PathPrefix != "" {
return nil, invalidPathsErr
}
m.PathSpecifier = &gloov1.Matcher_Regex{
Regex: input.PathRegex,
}
case input.PathPrefix != "":
if input.PathExact != "" || input.PathRegex != "" {
return nil, invalidPathsErr
}
m.PathSpecifier = &gloov1.Matcher_Prefix{
Prefix: input.PathPrefix,
}
default:
return nil, errors.Errorf("must provide path prefix, path exact, or path regex for route matcher")
}
if len(input.Methods) > 0 {
m.Methods = input.Methods
}
for k, v := range input.HeaderMatcher {
m.Headers = append(m.Headers, &gloov1.HeaderMatcher{
Name: k,
Value: v,
Regex: true,
})
}
sort.SliceStable(m.Headers, func(i, j int) bool {
return m.Headers[i].Name < m.Headers[j].Name
})
return m, nil
}