| 1 |
# xDS Kubernetes Interop Tests |
| 2 |
|
| 3 |
Proxyless Security Mesh Interop Tests executed on Kubernetes. |
| 4 |
|
| 5 |
### Experimental |
| 6 |
Work in progress. Internal APIs may and will change. Please refrain from making |
| 7 |
changes to this codebase at the moment. |
| 8 |
|
| 9 |
### Stabilization roadmap |
| 10 |
- [x] Replace retrying with tenacity |
| 11 |
- [x] Generate namespace for each test to prevent resource name conflicts and |
| 12 |
allow running tests in parallel |
| 13 |
- [x] Security: run server and client in separate namespaces |
| 14 |
- [ ] Make framework.infrastructure.gcp resources [first-class |
| 15 |
citizen](https://en.wikipedia.org/wiki/First-class_citizen), support |
| 16 |
simpler CRUD |
| 17 |
- [x] Security: manage `roles/iam.workloadIdentityUser` role grant lifecycle for |
| 18 |
dynamically-named namespaces |
| 19 |
- [x] Restructure `framework.test_app` and `framework.xds_k8s*` into a module |
| 20 |
containing xDS-interop-specific logic |
| 21 |
- [ ] Address inline TODOs in code |
| 22 |
- [x] Improve README.md documentation, explain helpers in bin/ folder |
| 23 |
|
| 24 |
## Installation |
| 25 |
|
| 26 |
#### Requirements |
| 27 |
1. Python v3.7+ |
| 28 |
2. [Google Cloud SDK](https://cloud.google.com/sdk/docs/install) |
| 29 |
3. `kubectl` |
| 30 |
|
| 31 |
`kubectl` can be installed via `gcloud components install kubectl`, or system package manager: https://kubernetes.io/docs/tasks/tools/#kubectl |
| 32 |
|
| 33 |
##### Getting Started |
| 34 |
|
| 35 |
1. If you haven't, [initialize](https://cloud.google.com/sdk/docs/install-sdk) gcloud SDK |
| 36 |
2. Activate gcloud [configuration](https://cloud.google.com/sdk/docs/configurations) with your project |
| 37 |
3. Enable gcloud services: |
| 38 |
```shell |
| 39 |
gcloud services enable \ |
| 40 |
compute.googleapis.com \ |
| 41 |
container.googleapis.com \ |
| 42 |
networksecurity.googleapis.com \ |
| 43 |
networkservices.googleapis.com \ |
| 44 |
secretmanager.googleapis.com \ |
| 45 |
trafficdirector.googleapis.com |
| 46 |
``` |
| 47 |
|
| 48 |
#### Configure GKE cluster |
| 49 |
This is an example outlining minimal requirements to run the [baseline tests](xds-baseline-tests). |
| 50 |
|
| 51 |
Update gloud sdk: |
| 52 |
```shell |
| 53 |
gcloud -q components update |
| 54 |
``` |
| 55 |
|
| 56 |
Pre-populate environment variables for convenience. To find project id, refer to |
| 57 |
[Identifying projects](https://cloud.google.com/resource-manager/docs/creating-managing-projects#identifying_projects). |
| 58 |
```shell |
| 59 |
export PROJECT_ID="your-project-id" |
| 60 |
export PROJECT_NUMBER=$(gcloud projects describe "${PROJECT_ID}" --format="value(projectNumber)") |
| 61 |
# Compute Engine default service account |
| 62 |
export GCE_SA="${PROJECT_NUMBER}[email protected]" |
| 63 |
# The prefix to name GCP resources used by the framework |
| 64 |
export RESOURCE_PREFIX="xds-k8s-interop-tests" |
| 65 |
|
| 66 |
# The zone name your cluster, f.e. xds-k8s-test-cluster |
| 67 |
export CLUSTER_NAME="${RESOURCE_PREFIX}-cluster" |
| 68 |
# The zone of your cluster, f.e. us-central1-a |
| 69 |
export ZONE="us-central1-a" |
| 70 |
# Dedicated GCP Service Account to use with workload identity. |
| 71 |
export WORKLOAD_SA_NAME="${RESOURCE_PREFIX}" |
| 72 |
export WORKLOAD_SA_EMAIL="${WORKLOAD_SA_NAME}@${PROJECT_ID}.iam.gserviceaccount.com" |
| 73 |
``` |
| 74 |
|
| 75 |
##### Create the cluster |
| 76 |
Minimal requirements: [VPC-native](https://cloud.google.com/traffic-director/docs/security-proxyless-setup) |
| 77 |
cluster with [Workload Identity](https://cloud.google.com/kubernetes-engine/docs/how-to/workload-identity) enabled. |
| 78 |
```shell |
| 79 |
gcloud container clusters create "${CLUSTER_NAME}" \ |
| 80 |
--scopes=cloud-platform \ |
| 81 |
--zone="${ZONE}" \ |
| 82 |
--enable-ip-alias \ |
| 83 |
--workload-pool="${PROJECT_ID}.svc.id.goog" \ |
| 84 |
--workload-metadata=GKE_METADATA \ |
| 85 |
--tags=allow-health-checks |
| 86 |
``` |
| 87 |
|
| 88 |
For security tests you also need to create CAs and configure the cluster to use those CAs |
| 89 |
as described |
| 90 |
[here](https://cloud.google.com/traffic-director/docs/security-proxyless-setup#configure-cas). |
| 91 |
|
| 92 |
##### Create the firewall rule |
| 93 |
Allow [health checking mechanisms](https://cloud.google.com/traffic-director/docs/set-up-proxyless-gke#creating_the_health_check_firewall_rule_and_backend_service) |
| 94 |
to query the workloads health. |
| 95 |
This step can be skipped, if the driver is executed with `--ensure_firewall`. |
| 96 |
```shell |
| 97 |
gcloud compute firewall-rules create "${RESOURCE_PREFIX}-allow-health-checks" \ |
| 98 |
--network=default --action=allow --direction=INGRESS \ |
| 99 |
--source-ranges="35.191.0.0/16,130.211.0.0/22" \ |
| 100 |
--target-tags=allow-health-checks \ |
| 101 |
--rules=tcp:8080-8100 |
| 102 |
``` |
| 103 |
|
| 104 |
##### Setup GCP Service Account |
| 105 |
|
| 106 |
Create dedicated GCP Service Account to use |
| 107 |
with [workload identity](https://cloud.google.com/kubernetes-engine/docs/how-to/workload-identity). |
| 108 |
|
| 109 |
```shell |
| 110 |
gcloud iam service-accounts create "${WORKLOAD_SA_NAME}" \ |
| 111 |
--display-name="xDS K8S Interop Tests Workload Identity Service Account" |
| 112 |
``` |
| 113 |
|
| 114 |
Enable the service account to [access the Traffic Director API](https://cloud.google.com/traffic-director/docs/prepare-for-envoy-setup#enable-service-account). |
| 115 |
```shell |
| 116 |
gcloud projects add-iam-policy-binding "${PROJECT_ID}" \ |
| 117 |
--member="serviceAccount:${WORKLOAD_SA_EMAIL}" \ |
| 118 |
--role="roles/trafficdirector.client" |
| 119 |
``` |
| 120 |
|
| 121 |
##### Allow access to images |
| 122 |
The test framework needs read access to the client and server images and the bootstrap |
| 123 |
generator image. You may have these images in your project but if you want to use these |
| 124 |
from the grpc-testing project you will have to grant the necessary access to these images |
| 125 |
using https://cloud.google.com/container-registry/docs/access-control#grant or a |
| 126 |
gsutil command. For example, to grant access to images stored in `grpc-testing` project GCR, run: |
| 127 |
|
| 128 |
```sh |
| 129 |
gsutil iam ch "serviceAccount:${GCE_SA}:objectViewer" gs://artifacts.grpc-testing.appspot.com/ |
| 130 |
``` |
| 131 |
|
| 132 |
##### Allow test driver to configure workload identity automatically |
| 133 |
Test driver will automatically grant `roles/iam.workloadIdentityUser` to |
| 134 |
allow the Kubernetes service account to impersonate the dedicated GCP workload |
| 135 |
service account (corresponds to the step 5 |
| 136 |
of [Authenticating to Google Cloud](https://cloud.google.com/kubernetes-engine/docs/how-to/workload-identity#authenticating_to)). |
| 137 |
This action requires the test framework to have `iam.serviceAccounts.create` |
| 138 |
permission on the project. |
| 139 |
|
| 140 |
If you're running test framework locally, and you have `roles/owner` to your |
| 141 |
project, **you can skip this step**. |
| 142 |
If you're configuring the test framework to run on a CI: use `roles/owner` |
| 143 |
account once to allow test framework to grant `roles/iam.workloadIdentityUser`. |
| 144 |
|
| 145 |
```shell |
| 146 |
# Assuming CI is using Compute Engine default service account. |
| 147 |
gcloud projects add-iam-policy-binding "${PROJECT_ID}" \ |
| 148 |
--member="serviceAccount:${GCE_SA}" \ |
| 149 |
--role="roles/iam.serviceAccountAdmin" \ |
| 150 |
--condition-from-file=<(cat <<-END |
| 151 |
--- |
| 152 |
title: allow_workload_identity_only |
| 153 |
description: Restrict serviceAccountAdmin to granting role iam.workloadIdentityUser |
| 154 |
expression: |- |
| 155 |
api.getAttribute('iam.googleapis.com/modifiedGrantsByRole', []) |
| 156 |
.hasOnly(['roles/iam.workloadIdentityUser']) |
| 157 |
END |
| 158 |
) |
| 159 |
``` |
| 160 |
|
| 161 |
##### Configure GKE cluster access |
| 162 |
```shell |
| 163 |
# Unless you're using GCP VM with preconfigured Application Default Credentials, acquire them for your user |
| 164 |
gcloud auth application-default login |
| 165 |
|
| 166 |
# Configuring GKE cluster access for kubectl |
| 167 |
gcloud container clusters get-credentials "${CLUSTER_NAME}" --zone "${ZONE}" |
| 168 |
|
| 169 |
# Save generated kube context name |
| 170 |
export KUBE_CONTEXT="$(kubectl config current-context)" |
| 171 |
``` |
| 172 |
|
| 173 |
#### Install python dependencies |
| 174 |
|
| 175 |
```shell |
| 176 |
# Create python virtual environment |
| 177 |
python3.7 -m venv venv |
| 178 |
|
| 179 |
# Activate virtual environment |
| 180 |
. ./venv/bin/activate |
| 181 |
|
| 182 |
# Install requirements |
| 183 |
pip install -r requirements.txt |
| 184 |
|
| 185 |
# Generate protos |
| 186 |
python -m grpc_tools.protoc --proto_path=../../../ \ |
| 187 |
--python_out=. --grpc_python_out=. \ |
| 188 |
src/proto/grpc/testing/empty.proto \ |
| 189 |
src/proto/grpc/testing/messages.proto \ |
| 190 |
src/proto/grpc/testing/test.proto |
| 191 |
``` |
| 192 |
|
| 193 |
# Basic usage |
| 194 |
|
| 195 |
## Local development |
| 196 |
This test driver allows running tests locally against remote GKE clusters, right |
| 197 |
from your dev environment. You need: |
| 198 |
|
| 199 |
1. Follow [installation](#installation) instructions |
| 200 |
2. Authenticated `gcloud` |
| 201 |
3. `kubectl` context (see [Configure GKE cluster access](#configure-gke-cluster-access)) |
| 202 |
4. Run tests with `--debug_use_port_forwarding` argument. The test driver |
| 203 |
will automatically start and stop port forwarding using |
| 204 |
`kubectl` subprocesses. (experimental) |
| 205 |
|
| 206 |
### Making changes to the driver |
| 207 |
1. Install additional dev packages: `pip install -r requirements-dev.txt` |
| 208 |
2. Use `./bin/yapf.sh` and `./bin/isort.sh` helpers to auto-format code. |
| 209 |
|
| 210 |
### Setup test configuration |
| 211 |
|
| 212 |
There are many arguments to be passed into the test run. You can save the |
| 213 |
arguments to a config file ("flagfile") for your development environment. |
| 214 |
Use [`config/local-dev.cfg.example`](https://github.com/grpc/grpc/blob/master/tools/run_tests/xds_k8s_test_driver/config/local-dev.cfg.example) |
| 215 |
as a starting point: |
| 216 |
|
| 217 |
```shell |
| 218 |
cp config/local-dev.cfg.example config/local-dev.cfg |
| 219 |
``` |
| 220 |
|
| 221 |
If you exported environment variables in the above sections, you can |
| 222 |
template them into the local config (note this recreates the config): |
| 223 |
|
| 224 |
```shell |
| 225 |
envsubst < config/local-dev.cfg.example > config/local-dev.cfg |
| 226 |
``` |
| 227 |
|
| 228 |
Learn more about flagfiles in [abseil documentation](https://abseil.io/docs/python/guides/flags#a-note-about---flagfile). |
| 229 |
|
| 230 |
## Test suites |
| 231 |
|
| 232 |
See the full list of available test suites in the [`tests/`](https://github.com/grpc/grpc/tree/master/tools/run_tests/xds_k8s_test_driver/tests) folder. |
| 233 |
|
| 234 |
### xDS Baseline Tests |
| 235 |
|
| 236 |
Test suite meant to confirm that basic xDS features work as expected. Executing |
| 237 |
it before other test suites will help to identify whether test failure related |
| 238 |
to specific features under test, or caused by unrelated infrastructure |
| 239 |
disturbances. |
| 240 |
|
| 241 |
```shell |
| 242 |
# Help |
| 243 |
python -m tests.baseline_test --help |
| 244 |
python -m tests.baseline_test --helpfull |
| 245 |
|
| 246 |
# Run the baseline test with local-dev.cfg settings |
| 247 |
python -m tests.baseline_test --flagfile="config/local-dev.cfg" |
| 248 |
|
| 249 |
# Same as above, but using the helper script |
| 250 |
./run.sh tests/baseline_test.py |
| 251 |
``` |
| 252 |
|
| 253 |
### xDS Security Tests |
| 254 |
Test suite meant to verify mTLS/TLS features. Note that this requires |
| 255 |
additional environment configuration. For more details, and for the |
| 256 |
setup for the security tests, see |
| 257 |
["Setting up Traffic Director service security with proxyless gRPC"](https://cloud.google.com/traffic-director/docs/security-proxyless-setup) user guide. |
| 258 |
|
| 259 |
```shell |
| 260 |
# Run the security test with local-dev.cfg settings |
| 261 |
python -m tests.security_test --flagfile="config/local-dev.cfg" |
| 262 |
|
| 263 |
# Same as above, but using the helper script |
| 264 |
./run.sh tests/security_test.py |
| 265 |
``` |
| 266 |
|
| 267 |
## Helper scripts |
| 268 |
You can use interop xds-k8s [`bin/`](https://github.com/grpc/grpc/tree/master/tools/run_tests/xds_k8s_test_driver/bin) |
| 269 |
scripts to configure TD, start k8s instances step-by-step, and keep them alive |
| 270 |
for as long as you need. |
| 271 |
|
| 272 |
* To run helper scripts using local config: |
| 273 |
* `python -m bin.script_name --flagfile=config/local-dev.cfg` |
| 274 |
* `./run.sh bin/script_name.py` automatically appends the flagfile |
| 275 |
* Use `--help` to see script-specific argument |
| 276 |
* Use `--helpfull` to see all available argument |
| 277 |
|
| 278 |
#### Overview |
| 279 |
```shell |
| 280 |
# Helper tool to configure Traffic Director with different security options |
| 281 |
python -m bin.run_td_setup --help |
| 282 |
|
| 283 |
# Helper tools to run the test server, client (with or without security) |
| 284 |
python -m bin.run_test_server --help |
| 285 |
python -m bin.run_test_client --help |
| 286 |
|
| 287 |
# Helper tool to verify different security configurations via channelz |
| 288 |
python -m bin.run_channelz --help |
| 289 |
``` |
| 290 |
|
| 291 |
#### `./run.sh` helper |
| 292 |
Use `./run.sh` to execute helper scripts and tests with `config/local-dev.cfg`. |
| 293 |
|
| 294 |
```sh |
| 295 |
USAGE: ./run.sh script_path [arguments] |
| 296 |
script_path: path to python script to execute, relative to driver root folder |
| 297 |
arguments ...: arguments passed to program in sys.argv |
| 298 |
|
| 299 |
ENVIRONMENT: |
| 300 |
XDS_K8S_CONFIG: file path to the config flagfile, relative to |
| 301 |
driver root folder. Default: config/local-dev.cfg |
| 302 |
Will be appended as --flagfile="config_absolute_path" argument |
| 303 |
XDS_K8S_DRIVER_VENV_DIR: the path to python virtual environment directory |
| 304 |
Default: $XDS_K8S_DRIVER_DIR/venv |
| 305 |
DESCRIPTION: |
| 306 |
This tool performs the following: |
| 307 |
1) Ensures python virtual env installed and activated |
| 308 |
2) Exports test driver root in PYTHONPATH |
| 309 |
3) Automatically appends --flagfile="\$XDS_K8S_CONFIG" argument |
| 310 |
|
| 311 |
EXAMPLES: |
| 312 |
./run.sh bin/run_td_setup.py --help |
| 313 |
./run.sh bin/run_td_setup.py --helpfull |
| 314 |
XDS_K8S_CONFIG=./path-to-flagfile.cfg ./run.sh bin/run_td_setup.py --resource_suffix=override-suffix |
| 315 |
./run.sh tests/baseline_test.py |
| 316 |
./run.sh tests/security_test.py --verbosity=1 --logger_levels=__main__:DEBUG,framework:DEBUG |
| 317 |
./run.sh tests/security_test.py SecurityTest.test_mtls --nocheck_local_certs |
| 318 |
``` |
| 319 |
|
| 320 |
## Partial setups |
| 321 |
### Regular workflow |
| 322 |
```shell |
| 323 |
# Setup Traffic Director |
| 324 |
./run.sh bin/run_td_setup.py |
| 325 |
|
| 326 |
# Start test server |
| 327 |
./run.sh bin/run_test_server.py |
| 328 |
|
| 329 |
# Add test server to the backend service |
| 330 |
./run.sh bin/run_td_setup.py --cmd=backends-add |
| 331 |
|
| 332 |
# Start test client |
| 333 |
./run.sh bin/run_test_client.py |
| 334 |
``` |
| 335 |
|
| 336 |
### Secure workflow |
| 337 |
```shell |
| 338 |
# Setup Traffic Director in mtls. See --help for all options |
| 339 |
./run.sh bin/run_td_setup.py --security=mtls |
| 340 |
|
| 341 |
# Start test server in a secure mode |
| 342 |
./run.sh bin/run_test_server.py --secure |
| 343 |
|
| 344 |
# Add test server to the backend service |
| 345 |
./run.sh bin/run_td_setup.py --cmd=backends-add |
| 346 |
|
| 347 |
# Start test client in a secure more --secure |
| 348 |
./run.sh bin/run_test_client.py --secure |
| 349 |
``` |
| 350 |
|
| 351 |
### Sending RPCs |
| 352 |
#### Start port forwarding |
| 353 |
```shell |
| 354 |
# Client: all services always on port 8079 |
| 355 |
kubectl port-forward deployment.apps/psm-grpc-client 8079 |
| 356 |
|
| 357 |
# Server regular mode: all grpc services on port 8080 |
| 358 |
kubectl port-forward deployment.apps/psm-grpc-server 8080 |
| 359 |
# OR |
| 360 |
# Server secure mode: TestServiceImpl is on 8080, |
| 361 |
kubectl port-forward deployment.apps/psm-grpc-server 8080 |
| 362 |
# everything else (channelz, healthcheck, CSDS) on 8081 |
| 363 |
kubectl port-forward deployment.apps/psm-grpc-server 8081 |
| 364 |
``` |
| 365 |
|
| 366 |
#### Send RPCs with grpccurl |
| 367 |
```shell |
| 368 |
# 8081 if security enabled |
| 369 |
export SERVER_ADMIN_PORT=8080 |
| 370 |
|
| 371 |
# List server services using reflection |
| 372 |
grpcurl --plaintext 127.0.0.1:$SERVER_ADMIN_PORT list |
| 373 |
# List client services using reflection |
| 374 |
grpcurl --plaintext 127.0.0.1:8079 list |
| 375 |
|
| 376 |
# List channels via channelz |
| 377 |
grpcurl --plaintext 127.0.0.1:$SERVER_ADMIN_PORT grpc.channelz.v1.Channelz.GetTopChannels |
| 378 |
grpcurl --plaintext 127.0.0.1:8079 grpc.channelz.v1.Channelz.GetTopChannels |
| 379 |
|
| 380 |
# Send GetClientStats to the client |
| 381 |
grpcurl --plaintext -d '{"num_rpcs": 10, "timeout_sec": 30}' 127.0.0.1:8079 \ |
| 382 |
grpc.testing.LoadBalancerStatsService.GetClientStats |
| 383 |
``` |
| 384 |
|
| 385 |
### Cleanup |
| 386 |
* First, make sure to stop port forwarding, if any |
| 387 |
* Run `./bin/cleanup.sh` |
| 388 |
|
| 389 |
##### Partial cleanup |
| 390 |
You can run commands below to stop/start, create/delete resources however you want. |
| 391 |
Generally, it's better to remove resources in the opposite order of their creation. |
| 392 |
|
| 393 |
Cleanup regular resources: |
| 394 |
```shell |
| 395 |
# Cleanup TD resources |
| 396 |
./run.sh bin/run_td_setup.py --cmd=cleanup |
| 397 |
# Stop test client |
| 398 |
./run.sh bin/run_test_client.py --cmd=cleanup |
| 399 |
# Stop test server, and remove the namespace |
| 400 |
./run.sh bin/run_test_server.py --cmd=cleanup --cleanup_namespace |
| 401 |
``` |
| 402 |
|
| 403 |
Cleanup regular and security-specific resources: |
| 404 |
```shell |
| 405 |
# Cleanup TD resources, with security |
| 406 |
./run.sh bin/run_td_setup.py --cmd=cleanup --security=mtls |
| 407 |
# Stop test client (secure) |
| 408 |
./run.sh bin/run_test_client.py --cmd=cleanup --secure |
| 409 |
# Stop test server (secure), and remove the namespace |
| 410 |
./run.sh bin/run_test_server.py --cmd=cleanup --cleanup_namespace --secure |
| 411 |
``` |
| 412 |
|
| 413 |
In addition, here's some other helpful partial cleanup commands: |
| 414 |
```shell |
| 415 |
# Remove all backends from the backend services |
| 416 |
./run.sh bin/run_td_setup.py --cmd=backends-cleanup |
| 417 |
|
| 418 |
# Stop the server, but keep the namespace |
| 419 |
./run.sh bin/run_test_server.py --cmd=cleanup --nocleanup_namespace |
| 420 |
``` |
| 421 |
|
| 422 |
### Known errors |
| 423 |
#### Error forwarding port |
| 424 |
If you stopped a test with `ctrl+c`, while using `--debug_use_port_forwarding`, |
| 425 |
you might see an error like this: |
| 426 |
|
| 427 |
> `framework.infrastructure.k8s.PortForwardingError: Error forwarding port, unexpected output Unable to listen on port 8081: Listeners failed to create with the following errors: [unable to create listener: Error listen tcp4 127.0.0.1:8081: bind: address already in use]` |
| 428 |
|
| 429 |
Unless you're running `kubectl port-forward` manually, it's likely that `ctrl+c` |
| 430 |
interrupted python before it could clean up subprocesses. |
| 431 |
|
| 432 |
You can do `ps aux | grep port-forward` and then kill the processes by id, |
| 433 |
or with `killall kubectl` |