Skip to content

dynamic forward proxy #11197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions api/applyconfiguration/api/v1alpha1/backendspec.go

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

17 changes: 17 additions & 0 deletions api/applyconfiguration/internal/internal.go

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

13 changes: 12 additions & 1 deletion api/v1alpha1/backend_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const (
BackendTypeAWS BackendType = "AWS"
// BackendTypeStatic is the type for static backends.
BackendTypeStatic BackendType = "Static"
// BackendTypeDynamicForwardProxy is the type for dynamic forward proxy backends.
BackendTypeDynamicForwardProxy BackendType = "DynamicForwardProxy"
)

// BackendSpec defines the desired state of Backend.
Expand All @@ -45,10 +47,12 @@ const (
// +kubebuilder:validation:XValidation:message="aws backend must be specified when type is 'aws'",rule="!(!has(self.aws) && self.type == 'AWS')"
// +kubebuilder:validation:XValidation:message="static backend must be nil if the type is not 'static'",rule="!(has(self.static) && self.type != 'Static')"
// +kubebuilder:validation:XValidation:message="static backend must be specified when type is 'static'",rule="!(!has(self.static) && self.type == 'Static')"
// +kubebuilder:validation:XValidation:message="dynamic forward proxy backend must be nil if the type is not 'dynamicForwardProxy'",rule="!(has(self.dynamicForwardProxy) && self.type != 'DynamicForwardProxy')"
// +kubebuilder:validation:XValidation:message="dynamic forward proxy backend must be specified when type is 'dynamicForwardProxy'",rule="!(!has(self.dynamicForwardProxy) && self.type == 'DynamicForwardProxy')"
type BackendSpec struct {
// Type indicates the type of the backend to be used.
// +unionDiscriminator
// +kubebuilder:validation:Enum=AI;AWS;Static
// +kubebuilder:validation:Enum=AI;AWS;Static;DynamicForwardProxy
// +kubebuilder:validation:Required
Type BackendType `json:"type"`
// AI is the AI backend configuration.
Expand All @@ -60,6 +64,13 @@ type BackendSpec struct {
// Static is the static backend configuration.
// +optional
Static *StaticBackend `json:"static,omitempty"`
// DynamicForwardProxy is the dynamic forward proxy backend configuration.
// +optional
DynamicForwardProxy *DynamicForwardProxyBackend `json:"dynamicForwardProxy,omitempty"`
}

// DynamicForwardProxyBackend is the dynamic forward proxy backend configuration.
type DynamicForwardProxyBackend struct {
}

// AwsBackend is the AWS backend configuration.
Expand Down
20 changes: 20 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,8 @@ spec:
required:
- accountId
type: object
dynamicForwardProxy:
type: object
static:
properties:
hosts:
Expand Down Expand Up @@ -546,6 +548,7 @@ spec:
- AI
- AWS
- Static
- DynamicForwardProxy
type: string
required:
- type
Expand All @@ -563,6 +566,12 @@ spec:
rule: '!(has(self.static) && self.type != ''Static'')'
- message: static backend must be specified when type is 'static'
rule: '!(!has(self.static) && self.type == ''Static'')'
- message: dynamic forward proxy backend must be nil if the type is not
'dynamicForwardProxy'
rule: '!(has(self.dynamicForwardProxy) && self.type != ''DynamicForwardProxy'')'
- message: dynamic forward proxy backend must be specified when type is
'dynamicForwardProxy'
rule: '!(!has(self.dynamicForwardProxy) && self.type == ''DynamicForwardProxy'')'
status:
properties:
conditions:
Expand Down
46 changes: 46 additions & 0 deletions internal/kgateway/extensions2/plugins/backend/dfp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package backend

import (
"context"

envoy_config_cluster_v3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
envoy_dfp_cluster "github.com/envoyproxy/go-control-plane/envoy/extensions/clusters/dynamic_forward_proxy/v3"
envoydfp "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/dynamic_forward_proxy/v3"

"github.com/kgateway-dev/kgateway/v2/api/v1alpha1"
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/utils"
)

var (
dfpFilterConfig = &envoydfp.FilterConfig{
ImplementationSpecifier: &envoydfp.FilterConfig_SubClusterConfig{
SubClusterConfig: &envoydfp.SubClusterConfig{},
},
}
)

func processDynamicForwardProxy(ctx context.Context, in *v1alpha1.DynamicForwardProxyBackend, out *envoy_config_cluster_v3.Cluster) error {
out.LbPolicy = envoy_config_cluster_v3.Cluster_CLUSTER_PROVIDED
c := &envoy_dfp_cluster.ClusterConfig{
ClusterImplementationSpecifier: &envoy_dfp_cluster.ClusterConfig_SubClustersConfig{
SubClustersConfig: &envoy_dfp_cluster.SubClustersConfig{
LbPolicy: envoy_config_cluster_v3.Cluster_LEAST_REQUEST,
},
},
}
anyCluster, err := utils.MessageToAny(c)
if err != nil {
return err
}

// the upstream has a DNS name. We need Envoy to resolve the DNS name
// set the type to strict dns
out.ClusterDiscoveryType = &envoy_config_cluster_v3.Cluster_ClusterType{
ClusterType: &envoy_config_cluster_v3.Cluster_CustomClusterType{
Name: "envoy.clusters.dynamic_forward_proxy",
TypedConfig: anyCluster,
},
}

return nil
}
39 changes: 19 additions & 20 deletions internal/kgateway/extensions2/plugins/backend/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (

envoy_config_cluster_v3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
envoy_config_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
envoy_config_listener_v3 "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
envoy_config_route_v3 "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
envoy_ext_proc_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/ext_proc/v3"
envoy_hcm "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3"
envoyauth "github.com/envoyproxy/go-control-plane/envoy/extensions/transport_sockets/tls/v3"
envoywellknown "github.com/envoyproxy/go-control-plane/pkg/wellknown"
"github.com/solo-io/go-utils/contextutils"
Expand Down Expand Up @@ -261,16 +259,16 @@ func processBackend(ctx context.Context, in ir.BackendObjectIR, out *envoy_confi
// TODO(tim): Bubble up error to Backend status once https://github.com/kgateway-dev/kgateway/issues/10555
// is resolved and add test cases for invalid endpoint URLs.
spec := up.Spec
switch {
case spec.Type == v1alpha1.BackendTypeStatic:
switch spec.Type {
case v1alpha1.BackendTypeStatic:
if err := processStatic(ctx, spec.Static, out); err != nil {
log.Error("failed to process static backend", "error", err)
}
case spec.Type == v1alpha1.BackendTypeAWS:
case v1alpha1.BackendTypeAWS:
if err := processAws(ctx, spec.Aws, ir.AwsIr, out); err != nil {
log.Error("failed to process aws backend", "error", err)
}
case spec.Type == v1alpha1.BackendTypeAI:
case v1alpha1.BackendTypeAI:
err := ai.ProcessAIBackend(ctx, spec.AI, ir.AIIr.AISecret, ir.AIIr.AIMultiSecret, out)
if err != nil {
log.Error(err)
Expand All @@ -279,6 +277,10 @@ func processBackend(ctx context.Context, in ir.BackendObjectIR, out *envoy_confi
if err != nil {
log.Error(err)
}
case v1alpha1.BackendTypeDynamicForwardProxy:
if err := processDynamicForwardProxy(ctx, spec.DynamicForwardProxy, out); err != nil {
log.Error("failed to process dynamic forward proxy backend", "error", err)
}
}
return nil
}
Expand Down Expand Up @@ -308,6 +310,7 @@ func processEndpoints(up *v1alpha1.Backend) *ir.EndpointsForBackend {
type backendPlugin struct {
ir.UnimplementedProxyTranslationPass
aiGatewayEnabled map[string]bool
neededDfpFilter map[string]bool
}

func newPlug(ctx context.Context, tctx ir.GwTranslationCtx, reporter reports.Reporter) ir.ProxyTranslationPass {
Expand All @@ -318,20 +321,6 @@ func (p *backendPlugin) Name() string {
return ExtensionName
}

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

func (p *backendPlugin) ApplyHCM(ctx context.Context, pCtx *ir.HcmContext, out *envoy_hcm.HttpConnectionManager) error { // no-op
return nil
}

func (p *backendPlugin) ApplyVhostPlugin(ctx context.Context, pCtx *ir.VirtualHostContext, out *envoy_config_route_v3.VirtualHost) {
}

func (p *backendPlugin) ApplyForRoute(ctx context.Context, pCtx *ir.RouteContext, outputRoute *envoy_config_route_v3.Route) error {
return nil
}

func (p *backendPlugin) ApplyForBackend(ctx context.Context, pCtx *ir.RouteBackendContext, in ir.HttpBackend, out *envoy_config_route_v3.Route) error {
backend := pCtx.Backend.Obj.(*v1alpha1.Backend)
backendIr := pCtx.Backend.ObjIr.(*BackendIr)
Expand All @@ -356,6 +345,11 @@ func (p *backendPlugin) ApplyForBackend(ctx context.Context, pCtx *ir.RouteBacke
},
}
pCtx.TypedFilterConfig.AddTypedConfig(wellknown.AIExtProcFilterName, disabledExtprocSettings)
case v1alpha1.BackendTypeDynamicForwardProxy:
if p.neededDfpFilter == nil {
p.neededDfpFilter = make(map[string]bool)
}
p.neededDfpFilter[pCtx.FilterChainName] = true
}

return nil
Expand Down Expand Up @@ -383,6 +377,11 @@ func (p *backendPlugin) HttpFilters(ctx context.Context, fc ir.FilterChainCommon
}
result = append(result, aiFilters...)
}
if p.neededDfpFilter[fc.FilterChainName] {
pluginStage := plugins.DuringStage(plugins.OutAuthStage)
f, _ := plugins.NewStagedFilter("envoy.filters.http.dynamic_forward_proxy", dfpFilterConfig, pluginStage)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we're ignoring the error, should we use MustNewStagedFilter?

result = append(result, f)
}
return result, errors.Join(errs...)
}

Expand Down
27 changes: 23 additions & 4 deletions pkg/generated/openapi/zz_generated.openapi.go

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

Loading
Loading