Skip to content

Commit

Permalink
chore(wasmcloud-proto-types): cleanup + nest ctl
Browse files Browse the repository at this point in the history
Signed-off-by: Brooks Townsend <brooksmtownsend@gmail.com>
  • Loading branch information
brooksmtownsend committed Jan 31, 2025
1 parent 09e17e3 commit adfe0ef
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 34 deletions.
11 changes: 4 additions & 7 deletions crates/wasmcloud-proto-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ license.workspace = true
repository.workspace = true

[dependencies]
# Required for generating
proto = { workspace = true }
# Required for using the types
prost = { workspace = true, features = ["derive", "std"] }

# NOTE: not required for this crate, required for consumers of the generated code
# Consider either vendoring these dependencies or removing them, or a feature flag
# Dependencies for the generated code
anyhow = { workspace = true }
async-nats = { workspace = true, features = ["ring"] }
bytes = { workspace = true }
futures = { workspace = true }
prost = { workspace = true, features = ["derive", "std"] }
# Required for generating types
proto = { workspace = true }

[build-dependencies]
prost-build = { workspace = true, features = ["format"] }
Expand Down
1 change: 0 additions & 1 deletion crates/wasmcloud-proto-types/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ It's recommended to set the following configuration option in your rust-analyzer

## Considerations

- [ ] Should the generated types be output to `src/generated`, or just kept in the `OUT_DIR`?
- [ ] Should required dependencies for generated types be included in this crate and vendored, or left up to the client?
4 changes: 3 additions & 1 deletion crates/wasmcloud-proto-types/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use protobuf_nats_service_generator::NatsServiceGenerator;

fn main() -> std::io::Result<()> {
println!("cargo::rerun-if-changed=src/*.rs");
println!("cargo::rerun-if-changed=src/*.proto");

// Find all the .proto files
let protos = std::fs::read_dir("src")?
.filter_map(|entry| {
Expand All @@ -16,7 +19,6 @@ fn main() -> std::io::Result<()> {
.collect::<Vec<_>>();

prost_build::Config::new()
.out_dir("src/generated")
.service_generator(Box::new(NatsServiceGenerator))
.compile_protos(&protos, &["src/"])?;
Ok(())
Expand Down
38 changes: 28 additions & 10 deletions crates/wasmcloud-proto-types/src/ctl.proto
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
syntax = "proto3";

package wasmcloud.types;
package wasmcloud.ctl;

service ControlInterfaceService {
// Request/Response
rpc StartComponent(StartComponentRequest) returns (StartComponentResponse);
rpc ScaleComponent(ScaleComponentRequest) returns (ScaleComponentResponse);
}

// CTL message to start component
message StartComponentRequest {
// OCI image reference
string reference = 1;
// Max number of instances to support scaling to
uint32 max_instances = 2;
message ScaleComponentRequest {
// Image reference for the component
string component_ref = 1;

// Unique identifier of the component to scale
string component_id = 2;

// Optional set of annotations
map<string, string> annotations = 3;

// The maximum number of concurrent executing instances of this component
uint32 max_instances = 4;

// Host ID on which to scale this component
string host_id = 5;

// A list of named configs to use for this component
repeated string config = 6;

// Whether to perform an update if details of the component change
bool allow_update = 7;
}

// CTL message response to start component
message StartComponentResponse {
// ID of the component we started
string component_id = 1;
message ScaleComponentResponse {
// Whether or not the operation was successful
bool success = 1;
// Error message if any
string message = 2;
// ID of the component we started
string component_id = 3;
}
36 changes: 21 additions & 15 deletions crates/wasmcloud-proto-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
// Include the `items` module, which is generated from items.proto.
// It is important to maintain the same structure as in the proto.
pub mod wasmcloud {
pub mod generated {
pub mod ctl {
include!("generated/wasmcloud.types.rs");
include!(concat!(env!("OUT_DIR"), "/wasmcloud.ctl.rs"));
}
// Each .proto should be included as its own module for organization purposes.
}

#[cfg(test)]
mod test {
use super::wasmcloud;
use crate::wasmcloud::ctl::{ControlInterfaceServiceClient, ControlInterfaceServiceServer};
use crate::generated::ctl::{
start_server, ControlInterfaceServiceClient, ControlInterfaceServiceServer,
ScaleComponentRequest, ScaleComponentResponse,
};

struct Host;
impl ControlInterfaceServiceServer for Host {
async fn start_component(
async fn scale_component(
&self,
request: wasmcloud::ctl::StartComponentRequest,
) -> anyhow::Result<wasmcloud::ctl::StartComponentResponse> {
let component_id = format!("{}-{}", request.reference, request.max_instances);
Ok(wasmcloud::ctl::StartComponentResponse {
request: ScaleComponentRequest,
) -> anyhow::Result<ScaleComponentResponse> {
let component_id = format!("{}-{}", request.component_ref, request.max_instances);
Ok(ScaleComponentResponse {
component_id,
success: true,
message: "everything went smoothly and we're going to be okay".to_string(),
})
}
Expand All @@ -29,12 +33,13 @@ mod test {
#[tokio::test]
async fn unit_test_proto() {
let host = Host {};
let request = wasmcloud::ctl::StartComponentRequest {
reference: "test".to_string(),
let request = ScaleComponentRequest {
component_ref: "test".to_string(),
max_instances: 1,
..Default::default()
};
let response = host
.start_component(request)
.scale_component(request)
.await
.expect("host to handle request");

Expand All @@ -54,15 +59,16 @@ mod test {
let host = Host {};
let server = tokio::spawn({
let nats_client = nats_client.clone();
wasmcloud::ctl::start_server(host, nats_client)
start_server(host, nats_client)
.await
.expect("to subscribe and start server")
});

let reply = nats_client
.start_component(crate::wasmcloud::ctl::StartComponentRequest {
reference: "test".to_string(),
.scale_component(ScaleComponentRequest {
component_ref: "test".to_string(),
max_instances: 1,
..Default::default()
})
.await
.expect("should start component");
Expand Down

0 comments on commit adfe0ef

Please sign in to comment.