Skip to content

Commit 3f01ca3

Browse files
committed
Add database layer for assigning an IP Pool to a service
- Add an enum for external networking services. - Add a join table for associating `ip_pool` records with specific services. - Add datastore methods for assigning / unassigning, with a bunch of tests. - The join table is empty and the datastore methods aren't consumed. This is all plumbing work, to support the planned Nexus API for operators and letting Reconfigurator read the pools it should use to plan external addresses for its zones.
1 parent 6b1ca22 commit 3f01ca3

15 files changed

Lines changed: 1092 additions & 2 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
//! Model types for assigning IP pools to individual external services.
6+
7+
use super::impl_enum_type;
8+
use diesel::Insertable;
9+
use diesel::Queryable;
10+
use diesel::Selectable;
11+
use nexus_db_schema::schema::external_service_ip_pool;
12+
use serde::Deserialize;
13+
use serde::Serialize;
14+
use uuid::Uuid;
15+
16+
impl_enum_type!(
17+
ExternalServiceKindEnum:
18+
19+
#[derive(
20+
Copy,
21+
Clone,
22+
Debug,
23+
PartialEq,
24+
Eq,
25+
Serialize,
26+
Deserialize,
27+
AsExpression,
28+
FromSqlRow,
29+
)]
30+
#[serde(rename_all = "snake_case")]
31+
pub enum ExternalServiceKind;
32+
33+
Nexus => b"nexus"
34+
BoundaryNtp => b"boundary_ntp"
35+
ExternalDns => b"external_dns"
36+
);
37+
38+
impl std::fmt::Display for ExternalServiceKind {
39+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40+
let s = match self {
41+
ExternalServiceKind::Nexus => "nexus",
42+
ExternalServiceKind::BoundaryNtp => "boundary_ntp",
43+
ExternalServiceKind::ExternalDns => "external_dns",
44+
};
45+
f.write_str(s)
46+
}
47+
}
48+
49+
/// Assignment of an IP pool to an external service (Nexus or boundary NTP).
50+
//
51+
// NOTE: The associations here intentionally avoid any semantics like
52+
// exclusivity or what an assignment means for the application. Each service
53+
// could be different in that respect, and we haven't really fleshed out enough
54+
// of the system to know how to use the records here. For now, we just enforce
55+
// basic sanity checks, like that operators cannot delete the last pool assigned
56+
// to a service, or concurrent operations on the pool and this record.
57+
#[derive(
58+
Queryable, Clone, Copy, Debug, PartialEq, Eq, Selectable, Insertable,
59+
)]
60+
#[diesel(table_name = external_service_ip_pool)]
61+
pub struct ExternalServiceIpPool {
62+
pub service: ExternalServiceKind,
63+
pub ip_pool_id: Uuid,
64+
}

nexus/db-model/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ pub mod ereport;
151151
mod ereporter_restart;
152152
mod ereporter_type;
153153
mod external_ip;
154+
mod external_service_ip_pool;
154155
mod external_subnet;
155156
pub mod fm;
156157
mod generation;
@@ -305,6 +306,7 @@ pub use ereport::Ereport;
305306
pub use ereporter_restart::*;
306307
pub use ereporter_type::*;
307308
pub use external_ip::*;
309+
pub use external_service_ip_pool::*;
308310
pub use external_subnet::*;
309311
pub use fm::{SitrepMetadata, SitrepVersion};
310312
pub use generation::*;

nexus/db-model/src/schema_versions.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::{collections::BTreeMap, sync::LazyLock};
1616
///
1717
/// This must be updated when you change the database schema. Refer to
1818
/// schema/crdb/README.adoc in the root of this repository for details.
19-
pub const SCHEMA_VERSION: Version = Version::new(291, 0, 0);
19+
pub const SCHEMA_VERSION: Version = Version::new(292, 0, 0);
2020

2121
/// List of all past database schema versions, in *reverse* order
2222
///
@@ -28,6 +28,7 @@ pub static KNOWN_VERSIONS: LazyLock<Vec<KnownVersion>> = LazyLock::new(|| {
2828
// | leaving the first copy as an example for the next person.
2929
// v
3030
// KnownVersion::new(next_int, "unique-dirname-with-the-sql-files"),
31+
KnownVersion::new(292, "external-service-ip-pool"),
3132
KnownVersion::new(291, "bgp-peer-src-addr"),
3233
KnownVersion::new(290, "saga-abandon-reason-orphaned"),
3334
KnownVersion::new(289, "normalize-service-external-ips"),

0 commit comments

Comments
 (0)