Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: provisioner idempotence changes #1795

Merged
merged 9 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions backends/src/resource.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use serde::{Deserialize, Serialize};

/// Used by the runner service to send requests to control plane, where the requested resources
/// will be provisioned.
#[derive(Serialize, Deserialize)]
pub struct ResourceRequest {
/// The resource input returned from the runtime::load call.
pub resources: Vec<Vec<u8>>,
/// Used by the control plane to return provisioned resource data to the runner.
#[derive(Serialize, Deserialize, Clone)]
pub struct ResourceResponse {
/// The resource output returned from provisioning.
pub resource: Vec<u8>,
}
3 changes: 3 additions & 0 deletions cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,7 @@ impl Shuttle {
*bytes = serde_json::to_vec(&ShuttleResourceOutput {
output: res,
custom: shuttle_resource.custom,
state: None
})
.unwrap();
}
Expand All @@ -1385,6 +1386,7 @@ impl Shuttle {
*bytes = serde_json::to_vec(&ShuttleResourceOutput {
output: secrets.clone(),
custom: shuttle_resource.custom,
state: None
})
.unwrap();
}
Expand All @@ -1403,6 +1405,7 @@ impl Shuttle {
*bytes = serde_json::to_vec(&ShuttleResourceOutput {
output: res,
custom: shuttle_resource.custom,
state: None
})
.unwrap();
}
Expand Down
2 changes: 1 addition & 1 deletion common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ extract_propagation = [
]
models = ["async-trait", "reqwest", "service"]
persist = ["sqlx", "rand"]
sqlx = ["dep:sqlx", "sqlx/sqlite"]
sqlx = ["dep:sqlx", "sqlx/sqlite", "sqlx/postgres"]
service = ["chrono/serde", "display", "tracing", "tracing-subscriber", "uuid"]
test-utils = ["wiremock"]
tonic = ["dep:tonic"]
Expand Down
45 changes: 43 additions & 2 deletions common/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ pub enum ResourceInput {
Custom(Value),
}

/// The resource state represents the stage of the provisioning process the resource is in.
#[derive(
Debug, Clone, PartialEq, Eq, strum::Display, strum::EnumString, Serialize, Deserialize,
)]
#[strum(serialize_all = "lowercase")]
pub enum ResourceState {
Authorizing,
Provisioning,
Failed,
Ready,
Deleting,
}

/// Returned when provisioning a Shuttle resource
#[derive(Serialize, Deserialize)]
pub struct ShuttleResourceOutput<T> {
Expand All @@ -49,6 +62,9 @@ pub struct ShuttleResourceOutput<T> {

/// Arbitrary extra data in this resource
pub custom: Value,

/// The state of the resource.
pub state: Option<ResourceState>,
}

/// Common type to hold all the information we need for a generic resource
Expand Down Expand Up @@ -136,11 +152,12 @@ mod _sqlx {
use std::{borrow::Cow, str::FromStr};

use sqlx::{
postgres::{PgArgumentBuffer, PgValueRef},
sqlite::{SqliteArgumentValue, SqliteValueRef},
Database, Sqlite,
Database, Postgres, Sqlite,
};

use super::Type;
use super::{ResourceState, Type};

impl<DB: Database> sqlx::Type<DB> for Type
where
Expand All @@ -166,6 +183,30 @@ mod _sqlx {
Self::from_str(value).map_err(Into::into)
}
}

impl<DB: sqlx::Database> sqlx::Type<DB> for ResourceState
where
str: sqlx::Type<DB>,
{
fn type_info() -> <DB as sqlx::Database>::TypeInfo {
<str as sqlx::Type<DB>>::type_info()
}
}

impl<'q> sqlx::Encode<'q, sqlx::Postgres> for ResourceState {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> sqlx::encode::IsNull {
<&str as sqlx::Encode<Postgres>>::encode(&self.to_string(), buf)
}
}

impl<'r> sqlx::Decode<'r, Postgres> for ResourceState {
fn decode(value: PgValueRef<'r>) -> Result<Self, sqlx::error::BoxDynError> {
let value = <&str as sqlx::Decode<Postgres>>::decode(value)?;

let state = ResourceState::from_str(value)?;
Ok(state)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Feels like there should be a better way to do this. 🤔 Not a must.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The better way to do it would be to use an enum on Postgres I feel like.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or doesn't https://users.rust-lang.org/t/sqlx-how-to-deserialize-an-enum-value/107215/4 work ? Though it has to be repeated in the query, so not ideal..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll reconsider this as I implement RDS. We could use an enum, I just want to see if we need to add more states in the RDS PR first (I don't expect that, but it doesn't hurt to wait).

}

#[cfg(test)]
Expand Down
2 changes: 2 additions & 0 deletions deployer/src/deployment/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ async fn provision(
*bytes = serde_json::to_vec(&ShuttleResourceOutput {
output,
custom: shuttle_resource.custom,
state: None
})
.expect("to serialize struct");
}
Expand All @@ -549,6 +550,7 @@ async fn provision(
*bytes = serde_json::to_vec(&ShuttleResourceOutput {
output: new_secrets.clone(),
custom: shuttle_resource.custom,
state: None
})
.expect("to serialize struct");
}
Expand Down