-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for RequestHeaderModifier for HTTPRouteRule objects (#717)
- Loading branch information
Showing
27 changed files
with
1,250 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Example | ||
|
||
In this example we will deploy NGINX Kubernetes Gateway and configure traffic routing for a simple echo server. | ||
We will use `HTTPRoute` resources to route traffic to the echo server, using the RequestHeaderModifier filter to modify | ||
headers to the request. | ||
## Running the Example | ||
|
||
## 1. Deploy NGINX Kubernetes Gateway | ||
|
||
1. Follow the [installation instructions](/docs/installation.md) to deploy NGINX Gateway. | ||
|
||
1. Save the public IP address of NGINX Kubernetes Gateway into a shell variable: | ||
|
||
``` | ||
GW_IP=XXX.YYY.ZZZ.III | ||
``` | ||
|
||
1. Save the port of NGINX Kubernetes Gateway: | ||
|
||
``` | ||
GW_PORT=<port number> | ||
``` | ||
|
||
## 2. Deploy the Cafe Application | ||
|
||
1. Create the headers Deployment and Service: | ||
|
||
``` | ||
kubectl apply -f headers.yaml | ||
``` | ||
|
||
1. Check that the Pod is running in the `default` namespace: | ||
|
||
``` | ||
kubectl -n default get pods | ||
NAME READY STATUS RESTARTS AGE | ||
headers-6f4b79b975-2sb28 1/1 Running 0 12s | ||
``` | ||
|
||
## 3. Configure Routing | ||
|
||
1. Create the `Gateway`: | ||
|
||
``` | ||
kubectl apply -f gateway.yaml | ||
``` | ||
|
||
1. Create the `HTTPRoute` resources: | ||
|
||
``` | ||
kubectl apply -f echo-route.yaml | ||
``` | ||
|
||
## 4. Test the Application | ||
|
||
To access the application, we will use `curl` to send requests to the `headers` Service, including sending headers with | ||
our request. | ||
Notice our configured header values can be seen in the `requestHeaders` section below, and that the `User-Agent` header | ||
is absent. | ||
|
||
``` | ||
curl -s --resolve echo.example.com:$GW_PORT:$GW_IP http://echo.example.com:$GW_PORT/headers -H "My-Cool-Header:my-client-value" -H "My-Overwrite-Header:dont-see-this" | ||
Headers: | ||
header 'Accept-Encoding' is 'compress' | ||
header 'My-cool-header' is 'my-client-value, this-is-an-appended-value' | ||
header 'My-Overwrite-Header' is 'this-is-the-only-value' | ||
header 'Host' is 'echo.example.com' | ||
header 'Connection' is 'close' | ||
header 'Accept' is '*/*' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
apiVersion: gateway.networking.k8s.io/v1beta1 | ||
kind: HTTPRoute | ||
metadata: | ||
name: headers | ||
spec: | ||
parentRefs: | ||
- name: gateway | ||
sectionName: http | ||
hostnames: | ||
- "echo.example.com" | ||
rules: | ||
- matches: | ||
- path: | ||
type: PathPrefix | ||
value: /headers | ||
filters: | ||
- type: RequestHeaderModifier | ||
requestHeaderModifier: | ||
set: | ||
- name: My-Overwrite-Header | ||
value: this-is-the-only-value | ||
add: | ||
- name: Accept-Encoding | ||
value: compress | ||
- name: My-cool-header | ||
value: this-is-an-appended-value | ||
remove: | ||
- User-Agent | ||
backendRefs: | ||
- name: headers | ||
port: 80 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
apiVersion: gateway.networking.k8s.io/v1beta1 | ||
kind: Gateway | ||
metadata: | ||
name: gateway | ||
labels: | ||
domain: k8s-gateway.nginx.org | ||
spec: | ||
gatewayClassName: nginx | ||
listeners: | ||
- name: http | ||
port: 80 | ||
protocol: HTTP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: headers | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: headers | ||
template: | ||
metadata: | ||
labels: | ||
app: headers | ||
spec: | ||
containers: | ||
- name: headers | ||
image: nginx | ||
ports: | ||
- containerPort: 8080 | ||
volumeMounts: | ||
- name: config-volume | ||
mountPath: /etc/nginx | ||
readOnly: true | ||
volumes: | ||
- name: config-volume | ||
configMap: | ||
name: headers-config | ||
--- | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: headers-config | ||
data: | ||
nginx.conf: |- | ||
user nginx; | ||
worker_processes 1; | ||
pid /var/run/nginx.pid; | ||
load_module /usr/lib/nginx/modules/ngx_http_js_module.so; | ||
events {} | ||
http { | ||
default_type text/plain; | ||
js_import /etc/nginx/headers.js; | ||
js_set $headers headers.getRequestHeaders; | ||
server { | ||
listen 8080; | ||
return 200 "$headers"; | ||
} | ||
} | ||
headers.js: |- | ||
function getRequestHeaders(r) { | ||
let s = "Headers:\n"; | ||
for (let h in r.headersIn) { | ||
s += ` header '${h}' is '${r.headersIn[h]}'\n`; | ||
} | ||
return s; | ||
} | ||
export default {getRequestHeaders}; | ||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: headers | ||
spec: | ||
ports: | ||
- port: 80 | ||
targetPort: 8080 | ||
protocol: TCP | ||
name: http | ||
selector: | ||
app: headers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,5 +42,6 @@ func getExecuteFuncs() []executeFunc { | |
executeUpstreams, | ||
executeSplitClients, | ||
executeServers, | ||
executeMaps, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package config | ||
|
||
import ( | ||
"strings" | ||
gotemplate "text/template" | ||
|
||
"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config/http" | ||
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane" | ||
) | ||
|
||
var mapsTemplate = gotemplate.Must(gotemplate.New("maps").Parse(mapsTemplateText)) | ||
|
||
func executeMaps(conf dataplane.Configuration) []byte { | ||
maps := createMaps(append(conf.HTTPServers, conf.SSLServers...)) | ||
return execute(mapsTemplate, maps) | ||
} | ||
|
||
func createMaps(servers []dataplane.VirtualServer) []http.Map { | ||
return buildAddHeaderMaps(servers) | ||
} | ||
|
||
func buildAddHeaderMaps(servers []dataplane.VirtualServer) []http.Map { | ||
addHeaderNames := make(map[string]struct{}) | ||
|
||
for _, s := range servers { | ||
for _, pr := range s.PathRules { | ||
for _, mr := range pr.MatchRules { | ||
if mr.Filters.RequestHeaderModifiers != nil { | ||
for _, addHeader := range mr.Filters.RequestHeaderModifiers.Add { | ||
lowerName := strings.ToLower(addHeader.Name) | ||
if _, ok := addHeaderNames[lowerName]; !ok { | ||
addHeaderNames[lowerName] = struct{}{} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
maps := make([]http.Map, 0, len(addHeaderNames)) | ||
for m := range addHeaderNames { | ||
maps = append(maps, createAddHeadersMap(m)) | ||
} | ||
return maps | ||
} | ||
|
||
const ( | ||
// In order to prepend any passed client header values to values specified in the add headers field of request | ||
// header modifiers, we need to create a map parameter regex for any string value | ||
anyStringFmt = `~.*` | ||
) | ||
|
||
func createAddHeadersMap(name string) http.Map { | ||
underscoreName := convertStringToSafeVariableName(name) | ||
httpVarSource := "${http_" + underscoreName + "}" | ||
mapVarName := generateAddHeaderMapVariableName(name) | ||
params := []http.MapParameter{ | ||
{ | ||
Value: "default", | ||
Result: "''", | ||
}, | ||
{ | ||
Value: anyStringFmt, | ||
Result: httpVarSource + ",", | ||
}, | ||
} | ||
return http.Map{ | ||
Source: httpVarSource, | ||
Variable: "$" + mapVarName, | ||
Parameters: params, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package config | ||
|
||
var mapsTemplateText = ` | ||
{{ range $m := . }} | ||
map {{ $m.Source }} {{ $m.Variable }} { | ||
{{ range $p := $m.Parameters }} | ||
{{ $p.Value }} {{ $p.Result }}; | ||
{{ end }} | ||
} | ||
{{- end }} | ||
` |
Oops, something went wrong.