Skip to content

Commit

Permalink
chore(kuma-cp) add route and virtual host configuration helpers (#2517)…
Browse files Browse the repository at this point in the history
… (#2536)

Add configuration helper adaptors for envoy VirtualHost and
RouteConfiguration types.

Signed-off-by: James Peach <james.peach@konghq.com>
(cherry picked from commit 4012ece)

Co-authored-by: James Peach <james.peach@konghq.com>
  • Loading branch information
mergify[bot] and jpeach authored Aug 9, 2021
1 parent 8e3306c commit 455b335
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 45 deletions.
54 changes: 24 additions & 30 deletions pkg/xds/envoy/routes/configurers.go
Original file line number Diff line number Diff line change
@@ -1,78 +1,72 @@
package routes

import (
envoy_route "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"

mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
envoy_common "github.com/kumahq/kuma/pkg/xds/envoy"
v3 "github.com/kumahq/kuma/pkg/xds/envoy/routes/v3"
)

func CommonVirtualHost(name string) VirtualHostBuilderOpt {
return VirtualHostBuilderOptFunc(func(config *VirtualHostBuilderConfig) {
config.AddV3(&v3.CommonVirtualHostConfigurer{
Name: name,
})
})
return AddVirtualHostConfigurer(
v3.VirtualHostMustConfigureFunc(func(vh *envoy_route.VirtualHost) {
vh.Name = name
vh.Domains = []string{"*"}
}),
)
}

func Routes(routes envoy_common.Routes) VirtualHostBuilderOpt {
return VirtualHostBuilderOptFunc(func(config *VirtualHostBuilderConfig) {
config.AddV3(&v3.RoutesConfigurer{
return AddVirtualHostConfigurer(
&v3.RoutesConfigurer{
Routes: routes,
})
})
}

// Redirect for paths that match to matchPath returns 301 status code with new port and path
func Redirect(matchPath, newPath string, allowGetOnly bool, port uint32) VirtualHostBuilderOpt {
return VirtualHostBuilderOptFunc(func(config *VirtualHostBuilderConfig) {
config.AddV3(&v3.RedirectConfigurer{
MatchPath: matchPath,
NewPath: newPath,
Port: port,
AllowGetOnly: allowGetOnly,
})
return AddVirtualHostConfigurer(&v3.RedirectConfigurer{
MatchPath: matchPath,
NewPath: newPath,
Port: port,
AllowGetOnly: allowGetOnly,
})
}

// ResetTagsHeader adds x-kuma-tags header to the RequestHeadersToRemove list. x-kuma-tags header is planned to be used
// internally, so we don't want to expose it to the destination application.
func ResetTagsHeader() RouteConfigurationBuilderOpt {
return RouteConfigurationBuilderOptFunc(func(config *RouteConfigurationBuilderConfig) {
config.AddV3(&v3.ResetTagsHeaderConfigurer{})
})
return AddRouteConfigurationConfigurer(&v3.ResetTagsHeaderConfigurer{})
}

func TagsHeader(tags mesh_proto.MultiValueTagSet) RouteConfigurationBuilderOpt {
return RouteConfigurationBuilderOptFunc(func(config *RouteConfigurationBuilderConfig) {
config.AddV3(&v3.TagsHeaderConfigurer{
return AddRouteConfigurationConfigurer(
&v3.TagsHeaderConfigurer{
Tags: tags,
})
})
}

func Route(matchPath, newPath, cluster string, allowGetOnly bool) VirtualHostBuilderOpt {
return VirtualHostBuilderOptFunc(func(config *VirtualHostBuilderConfig) {
config.AddV3(&v3.RouteConfigurer{
return AddVirtualHostConfigurer(
&v3.RouteConfigurer{
MatchPath: matchPath,
NewPath: newPath,
Cluster: cluster,
AllowGetOnly: allowGetOnly,
})
})
}

func VirtualHost(builder *VirtualHostBuilder) RouteConfigurationBuilderOpt {
return RouteConfigurationBuilderOptFunc(func(config *RouteConfigurationBuilderConfig) {
config.AddV3(&RouteConfigurationVirtualHostConfigurerV3{
return AddRouteConfigurationConfigurer(
&RouteConfigurationVirtualHostConfigurerV3{
builder: builder,
})
})
}

func CommonRouteConfiguration(name string) RouteConfigurationBuilderOpt {
return RouteConfigurationBuilderOptFunc(func(config *RouteConfigurationBuilderConfig) {
config.AddV3(&v3.CommonRouteConfigurationConfigurer{
return AddRouteConfigurationConfigurer(
&v3.CommonRouteConfigurationConfigurer{
Name: name,
})
})
}
8 changes: 8 additions & 0 deletions pkg/xds/envoy/routes/route_configuration_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,11 @@ type RouteConfigurationBuilderOptFunc func(config *RouteConfigurationBuilderConf
func (f RouteConfigurationBuilderOptFunc) ApplyTo(config *RouteConfigurationBuilderConfig) {
f(config)
}

// AddRouteConfigurationConfigurer produces an option that adds the given
// configurer to the route coonfiguration builder.
func AddRouteConfigurationConfigurer(c v3.RouteConfigurationConfigurer) RouteConfigurationBuilderOpt {
return RouteConfigurationBuilderOptFunc(func(config *RouteConfigurationBuilderConfig) {
config.AddV3(c)
})
}
15 changes: 0 additions & 15 deletions pkg/xds/envoy/routes/v3/common_virtual_host_configurer.go

This file was deleted.

48 changes: 48 additions & 0 deletions pkg/xds/envoy/routes/v3/configurer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,57 @@ type VirtualHostConfigurer interface {
Configure(virtualHost *envoy_route.VirtualHost) error
}

// VirtualHostConfigureFunc adapts a configuration function to the
// VirtualHostConfigurer interface.
type VirtualHostConfigureFunc func(vh *envoy_route.VirtualHost) error

func (f VirtualHostConfigureFunc) Configure(vh *envoy_route.VirtualHost) error {
if f != nil {
return f(vh)
}

return nil
}

// VirtualHostMustConfigureFunc adapts a configuration function that
// never fails to the VirtualHostConfigurer interface.
type VirtualHostMustConfigureFunc func(vh *envoy_route.VirtualHost)

func (f VirtualHostMustConfigureFunc) Configure(vh *envoy_route.VirtualHost) error {
if f != nil {
f(vh)
}

return nil
}

// RouteConfigurationConfigurer is responsible for configuring a single aspect of the entire Envoy RouteConfiguration,
// such as VirtualHost, HTTP headers to add or remove, etc.
type RouteConfigurationConfigurer interface {
// Configure configures a single aspect on a given Envoy RouteConfiguration.
Configure(routeConfiguration *envoy_route.RouteConfiguration) error
}

// RouteConfigurationConfigureFunc adapts a configuration function to the
// RouteConfigurationConfigurer interface.
type RouteConfigurationConfigureFunc func(rc *envoy_route.RouteConfiguration) error

func (f RouteConfigurationConfigureFunc) Configure(rc *envoy_route.RouteConfiguration) error {
if f != nil {
return f(rc)
}

return nil
}

// RouteConfigurationMustConfigureFunc adapts a configuration function that
// never fails to the RouteConfigurationConfigurer interface.
type RouteConfigurationMustConfigureFunc func(rc *envoy_route.RouteConfiguration)

func (f RouteConfigurationMustConfigureFunc) Configure(rc *envoy_route.RouteConfiguration) error {
if f != nil {
f(rc)
}

return nil
}
8 changes: 8 additions & 0 deletions pkg/xds/envoy/routes/virtual_host_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,11 @@ type VirtualHostBuilderOptFunc func(config *VirtualHostBuilderConfig)
func (f VirtualHostBuilderOptFunc) ApplyTo(config *VirtualHostBuilderConfig) {
f(config)
}

// AddVirtualHostConfigurer production an option that adds the given
// configurer to the virtual host builder.
func AddVirtualHostConfigurer(c v3.VirtualHostConfigurer) VirtualHostBuilderOpt {
return VirtualHostBuilderOptFunc(func(config *VirtualHostBuilderConfig) {
config.AddV3(c)
})
}

0 comments on commit 455b335

Please sign in to comment.