diff --git a/Cargo.lock b/Cargo.lock index 1e83bdebbc9..1adbf0de6f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2772,6 +2772,17 @@ dependencies = [ "uuid", ] +[[package]] +name = "nexus-defaults" +version = "0.1.0" +dependencies = [ + "ipnetwork", + "lazy_static", + "omicron-common 0.1.0", + "rand 0.8.5", + "serde_json", +] + [[package]] name = "nexus-test-utils" version = "0.1.0" @@ -3096,6 +3107,7 @@ dependencies = [ "macaddr", "mime_guess", "newtype_derive", + "nexus-defaults", "nexus-test-utils", "nexus-test-utils-macros", "nexus-types", diff --git a/Cargo.toml b/Cargo.toml index ff81379171e..cb051e78a4b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ members = [ "nexus", "nexus/authz-macros", "nexus/db-macros", + "nexus/defaults", "nexus/test-utils", "nexus/test-utils-macros", "nexus/types", @@ -46,6 +47,7 @@ default-members = [ "nexus", "nexus/authz-macros", "nexus/db-macros", + "nexus/defaults", "nexus/types", "package", "rpaths", diff --git a/nexus/Cargo.toml b/nexus/Cargo.toml index 1a3719a6a40..700a4e1eb21 100644 --- a/nexus/Cargo.toml +++ b/nexus/Cargo.toml @@ -60,6 +60,7 @@ usdt = "0.3.1" authz-macros = { path = "authz-macros" } db-macros = { path = "db-macros" } +nexus-defaults = { path = "defaults" } nexus-types = { path = "types" } [dependencies.chrono] diff --git a/nexus/defaults/Cargo.toml b/nexus/defaults/Cargo.toml new file mode 100644 index 00000000000..61e9ba65d14 --- /dev/null +++ b/nexus/defaults/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "nexus-defaults" +version = "0.1.0" +edition = "2021" +license = "MPL-2.0" + +[dependencies] +ipnetwork = "0.18" +lazy_static = "1.4.0" +rand = "0.8.5" +serde_json = "1.0" + +omicron-common = { path = "../../common" } diff --git a/nexus/src/defaults.rs b/nexus/defaults/src/lib.rs similarity index 100% rename from nexus/src/defaults.rs rename to nexus/defaults/src/lib.rs diff --git a/nexus/src/app/project.rs b/nexus/src/app/project.rs index f5cb97255ec..a76262ca219 100644 --- a/nexus/src/app/project.rs +++ b/nexus/src/app/project.rs @@ -9,10 +9,10 @@ use crate::context::OpContext; use crate::db; use crate::db::lookup::LookupPath; use crate::db::model::Name; -use crate::defaults; use crate::external_api::params; use crate::external_api::shared; use anyhow::Context; +use nexus_defaults as defaults; use omicron_common::api::external::CreateResult; use omicron_common::api::external::DataPageParams; use omicron_common::api::external::DeleteResult; diff --git a/nexus/src/app/sagas/instance_create.rs b/nexus/src/app/sagas/instance_create.rs index d6f6372522c..1cdcd8d0cc2 100644 --- a/nexus/src/app/sagas/instance_create.rs +++ b/nexus/src/app/sagas/instance_create.rs @@ -13,12 +13,12 @@ use crate::context::OpContext; use crate::db::identity::Resource; use crate::db::lookup::LookupPath; use crate::db::queries::network_interface::InsertError as InsertNicError; -use crate::defaults::DEFAULT_PRIMARY_NIC_NAME; use crate::external_api::params; use crate::saga_interface::SagaContext; use crate::{authn, authz, db}; use chrono::Utc; use lazy_static::lazy_static; +use nexus_defaults::DEFAULT_PRIMARY_NIC_NAME; use omicron_common::api::external::Error; use omicron_common::api::external::Generation; use omicron_common::api::external::IdentityMetadataCreateParams; diff --git a/nexus/src/app/vpc.rs b/nexus/src/app/vpc.rs index af6017ccfe3..c2ad4b781e0 100644 --- a/nexus/src/app/vpc.rs +++ b/nexus/src/app/vpc.rs @@ -11,8 +11,8 @@ use crate::db::lookup::LookupPath; use crate::db::model::Name; use crate::db::model::VpcRouterKind; use crate::db::queries::vpc_subnet::SubnetError; -use crate::defaults; use crate::external_api::params; +use nexus_defaults as defaults; use omicron_common::api::external; use omicron_common::api::external::CreateResult; use omicron_common::api::external::DataPageParams; diff --git a/nexus/src/app/vpc_subnet.rs b/nexus/src/app/vpc_subnet.rs index 26f9089c3e6..cacc2dc441e 100644 --- a/nexus/src/app/vpc_subnet.rs +++ b/nexus/src/app/vpc_subnet.rs @@ -12,8 +12,8 @@ use crate::db::lookup::LookupPath; use crate::db::model::Name; use crate::db::model::VpcSubnet; use crate::db::queries::vpc_subnet::SubnetError; -use crate::defaults; use crate::external_api::params; +use nexus_defaults as defaults; use omicron_common::api::external; use omicron_common::api::external::CreateResult; use omicron_common::api::external::DataPageParams; diff --git a/nexus/src/config.rs b/nexus/src/config.rs index 65dc4ce8176..7266a3abd10 100644 --- a/nexus/src/config.rs +++ b/nexus/src/config.rs @@ -84,15 +84,17 @@ impl TryFrom for Tunables { impl Tunables { fn validate_ipv4_prefix(prefix: u8) -> Result<(), InvalidTunable> { - let absolute_max: u8 = 32_u8.checked_sub( - // Always need space for the reserved Oxide addresses, including the - // broadcast address at the end of the subnet. - ((crate::defaults::NUM_INITIAL_RESERVED_IP_ADDRESSES + 1) as f32) + let absolute_max: u8 = 32_u8 + .checked_sub( + // Always need space for the reserved Oxide addresses, including the + // broadcast address at the end of the subnet. + ((nexus_defaults::NUM_INITIAL_RESERVED_IP_ADDRESSES + 1) as f32) .log2() // Subnet size to bit prefix. .ceil() // Round up to a whole number of bits. - as u8 - ).expect("Invalid absolute maximum IPv4 subnet prefix"); - if prefix >= crate::defaults::MIN_VPC_IPV4_SUBNET_PREFIX + as u8, + ) + .expect("Invalid absolute maximum IPv4 subnet prefix"); + if prefix >= nexus_defaults::MIN_VPC_IPV4_SUBNET_PREFIX && prefix <= absolute_max { Ok(()) diff --git a/nexus/src/db/model/ipv4net.rs b/nexus/src/db/model/ipv4net.rs index 6a90b16cc62..664dda5c80c 100644 --- a/nexus/src/db/model/ipv4net.rs +++ b/nexus/src/db/model/ipv4net.rs @@ -2,13 +2,13 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -use crate::defaults; use diesel::backend::{Backend, RawValue}; use diesel::deserialize::{self, FromSql}; use diesel::pg::Pg; use diesel::serialize::{self, ToSql}; use diesel::sql_types; use ipnetwork::IpNetwork; +use nexus_defaults as defaults; use omicron_common::api::external; use std::net::Ipv4Addr; diff --git a/nexus/src/db/model/ipv6net.rs b/nexus/src/db/model/ipv6net.rs index 0321aa02b11..a91c3efcd4d 100644 --- a/nexus/src/db/model/ipv6net.rs +++ b/nexus/src/db/model/ipv6net.rs @@ -2,13 +2,13 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -use crate::defaults; use diesel::backend::{Backend, RawValue}; use diesel::deserialize::{self, FromSql}; use diesel::pg::Pg; use diesel::serialize::{self, ToSql}; use diesel::sql_types; use ipnetwork::IpNetwork; +use nexus_defaults as defaults; use omicron_common::api::external; use rand::{rngs::StdRng, SeedableRng}; use std::net::Ipv6Addr; diff --git a/nexus/src/db/model/vpc.rs b/nexus/src/db/model/vpc.rs index 51058b5d0c7..84d55ab7702 100644 --- a/nexus/src/db/model/vpc.rs +++ b/nexus/src/db/model/vpc.rs @@ -7,11 +7,11 @@ use crate::db::collection_insert::DatastoreCollection; use crate::db::identity::Resource; use crate::db::model::Vni; use crate::db::schema::{vpc, vpc_firewall_rule}; -use crate::defaults; use crate::external_api::params; use chrono::{DateTime, Utc}; use db_macros::Resource; use ipnetwork::IpNetwork; +use nexus_defaults as defaults; use nexus_types::external_api::views; use omicron_common::api::external; use uuid::Uuid; diff --git a/nexus/src/db/queries/network_interface.rs b/nexus/src/db/queries/network_interface.rs index a9b0a8dbaf4..585411b2b84 100644 --- a/nexus/src/db/queries/network_interface.rs +++ b/nexus/src/db/queries/network_interface.rs @@ -12,7 +12,6 @@ use crate::db::pool::DbConnection; use crate::db::queries::next_item::DefaultShiftGenerator; use crate::db::queries::next_item::NextItem; use crate::db::schema::network_interface::dsl; -use crate::defaults::NUM_INITIAL_RESERVED_IP_ADDRESSES; use chrono::DateTime; use chrono::Utc; use diesel::pg::Pg; @@ -26,6 +25,7 @@ use diesel::QueryResult; use diesel::RunQueryDsl; use ipnetwork::IpNetwork; use ipnetwork::Ipv4Network; +use nexus_defaults::NUM_INITIAL_RESERVED_IP_ADDRESSES; use omicron_common::api::external; use std::net::IpAddr; use uuid::Uuid; @@ -1806,10 +1806,10 @@ mod tests { fn available_ipv4_addresses(&self) -> [usize; 2] { [ self.subnets[0].ipv4_block.size() as usize - - crate::defaults::NUM_INITIAL_RESERVED_IP_ADDRESSES + - nexus_defaults::NUM_INITIAL_RESERVED_IP_ADDRESSES - 1, self.subnets[1].ipv4_block.size() as usize - - crate::defaults::NUM_INITIAL_RESERVED_IP_ADDRESSES + - nexus_defaults::NUM_INITIAL_RESERVED_IP_ADDRESSES - 1, ] } @@ -1969,7 +1969,7 @@ mod tests { let addresses = context.net1.subnets[0] .ipv4_block .iter() - .skip(crate::defaults::NUM_INITIAL_RESERVED_IP_ADDRESSES); + .skip(nexus_defaults::NUM_INITIAL_RESERVED_IP_ADDRESSES); for (i, expected_address) in addresses.take(2).enumerate() { let instance = diff --git a/nexus/src/lib.rs b/nexus/src/lib.rs index 5ab34280c74..7597bfc1e0b 100644 --- a/nexus/src/lib.rs +++ b/nexus/src/lib.rs @@ -20,7 +20,6 @@ mod cidata; pub mod config; // Public for testing pub mod context; // Public for documentation examples pub mod db; // Public for documentation examples -pub mod defaults; // Public for testing pub mod external_api; // Public for testing pub mod internal_api; // Public for testing mod populate; diff --git a/nexus/tests/integration_tests/endpoints.rs b/nexus/tests/integration_tests/endpoints.rs index 2d80adbd6d4..aaecd3b960c 100644 --- a/nexus/tests/integration_tests/endpoints.rs +++ b/nexus/tests/integration_tests/endpoints.rs @@ -222,7 +222,7 @@ lazy_static! { // The instance needs a network interface, too. pub static ref DEMO_INSTANCE_NIC_NAME: Name = - omicron_nexus::defaults::DEFAULT_PRIMARY_NIC_NAME.parse().unwrap(); + nexus_defaults::DEFAULT_PRIMARY_NIC_NAME.parse().unwrap(); pub static ref DEMO_INSTANCE_NIC_URL: String = format!("{}/{}", *DEMO_INSTANCE_NICS_URL, *DEMO_INSTANCE_NIC_NAME); pub static ref DEMO_INSTANCE_NIC_CREATE: params::NetworkInterfaceCreate = diff --git a/nexus/tests/integration_tests/instances.rs b/nexus/tests/integration_tests/instances.rs index 92d64b13ecd..a5ce9667550 100644 --- a/nexus/tests/integration_tests/instances.rs +++ b/nexus/tests/integration_tests/instances.rs @@ -208,7 +208,7 @@ async fn test_instances_create_reboot_halt( assert_eq!(network_interfaces[0].instance_id, instance.identity.id); assert_eq!( network_interfaces[0].identity.name, - omicron_nexus::defaults::DEFAULT_PRIMARY_NIC_NAME + nexus_defaults::DEFAULT_PRIMARY_NIC_NAME ); // Now, simulate completion of instance boot and check the state reported. diff --git a/nexus/tests/integration_tests/subnet_allocation.rs b/nexus/tests/integration_tests/subnet_allocation.rs index 70836f65515..149d86903a4 100644 --- a/nexus/tests/integration_tests/subnet_allocation.rs +++ b/nexus/tests/integration_tests/subnet_allocation.rs @@ -10,6 +10,7 @@ use dropshot::HttpErrorResponseBody; use http::method::Method; use http::StatusCode; use ipnetwork::Ipv4Network; +use nexus_defaults::NUM_INITIAL_RESERVED_IP_ADDRESSES; use nexus_test_utils::http_testing::AuthnMode; use nexus_test_utils::http_testing::NexusRequest; use nexus_test_utils::http_testing::RequestBuilder; @@ -23,7 +24,6 @@ use omicron_common::api::external::{ ByteCount, IdentityMetadataCreateParams, InstanceCpuCount, Ipv4Net, NetworkInterface, }; -use omicron_nexus::defaults::NUM_INITIAL_RESERVED_IP_ADDRESSES; use omicron_nexus::external_api::params; use std::net::Ipv4Addr;