Skip to content
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

Backendref httproute filter request redirect #3134

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Empty file added conformance/tests/http
Empty file.
72 changes: 72 additions & 0 deletions conformance/tests/httproute-backend-request-redirect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2022 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package tests

import (
"testing"

"k8s.io/apimachinery/pkg/types"

"sigs.k8s.io/gateway-api/conformance/utils/http"
"sigs.k8s.io/gateway-api/conformance/utils/kubernetes"
"sigs.k8s.io/gateway-api/conformance/utils/suite"
"sigs.k8s.io/gateway-api/pkg/features"
)

func init() {
ConformanceTests = append(ConformanceTests, HTTPRouteBackendPathRedirect)
}

var HTTPRouteBackendPathRedirect = suite.ConformanceTest{
ShortName: "HTTPRouteBackendPathRedirect",
Description: "A single HTTPRoute with a redirection filter for different backends",
Features: []features.SupportedFeature{
features.SupportGateway,
features.SupportHTTPRoute,
features.SupportHTTPRouteBackendPathRedirect,
},
Manifests: []string{"tests/httproute-backend-request-redirect.yaml"},
Test: func(t *testing.T, suite *suite.ConformanceTestSuite) {
ns := "gateway-conformance-infra"
routeNN := types.NamespacedName{Name: "backend-request-redirect", Namespace: ns}
gwNN := types.NamespacedName{Name: "same-namespace", Namespace: ns}
gwAddr := kubernetes.GatewayAndHTTPRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), routeNN)
kubernetes.HTTPRouteMustHaveResolvedRefsConditionsTrue(t, suite.Client, suite.TimeoutConfig, routeNN, gwNN)

testCases := []http.ExpectedResponse{{
Request: http.Request{Path: "/"},
Backend: "infra-backend-v1",
Namespace: ns,
Response: http.Response{StatusCode: 302, Headers: map[string]string{"Location": "https://gateway-api.sigs.k8s.io"}},
}, {
Request: http.Request{Path: "/"},
Backend: "infra-backend-v2",
Namespace: ns,
Response: http.Response{StatusCode: 302, Headers: map[string]string{"Location": "http://www.redirect-example.com"}},
}}

for i := range testCases {
// Declare tc here to avoid loop variable
// reuse issues across parallel tests.
tc := testCases[i]
t.Run(tc.GetTestCaseName(i), func(t *testing.T) {
t.Parallel()
http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, tc)
})
}
},
}
32 changes: 32 additions & 0 deletions conformance/tests/httproute-backend-request-redirect.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: backend-request-redirect
namespace: gateway-conformance-infra
spec:
parentRefs:
- name: same-namespace
rules:
- matches:
- path:
type: PathPrefix
value: /
backendRefs:
- name: infra-backend-v1
port: 8080
filters:
- type: RequestRedirect
requestRedirect:
scheme: https
hostname: redirect-example.com
port: 443
statusCode: 302
- name: infra-backend-v2
port: 8080
filters:
- type: RequestRedirect
requestRedirect:
scheme: http
hostname: redirect-example.com
port: 80
statusCode: 301
4 changes: 4 additions & 0 deletions pkg/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ const (

// This option indicates support for HTTPRoute with a backendref with an appProtoocol 'kubernetes.io/ws' (extended conformance)
SupportHTTPRouteBackendProtocolWebSocket SupportedFeature = "HTTPRouteBackendProtocolWebSocket"

// This option indicates support for HTTPRoute backend path redirect (extended conformance)
SupportHTTPRouteBackendPathRedirect SupportedFeature = "HTTPRouteBackendPathRedirect"
)

// HTTPRouteExtendedFeatures includes all extended features for HTTPRoute
Expand All @@ -172,6 +175,7 @@ var HTTPRouteExtendedFeatures = sets.New(
SupportHTTPRouteBackendRequestHeaderModification,
SupportHTTPRouteBackendProtocolH2C,
SupportHTTPRouteBackendProtocolWebSocket,
SupportHTTPRouteBackendPathRedirect,
)

// -----------------------------------------------------------------------------
Expand Down