Skip to content

Commit

Permalink
feat: add internal api monolith (#641)
Browse files Browse the repository at this point in the history
<!-- Please make sure there is an issue that this PR is correlated to. -->

## Changes

<!-- If there are frontend changes, please include screenshots. -->
  • Loading branch information
MasterPtato committed Apr 17, 2024
1 parent 66e49dd commit f25ffe4
Show file tree
Hide file tree
Showing 28 changed files with 228 additions and 137 deletions.
11 changes: 5 additions & 6 deletions docs/infrastructure/traefik/ROUTER_PRIORITIES.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Router Priorities

| Priority | Router | Notes |
| -------- | ------------------------- | ------------------------------------------------------------------------ |
| 50 | api-monolith | Lives at the root, so anything that wants a path needs a higher priority |
| 51 | _Other Bolt API services_ | |
| 60 | Media fallback (imagor) | Anything without a query will route here |
| 61 | Media with config | Anything with a query will route here |
| Priority | Router | Notes |
| -------- | ----------------------- | ---------------------------------------- |
| 50 | _Bolt API services_ | |
| 60 | Media fallback (imagor) | Anything without a query will route here |
| 61 | Media with config | Anything with a query will route here |
4 changes: 2 additions & 2 deletions infra/tf/k8s_infra/prometheus_rules.tf
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ resource "kubectl_manifest" "api_rules" {
rivet_api_request_duration_bucket{
watch="0",
path!~"/find|/create",
service!="rivet-api-route",
service!="rivet-api-internal-monolith",
le="+Inf"
} [2m]
)
Expand All @@ -309,7 +309,7 @@ resource "kubectl_manifest" "api_rules" {
rivet_api_request_duration_bucket{
watch="0",
path!~"/find|/create",
service!="rivet-api-route",
service!="rivet-api-internal-monolith",
le="1"
} [2m]
)
Expand Down
14 changes: 7 additions & 7 deletions infra/tf/k8s_infra/traefik.tf
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ resource "helm_release" "traefik" {
} : null

additionalArguments = [
"--providers.http.endpoint=http://rivet-api-route.rivet-service.svc.cluster.local/traefik/config/core?token=${module.traefik_secrets.values["rivet/api_route/token"]}",
"--providers.http.endpoint=http://rivet-api-internal-monolith.rivet-service.svc.cluster.local/route/traefik/config/core?token=${module.traefik_secrets.values["rivet/api_route/token"]}",
"--providers.http.pollInterval=2.5s",
# See docs/infrastructure/TIMEOUTS.md
"--entryPoints.web.transport.lifeCycle.graceTimeOut=60s",
Expand All @@ -76,12 +76,12 @@ resource "helm_release" "traefik" {
]

logs = {
# general = {
# level = "DEBUG"
# }
# access = {
# enabled = true
# }
general = {
level = "DEBUG"
}
access = {
enabled = true
}
}

deployment = {
Expand Down
4 changes: 2 additions & 2 deletions infra/tf/k8s_infra/traefik_tunnel.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ locals {
service_port = 4647
}

"api-route" = {
service = "rivet-api-route"
"api-internal" = {
service = "rivet-api-internal-monolith"
service_namespace = kubernetes_namespace.rivet_service.metadata[0].name
service_port = 80
}
Expand Down
4 changes: 3 additions & 1 deletion lib/bolt/config/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ pub struct ServiceMount {
#[serde(default)]
pub subdomain: Option<String>,
#[serde(default)]
pub path: Option<String>,
pub paths: Vec<String>,
#[serde(default)]
pub strip_prefix: Option<String>,
#[serde(default)]
pub add_path: Option<String>,
}
Expand Down
73 changes: 35 additions & 38 deletions lib/bolt/core/src/dep/k8s/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,10 +832,10 @@ fn build_ingress_router(
// Register all mounts with Traefik
// TODO: move this in to a single ingressroute crd for web and websecure
for (i, mount) in mounts.enumerate() {
// Build host rule
let mut rule = String::new();
let mut middlewares = Vec::new();

// Build host
// Build host for rule
if let (Some(domain_main), Some(domain_main_api)) =
(project_ctx.domain_main(), project_ctx.domain_main_api())
{
Expand All @@ -849,39 +849,42 @@ fn build_ingress_router(
rule.push_str(&format!("Host(`{domain}`)"));
}

// Build middlewares
let mut middlewares = Vec::new();

// Build path
if !rule.is_empty() {
rule.push_str(" && ");
}

let path = match &mount.path {
Some(path) => path,
None => "/",
};

rule.push_str(&format!("PathPrefix(`{path}`)"));

let mw_name = format!("{}-{i}-strip-prefix", svc_ctx.name());
middlewares.push(json!({
"apiVersion": "traefik.io/v1alpha1",
"kind": "Middleware",
"metadata": {
"name": mw_name,
"namespace": "rivet-service",
"labels": {
"traefik-instance": "main"
}
},
"spec": {
"stripPrefix": {
"prefixes": [ path ],
"forceSlash": true
// Build paths for rule
rule.push_str("(");
rule.push_str(
&mount
.paths
.iter()
.map(|path| format!("PathPrefix(`{path}`)"))
.collect::<Vec<_>>()
.join(" || "),
);
rule.push_str(")");

if let Some(strip_prefix) = &mount.strip_prefix {
let mw_name = format!("{}-{i}-strip-prefix", svc_ctx.name());
middlewares.push(json!({
"apiVersion": "traefik.io/v1alpha1",
"kind": "Middleware",
"metadata": {
"name": mw_name,
"namespace": "rivet-service",
"labels": {
"traefik-instance": "main"
}
},
"spec": {
"stripPrefix": {
"prefixes": [ strip_prefix ],
"forceSlash": true
}
}
}
}));
}));
}

if let Some(add_path) = &mount.add_path {
let mw_name = format!("{}-{i}-add-prefix", svc_ctx.name());
Expand Down Expand Up @@ -968,13 +971,7 @@ fn build_ingress_router(

specs.extend(middlewares);

let priority = if svc_ctx.name() == "api-monolith" {
// Default priority
50
} else {
// Override monolith's priority
51
};
let priority = 50;

// Build insecure router
specs.push(json!({
Expand Down Expand Up @@ -1049,7 +1046,7 @@ fn build_ingress_router(
}));
}

// if svc_ctx.name() == "api-cf-verification" {
// Add CF challenge routes
if svc_ctx.name() == "api-monolith" {
specs.push(json!({
"apiVersion": "traefik.io/v1alpha1",
Expand Down
19 changes: 18 additions & 1 deletion svc/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 svc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"api/cloud",
"api/group",
"api/identity",
"api/internal-monolith",
"api/job",
"api/kv",
"api/matchmaker",
Expand Down
2 changes: 1 addition & 1 deletion svc/api/admin/src/route/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde::Deserialize;

use crate::auth::Auth;

// MARK: GET /server_ip
// MARK: GET /cluster/server_ips
#[derive(Debug, Clone, Deserialize)]
pub struct ServerIpsQuery {
server_id: Option<Uuid>,
Expand Down
25 changes: 25 additions & 0 deletions svc/api/internal-monolith/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "api-internal-monolith"
version = "0.0.1"
authors = ["Rivet Gaming, LLC <developer@rivet.gg>"]
edition = "2021"
license = "Apache-2.0"

[dependencies]
api-helper = { path = "../../../lib/api-helper/build" }
async-trait = "0.1"
chirp-client = { path = "../../../lib/chirp/client" }
http = "0.2"
hyper = { version = "0.14", features = ["server", "http1", "tcp"] }
rivet-operation = { path = "../../../lib/operation/core" }
tokio = { version = "1.29" }
tracing = "0.1"
tracing-subscriber = { version = "0.3", default-features = false, features = [
"fmt",
"json",
"ansi",
] }
url = "2.2.2"

api-route = { path = "../route" }
api-provision = { path = "../provision" }
18 changes: 18 additions & 0 deletions svc/api/internal-monolith/Service.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[service]
name = "api-internal-monolith"
essential = true
priority = 70

[runtime]
kind = "rust"

# Has no mounts, internal only
[api]

[resources.single-node]
cpu = 50
memory = 64

[resources.distributed]
cpu = 1000
memory = 512
1 change: 1 addition & 0 deletions svc/api/internal-monolith/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod route;
5 changes: 5 additions & 0 deletions svc/api/internal-monolith/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use api_helper::start;

fn main() {
start(api_internal_monolith::route::handle);
}
30 changes: 30 additions & 0 deletions svc/api/internal-monolith/src/route/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use api_helper::define_router;
use hyper::{Body, Request, Response};
use rivet_operation::prelude::*;

pub async fn handle(
shared_client: chirp_client::SharedClientHandle,
pools: rivet_pools::Pools,
cache: rivet_cache::Cache,
ray_id: uuid::Uuid,
request: Request<Body>,
) -> Result<Response<Body>, http::Error> {
let response = Response::builder();

// Handle route
Router::handle(shared_client, pools, cache, ray_id, request, response).await
}

define_router! {
routes: {},
mounts: [
{
path: api_route::route::Router,
prefix: "route",
},
{
path: api_provision::route::Router,
prefix: "provision",
},
],
}
1 change: 1 addition & 0 deletions svc/api/internal-monolith/tests/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO:
1 change: 0 additions & 1 deletion svc/api/monolith/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@ api-kv = { path = "../kv" }
api-matchmaker = { path = "../matchmaker" }
api-module = { path = "../module" }
api-portal = { path = "../portal" }
api-provision = { path = "../provision" }
api-status = { path = "../status" }
Loading

0 comments on commit f25ffe4

Please sign in to comment.