Skip to content

Commit

Permalink
gRPC server reflection
Browse files Browse the repository at this point in the history
Signed-off-by: Eguzki Astiz Lezaun <[email protected]>
  • Loading branch information
eguzki committed Nov 9, 2023
1 parent 82d6b55 commit 39c5cd7
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 9 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions limitador-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ limitador = { path = "../limitador", features = ['lenient_conditions'] }
tokio = { version = "1", features = ["full"] }
thiserror = "1"
tonic = "0.10"
tonic-reflection = "0.10.2"
prost = "0.12"
prost-types = "0.12"
serde_yaml = "0.9"
Expand Down
22 changes: 14 additions & 8 deletions limitador-server/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::env;
use std::error::Error;
use std::path::PathBuf;
use std::process::Command;

fn main() -> Result<(), Box<dyn Error>> {
Expand All @@ -9,14 +11,18 @@ fn main() -> Result<(), Box<dyn Error>> {
}

fn generate_protobuf() -> Result<(), Box<dyn Error>> {
tonic_build::configure().build_server(true).compile(
&["envoy/service/ratelimit/v3/rls.proto"],
&[
"vendor/protobufs/data-plane-api",
"vendor/protobufs/protoc-gen-validate",
"vendor/protobufs/xds",
],
)?;
let original_out_dir = PathBuf::from(env::var("OUT_DIR")?);
tonic_build::configure()
.build_server(true)
.file_descriptor_set_path(original_out_dir.join("rls.bin"))
.compile(
&["envoy/service/ratelimit/v3/rls.proto"],
&[
"vendor/protobufs/data-plane-api",
"vendor/protobufs/protoc-gen-validate",
"vendor/protobufs/xds",
],
)?;
Ok(())
}

Expand Down
75 changes: 74 additions & 1 deletion limitador-server/docs/sandbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,83 @@ Check out `make help` for all the targets.

### Limitador's admin HTTP endpoint

Limits

```bash
curl -i http://127.0.0.1:18080/limits/test_namespace
```

Counters

```bash
curl -i http://127.0.0.1:18080/counters/test_namespace
```

Metrics

```bash
curl -i http://127.0.0.1:18080/metrics
```

### Limitador's GRPC RateLimitService endpoint

Get `grpcurl`. You need [Go SDK](https://golang.org/doc/install) installed.

Golang version >= 1.18 (from [fullstorydev/grpcurl](https://github.com/fullstorydev/grpcurl/blob/v1.8.9/go.mod#L3))

```bash
make grpcurl
```

Inspect `RateLimitService` GRPC service

```bash
bin/grpcurl -plaintext 127.0.0.1:18081 describe envoy.service.ratelimit.v3.RateLimitService
```

Make a custom request

```bash
bin/grpcurl -plaintext -d @ 127.0.0.1:18081 envoy.service.ratelimit.v3.RateLimitService.ShouldRateLimit <<EOM
{
"domain": "test_namespace",
"hits_addend": 1,
"descriptors": [
{
"entries": [
{
"key": "req.method",
"value": "POST"
}
]
}
]
}
EOM
```

Do repeated requests. As the limit is set to max 5 request for 60 seconds,
you should see `OVER_LIMIT` response after 5 requests.

```bash
while :; do bin/grpcurl -plaintext -d @ 127.0.0.1:18081 envoy.service.ratelimit.v3.RateLimitService.ShouldRateLimit <<EOM; sleep 1; done
{
"domain": "test_namespace",
"hits_addend": 1,
"descriptors": [
{
"entries": [
{
"key": "req.method",
"value": "POST"
}
]
}
]
}
EOM
```
### Downstream traffic
**Upstream** service implemented by [httpbin.org](https://httpbin.org/)
Expand Down Expand Up @@ -64,5 +137,5 @@ make deploy-in-memory LIMITADOR_IMAGE=quay.io/kuadrant/limitador:latest
### Tear Down
```bash
make tear-down
make clean
```
1 change: 1 addition & 0 deletions limitador-server/sandbox/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
20 changes: 20 additions & 0 deletions limitador-server/sandbox/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,23 @@ clean: ## clean all containers
- $(DOCKER_COMPOSE) -f docker-compose-envoy.yaml -f docker-compose-limitador-redis.yaml down --volumes --remove-orphans
- $(DOCKER_COMPOSE) -f docker-compose-envoy.yaml -f docker-compose-limitador-redis-cached.yaml down --volumes --remove-orphans
- $(DOCKER_COMPOSE) -f docker-compose-envoy.yaml -f docker-compose-limitador-infinispan.yaml down --volumes --remove-orphans

GRPCURL=$(PROJECT_PATH)/bin/grpcurl
$(GRPCURL):
$(call go-install-tool,$(GRPCURL),github.com/fullstorydev/grpcurl/cmd/[email protected])

.PHONY: grpcurl
grpcurl: $(GRPCURL) ## Download grpcurl locally if necessary.

# go-install-tool will 'go install' any package $2 and install it to $1.
define go-install-tool
@[ -f $(1) ] || { \
set -e ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
go mod init tmp ;\
echo "Downloading $(2)" ;\
GOBIN=$(PROJECT_PATH)/bin go install $(2) ;\
rm -rf $$TMP_DIR ;\
}
endef
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ services:
- "8081"
ports:
- "18080:8080"
- "18081:8081"
volumes:
- ./limits.yaml:/opt/kuadrant/limits/limits.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ services:
- "8081"
ports:
- "18080:8080"
- "18081:8081"
volumes:
- ./limits.yaml:/opt/kuadrant/limits/limits.yaml
infinispan:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ services:
- "8081"
ports:
- "18080:8080"
- "18081:8081"
volumes:
- ./limits.yaml:/opt/kuadrant/limits/limits.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ services:
- "8081"
ports:
- "18080:8080"
- "18081:8081"
volumes:
- ./limits.yaml:/opt/kuadrant/limits/limits.yaml
redis:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ services:
- "8081"
ports:
- "18080:8080"
- "18081:8081"
volumes:
- ./limits.yaml:/opt/kuadrant/limits/limits.yaml
redis:
Expand Down
10 changes: 10 additions & 0 deletions limitador-server/src/envoy_rls/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ pub fn to_response_header(
headers
}

mod rls_proto {
pub(crate) const RLS_DESCRIPTOR_SET: &[u8] = tonic::include_file_descriptor_set!("rls");
}

pub async fn run_envoy_rls_server(
address: String,
limiter: Arc<Limiter>,
Expand All @@ -202,8 +206,14 @@ pub async fn run_envoy_rls_server(
let rate_limiter = MyRateLimiter::new(limiter, rate_limit_headers);
let svc = RateLimitServiceServer::new(rate_limiter);

let reflection_service = tonic_reflection::server::Builder::configure()
.register_encoded_file_descriptor_set(rls_proto::RLS_DESCRIPTOR_SET)
.build()
.unwrap();

Server::builder()
.add_service(svc)
.add_service(reflection_service)
.serve(address.parse().unwrap())
.await
}
Expand Down

0 comments on commit 39c5cd7

Please sign in to comment.