Skip to content

Commit b9852d1

Browse files
yuval-knfuden
andauthored
feat: add callback for route configurations. (#10728)
Co-authored-by: Nathan Fudenberg <[email protected]>
1 parent 68931d3 commit b9852d1

File tree

8 files changed

+60
-24
lines changed

8 files changed

+60
-24
lines changed

internal/kgateway/extensions2/plugins/backend/plugin.go

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func (u *BackendIr) Equals(other any) bool {
8484
}
8585

8686
type backendPlugin struct {
87+
ir.UnimplementedProxyTranslationPass
8788
needFilter map[string]bool
8889
}
8990

internal/kgateway/extensions2/plugins/directresponse/direct_response_plugin.go

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func (d *directResponse) Equals(in any) bool {
4646
}
4747

4848
type directResponsePluginGwPass struct {
49+
ir.UnimplementedProxyTranslationPass
4950
}
5051

5152
func (p *directResponsePluginGwPass) ApplyHCM(ctx context.Context, pCtx *ir.HcmContext, out *envoyhttp.HttpConnectionManager) error {

internal/kgateway/extensions2/plugins/httplistenerpolicy/httplistener_plugin.go

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func (d *httpListenerPolicy) Equals(in any) bool {
5656
}
5757

5858
type httpListenerPolicyPluginGwPass struct {
59+
ir.UnimplementedProxyTranslationPass
5960
}
6061

6162
func (p *httpListenerPolicyPluginGwPass) ApplyListenerPlugin(ctx context.Context, pCtx *ir.ListenerContext, out *envoy_config_listener_v3.Listener) {

internal/kgateway/extensions2/plugins/listenerpolicy/listener_policy_plugin.go

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func (d *listenerPolicy) Equals(in any) bool {
3939
}
4040

4141
type listenerPolicyPluginGwPass struct {
42+
ir.UnimplementedProxyTranslationPass
4243
}
4344

4445
func NewPlugin(ctx context.Context, commoncol *common.CommonCollections) extensionplug.Plugin {

internal/kgateway/extensions2/plugins/routepolicy/route_policy_plugin.go

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func (d *routePolicy) Equals(in any) bool {
3939
}
4040

4141
type routePolicyPluginGwPass struct {
42+
ir.UnimplementedProxyTranslationPass
4243
}
4344

4445
func (p *routePolicyPluginGwPass) ApplyHCM(ctx context.Context, pCtx *ir.HcmContext, out *envoyhttp.HttpConnectionManager) error {

internal/kgateway/ir/iface.go

+45
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ import (
1818
type ListenerContext struct {
1919
Policy PolicyIR
2020
}
21+
22+
type RouteConfigContext struct {
23+
// No policy here, as you can't attach policies to route configs.
24+
// we will call every policy with this to set defaults.
25+
}
26+
2127
type VirtualHostContext struct {
2228
Policy PolicyIR
2329
}
@@ -57,6 +63,12 @@ type ProxyTranslationPass interface {
5763
pCtx *HcmContext,
5864
out *envoy_hcm.HttpConnectionManager) error
5965

66+
// called 1 time for all the routes in a filter chain.
67+
ApplyRouteConfigPlugin(
68+
ctx context.Context,
69+
pCtx *RouteConfigContext,
70+
out *envoy_config_route_v3.RouteConfiguration,
71+
)
6072
ApplyVhostPlugin(
6173
ctx context.Context,
6274
pCtx *VirtualHostContext,
@@ -83,6 +95,39 @@ type ProxyTranslationPass interface {
8395
ResourcesToAdd(ctx context.Context) Resources
8496
}
8597

98+
type UnimplementedProxyTranslationPass struct{}
99+
100+
var _ ProxyTranslationPass = UnimplementedProxyTranslationPass{}
101+
102+
func (s UnimplementedProxyTranslationPass) ApplyListenerPlugin(ctx context.Context, pCtx *ListenerContext, out *envoy_config_listener_v3.Listener) {
103+
}
104+
func (s UnimplementedProxyTranslationPass) ApplyHCM(ctx context.Context, pCtx *HcmContext, out *envoy_hcm.HttpConnectionManager) error {
105+
return nil
106+
}
107+
func (s UnimplementedProxyTranslationPass) ApplyRouteConfigPlugin(ctx context.Context, pCtx *RouteConfigContext, out *envoy_config_route_v3.RouteConfiguration) {
108+
}
109+
110+
func (s UnimplementedProxyTranslationPass) ApplyVhostPlugin(ctx context.Context, pCtx *VirtualHostContext, out *envoy_config_route_v3.VirtualHost) {
111+
}
112+
func (s UnimplementedProxyTranslationPass) ApplyForRoute(ctx context.Context, pCtx *RouteContext, out *envoy_config_route_v3.Route) error {
113+
return nil
114+
}
115+
func (s UnimplementedProxyTranslationPass) ApplyForRouteBackend(ctx context.Context, policy PolicyIR, pCtx *RouteBackendContext) error {
116+
return nil
117+
}
118+
func (s UnimplementedProxyTranslationPass) HttpFilters(ctx context.Context, fc FilterChainCommon) ([]plugins.StagedHttpFilter, error) {
119+
return nil, nil
120+
}
121+
func (s UnimplementedProxyTranslationPass) UpstreamHttpFilters(ctx context.Context) ([]plugins.StagedUpstreamHttpFilter, error) {
122+
return nil, nil
123+
}
124+
func (s UnimplementedProxyTranslationPass) NetworkFilters(ctx context.Context) ([]plugins.StagedNetworkFilter, error) {
125+
return nil, nil
126+
}
127+
func (s UnimplementedProxyTranslationPass) ResourcesToAdd(ctx context.Context) Resources {
128+
return Resources{}
129+
}
130+
86131
type Resources struct {
87132
Clusters []envoy_config_cluster_v3.Cluster
88133
}

internal/kgateway/krtcollections/builtin.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,10 @@ func (d *builtinPlugin) Equals(in any) bool {
4545
// we don't really need equality check here, because this policy is embedded in the httproute,
4646
// and we have generation based equality checks for that already.
4747
return true
48-
// d2, ok := in.(*builtinPlugin)
49-
//
50-
// if !ok {
51-
// return false
52-
// }
53-
//
54-
// // TODO: implement equality check
55-
// return d.spec == d2.spec
5648
}
5749

5850
type builtinPluginGwPass struct {
51+
ir.UnimplementedProxyTranslationPass
5952
}
6053

6154
func (p *builtinPluginGwPass) ApplyHCM(ctx context.Context, pCtx *ir.HcmContext, out *envoyhttp.HttpConnectionManager) error {

internal/kgateway/translator/irtranslator/route.go

+9-16
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,17 @@ type httpRouteConfigurationTranslator struct {
3636
func (h *httpRouteConfigurationTranslator) ComputeRouteConfiguration(ctx context.Context, vhosts []*ir.VirtualHost) *envoy_config_route_v3.RouteConfiguration {
3737
ctx = contextutils.WithLogger(ctx, "compute_route_config."+h.routeConfigName)
3838
cfg := &envoy_config_route_v3.RouteConfiguration{
39-
Name: h.routeConfigName,
40-
VirtualHosts: h.computeVirtualHosts(ctx, vhosts),
39+
Name: h.routeConfigName,
4140
// MaxDirectResponseBodySizeBytes: h.parentListener.GetRouteOptions().GetMaxDirectResponseBodySizeBytes(),
4241
}
42+
for _, pass := range h.PluginPass {
43+
if pass == nil {
44+
continue
45+
}
46+
pass.ApplyRouteConfigPlugin(ctx, &ir.RouteConfigContext{}, cfg)
47+
}
48+
cfg.VirtualHosts = h.computeVirtualHosts(ctx, vhosts)
49+
4350
// Gateway API spec requires that port values in HTTP Host headers be ignored when performing a match
4451
// See https://gateway-api.sigs.k8s.io/reference/spec/#gateway.networking.k8s.io/v1.HTTPRouteSpec - hostnames field
4552
cfg.IgnorePortInHostMatching = true
@@ -94,20 +101,6 @@ func (h *httpRouteConfigurationTranslator) computeVirtualHost(
94101

95102
// run the http plugins that are attached to the listener or gateway on the virtual host
96103
h.runVhostPlugins(ctx, out)
97-
for gvk, pols := range h.listener.AttachedPolicies.Policies {
98-
pass := h.PluginPass[gvk]
99-
if pass == nil {
100-
// TODO: should never happen, log error and report condition
101-
continue
102-
}
103-
for _, pol := range pols {
104-
pctx := &ir.VirtualHostContext{
105-
Policy: pol.PolicyIr,
106-
}
107-
pass.ApplyVhostPlugin(ctx, pctx, out)
108-
// TODO: check return value, if error returned, log error and report condition
109-
}
110-
}
111104

112105
return out
113106
}

0 commit comments

Comments
 (0)