Skip to content

Commit

Permalink
Backport of [API Gateway] Various fixes for Config Entry fields into …
Browse files Browse the repository at this point in the history
…release/1.15.x (#16354)

* [API Gateway] Various fixes for Config Entry fields

* simplify logic per PR review

---------

Co-authored-by: Andrew Stucki <[email protected]>
  • Loading branch information
hc-github-team-consul-core and Andrew Stucki authored Feb 22, 2023
1 parent 3d23266 commit fca4b56
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 240 deletions.
17 changes: 7 additions & 10 deletions agent/consul/discoverychain/gateway_httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,14 @@ func httpRouteToDiscoveryChain(route structs.HTTPRouteConfigEntry) (*structs.Ser

for idx, rule := range route.Rules {
modifier := httpRouteFiltersToServiceRouteHeaderModifier(rule.Filters.Headers)
prefixRewrite := httpRouteFiltersToDestinationPrefixRewrite(rule.Filters.URLRewrites)
prefixRewrite := httpRouteFiltersToDestinationPrefixRewrite(rule.Filters.URLRewrite)

var destination structs.ServiceRouteDestination
if len(rule.Services) == 1 {
// TODO open question: is there a use case where someone might want to set the rewrite to ""?
service := rule.Services[0]

servicePrefixRewrite := httpRouteFiltersToDestinationPrefixRewrite(service.Filters.URLRewrites)
if servicePrefixRewrite == "" {
servicePrefixRewrite := httpRouteFiltersToDestinationPrefixRewrite(service.Filters.URLRewrite)
if service.Filters.URLRewrite == nil {
servicePrefixRewrite = prefixRewrite
}
serviceModifier := httpRouteFiltersToServiceRouteHeaderModifier(service.Filters.Headers)
Expand Down Expand Up @@ -176,13 +175,11 @@ func httpRouteToDiscoveryChain(route structs.HTTPRouteConfigEntry) (*structs.Ser
return router, splitters, defaults
}

func httpRouteFiltersToDestinationPrefixRewrite(rewrites []structs.URLRewrite) string {
for _, rewrite := range rewrites {
if rewrite.Path != "" {
return rewrite.Path
}
func httpRouteFiltersToDestinationPrefixRewrite(rewrite *structs.URLRewrite) string {
if rewrite == nil {
return ""
}
return ""
return rewrite.Path
}

// httpRouteFiltersToServiceRouteHeaderModifier will consolidate a list of HTTP filters
Expand Down
15 changes: 5 additions & 10 deletions agent/structs/config_entry_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,14 @@ func validateFilters(filter HTTPFilters) error {
}
}

for i, rewrite := range filter.URLRewrites {
if err := validateURLRewrite(rewrite); err != nil {
return fmt.Errorf("HTTPFilters, URLRewrite[%d], %w", i, err)
}
if err := validateURLRewrite(filter.URLRewrite); err != nil {
return fmt.Errorf("HTTPFilters, URLRewrite, %w", err)
}

return nil
}

func validateURLRewrite(rewrite URLRewrite) error {
func validateURLRewrite(rewrite *URLRewrite) error {
// TODO: we don't really have validation of the actual params
// passed as "PrefixRewrite" in our discoverychain config
// entries, figure out if we should have something here
Expand Down Expand Up @@ -403,8 +401,8 @@ type HTTPQueryMatch struct {
// HTTPFilters specifies a list of filters used to modify a request
// before it is routed to an upstream.
type HTTPFilters struct {
Headers []HTTPHeaderFilter
URLRewrites []URLRewrite
Headers []HTTPHeaderFilter
URLRewrite *URLRewrite
}

// HTTPHeaderFilter specifies how HTTP headers should be modified.
Expand Down Expand Up @@ -554,9 +552,6 @@ func (e *TCPRouteConfigEntry) CanWrite(authz acl.Authorizer) error {
// TCPService is a service reference for a TCPRoute
type TCPService struct {
Name string
// Weight specifies the proportion of requests forwarded to the referenced service.
// This is computed as weight/(sum of all weights in the list of services).
Weight int

acl.EnterpriseMeta
}
Expand Down
12 changes: 6 additions & 6 deletions agent/structs/structs.deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,9 @@ func (o *HTTPRouteConfigEntry) DeepCopy() *HTTPRouteConfigEntry {
}
}
}
if o.Rules[i2].Filters.URLRewrites != nil {
cp.Rules[i2].Filters.URLRewrites = make([]URLRewrite, len(o.Rules[i2].Filters.URLRewrites))
copy(cp.Rules[i2].Filters.URLRewrites, o.Rules[i2].Filters.URLRewrites)
if o.Rules[i2].Filters.URLRewrite != nil {
cp.Rules[i2].Filters.URLRewrite = new(URLRewrite)
*cp.Rules[i2].Filters.URLRewrite = *o.Rules[i2].Filters.URLRewrite
}
if o.Rules[i2].Matches != nil {
cp.Rules[i2].Matches = make([]HTTPMatch, len(o.Rules[i2].Matches))
Expand Down Expand Up @@ -373,9 +373,9 @@ func (o *HTTPRouteConfigEntry) DeepCopy() *HTTPRouteConfigEntry {
}
}
}
if o.Rules[i2].Services[i4].Filters.URLRewrites != nil {
cp.Rules[i2].Services[i4].Filters.URLRewrites = make([]URLRewrite, len(o.Rules[i2].Services[i4].Filters.URLRewrites))
copy(cp.Rules[i2].Services[i4].Filters.URLRewrites, o.Rules[i2].Services[i4].Filters.URLRewrites)
if o.Rules[i2].Services[i4].Filters.URLRewrite != nil {
cp.Rules[i2].Services[i4].Filters.URLRewrite = new(URLRewrite)
*cp.Rules[i2].Services[i4].Filters.URLRewrite = *o.Rules[i2].Services[i4].Filters.URLRewrite
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions api/config_entry_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ func (a *TCPRouteConfigEntry) GetModifyIndex() uint64 { return a.ModifyIndex
// TCPService is a service reference for a TCPRoute
type TCPService struct {
Name string
// Weight specifies the proportion of requests forwarded to the referenced service.
// This is computed as weight/(sum of all weights in the list of services).
Weight int

// Partition is the partition the config entry is associated with.
// Partitioning is a Consul Enterprise feature.
Expand Down Expand Up @@ -195,8 +192,8 @@ type HTTPQueryMatch struct {
// HTTPFilters specifies a list of filters used to modify a request
// before it is routed to an upstream.
type HTTPFilters struct {
Headers []HTTPHeaderFilter
URLRewrites []URLRewrite
Headers []HTTPHeaderFilter
URLRewrite *URLRewrite
}

// HTTPHeaderFilter specifies how HTTP headers should be modified.
Expand Down
26 changes: 8 additions & 18 deletions proto/pbconfigentry/config_entry.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fca4b56

Please sign in to comment.