Skip to content

Commit

Permalink
feat: add provider api token to all linode calls (#613)
Browse files Browse the repository at this point in the history
<!-- Please make sure there is an issue that this PR is correlated to. -->

## Changes

<!-- If there are frontend changes, please include screenshots. -->
  • Loading branch information
MasterPtato committed Apr 17, 2024
1 parent 602749f commit 3882047
Show file tree
Hide file tree
Showing 40 changed files with 115 additions and 45 deletions.
7 changes: 4 additions & 3 deletions proto/backend/cluster.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ message Datacenter {

Provider provider = 5;
string provider_datacenter_id = 6;
optional string provider_api_token = 7;

repeated Pool pools = 7;
BuildDeliveryMethod build_delivery_method = 8;
repeated Pool pools = 8;
BuildDeliveryMethod build_delivery_method = 9;
// Nomad drain time in seconds.
uint64 drain_timeout = 9;
uint64 drain_timeout = 10;
}

message Pool {
Expand Down
2 changes: 2 additions & 0 deletions svc/pkg/cluster/ops/datacenter-get/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct Datacenter {
display_name: String,
provider: i64,
provider_datacenter_id: String,
provider_api_token: Option<String>,
pools: Vec<u8>,
build_delivery_method: i64,
drain_timeout: i64,
Expand All @@ -29,6 +30,7 @@ impl TryFrom<Datacenter> for backend::cluster::Datacenter {
display_name: value.display_name,
provider: value.provider as i32,
provider_datacenter_id: value.provider_datacenter_id,
provider_api_token: value.provider_api_token,
pools,
build_delivery_method: value.build_delivery_method as i32,
drain_timeout: value.drain_timeout as u64,
Expand Down
1 change: 1 addition & 0 deletions svc/pkg/cluster/ops/datacenter-get/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async fn empty(ctx: TestCtx) {

provider: backend::cluster::Provider::Linode as i32,
provider_datacenter_id: "us-southeast".to_string(),
provider_api_token: None,

pools: Vec::new(),

Expand Down
1 change: 1 addition & 0 deletions svc/pkg/cluster/ops/datacenter-list/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async fn empty(ctx: TestCtx) {

provider: backend::cluster::Provider::Linode as i32,
provider_datacenter_id: "us-southeast".to_string(),
provider_api_token: None,

pools: Vec::new(),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ async fn empty(ctx: TestCtx) {

provider: backend::cluster::Provider::Linode as i32,
provider_datacenter_id: "us-southeast".to_string(),
provider_api_token: None,

pools: Vec::new(),

Expand Down
1 change: 1 addition & 0 deletions svc/pkg/cluster/standalone/default-update/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ pub async fn run_from_env(use_autoscaler: bool) -> GlobalResult<()> {

provider: Into::<backend::cluster::Provider>::into(datacenter.provider) as i32,
provider_datacenter_id: datacenter.provider_datacenter_name,
provider_api_token: None,

pools: datacenter.pools.into_iter().map(|(pool_type, pool)| {
backend::cluster::Pool {
Expand Down
1 change: 1 addition & 0 deletions svc/pkg/cluster/standalone/gc/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ async fn setup(

provider: backend::cluster::Provider::Linode as i32,
provider_datacenter_id: "us-southeast".to_string(),
provider_api_token: None,

pools: vec![backend::cluster::Pool {
pool_type,
Expand Down
3 changes: 2 additions & 1 deletion svc/pkg/cluster/types/msg/server-install-complete.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import "proto/backend/cluster.proto";
/// { name = "public_ip" },
/// ]
message Message {
string ip = 1;
string public_ip = 1;
// If set in server install message
optional rivet.common.Uuid server_id = 2;
rivet.backend.cluster.Provider provider = 3;
optional string provider_api_token = 4;
}
3 changes: 2 additions & 1 deletion svc/pkg/cluster/types/msg/server-install.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ message Message {
optional rivet.common.Uuid server_id = 3;
// Simply passed to the install complete message
rivet.backend.cluster.Provider provider = 4;
bool initialize_immediately = 5;
optional string provider_api_token = 5;
bool initialize_immediately = 6;
}
4 changes: 3 additions & 1 deletion svc/pkg/cluster/worker/src/workers/datacenter_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,20 @@ async fn worker(
display_name,
provider,
provider_datacenter_id,
provider_api_token,
pools,
build_delivery_method,
drain_timeout
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
",
datacenter_id,
cluster_id,
&config.name_id,
&config.display_name,
config.provider as i64,
&config.provider_datacenter_id,
&config.provider_api_token,
pools_buf,
config.build_delivery_method as i64,
config.drain_timeout as i64
Expand Down
68 changes: 45 additions & 23 deletions svc/pkg/cluster/worker/src/workers/datacenter_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ async fn worker(
datacenter_ids: vec![datacenter_id.into()],
})
.await?;
let datacenter_config = unwrap!(
let datacenter = unwrap!(
datacenter_res.datacenters.first(),
"datacenter does not exist"
);

// Update config
let mut new_config = datacenter_config.clone();

// Update pools config
let mut new_pools = cluster::msg::datacenter_create::Pools {
pools: datacenter.pools.clone(),
};
for pool in &ctx.pools {
let mut current_pool = unwrap!(
new_config
let current_pool = unwrap!(
new_pools
.pools
.iter_mut()
.find(|p| p.pool_type == pool.pool_type),
Expand All @@ -40,25 +41,46 @@ async fn worker(
}
}

if let Some(drain_timeout) = ctx.drain_timeout {
new_config.drain_timeout = drain_timeout;
}

// Encode config
let mut config_buf = Vec::with_capacity(new_config.encoded_len());
new_config.encode(&mut config_buf)?;
let mut pools_buf = Vec::with_capacity(new_pools.encoded_len());
new_pools.encode(&mut pools_buf)?;

rivet_pools::utils::crdb::tx(&ctx.crdb().await?, |tx| {
let ctx = ctx.clone();
let pools_buf = pools_buf.clone();

// Write config
sql_execute!(
[ctx]
"
UPDATE db_cluster.datacenters
SET config = $2
WHERE datacenter_id = $1
",
datacenter_id,
config_buf,
)
Box::pin(async move {
// Update pools
sql_execute!(
[ctx, @tx tx]
"
UPDATE db_cluster.datacenters
SET pools = $2
WHERE datacenter_id = $1
",
datacenter_id,
pools_buf,
)
.await?;

// Update drain timeout
if let Some(drain_timeout) = ctx.drain_timeout {
sql_execute!(
[ctx, @tx tx]
"
UPDATE db_cluster.datacenters
SET drain_timeout = $2
WHERE datacenter_id = $1
",
datacenter_id,
drain_timeout as i64,
)
.await?;
}

Ok(())
})
})
.await?;

msg!([ctx] cluster::msg::datacenter_scale(datacenter_id) {
Expand Down
1 change: 1 addition & 0 deletions svc/pkg/cluster/worker/src/workers/server_destroy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ async fn worker(ctx: &OperationContext<cluster::msg::server_destroy::Message>) -

op!([ctx] linode_server_destroy {
server_id: ctx.server_id,
api_token: datacenter.provider_api_token.clone(),
})
.await?;
}
Expand Down
3 changes: 2 additions & 1 deletion svc/pkg/cluster/worker/src/workers/server_install/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ async fn worker(ctx: &OperationContext<cluster::msg::server_install::Message>) -
.await??;

msg!([ctx] cluster::msg::server_install_complete(&ctx.public_ip) {
ip: ctx.public_ip.clone(),
public_ip: ctx.public_ip.clone(),
server_id: ctx.server_id,
provider: ctx.provider,
provider_api_token: ctx.provider_api_token.clone(),
})
.await?;

Expand Down
5 changes: 3 additions & 2 deletions svc/pkg/cluster/worker/src/workers/server_install_complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ async fn worker(
match provider {
backend::cluster::Provider::Linode => {
if ctx.server_id.is_none() {
msg!([ctx] linode::msg::prebake_install_complete(&ctx.ip) {
ip: ctx.ip.clone(),
msg!([ctx] linode::msg::prebake_install_complete(&ctx.public_ip) {
public_ip: ctx.public_ip.clone(),
api_token: ctx.provider_api_token.clone(),
})
.await?;
}
Expand Down
4 changes: 3 additions & 1 deletion svc/pkg/cluster/worker/src/workers/server_provision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ async fn worker(
pool_type: ctx.pool_type,
vlan_ip: vlan_ip.clone(),
tags: ctx.tags.clone(),
api_token: datacenter.provider_api_token.clone(),
})
.await;

Expand Down Expand Up @@ -138,9 +139,10 @@ async fn worker(
if !provision_res.already_installed {
msg!([ctx] cluster::msg::server_install(&provision_res.public_ip) {
public_ip: provision_res.public_ip,
pool_type: ctx.pool_type,
server_id: ctx.server_id,
pool_type: ctx.pool_type,
provider: ctx.provider,
provider_api_token: datacenter.provider_api_token.clone(),
initialize_immediately: true,
})
.await?;
Expand Down
1 change: 1 addition & 0 deletions svc/pkg/cluster/worker/tests/datacenter_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async fn datacenter_create(ctx: TestCtx) {

provider: backend::cluster::Provider::Linode as i32,
provider_datacenter_id: "us-southeast".to_string(),
provider_api_token: None,

pools: Vec::new(),

Expand Down
1 change: 1 addition & 0 deletions svc/pkg/cluster/worker/tests/datacenter_taint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ async fn setup(

provider: backend::cluster::Provider::Linode as i32,
provider_datacenter_id: "us-southeast".to_string(),
provider_api_token: None,

pools: vec![backend::cluster::Pool {
pool_type,
Expand Down
1 change: 1 addition & 0 deletions svc/pkg/cluster/worker/tests/datacenter_taint_complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ async fn setup(

provider: backend::cluster::Provider::Linode as i32,
provider_datacenter_id: "us-southeast".to_string(),
provider_api_token: None,

pools: vec![backend::cluster::Pool {
pool_type,
Expand Down
1 change: 1 addition & 0 deletions svc/pkg/cluster/worker/tests/datacenter_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async fn datacenter_update(ctx: TestCtx) {

provider: backend::cluster::Provider::Linode as i32,
provider_datacenter_id: "us-southeast".to_string(),
provider_api_token: None,

pools: vec![backend::cluster::Pool {
pool_type: backend::cluster::PoolType::Ats as i32,
Expand Down
1 change: 1 addition & 0 deletions svc/pkg/cluster/worker/tests/nomad_node_drain_complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ async fn setup(

provider: backend::cluster::Provider::Linode as i32,
provider_datacenter_id: "us-southeast".to_string(),
provider_api_token: None,

pools: vec![backend::cluster::Pool {
pool_type,
Expand Down
1 change: 1 addition & 0 deletions svc/pkg/cluster/worker/tests/nomad_node_registered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ async fn setup(

provider: backend::cluster::Provider::Linode as i32,
provider_datacenter_id: "us-southeast".to_string(),
provider_api_token: None,

pools: vec![backend::cluster::Pool {
pool_type,
Expand Down
1 change: 1 addition & 0 deletions svc/pkg/cluster/worker/tests/server_destroy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ async fn setup(

provider: backend::cluster::Provider::Linode as i32,
provider_datacenter_id: "us-southeast".to_string(),
provider_api_token: None,

pools: vec![backend::cluster::Pool {
pool_type,
Expand Down
1 change: 1 addition & 0 deletions svc/pkg/cluster/worker/tests/server_dns_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ async fn setup(

provider: backend::cluster::Provider::Linode as i32,
provider_datacenter_id: "us-southeast".to_string(),
provider_api_token: None,

pools: vec![backend::cluster::Pool {
pool_type,
Expand Down
1 change: 1 addition & 0 deletions svc/pkg/cluster/worker/tests/server_dns_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ async fn setup(

provider: backend::cluster::Provider::Linode as i32,
provider_datacenter_id: "us-southeast".to_string(),
provider_api_token: None,

pools: vec![backend::cluster::Pool {
pool_type,
Expand Down
1 change: 1 addition & 0 deletions svc/pkg/cluster/worker/tests/server_drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ async fn setup(

provider: backend::cluster::Provider::Linode as i32,
provider_datacenter_id: "us-southeast".to_string(),
provider_api_token: None,

pools: vec![backend::cluster::Pool {
pool_type,
Expand Down
1 change: 1 addition & 0 deletions svc/pkg/cluster/worker/tests/server_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ async fn setup(

provider: backend::cluster::Provider::Linode as i32,
provider_datacenter_id: "us-southeast".to_string(),
provider_api_token: None,

pools: vec![backend::cluster::Pool {
pool_type,
Expand Down
1 change: 1 addition & 0 deletions svc/pkg/cluster/worker/tests/server_provision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ async fn setup(

provider: backend::cluster::Provider::Linode as i32,
provider_datacenter_id: "us-southeast".to_string(),
provider_api_token: None,

pools: vec![backend::cluster::Pool {
pool_type,
Expand Down
2 changes: 1 addition & 1 deletion svc/pkg/linode/ops/instance-type-get/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub async fn handle(
ctx: OperationContext<linode::instance_type_get::Request>,
) -> GlobalResult<linode::instance_type_get::Response> {
// Build HTTP client
let client = util_linode::Client::new().await?;
let client = util_linode::Client::new(None).await?;

// Get hardware stats from linode and cache
let instance_types_res = ctx
Expand Down
2 changes: 1 addition & 1 deletion svc/pkg/linode/ops/server-destroy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub async fn handle(
};

// Build HTTP client
let client = util_linode::Client::new().await?;
let client = util_linode::Client::new(ctx.api_token.clone()).await?;

if let Some(linode_id) = data.linode_id {
api::delete_instance(&client, linode_id).await?;
Expand Down
3 changes: 2 additions & 1 deletion svc/pkg/linode/ops/server-provision/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub async fn handle(
};

// Build HTTP client
let client = util_linode::Client::new().await?;
let client = util_linode::Client::new(ctx.api_token.clone()).await?;

// Create SSH key
let ssh_key_res = api::create_ssh_key(&client, &server_id.to_string()).await?;
Expand Down Expand Up @@ -168,6 +168,7 @@ async fn create_disks(
provider_datacenter_id: server.datacenter.clone(),
pool_type: pool_type as i32,
tags: Vec::new(),
api_token: ctx.api_token.clone(),
})
.await?;
}
Expand Down
2 changes: 1 addition & 1 deletion svc/pkg/linode/standalone/gc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub async fn run_from_env(pools: rivet_pools::Pools) -> GlobalResult<()> {
);

// Build HTTP client
let client = util_linode::Client::new_with_headers(headers).await?;
let client = util_linode::Client::new_with_headers(None, headers).await?;

let complete_images = api::list_custom_images(&client).await?;

Expand Down
Loading

0 comments on commit 3882047

Please sign in to comment.