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 e083af7
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 8 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
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
89 changes: 89 additions & 0 deletions limitador-server/sandbox/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
## Sandbox Environment

Run Limitador in docker compose environment

### Build limitador image

Build "limitador-testing" image

```
make build
```

### Run

*** Counters are held in Limitador (ephemeral)

```
make deploy-in-memory
```

*** Uses Redis to store counters

```
make deploy-redis
```

*** Uses Redis to store counters, with an in-memory cache

```
make deploy-redis-cached
```

*** Uses disk to store counters

```
make deploy-disk
```

*** Uses Infinispan to store counters

```
make deploy-infinispan
```

### Test HTTP endpoint

Limits

```
curl -v http://127.0.0.1:18080/limits/test_namespace
```

Counters

```
curl -v http://127.0.0.1:18080/counters/test_namespace
```

Metrics

```
curl -v http://127.0.0.1:18080/metrics
```

### Test RLS GRPC 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))

```sh
make grpcurl
```

Run request


```sh
bin/grpcurl -vv -plaintext 127.0.0.1:18081
```

### Clean

Clean environment

```
make clean
```

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
8 changes: 8 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,8 @@ pub fn to_response_header(
headers
}

pub 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 +204,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_DESCRIPTOR_SET)
.build()
.unwrap();

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

0 comments on commit e083af7

Please sign in to comment.