Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omdb networking #4147

Merged
merged 6 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dev-tools/omdb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ textwrap.workspace = true
tokio = { workspace = true, features = [ "full" ] }
uuid.workspace = true
omicron-workspace-hack = { version = "0.1", path = "../../workspace-hack" }
ipnetwork.workspace = true

[dev-dependencies]
expectorate.workspace = true
Expand Down
180 changes: 180 additions & 0 deletions dev-tools/omdb/src/bin/omdb/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
//! would be the only consumer -- and in that case it's okay to query the
//! database directly.

// NOTE: eminates from Tabled macros
#![allow(clippy::useless_vec)]

use crate::Omdb;
use anyhow::anyhow;
use anyhow::bail;
Expand All @@ -30,7 +33,9 @@ use nexus_db_model::DnsGroup;
use nexus_db_model::DnsName;
use nexus_db_model::DnsVersion;
use nexus_db_model::DnsZone;
use nexus_db_model::ExternalIp;
use nexus_db_model::Instance;
use nexus_db_model::Project;
use nexus_db_model::Region;
use nexus_db_model::Sled;
use nexus_db_model::Zpool;
Expand Down Expand Up @@ -86,6 +91,8 @@ enum DbCommands {
Sleds,
/// Print information about customer instances
Instances,
/// Print information about the network
Network(NetworkArgs),
}

#[derive(Debug, Args)]
Expand Down Expand Up @@ -170,6 +177,22 @@ enum ServicesCommands {
ListBySled,
}

#[derive(Debug, Args)]
struct NetworkArgs {
#[command(subcommand)]
command: NetworkCommands,

/// Print out raw data structures from the data store.
#[clap(long)]
verbose: bool,
}

#[derive(Debug, Subcommand)]
enum NetworkCommands {
/// List external IPs
ListEips,
}

impl DbArgs {
/// Run a `omdb db` subcommand.
pub(crate) async fn run_cmd(
Expand Down Expand Up @@ -269,6 +292,13 @@ impl DbArgs {
DbCommands::Instances => {
cmd_db_instances(&datastore, self.fetch_limit).await
}
DbCommands::Network(NetworkArgs {
command: NetworkCommands::ListEips,
verbose,
}) => {
cmd_db_eips(&opctx, &datastore, self.fetch_limit, *verbose)
.await
}
}
}
}
Expand Down Expand Up @@ -1098,6 +1128,156 @@ async fn cmd_db_dns_names(
Ok(())
}

async fn cmd_db_eips(
opctx: &OpContext,
datastore: &DataStore,
limit: NonZeroU32,
verbose: bool,
) -> Result<(), anyhow::Error> {
use db::schema::external_ip::dsl;
let ips: Vec<ExternalIp> = dsl::external_ip
.filter(dsl::time_deleted.is_null())
.select(ExternalIp::as_select())
.get_results_async(&*datastore.pool_connection_for_tests().await?)
.await?;

check_limit(&ips, limit, || String::from("listing external ips"));

struct PortRange {
first: u16,
last: u16,
}

impl Display for PortRange {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}/{}", self.first, self.last)
}
}

#[derive(Tabled)]
enum Owner {
Instance { project: String, name: String },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always wonder if the tool should report ids (which are immutable and unique across silos) or names like this that potentially save someone a separate lookup (or both, but that uses up valuable space too). It's all tradeoffs -- just mentioning it to see what you think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I explicitly chose names here - as my use case for this is mapping external IPs to something immediately tangible. I added an option for verbose output that provides the raw data structures from the DB that shows references based on UUID.

Service { kind: String },
None,
}

impl Display for Owner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Instance { project, name } => {
write!(f, "Instance {project}/{name}")
}
Self::Service { kind } => write!(f, "Service {kind}"),
Self::None => write!(f, "None"),
}
}
}

#[derive(Tabled)]
struct IpRow {
ip: ipnetwork::IpNetwork,
ports: PortRange,
kind: String,
owner: Owner,
}

if verbose {
for ip in &ips {
if verbose {
println!("{ip:#?}");
}
}
return Ok(());
}

let mut rows = Vec::new();

for ip in &ips {
let owner = if let Some(owner_id) = ip.parent_id {
if ip.is_service {
let service = match LookupPath::new(opctx, datastore)
.service_id(owner_id)
.fetch()
.await
{
Ok(instance) => instance,
Err(e) => {
eprintln!(
"error looking up service with id {owner_id}: {e}"
);
continue;
}
};
Owner::Service { kind: format!("{:?}", service.1.kind) }
} else {
use db::schema::instance::dsl as instance_dsl;
let instance = match instance_dsl::instance
.filter(instance_dsl::id.eq(owner_id))
.limit(1)
.select(Instance::as_select())
.load_async(&*datastore.pool_connection_for_tests().await?)
.await
.context("loading requested instance")?
.pop()
{
Some(instance) => instance,
None => {
eprintln!("instance with id {owner_id} not found");
continue;
}
};

use db::schema::project::dsl as project_dsl;
let project = match project_dsl::project
.filter(project_dsl::id.eq(instance.project_id))
.limit(1)
.select(Project::as_select())
.load_async(&*datastore.pool_connection_for_tests().await?)
.await
.context("loading requested project")?
.pop()
{
Some(instance) => instance,
None => {
eprintln!(
"project with id {} not found",
instance.project_id
);
continue;
}
};

Owner::Instance {
project: project.name().to_string(),
name: instance.name().to_string(),
}
}
} else {
Owner::None
};

let row = IpRow {
ip: ip.ip,
ports: PortRange {
first: ip.first_port.into(),
last: ip.last_port.into(),
},
kind: format!("{:?}", ip.kind),
owner,
};
rows.push(row);
}

rows.sort_by(|a, b| a.ip.cmp(&b.ip));
let table = tabled::Table::new(rows)
.with(tabled::settings::Style::empty())
.to_string();

println!("{}", table);

Ok(())
}

fn print_name(
prefix: &str,
name: &str,
Expand Down
1 change: 1 addition & 0 deletions dev-tools/omdb/tests/test_all_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ async fn test_omdb_usage_errors() {
&["db", "dns", "diff"],
&["db", "dns", "names"],
&["db", "services"],
&["db", "network"],
&["nexus"],
&["nexus", "background-tasks"],
&["sled-agent"],
Expand Down
20 changes: 20 additions & 0 deletions dev-tools/omdb/tests/usage_errors.out
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Commands:
services Print information about control plane services
sleds Print information about sleds
instances Print information about customer instances
network Print information about the network
help Print this message or the help of the given subcommand(s)

Options:
Expand All @@ -112,6 +113,7 @@ Commands:
services Print information about control plane services
sleds Print information about sleds
instances Print information about customer instances
network Print information about the network
help Print this message or the help of the given subcommand(s)

Options:
Expand Down Expand Up @@ -186,6 +188,24 @@ Commands:
Options:
-h, --help Print help
=============================================
EXECUTING COMMAND: omdb ["db", "network"]
termination: Exited(2)
---------------------------------------------
stdout:
---------------------------------------------
stderr:
Print information about the network

Usage: omdb db network [OPTIONS] <COMMAND>

Commands:
list-eips List external IPs
help Print this message or the help of the given subcommand(s)

Options:
--verbose Print out raw data structures from the data store
-h, --help Print help
=============================================
EXECUTING COMMAND: omdb ["nexus"]
termination: Exited(2)
---------------------------------------------
Expand Down
Loading