Skip to content

Commit

Permalink
Allow backend id to be passed in spawn request (#591)
Browse files Browse the repository at this point in the history
* Allow backend id to be passed in spawn request

* fix tests

* fix typo
  • Loading branch information
paulgb authored Jan 31, 2024
1 parent 7041219 commit 36a8950
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 18 deletions.
1 change: 1 addition & 0 deletions plane/plane-tests/tests/backend_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod common;
fn connect_request(cluster: &ClusterName) -> ConnectRequest {
ConnectRequest {
spawn_config: Some(SpawnConfig {
id: None,
cluster: Some(cluster.clone()),
executable: ExecutorConfig::from_image_with_defaults("alpine"),
lifetime_limit_seconds: None,
Expand Down
1 change: 1 addition & 0 deletions plane/plane-tests/tests/backend_lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ async fn backend_lifecycle(env: TestEnvironment) {
tracing::info!("Requesting backend.");
let connect_request = ConnectRequest {
spawn_config: Some(SpawnConfig {
id: None,
cluster: Some(env.cluster.clone()),
executable: ExecutorConfig {
image: "ghcr.io/drifting-in-space/demo-image-drop-four".to_string(),
Expand Down
1 change: 1 addition & 0 deletions plane/plane-tests/tests/backend_status_in_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async fn backend_status_in_response(env: TestEnvironment) {
tracing::info!("Requesting backend.");
let connect_request = ConnectRequest {
spawn_config: Some(SpawnConfig {
id: None,
cluster: Some(env.cluster.clone()),
executable: ExecutorConfig {
image: "ghcr.io/drifting-in-space/demo-image-drop-four".to_string(),
Expand Down
1 change: 1 addition & 0 deletions plane/plane-tests/tests/reuse_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async fn reuse_key(env: TestEnvironment) {
tracing::info!("Requesting backend.");
let connect_request = ConnectRequest {
spawn_config: Some(SpawnConfig {
id: None,
cluster: Some(env.cluster.clone()),
executable: ExecutorConfig {
image: "ghcr.io/drifting-in-space/demo-image-drop-four".to_string(),
Expand Down
7 changes: 7 additions & 0 deletions plane/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ enum AdminCommand {
/// The number of seconds without any connected clients to wait before terminating the backend.
#[clap(long)]
max_idle_seconds: Option<i32>,

/// An optional backend to assign the string (not recommended and may be removed. Omit to allow Plane to auto-assign
/// one instead).
#[clap(long)]
id: Option<BackendName>,
},
Terminate {
#[clap(long)]
Expand Down Expand Up @@ -144,10 +149,12 @@ pub async fn run_admin_command_inner(opts: AdminOpts) -> Result<(), PlaneClientE
key,
wait,
max_idle_seconds,
id,
} => {
let executor_config = ExecutorConfig::from_image_with_defaults(image);
let max_idle_seconds = max_idle_seconds.unwrap_or(500);
let spawn_config = SpawnConfig {
id,
cluster: cluster.clone(),
executable: executor_config.clone(),
lifetime_limit_seconds: None,
Expand Down
4 changes: 2 additions & 2 deletions plane/src/database/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
drone::DroneDatabase,
},
log_types::LoggableTime,
names::{BackendName, Name},
names::{BackendName, OrRandom},
protocol::{AcquiredKey, BackendAction, KeyDeadlines},
types::{
BackendState, BackendStatus, BearerToken, ClusterName, ConnectRequest, ConnectResponse,
Expand Down Expand Up @@ -81,7 +81,7 @@ async fn create_backend_with_key(
cluster: &ClusterName,
drone_for_spawn: &DroneForSpawn,
) -> Result<Option<BackendName>> {
let backend_id = BackendName::new_random();
let backend_id = spawn_config.id.clone().or_random();
let mut txn = pool.begin().await?;

let result = sqlx::query!(
Expand Down
37 changes: 21 additions & 16 deletions plane/src/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ pub trait Name:

fn new_random() -> Self;

fn prefix() -> &'static str;
fn prefix() -> Option<&'static str>;
}

macro_rules! entity_name {
($name:ident, $prefix:literal) => {
($name:ident, $prefix:expr) => {
#[derive(
Debug,
Clone,
Expand All @@ -52,11 +52,14 @@ macro_rules! entity_name {
}

fn new_random() -> Self {
let st = crate::util::random_prefixed_string($prefix);
Self(st)
if let Some(prefix) = $prefix {
Self(crate::util::random_prefixed_string(prefix))
} else {
Self(crate::util::random_string())
}
}

fn prefix() -> &'static str {
fn prefix() -> Option<&'static str> {
$prefix
}
}
Expand All @@ -71,8 +74,10 @@ macro_rules! entity_name {
type Error = NameError;

fn try_from(s: String) -> Result<Self, NameError> {
if !s.starts_with($prefix) {
return Err(NameError::InvalidPrefix(s, $prefix.to_string()));
if let Some(prefix) = $prefix {
if !s.starts_with(prefix) {
return Err(NameError::InvalidPrefix(s, prefix.to_string()));
}
}

if s.len() > MAX_NAME_LENGTH {
Expand Down Expand Up @@ -146,12 +151,12 @@ impl<T: Name> clap::builder::TypedValueParser for NameParser<T> {
}
}

entity_name!(ControllerName, "co");
entity_name!(BackendName, "ba");
entity_name!(ProxyName, "pr");
entity_name!(DroneName, "dr");
entity_name!(AcmeDnsServerName, "ns");
entity_name!(BackendActionName, "ak");
entity_name!(ControllerName, Some("co"));
entity_name!(BackendName, None::<&'static str>);
entity_name!(ProxyName, Some("pr"));
entity_name!(DroneName, Some("dr"));
entity_name!(AcmeDnsServerName, Some("ns"));
entity_name!(BackendActionName, Some("ak"));

pub trait NodeName: Name {
fn kind(&self) -> NodeKind;
Expand Down Expand Up @@ -195,11 +200,11 @@ impl TryFrom<String> for AnyNodeName {
type Error = NameError;

fn try_from(s: String) -> Result<Self, Self::Error> {
if s.starts_with(ProxyName::prefix()) {
if s.starts_with(ProxyName::prefix().expect("has prefix")) {
Ok(AnyNodeName::Proxy(ProxyName::try_from(s)?))
} else if s.starts_with(DroneName::prefix()) {
} else if s.starts_with(DroneName::prefix().expect("has prefix")) {
Ok(AnyNodeName::Drone(DroneName::try_from(s)?))
} else if s.starts_with(AcmeDnsServerName::prefix()) {
} else if s.starts_with(AcmeDnsServerName::prefix().expect("has prefix")) {
Ok(AnyNodeName::AcmeDnsServer(AcmeDnsServerName::try_from(s)?))
} else {
Err(NameError::InvalidAnyPrefix(s))
Expand Down
6 changes: 6 additions & 0 deletions plane/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ impl ExecutorConfig {

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct SpawnConfig {
/// ID to assign to the new backend. Must be unique.
/// This should only be used if you really need it, otherwise you can leave it blank
/// and let Plane assign a unique ID automatically. This may be removed from
/// future versions of Plane.
pub id: Option<BackendName>,

/// Cluster to spawn to. Uses the controller default if not provided.
pub cluster: Option<ClusterName>,

Expand Down

0 comments on commit 36a8950

Please sign in to comment.