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

Add documentation for HTTPRoute filters #596

Merged
merged 3 commits into from
Sep 19, 2024
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
2 changes: 1 addition & 1 deletion en/docs/api-management-overview/create-api-using-crs.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
We support API management through a set of Custom Resources (CRs) that adhere to Kubernetes Gateway Specification. See [APK Kubernetes CRD Catalog]({{base_path}}/en/latest/catalogs/kubernetes-crds) for more information.

Additionally, our REST API flow can generate a set of CRs based on the API definitions provided through OpenAPI Specifications, Service Description Languages (SDLs), and APK configuration files. This flow is designed to streamline the API development process and reduce the manual effort required to create basic API related CRs. Follow steps mentions in [Generate APK Configuration File]({{base_path}}/en/latest/api-management-overview/tools-for-api-development/#option-2-generate-k8s-custom-resources-using-config-generator-tool-and-deploy-the-api-using-kubernetes-client) to generate APK configuration files.
Additionally, our REST API flow can generate a set of CRs based on the API definitions provided through OpenAPI Specifications, Service Description Languages (SDLs), and APK configuration files. This flow is designed to streamline the API development process and reduce the manual effort required to create basic API related CRs. Follow the steps mentioned in [Generate APK Configuration File]({{base_path}}/en/latest/api-management-overview/tools-for-api-development/#option-2-generate-k8s-custom-resources-using-config-generator-tool-and-deploy-the-api-using-kubernetes-client) to generate APK configuration files.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ corsConfiguration:
- "*"
```

Sample APK configuration content after the modification is shown below.
Sample APK configuration content after the modification is shown below.

```
name: "EmployeeServiceAPI"
basePath: "/test"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
# Header Modification via CRs

This functionality enables the addition, modification, and removal of request and response headers for APIs. By customizing headers, you can enhance the control and flexibility of API interactions, ensuring that both incoming requests and outgoing responses meet specific requirements.

### Step 1 - Get the CRs for the relevant API configuration

Here, you can follow the steps in [Develop and Deploy a REST API via CRs](../../create-and-deploy-apis/rest/create-rest-api-using-crs.md) documentation and create the CRs to deploy an API from scratch.

Alternatively, you can generate the CRs for a given apk-conf file using the steps as detailed in [this section]({{base_path}}/en/latest/api-management-overview/tools-for-api-development/#option-2-generate-k8s-custom-resources-using-config-generator-tool-and-deploy-the-api-using-kubernetes-client)

### Step 2 - Add the header modification filters to the HTTPRoute CR

Header modification can be done using an HTTPRoute filter as follows.

```
- type: "RequestHeaderModifier"
requestHeaderModifier:
set:
- name: "Set-Request-Header"
value: "Set-Value"
add:
- name: "Add-Request-Header"
value: "Added-Value"
remove:
- "Remove-Request-Header"
```

This filter does the following modifications to the request headers.

1. Update the header named "Set-Request-Header" with the value "Set-Value".
2. Adds a header named "Add-Request-Header" with the value "Added-Value".
3. Removes the header named "Remove-Request-Header".

!!! Note
- By replacing the type with "ResponseHeaderModifier", the modifications can be done to the response.
- Both RequestHeaderModifier and ResponseHeaderModifier can be added to the same rule.

An HTTPRoute with the header modifiers is given below.

```
---
apiVersion: "gateway.networking.k8s.io/v1beta1"
kind: "HTTPRoute"
metadata:
name: "production-httproute"
spec:
hostnames:
- "default.gw.wso2.com"
rules:
- matches:
- path:
type: "RegularExpression"
value: "/employee"
method: "GET"
filters:
- type: "URLRewrite"
urlRewrite:
path:
type: "ReplaceFullPath"
replaceFullPath: "/employee"
- type: "RequestHeaderModifier"
requestHeaderModifier:
set:
- name: "Set-Request-Header"
value: "Test-Value"
add:
- name: "Test-Request-Header"
value: "Test-Value"
remove:
- "Remove-Header"
backendRefs:
- group: "dp.wso2.com"
kind: "Backend"
name: "api-backend"
```

Sample configurations for each of them have been provided under the [Sample Configurations](#sample-configurations) section.

### Step 3 - Deploy the API in APK
You can deploy the API using the following command. Replace <namespace> with the correct namespace.
```
kubectl apply -f . -n <namespace>
```

### Sample Configurations

#### Request Header Modification

##### 1. Add Request Header

```
rules:
- matches:
- path:
type: "RegularExpression"
value: "/employee"
method: "GET"
filters:
- type: "RequestHeaderModifier"
requestHeaderModifier:
add:
- name: "Add-Request-Header"
value: "Added-Value"
```

##### 2. Update Request Header

```
rules:
- matches:
- path:
type: "RegularExpression"
value: "/employee"
method: "GET"
filters:
- type: "RequestHeaderModifier"
requestHeaderModifier:
add:
- name: "Set-Request-Header"
value: "Set-Value"
```

##### 3. Remove Request Header

```
rules:
- matches:
- path:
type: "RegularExpression"
value: "/employee"
method: "GET"
filters:
- type: "RequestHeaderModifier"
requestHeaderModifier:
add:
- name: "Add-Request-Header"
value: "Added-Value"
```

#### Response Header Modification

##### 1. Add Request Header

```
rules:
- matches:
- path:
type: "RegularExpression"
value: "/employee"
method: "GET"
filters:
- type: "ResponseHeaderModifier"
requestHeaderModifier:
add:
- name: "Add-Request-Header"
value: "Added-Value"
```

##### 2. Update Request Header

```
rules:
- matches:
- path:
type: "RegularExpression"
value: "/employee"
method: "GET"
filters:
- type: "ResponseHeaderModifier"
requestHeaderModifier:
add:
- name: "Set-Request-Header"
value: "Set-Value"
```

##### 3. Remove Response Header

```
rules:
- matches:
- path:
type: "RegularExpression"
value: "/employee"
method: "GET"
filters:
- type: "ResponseHeaderModifier"
requestHeaderModifier:
add:
- name: "Add-Request-Header"
value: "Added-Value"
```
Loading