Skip to content

Commit

Permalink
fix: make default cluster opt in (#632)
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 af41308 commit c98e6aa
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 35 deletions.
31 changes: 16 additions & 15 deletions lib/bolt/config/src/ns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ pub struct Rivet {
#[serde(default)]
pub upload: Upload,
#[serde(default)]
pub dynamic_servers: Option<DynamicServers>,
pub provisioning: Option<Provisioning>,
#[serde(default)]
pub cdn: Cdn,
#[serde(default)]
Expand Down Expand Up @@ -594,8 +594,9 @@ pub struct Upload {

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct DynamicServers {
pub cluster: DynamicServersCluster,
pub struct Provisioning {
/// Default cluster.
pub cluster: Option<ProvisioningCluster>,
/// Whether or not to send a taint message in the next cluster update.
#[serde(default)]
pub taint: bool,
Expand All @@ -605,7 +606,7 @@ pub struct DynamicServers {
}

#[derive(Serialize, Deserialize, Clone, Debug, Default, strum_macros::Display)]
pub enum DynamicServersBuildDeliveryMethod {
pub enum ProvisioningBuildDeliveryMethod {
#[serde(rename = "traffic_server")]
#[strum(serialize = "traffic_server")]
#[default]
Expand All @@ -617,50 +618,50 @@ pub enum DynamicServersBuildDeliveryMethod {

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct DynamicServersCluster {
pub struct ProvisioningCluster {
name_id: String,
#[serde(default)]
pub datacenters: HashMap<String, DynamicServersDatacenter>,
pub datacenters: HashMap<String, ProvisioningDatacenter>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct DynamicServersDatacenter {
pub struct ProvisioningDatacenter {
pub datacenter_id: Uuid,
pub display_name: String,
pub provider: DynamicServersProvider,
pub provider: ProvisioningProvider,
pub provider_datacenter_name: String,
#[serde(default)]
pub build_delivery_method: DynamicServersBuildDeliveryMethod,
pub build_delivery_method: ProvisioningBuildDeliveryMethod,
/// Nomad drain time in seconds.
pub drain_timeout: u32,

#[serde(default)]
pub pools: HashMap<DynamicServersDatacenterPoolType, DynamicServersDatacenterPool>,
pub pools: HashMap<ProvisioningDatacenterPoolType, ProvisioningDatacenterPool>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum DynamicServersProvider {
pub enum ProvisioningProvider {
#[serde(rename = "linode")]
Linode,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct DynamicServersDatacenterPool {
pub hardware: Vec<DynamicServersDatacenterHardware>,
pub struct ProvisioningDatacenterPool {
pub hardware: Vec<ProvisioningDatacenterHardware>,
pub desired_count: u32,
pub max_count: u32,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct DynamicServersDatacenterHardware {
pub struct ProvisioningDatacenterHardware {
pub name: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
pub enum DynamicServersDatacenterPoolType {
pub enum ProvisioningDatacenterPoolType {
#[serde(rename = "job")]
Job,
#[serde(rename = "gg")]
Expand Down
24 changes: 15 additions & 9 deletions lib/bolt/core/src/context/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl ProjectContextData {
);
} else {
assert!(
self.ns().rivet.dynamic_servers.is_none(),
self.ns().rivet.provisioning.is_none(),
"must have dns configured to provision servers"
);
}
Expand All @@ -215,11 +215,17 @@ impl ProjectContextData {
}
}

// MARK: Dynamic Servers
if let Some(dynamic_servers) = &self.ns().rivet.dynamic_servers {
// MARK: Cluster Provisioning
if let Some(cluster) = self
.ns()
.rivet
.provisioning
.as_ref()
.and_then(|p| p.cluster.as_ref())
{
let mut unique_datacenter_ids = HashSet::new();

for (name_id, datacenter) in &dynamic_servers.cluster.datacenters {
for (name_id, datacenter) in &cluster.datacenters {
assert!(
!unique_datacenter_ids.contains(&datacenter.datacenter_id),
"invalid datacenter ({}): datacenter_id not unique",
Expand All @@ -229,7 +235,7 @@ impl ProjectContextData {

let Some(ats_pool) = datacenter
.pools
.get(&config::ns::DynamicServersDatacenterPoolType::Ats)
.get(&config::ns::ProvisioningDatacenterPoolType::Ats)
else {
panic!("invalid datacenter ({}): Missing ATS pool", name_id);
};
Expand All @@ -241,14 +247,14 @@ impl ProjectContextData {
name_id
);
match datacenter.build_delivery_method {
config::ns::DynamicServersBuildDeliveryMethod::TrafficServer => {
config::ns::ProvisioningBuildDeliveryMethod::TrafficServer => {
assert_ne!(
0, ats_pool.desired_count,
"invalid datacenter ({}): TrafficServer delivery method will not work without ats servers. Either set datacenter.build_delivery_method = \"s3_direct\" to download builds directly from S3 or increase the ATS pool count.",
name_id,
);
}
config::ns::DynamicServersBuildDeliveryMethod::S3Direct => {
config::ns::ProvisioningBuildDeliveryMethod::S3Direct => {
assert_eq!(
0, ats_pool.desired_count,
"invalid datacenter ({}): S3Direct delivery method should not be used if ats servers are available",
Expand All @@ -260,7 +266,7 @@ impl ProjectContextData {
// Validate all required pools exist
let gg_pool = datacenter
.pools
.get(&config::ns::DynamicServersDatacenterPoolType::Gg);
.get(&config::ns::ProvisioningDatacenterPoolType::Gg);
let gg_count = gg_pool.map(|pool| pool.desired_count).unwrap_or_default();
assert_ne!(
gg_count, 0,
Expand All @@ -275,7 +281,7 @@ impl ProjectContextData {

let job_pool = datacenter
.pools
.get(&config::ns::DynamicServersDatacenterPoolType::Job);
.get(&config::ns::ProvisioningDatacenterPoolType::Job);
let job_count = job_pool.map(|pool| pool.desired_count).unwrap_or_default();

assert_ne!(
Expand Down
13 changes: 6 additions & 7 deletions lib/bolt/core/src/context/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1017,16 +1017,15 @@ impl ServiceContextData {
env.push(("RIVET_UPLOAD_NSFW_ERROR_VERBOSE".into(), "1".into()));
}

// Dynamic servers
if let Some(dynamic_servers) = &project_ctx.ns().rivet.dynamic_servers {
if let Some(provisioning) = &project_ctx.ns().rivet.provisioning {
if self.depends_on_cluster_config() || matches!(run_context, RunContext::Test { .. }) {
env.push((
"RIVET_DEFAULT_CLUSTER_CONFIG".into(),
serde_json::to_string(&dynamic_servers.cluster)?,
serde_json::to_string(&provisioning.cluster)?,
));
env.push((
"RIVET_TAINT_DEFAULT_CLUSTER".into(),
if dynamic_servers.taint {
if provisioning.taint {
"1".to_string()
} else {
"0".to_string()
Expand All @@ -1036,8 +1035,8 @@ impl ServiceContextData {

if self.depends_on_provision_margin() {
env.push((
format!("JOB_SERVER_PROVISION_MARGIN"),
dynamic_servers.job_server_provision_margin.to_string(),
format!("RIVET_JOB_SERVER_PROVISION_MARGIN"),
provisioning.job_server_provision_margin.to_string(),
));
}
}
Expand Down Expand Up @@ -1240,7 +1239,7 @@ impl ServiceContextData {
));
}

if self.depends_on_infra() && project_ctx.tls_enabled() {
if self.depends_on_infra() && project_ctx.ns().rivet.provisioning.is_some() {
let tls = terraform::output::read_tls(&project_ctx).await;
let k8s_infra = terraform::output::read_k8s_infra(&project_ctx).await;

Expand Down
10 changes: 7 additions & 3 deletions lib/bolt/core/src/dep/terraform/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,13 @@ async fn vars(ctx: &ProjectContext) {
}

// Datacenters
if let Some(dynamic_servers) = &config.rivet.dynamic_servers {
let datacenters = dynamic_servers
.cluster
if let Some(cluster) = config
.rivet
.provisioning
.as_ref()
.and_then(|p| p.cluster.as_ref())
{
let datacenters = cluster
.datacenters
.iter()
.map(|(name_id, dc)| {
Expand Down
2 changes: 1 addition & 1 deletion lib/bolt/core/src/tasks/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ struct Data {

// TODO: This only deletes linodes and firewalls, the ssh key still remains
async fn cleanup_servers(ctx: &ProjectContext) -> Result<()> {
if ctx.ns().rivet.dynamic_servers.is_none() {
if ctx.ns().rivet.provisioning.is_none() {
return Ok(());
}

Expand Down

0 comments on commit c98e6aa

Please sign in to comment.