Skip to content

Commit

Permalink
Add support for headers in action return (#5204)
Browse files Browse the repository at this point in the history
* added support for headers in action return

* Added headers for testing

* Matched ErrorPageReturn with ActionReturn, updated tests and generated config

* tests: added tests for headers on action return

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* docs: Updated headers type, minor example changes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
andrew-s and pre-commit-ci[bot] committed May 2, 2024
1 parent 3ce86ae commit 04ca913
Show file tree
Hide file tree
Showing 18 changed files with 347 additions and 48 deletions.
40 changes: 40 additions & 0 deletions config/crd/bases/k8s.nginx.org_virtualserverroutes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ spec:
type: string
code:
type: integer
headers:
items:
description: Header defines an HTTP Header.
properties:
name:
type: string
value:
type: string
type: object
type: array
type:
type: string
type: object
Expand Down Expand Up @@ -276,6 +286,16 @@ spec:
type: string
code:
type: integer
headers:
items:
description: Header defines an HTTP Header.
properties:
name:
type: string
value:
type: string
type: object
type: array
type:
type: string
type: object
Expand Down Expand Up @@ -383,6 +403,16 @@ spec:
type: string
code:
type: integer
headers:
items:
description: Header defines an HTTP Header.
properties:
name:
type: string
value:
type: string
type: object
type: array
type:
type: string
type: object
Expand Down Expand Up @@ -489,6 +519,16 @@ spec:
type: string
code:
type: integer
headers:
items:
description: Header defines an HTTP Header.
properties:
name:
type: string
value:
type: string
type: object
type: array
type:
type: string
type: object
Expand Down
40 changes: 40 additions & 0 deletions config/crd/bases/k8s.nginx.org_virtualservers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ spec:
type: string
code:
type: integer
headers:
items:
description: Header defines an HTTP Header.
properties:
name:
type: string
value:
type: string
type: object
type: array
type:
type: string
type: object
Expand Down Expand Up @@ -338,6 +348,16 @@ spec:
type: string
code:
type: integer
headers:
items:
description: Header defines an HTTP Header.
properties:
name:
type: string
value:
type: string
type: object
type: array
type:
type: string
type: object
Expand Down Expand Up @@ -445,6 +465,16 @@ spec:
type: string
code:
type: integer
headers:
items:
description: Header defines an HTTP Header.
properties:
name:
type: string
value:
type: string
type: object
type: array
type:
type: string
type: object
Expand Down Expand Up @@ -551,6 +581,16 @@ spec:
type: string
code:
type: integer
headers:
items:
description: Header defines an HTTP Header.
properties:
name:
type: string
value:
type: string
type: object
type: array
type:
type: string
type: object
Expand Down
80 changes: 80 additions & 0 deletions deploy/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,16 @@ spec:
type: string
code:
type: integer
headers:
items:
description: Header defines an HTTP Header.
properties:
name:
type: string
value:
type: string
type: object
type: array
type:
type: string
type: object
Expand Down Expand Up @@ -1149,6 +1159,16 @@ spec:
type: string
code:
type: integer
headers:
items:
description: Header defines an HTTP Header.
properties:
name:
type: string
value:
type: string
type: object
type: array
type:
type: string
type: object
Expand Down Expand Up @@ -1256,6 +1276,16 @@ spec:
type: string
code:
type: integer
headers:
items:
description: Header defines an HTTP Header.
properties:
name:
type: string
value:
type: string
type: object
type: array
type:
type: string
type: object
Expand Down Expand Up @@ -1362,6 +1392,16 @@ spec:
type: string
code:
type: integer
headers:
items:
description: Header defines an HTTP Header.
properties:
name:
type: string
value:
type: string
type: object
type: array
type:
type: string
type: object
Expand Down Expand Up @@ -1772,6 +1812,16 @@ spec:
type: string
code:
type: integer
headers:
items:
description: Header defines an HTTP Header.
properties:
name:
type: string
value:
type: string
type: object
type: array
type:
type: string
type: object
Expand Down Expand Up @@ -1900,6 +1950,16 @@ spec:
type: string
code:
type: integer
headers:
items:
description: Header defines an HTTP Header.
properties:
name:
type: string
value:
type: string
type: object
type: array
type:
type: string
type: object
Expand Down Expand Up @@ -2007,6 +2067,16 @@ spec:
type: string
code:
type: integer
headers:
items:
description: Header defines an HTTP Header.
properties:
name:
type: string
value:
type: string
type: object
type: array
type:
type: string
type: object
Expand Down Expand Up @@ -2113,6 +2183,16 @@ spec:
type: string
code:
type: integer
headers:
items:
description: Header defines an HTTP Header.
properties:
name:
type: string
value:
type: string
type: object
type: array
type:
type: string
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,9 @@ return:
code: 200
type: text/plain
body: "Hello World\n"
headers:
- name: x-coffee
value: espresso
```

{{% table %}}
Expand All @@ -592,10 +595,27 @@ return:
|``code`` | The status code of the response. The allowed values are: ``2XX``, ``4XX`` or ``5XX``. The default is ``200``. | ``int`` | No |
|``type`` | The MIME type of the response. The default is ``text/plain``. | ``string`` | No |
|``body`` | The body of the response. Supports NGINX variables*. Variables must be enclosed in curly brackets. For example: ``Request is ${request_uri}\n``. | ``string`` | Yes |
|``headers`` | The custom headers of the response. | [[]Action.Return.Header](#actionreturnheader) | No |
{{% /table %}}

\* -- Supported NGINX variables: `$request_uri`, `$request_method`, `$request_body`, `$scheme`, `$http_`, `$args`, `$arg_`, `$cookie_`, `$host`, `$request_time`, `$request_length`, `$nginx_version`, `$pid`, `$connection`, `$remote_addr`, `$remote_port`, `$time_iso8601`, `$time_local`, `$server_addr`, `$server_port`, `$server_name`, `$server_protocol`, `$connections_active`, `$connections_reading`, `$connections_writing` and `$connections_waiting`.

### Action.Return.Header

The header defines an HTTP Header for a canned response in an actionReturn:

```yaml
name: x-coffee
value: espresso
```

{{% table %}}
|Field | Description | Type | Required |
| ---| ---| ---| --- |
|``name`` | The name of the header. | ``string`` | Yes |
|``value`` | The value of the header. | ``string`` | Yes |
{{% /table %}}

### Action.Proxy

The proxy action passes requests to an upstream with the ability to modify the request/response (for example, rewrite the URI or modify the headers).
Expand Down Expand Up @@ -891,7 +911,7 @@ return:
|``code`` | The status code of the response. The default is the status code of the original response. | ``int`` | No |
|``type`` | The MIME type of the response. The default is ``text/html``. | ``string`` | No |
|``body`` | The body of the response. Supported NGINX variable: ``$upstream_status`` . Variables must be enclosed in curly braces. For example: ``${upstream_status}``. | ``string`` | Yes |
|``headers`` | The custom headers of the response. | [errorPage.Return.Header](#errorpagereturnheader) | No |
|``headers`` | The custom headers of the response. | [[]errorPage.Return.Header](#errorpagereturnheader) | No |
{{% /table %}}

### ErrorPage.Return.Header
Expand Down
1 change: 1 addition & 0 deletions internal/configs/version2/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ type ReturnLocation struct {
Name string
DefaultType string
Return Return
Headers []Header
}

// SplitClient defines a split_clients.
Expand Down
3 changes: 3 additions & 0 deletions internal/configs/version2/nginx-plus.virtualserver.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ server {
{{ range $l := $s.ReturnLocations }}
location {{ $l.Name }} {
default_type "{{ $l.DefaultType }}";
{{ range $h := $l.Headers }}
add_header {{ $h.Name }} "{{ $h.Value }}" always;
{{ end }}
# status code is ignored here, using 0
return 0 "{{ $l.Return.Text }}";
}
Expand Down
3 changes: 3 additions & 0 deletions internal/configs/version2/nginx.virtualserver.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ server {
{{ range $l := $s.ReturnLocations }}
location {{ $l.Name }} {
default_type "{{ $l.DefaultType }}";
{{ range $h := $l.Headers }}
add_header {{ $h.Name }} "{{ $h.Value }}" always;
{{ end }}
# status code is ignored here, using 0
return 0 "{{ $l.Return.Text }}";
}
Expand Down
10 changes: 10 additions & 0 deletions internal/configs/virtualserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2086,6 +2086,15 @@ func generateLocationForReturn(path string, locationSnippets []string, actionRet
code = 200
}

var headers []version2.Header

for _, h := range actionReturn.Headers {
headers = append(headers, version2.Header{
Name: h.Name,
Value: h.Value,
})
}

retLocName := fmt.Sprintf("@return_%d", retLocIndex)

return version2.Location{
Expand All @@ -2107,6 +2116,7 @@ func generateLocationForReturn(path string, locationSnippets []string, actionRet
Return: version2.Return{
Text: actionReturn.Body,
},
Headers: headers,
}
}

Expand Down
Loading

0 comments on commit 04ca913

Please sign in to comment.