Skip to content

Add support for RequestMirror filter #3306

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 2 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions internal/mode/static/nginx/config/http/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
)

const (
InternalRoutePathPrefix = "/_ngf-internal"
HTTPSScheme = "https"
InternalRoutePathPrefix = "/_ngf-internal"
InternalMirrorRoutePathPrefix = InternalRoutePathPrefix + "-mirror"
HTTPSScheme = "https"
)

// Server holds all configuration for an HTTP server.
Expand Down Expand Up @@ -41,6 +42,7 @@ type Location struct {
Return *Return
ResponseHeaders ResponseHeaders
Rewrites []string
MirrorPaths []string
Includes []shared.Include
GRPC bool
}
Expand Down
38 changes: 25 additions & 13 deletions internal/mode/static/nginx/config/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func createLocations(
matchPairs := make(httpMatchPairs)

var rootPathExists bool
var grpc bool
var grpcServer bool

for pathRuleIdx, rule := range server.PathRules {
matches := make([]routeMatch, 0, len(rule.MatchRules))
Expand All @@ -247,7 +247,7 @@ func createLocations(
}

if rule.GRPC {
grpc = true
grpcServer = true
}

extLocations := initializeExternalLocations(rule, pathsAndTypes)
Expand Down Expand Up @@ -277,7 +277,7 @@ func createLocations(
internalLocations := make([]http.Location, 0, len(rule.MatchRules))

for matchRuleIdx, r := range rule.MatchRules {
intLocation, match := initializeInternalLocation(pathRuleIdx, matchRuleIdx, r.Match, grpc)
intLocation, match := initializeInternalLocation(pathRuleIdx, matchRuleIdx, r.Match, rule.GRPC)
intLocation.Includes = createIncludesFromPolicyGenerateResult(
generator.GenerateForInternalLocation(rule.Policies),
)
Expand Down Expand Up @@ -313,7 +313,7 @@ func createLocations(
locs = append(locs, createDefaultRootLocation())
}

return locs, matchPairs, grpc
return locs, matchPairs, grpcServer
}

func needsInternalLocations(rule dataplane.PathRule) bool {
Expand Down Expand Up @@ -433,6 +433,13 @@ func updateLocation(
return location
}

if strings.HasPrefix(path, http.InternalMirrorRoutePathPrefix) {
location.Type = http.InternalLocationType
if grpc {
location.Rewrites = []string{"^ $request_uri break"}
}
}

location.Includes = append(location.Includes, createIncludesFromLocationSnippetsFilters(filters.SnippetsFilters)...)

if filters.RequestRedirect != nil {
Expand All @@ -445,6 +452,20 @@ func updateLocation(
}

rewrites := createRewritesValForRewriteFilter(filters.RequestURLRewrite, path)
if rewrites != nil {
if location.Type == http.InternalLocationType && rewrites.InternalRewrite != "" {
location.Rewrites = append(location.Rewrites, rewrites.InternalRewrite)
}
if rewrites.MainRewrite != "" {
location.Rewrites = append(location.Rewrites, rewrites.MainRewrite)
}
}

for _, filter := range filters.RequestMirrors {
if filter.Target != nil {
location.MirrorPaths = append(location.MirrorPaths, *filter.Target)
}
}

extraHeaders := make([]http.Header, 0, 3)
if grpc {
Expand All @@ -457,15 +478,6 @@ func updateLocation(
proxySetHeaders := generateProxySetHeaders(&matchRule.Filters, createBaseProxySetHeaders(extraHeaders...))
responseHeaders := generateResponseHeaders(&matchRule.Filters)

if rewrites != nil {
if location.Type == http.InternalLocationType && rewrites.InternalRewrite != "" {
location.Rewrites = append(location.Rewrites, rewrites.InternalRewrite)
}
if rewrites.MainRewrite != "" {
location.Rewrites = append(location.Rewrites, rewrites.MainRewrite)
}
}

location.ProxySetHeaders = proxySetHeaders
location.ProxySSLVerify = createProxyTLSFromBackends(matchRule.BackendGroup.Backends)
proxyPass := createProxyPass(
Expand Down
4 changes: 4 additions & 0 deletions internal/mode/static/nginx/config/servers_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ server {
rewrite {{ $r }};
{{- end }}

{{- range $m := $l.MirrorPaths }}
mirror {{ $m }};
{{- end }}

{{- if $l.Return }}
return {{ $l.Return.Code }} "{{ $l.Return.Body }}";
{{- end }}
Expand Down
130 changes: 116 additions & 14 deletions internal/mode/static/nginx/config/servers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

. "github.com/onsi/gomega"
"github.com/onsi/gomega/format"
"k8s.io/apimachinery/pkg/types"

"github.com/nginx/nginx-gateway-fabric/internal/framework/helpers"
Expand Down Expand Up @@ -862,6 +863,66 @@ func TestCreateServers(t *testing.T) {
},
},
},
{
Path: "/mirror",
PathType: dataplane.PathTypePrefix,
MatchRules: []dataplane.MatchRule{
{
Match: dataplane.Match{},
Filters: dataplane.HTTPFilters{
RequestMirrors: []*dataplane.HTTPRequestMirrorFilter{
{
Name: helpers.GetPointer("mirror-filter"),
Namespace: helpers.GetPointer("test-ns"),
Target: helpers.GetPointer(http.InternalMirrorRoutePathPrefix + "-my-backend"),
},
},
},
BackendGroup: fooGroup,
},
},
},
{
Path: http.InternalMirrorRoutePathPrefix + "-my-backend",
PathType: dataplane.PathTypeExact,
MatchRules: []dataplane.MatchRule{
{
Match: dataplane.Match{},
BackendGroup: fooGroup,
},
},
},
{
Path: "/grpc/mirror",
PathType: dataplane.PathTypeExact,
MatchRules: []dataplane.MatchRule{
{
Match: dataplane.Match{},
Filters: dataplane.HTTPFilters{
RequestMirrors: []*dataplane.HTTPRequestMirrorFilter{
{
Name: helpers.GetPointer("grpc-mirror-filter"),
Namespace: helpers.GetPointer("test-ns"),
Target: helpers.GetPointer(http.InternalMirrorRoutePathPrefix + "-my-grpc-backend"),
},
},
},
BackendGroup: fooGroup,
},
},
GRPC: true,
},
{
Path: http.InternalMirrorRoutePathPrefix + "-my-grpc-backend",
PathType: dataplane.PathTypeExact,
MatchRules: []dataplane.MatchRule{
{
Match: dataplane.Match{},
BackendGroup: fooGroup,
},
},
GRPC: true,
},
{
Path: "/invalid-filter",
PathType: dataplane.PathTypePrefix,
Expand Down Expand Up @@ -1093,25 +1154,25 @@ func TestCreateServers(t *testing.T) {
RedirectPath: "/_ngf-internal-rule8-route0",
},
},
"1_10": {
"1_14": {
{
Headers: []string{"filter:Exact:this"},
RedirectPath: "/_ngf-internal-rule10-route0",
RedirectPath: "/_ngf-internal-rule14-route0",
},
},
"1_12": {
"1_16": {
{
Method: "GET",
RedirectPath: "/_ngf-internal-rule12-route0",
RedirectPath: "/_ngf-internal-rule16-route0",
Headers: nil,
QueryParams: nil,
Any: false,
},
},
"1_17": {
"1_21": {
{
Method: "GET",
RedirectPath: "/_ngf-internal-rule17-route0",
RedirectPath: "/_ngf-internal-rule21-route0",
},
},
}
Expand Down Expand Up @@ -1342,6 +1403,47 @@ func TestCreateServers(t *testing.T) {
Type: http.InternalLocationType,
Includes: internalIncludes,
},
{
Path: "/mirror/",
ProxyPass: "http://test_foo_80$request_uri",
ProxySetHeaders: httpBaseHeaders,
MirrorPaths: []string{"/_ngf-internal-mirror-my-backend"},
Type: http.ExternalLocationType,
Includes: externalIncludes,
},
{
Path: "= /mirror",
ProxyPass: "http://test_foo_80$request_uri",
ProxySetHeaders: httpBaseHeaders,
MirrorPaths: []string{"/_ngf-internal-mirror-my-backend"},
Type: http.ExternalLocationType,
Includes: externalIncludes,
},
{
Path: "= /_ngf-internal-mirror-my-backend",
ProxyPass: "http://test_foo_80$request_uri",
ProxySetHeaders: httpBaseHeaders,
Type: http.InternalLocationType,
Includes: externalIncludes,
},
{
Path: "= /grpc/mirror",
GRPC: true,
ProxyPass: "grpc://test_foo_80",
ProxySetHeaders: grpcBaseHeaders,
MirrorPaths: []string{"/_ngf-internal-mirror-my-grpc-backend"},
Type: http.ExternalLocationType,
Includes: externalIncludes,
},
{
Path: "= /_ngf-internal-mirror-my-grpc-backend",
GRPC: true,
ProxyPass: "grpc://test_foo_80",
Rewrites: []string{"^ $request_uri break"},
ProxySetHeaders: grpcBaseHeaders,
Type: http.InternalLocationType,
Includes: externalIncludes,
},
{
Path: "/invalid-filter/",
Return: &http.Return{
Expand All @@ -1360,18 +1462,18 @@ func TestCreateServers(t *testing.T) {
},
{
Path: "/invalid-filter-with-headers/",
HTTPMatchKey: ssl + "1_10",
HTTPMatchKey: ssl + "1_14",
Type: http.RedirectLocationType,
Includes: externalIncludes,
},
{
Path: "= /invalid-filter-with-headers",
HTTPMatchKey: ssl + "1_10",
HTTPMatchKey: ssl + "1_14",
Type: http.RedirectLocationType,
Includes: externalIncludes,
},
{
Path: "/_ngf-internal-rule10-route0",
Path: "/_ngf-internal-rule14-route0",
Return: &http.Return{
Code: http.StatusInternalServerError,
},
Expand All @@ -1387,12 +1489,12 @@ func TestCreateServers(t *testing.T) {
},
{
Path: "= /test",
HTTPMatchKey: ssl + "1_12",
HTTPMatchKey: ssl + "1_16",
Type: http.RedirectLocationType,
Includes: externalIncludes,
},
{
Path: "/_ngf-internal-rule12-route0",
Path: "/_ngf-internal-rule16-route0",
ProxyPass: "http://test_foo_80$request_uri",
ProxySetHeaders: httpBaseHeaders,
Type: http.InternalLocationType,
Expand Down Expand Up @@ -1471,15 +1573,14 @@ func TestCreateServers(t *testing.T) {
},
{
Path: "= /include-header-match",
HTTPMatchKey: ssl + "1_17",
HTTPMatchKey: ssl + "1_21",
Type: http.RedirectLocationType,
Includes: externalIncludes,
},
{
Path: "/_ngf-internal-rule17-route0",
Path: "/_ngf-internal-rule21-route0",
ProxyPass: "http://test_foo_80$request_uri",
ProxySetHeaders: httpBaseHeaders,
Rewrites: []string{"^ $request_uri break"},
Type: http.InternalLocationType,
Includes: internalIncludes,
},
Expand Down Expand Up @@ -1572,6 +1673,7 @@ func TestCreateServers(t *testing.T) {

result, httpMatchPair := createServers(conf, fakeGenerator, keepAliveCheck)

format.MaxLength = 10000
g.Expect(httpMatchPair).To(Equal(allExpMatchPair))
g.Expect(helpers.Diff(expectedServers, result)).To(BeEmpty())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
HTTPPathValidator
}

func (HTTPValidator) SkipValidation() bool { return false }

Check warning on line 18 in internal/mode/static/nginx/config/validation/http_validator.go

View check run for this annotation

Codecov / codecov/patch

internal/mode/static/nginx/config/validation/http_validator.go#L18

Added line #L18 was not covered by tests

var _ validation.HTTPFieldsValidator = HTTPValidator{}
Loading