|
| 1 | +# Copilot Coding Agent Instructions |
| 2 | + |
| 3 | +This document provides instructions for the GitHub Copilot coding agent when working on this repository. |
| 4 | + |
| 5 | +## Project Structure |
| 6 | + |
| 7 | +- `gateway/` - Gateway components (controller, policy-engine, router) |
| 8 | + - `gateway/gateway-controller/` - Gateway controller service |
| 9 | + - `gateway/policy-engine/` - Policy engine service |
| 10 | + - `gateway/router/` - Envoy-based router |
| 11 | + - `gateway/it/` - Integration tests |
| 12 | +- `kubernetes/` - Kubernetes operator and Helm charts |
| 13 | + - `kubernetes/gateway-operator/` - Gateway Kubernetes operator |
| 14 | + |
| 15 | +## Running Integration Tests |
| 16 | + |
| 17 | +### When modifying code in `./gateway/**` (excluding `./gateway/it/`) |
| 18 | + |
| 19 | +If you change any code in the gateway components (gateway-controller, policy-engine, router, etc.), you MUST: |
| 20 | + |
| 21 | +1. **Rebuild the Docker images** to include your changes: |
| 22 | + ```bash |
| 23 | + cd gateway && make build-local |
| 24 | + ``` |
| 25 | + |
| 26 | +2. **Modify the test configuration** to use the non-coverage images. Edit `gateway/it/docker-compose.test.yaml` line 25 and remove the `-coverage` suffix from the gateway-controller image name: |
| 27 | + - Change: `ghcr.io/wso2/api-platform/gateway-controller-coverage:<version>` |
| 28 | + - To: `ghcr.io/wso2/api-platform/gateway-controller:<version>` |
| 29 | + |
| 30 | + **Important:** Keep the existing version tag (e.g., `0.3.0-SNAPSHOT`) as-is. The version in the file is the current version - do not hardcode a different version. |
| 31 | + |
| 32 | +3. **Run the integration tests**: |
| 33 | + ```bash |
| 34 | + cd gateway/it && make test |
| 35 | + ``` |
| 36 | + |
| 37 | +4. **Revert the gateway-controller image name before committing**. The `-coverage` suffix removal was only for local testing. Before creating a PR, restore the `-coverage` suffix of `gateway/it/docker-compose.test.yaml`: |
| 38 | + - Change: `ghcr.io/wso2/api-platform/gateway-controller:<version>` |
| 39 | + - Back to: `ghcr.io/wso2/api-platform/gateway-controller-coverage:<version>` |
| 40 | + |
| 41 | + **Important:** Do NOT include this image name change in your PR. The CI pipeline requires the coverage image. Other legitimate changes to docker-compose.test.yaml should still be committed. |
| 42 | + |
| 43 | +### When modifying only `./gateway/it/**` (integration test files only) |
| 44 | + |
| 45 | +If you are only modifying integration test files (test cases, test utilities, etc.) and not the gateway component source code: |
| 46 | + |
| 47 | +1. **Modify the test configuration** to use the non-coverage images. Edit `gateway/it/docker-compose.test.yaml` line 25 and remove the `-coverage` suffix from the gateway-controller image name: |
| 48 | + - Change: `ghcr.io/wso2/api-platform/gateway-controller-coverage:<version>` |
| 49 | + - To: `ghcr.io/wso2/api-platform/gateway-controller:<version>` |
| 50 | + |
| 51 | + **Important:** Keep the existing version tag as-is. Do not change the version number. |
| 52 | + |
| 53 | +2. **Run the integration tests**: |
| 54 | + ```bash |
| 55 | + cd gateway/it && make test |
| 56 | + ``` |
| 57 | + |
| 58 | +3. **Revert the gateway-controller image name before committing**. Restore the `-coverage` suffix of `gateway/it/docker-compose.test.yaml`: |
| 59 | + - Change: `ghcr.io/wso2/api-platform/gateway-controller:<version>` |
| 60 | + - Back to: `ghcr.io/wso2/api-platform/gateway-controller-coverage:<version>` |
| 61 | + |
| 62 | + **Important:** Do NOT include this image name change in your PR. Other legitimate changes to docker-compose.test.yaml should still be committed. |
| 63 | + |
| 64 | +Note: The images are pre-built during the setup phase, so you can run tests directly without rebuilding. |
| 65 | + |
| 66 | +## Important Notes |
| 67 | + |
| 68 | +- The `copilot-setup-steps.yml` workflow pre-builds gateway images using `make build-local` |
| 69 | +- The docker-compose file by default references coverage images (for CI coverage reporting) |
| 70 | +- You must switch to non-coverage images when running tests in the Copilot environment |
| 71 | +- **Version tags:** Always use whatever version tag is currently in the docker-compose.test.yaml file. The version may change over time (e.g., `0.3.0-SNAPSHOT`, `0.4.0-SNAPSHOT`, etc.) - never hardcode a specific version |
| 72 | + |
| 73 | +## Build Commands Reference |
| 74 | + |
| 75 | +| Command | Description | Working Directory | |
| 76 | +|---------|-------------|-------------------| |
| 77 | +| `make build-local` | Build all gateway Docker images locally | `gateway/` | |
| 78 | +| `make test` | Run integration tests | `gateway/it/` | |
| 79 | + |
| 80 | +## Policy Configuration in Integration Tests |
| 81 | + |
| 82 | +When writing integration tests for policies, you MUST understand how policy parameters work: |
| 83 | + |
| 84 | +### Policy Parameter Types |
| 85 | + |
| 86 | +Policies have two types of parameters defined in their `policy-definition.yaml` files (located in `gateway/policies/<policy-name>/v<version>/policy-definition.yaml`): |
| 87 | + |
| 88 | +1. **`parameters`** (User Parameters): User-defined parameters that are specified in the API configuration when deploying an API. These are set by API developers via the REST API or API YAML. |
| 89 | + |
| 90 | +2. **`systemParameters`** (System Parameters): Gateway admin-defined parameters that are configured in the gateway configuration file. These are NOT set in the API definition. |
| 91 | + |
| 92 | +### How System Parameters Work |
| 93 | + |
| 94 | +System parameters use the `"wso2/defaultValue"` attribute to reference values from the gateway config file using a JSON path syntax: |
| 95 | + |
| 96 | +```yaml |
| 97 | +# Example from a policy-definition.yaml |
| 98 | +systemParameters: |
| 99 | + type: object |
| 100 | + properties: |
| 101 | + keyManagers: |
| 102 | + type: array |
| 103 | + description: List of key manager definitions |
| 104 | + "wso2/defaultValue": "${config.policy_configurations.jwtauth_v010.keymanagers}" |
| 105 | + jwksCacheTtl: |
| 106 | + type: string |
| 107 | + "wso2/defaultValue": "${config.policy_configurations.jwtauth_v010.jwkscachettl}" |
| 108 | +``` |
| 109 | +
|
| 110 | +The `"wso2/defaultValue"` path `${config.policy_configurations.jwtauth_v010.keymanagers}` tells the gateway to fetch the value from: |
| 111 | + |
| 112 | +```yaml |
| 113 | +# In gateway/it/test-config.yaml |
| 114 | +policy_configurations: |
| 115 | + jwtauth_v010: |
| 116 | + keyManagers: |
| 117 | + - name: test-jwks |
| 118 | + issuer: http://mock-jwks.default.svc.cluster.local:8080/token |
| 119 | + jwks: |
| 120 | + remote: |
| 121 | + uri: http://mock-jwks:8080/jwks |
| 122 | +``` |
| 123 | + |
| 124 | +### Setting Up Policy Tests |
| 125 | + |
| 126 | +When creating integration tests for a policy: |
| 127 | + |
| 128 | +1. **Check the policy definition** at `gateway/policies/<policy-name>/v<version>/policy-definition.yaml` |
| 129 | + |
| 130 | +2. **Identify systemParameters**: Look for properties with `"wso2/defaultValue"` - these need gateway config entries |
| 131 | + |
| 132 | +3. **Add system parameter values to test-config.yaml**: Add the required configuration under `policy_configurations` in `gateway/it/test-config.yaml` following the JSON path from `"wso2/defaultValue"`: |
| 133 | + - Path: `${config.policy_configurations.<policy_id>.<param_name>}` |
| 134 | + - Location: `gateway/it/test-config.yaml` → `policy_configurations` → `<policy_id>` → `<param_name>` |
| 135 | + - Note: The `<policy_id>` in the path typically follows the pattern `<policyname>_v<version>` with underscores (e.g., `jwtauth_v010` for `jwt-auth` v0.1.0) |
| 136 | + |
| 137 | +4. **Use regular parameters in API definitions**: In your feature file's API YAML, only specify user `params` (not system parameters): |
| 138 | + |
| 139 | +```yaml |
| 140 | +# Example API definition in feature file |
| 141 | +operations: |
| 142 | + - method: GET |
| 143 | + path: /protected |
| 144 | + policies: |
| 145 | + - name: jwt-auth |
| 146 | + version: v0.1.0 |
| 147 | + params: # These are user parameters |
| 148 | + issuers: |
| 149 | + - test-jwks |
| 150 | + audiences: |
| 151 | + - my-api |
| 152 | + requiredScopes: |
| 153 | + - read |
| 154 | +``` |
| 155 | + |
| 156 | +### Example: Adding JWT Auth Policy Test |
| 157 | + |
| 158 | +1. **Policy definition** (`gateway/policies/jwt-auth/v0.1.0/policy-definition.yaml`) shows: |
| 159 | + - `parameters`: `issuers`, `audiences`, `requiredScopes`, etc. (user-defined) |
| 160 | + - `systemParameters`: `keyManagers`, `jwksCacheTtl`, etc. with `"wso2/defaultValue"` references |
| 161 | + |
| 162 | +2. **Add to test-config.yaml**: |
| 163 | +```yaml |
| 164 | +policy_configurations: |
| 165 | + jwtauth_v010: |
| 166 | + keyManagers: |
| 167 | + - name: test-jwks |
| 168 | + issuer: http://mock-jwks:8080/token |
| 169 | + jwks: |
| 170 | + remote: |
| 171 | + uri: http://mock-jwks:8080/jwks |
| 172 | + jwkscachettl: "5m" |
| 173 | +``` |
| 174 | + |
| 175 | +3. **In your feature file API definition**, only include user params: |
| 176 | +```yaml |
| 177 | +policies: |
| 178 | + - name: jwt-auth |
| 179 | + version: v0.1.0 |
| 180 | + params: |
| 181 | + issuers: |
| 182 | + - test-jwks |
| 183 | +``` |
| 184 | + |
| 185 | +### Key Points |
| 186 | + |
| 187 | +- **NEVER** put systemParameters in the API definition - they come from the gateway config |
| 188 | +- **ALWAYS** check the policy-definition.yaml for `"wso2/defaultValue"` to know what config entries are needed |
| 189 | +- The config path uses the format: `policy_configurations.<policy_id>.<lowercase_param_name>` |
| 190 | +- If a test fails with missing system parameters, check that `gateway/it/test-config.yaml` has the required `policy_configurations` entries |
| 191 | + |
| 192 | +## Unit Tests |
| 193 | + |
| 194 | +To run unit tests for individual components: |
| 195 | + |
| 196 | +```bash |
| 197 | +# Gateway Controller |
| 198 | +cd gateway/gateway-controller && go test ./... |
| 199 | +
|
| 200 | +# Policy Engine |
| 201 | +cd gateway/policy-engine && go test ./... |
| 202 | +
|
| 203 | +# Router |
| 204 | +cd gateway/router && go test ./... |
| 205 | +
|
| 206 | +# Gateway Operator |
| 207 | +cd kubernetes/gateway-operator && go test ./... |
| 208 | +``` |
0 commit comments