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

[cockroach-admin] turn API into a trait #6129

Merged
merged 3 commits into from
Jul 20, 2024
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
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 @@ -110,6 +112,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 @@ -263,7 +267,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
Loading