Skip to content

Commit

Permalink
[cockroach-admin] turn API into a trait (#6129)
Browse files Browse the repository at this point in the history
Move the more complex types into a shared cockroach-admin-types crate,
and the simple wrappers into the cockroach-admin-api crate.
  • Loading branch information
sunshowers authored Jul 20, 2024
1 parent 204ea7d commit 46b1d37
Show file tree
Hide file tree
Showing 18 changed files with 682 additions and 637 deletions.
30 changes: 30 additions & 0 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ members = [
"clients/sled-agent-client",
"clients/wicketd-client",
"cockroach-admin",
"cockroach-admin/api",
"cockroach-admin/types",
"common",
"dev-tools/crdb-seed",
"dev-tools/omdb",
Expand Down Expand Up @@ -111,6 +113,8 @@ default-members = [
"clients/sled-agent-client",
"clients/wicketd-client",
"cockroach-admin",
"cockroach-admin/api",
"cockroach-admin/types",
"common",
"dev-tools/crdb-seed",
"dev-tools/omdb",
Expand Down Expand Up @@ -265,7 +269,9 @@ ciborium = "0.2.2"
cfg-if = "1.0"
chrono = { version = "0.4", features = [ "serde" ] }
clap = { version = "4.5", features = ["cargo", "derive", "env", "wrap_help"] }
cockroach-admin-api = { path = "cockroach-admin/api" }
cockroach-admin-client = { path = "clients/cockroach-admin-client" }
cockroach-admin-types = { path = "cockroach-admin/types" }
colored = "2.1"
const_format = "0.2.32"
cookie = "0.18"
Expand Down
2 changes: 2 additions & 0 deletions cockroach-admin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ anyhow.workspace = true
camino.workspace = true
chrono.workspace = true
clap.workspace = true
cockroach-admin-api.workspace = true
cockroach-admin-types.workspace = true
csv.workspace = true
dropshot.workspace = true
http.workspace = true
Expand Down
17 changes: 17 additions & 0 deletions cockroach-admin/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "cockroach-admin-api"
version = "0.1.0"
edition = "2021"
license = "MPL-2.0"

[lints]
workspace = true

[dependencies]
cockroach-admin-types.workspace = true
dropshot.workspace = true
omicron-common.workspace = true
omicron-uuid-kinds.workspace = true
omicron-workspace-hack.workspace = true
schemars.workspace = true
serde.workspace = true
76 changes: 76 additions & 0 deletions cockroach-admin/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// 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 cockroach_admin_types::{NodeDecommission, NodeStatus};
use dropshot::{HttpError, HttpResponseOk, RequestContext, TypedBody};
use omicron_uuid_kinds::OmicronZoneUuid;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[dropshot::api_description {
module = "cockroach_admin_api_mod",
}]
pub trait CockroachAdminApi {
type Context;

/// Get the status of all nodes in the CRDB cluster.
#[endpoint {
method = GET,
path = "/node/status",
}]
async fn node_status(
rqctx: RequestContext<Self::Context>,
) -> Result<HttpResponseOk<ClusterNodeStatus>, HttpError>;

/// Get the CockroachDB node ID of the local cockroach instance.
#[endpoint {
method = GET,
path = "/node/id",
}]
async fn local_node_id(
rqctx: RequestContext<Self::Context>,
) -> Result<HttpResponseOk<LocalNodeId>, HttpError>;

/// Decommission a node from the CRDB cluster.
#[endpoint {
method = POST,
path = "/node/decommission",
}]
async fn node_decommission(
rqctx: RequestContext<Self::Context>,
body: TypedBody<NodeId>,
) -> Result<HttpResponseOk<NodeDecommission>, HttpError>;
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct ClusterNodeStatus {
pub all_nodes: Vec<NodeStatus>,
}

/// CockroachDB Node ID
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct LocalNodeId {
/// The ID of this Omicron zone.
///
/// This is included to ensure correctness even if a socket address on a
/// sled is reused for a different zone; if our caller is trying to
/// determine the node ID for a particular Omicron CockroachDB zone, they'll
/// contact us by socket address. We include our zone ID in the response for
/// their confirmation that we are the zone they intended to contact.
pub zone_id: OmicronZoneUuid,
// CockroachDB node IDs are integers, in practice, but our use of them is as
// input and output to the `cockroach` CLI. We use a string which is a bit
// more natural (no need to parse CLI output or stringify an ID to send it
// as input) and leaves open the door for the format to change in the
// future.
pub node_id: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct NodeId {
pub node_id: String,
}
5 changes: 0 additions & 5 deletions cockroach-admin/src/bin/cockroach-admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ use std::net::SocketAddrV6;
#[derive(Debug, Parser)]
#[clap(name = "cockroach-admin", about = "Omicron CRDB cluster admin server")]
enum Args {
/// Print the OpenAPI Spec document and exit
Openapi,

/// Start the CRDB admin server
Run {
/// Path to the `cockroach` CLI
Expand Down Expand Up @@ -57,8 +54,6 @@ async fn main_impl() -> Result<(), CmdError> {
let args = Args::parse();

match args {
Args::Openapi => omicron_cockroach_admin::run_openapi()
.map_err(|e| CmdError::Failure(anyhow!(e))),
Args::Run {
path_to_cockroach_binary,
cockroach_address,
Expand Down
Loading

0 comments on commit 46b1d37

Please sign in to comment.