Skip to content

Commit

Permalink
feat: introduce network flag at terraform level
Browse files Browse the repository at this point in the history
  • Loading branch information
yHSJ committed Nov 26, 2024
1 parent 495b3fd commit 91d1b34
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 49 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ jobs:
TF_VAR_api_key: ${{ secrets.API_KEY }}

# Vars
TF_VAR_network_id: 0
TF_VAR_dmtr_port_name: preprod-4raar2
TF_VAR_dmtr_project_id: b55545f5-31e7-4e6b-81d6-22f4e6b5a144
TF_VAR_external_domain: ${{ matrix.region }}.hydra-doom.sundae.fi
Expand Down
5 changes: 5 additions & 0 deletions bootstrap/stage2/control-plane.tf
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ resource "kubernetes_deployment_v1" "control_plane" {
value = var.api_key
}

env {
name = "NETWORK_ID"
value = var.network_id
}

env {
name = "KUBERNETES_NAMESPACE"
value_from {
Expand Down
4 changes: 4 additions & 0 deletions bootstrap/stage2/frontend.tf
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ resource "kubernetes_deployment_v1" "frontend" {
value = "067d20be-8baa-49cb-b501-e004af358870"
}

env { name = "VITE_NETWORK_ID"
value = var.network_id
}

resources {
limits = {
cpu = var.frontend_resources.limits.cpu
Expand Down
4 changes: 4 additions & 0 deletions bootstrap/stage2/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ variable "autoscaler_max_batch" {
type = number
}

variable "network_id" {
type = number
}

variable "tolerations" {
type = list(object({
effect = string
Expand Down
2 changes: 2 additions & 0 deletions crates/operator/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct Config {
pub bucket: String,
pub init_aws_access_key_id: String,
pub init_aws_secret_access_key: String,
pub network_id: String,
// Autoscaler
pub autoscaler_delay: Duration,
pub autoscaler_low_watermark: usize,
Expand Down Expand Up @@ -85,6 +86,7 @@ impl Config {
autoscaler_max_batch: env::var("AUTOSCALER_MAX_BATCH")
.map(|x| x.parse().expect("Failed to parse AUTOSCALER_MAX_BATCH"))
.expect("Missing AUTOSCALER_MAX_BATCH env var."),
network_id: env::var("NETWORK_ID").expect("Missing NETWORK_ID env var."),
}
}
}
10 changes: 10 additions & 0 deletions crates/operator/src/custom_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ impl HydraDoomNode {
value: Some(config.api_key.clone()),
value_from: None,
},
EnvVar {
name: "NETWORK_ID".to_string(),
value: Some(config.network_id.clone()),
value_from: None,
},
]),
volume_mounts: Some(vec![VolumeMount {
name: "secret".to_string(),
Expand All @@ -331,6 +336,11 @@ impl HydraDoomNode {
Container {
name: "ai-1".to_string(),
image: Some(config.ai_image.clone()),
env: Some(vec![EnvVar {
name: "NETWORK_ID".to_string(),
value: Some(config.network_id.clone()),
value_from: None,
}]),
..Default::default()
},
];
Expand Down
16 changes: 14 additions & 2 deletions crates/rpc/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::{Context, Result};
use model::cluster::ClusterState;
use pallas::ledger::addresses::Network;
use rocket::{http::Method, routes};
use rocket_cors::{AllowedOrigins, CorsOptions};
use routes::{
Expand All @@ -15,6 +16,8 @@ use routes::{
stats::{global_stats, refresh_stats, StatsState},
};
use serde::Deserialize;
use std::env;
use tracing::error;

mod guards;
mod model;
Expand All @@ -32,12 +35,21 @@ async fn main() -> Result<()> {
let rocket = rocket::build();
let figment = rocket.figment();
let config = figment.extract::<Config>().context("invalid config")?;

let network: Network = env::var("NETWORK_ID")
.map(|network_str| {
network_str
.parse::<u8>()
.inspect_err(|_| error!("Invalid NETWORK_ID value, defaulting to 0"))
.unwrap_or_default()
})
.inspect_err(|_| error!("Missing NETWORK_ID env var, defaulting to zero"))
.unwrap_or_default()
.into();
// This will start a reflector (aka: local cache) of the cluster state. The `try_default`
// initializer assumes that this process is running within the cluster or that the local kubeconfig
// context is set to the cluster. If you wanted to connect to a remote cluster, you can use the
// `ClusterState::remote` initializer.
let cluster = ClusterState::try_new(&config.admin_key_file, config.remote).await?;
let cluster = ClusterState::try_new(&config.admin_key_file, config.remote, network).await?;
let stats = StatsState::new(
refresh_stats()
.await
Expand Down
9 changes: 8 additions & 1 deletion crates/rpc/src/model/cluster/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use anyhow::Context;
use futures_util::StreamExt as _;
use kube::runtime::{reflector::ObjectRef, WatchStreamExt as _};
use pallas::crypto::key::ed25519::SecretKey;
use pallas::ledger::addresses::Network;
use serde::Deserialize;

mod crd;
Expand All @@ -30,10 +31,15 @@ pub struct ClusterState {
watcher_handle: Arc<tokio::task::JoinHandle<()>>,
pub admin_sk: SecretKey,
pub remote: bool,
pub network: Network,
}

impl ClusterState {
pub async fn try_new(admin_key_file: &str, remote: bool) -> anyhow::Result<Self> {
pub async fn try_new(
admin_key_file: &str,
remote: bool,
network: Network,
) -> anyhow::Result<Self> {
let admin_key_envelope: KeyEnvelope = serde_json::from_reader(
File::open(admin_key_file).context("unable to open key file")?,
)?;
Expand Down Expand Up @@ -81,6 +87,7 @@ impl ClusterState {
watcher_handle: Arc::new(watcher_handle),
admin_sk,
remote,
network,
})
}

Expand Down
21 changes: 12 additions & 9 deletions crates/rpc/src/model/cluster/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ impl TryInto<Vec<u8>> for KeyEnvelope {
}

impl NodeClient {
pub fn new(resource: Arc<HydraDoomNode>, admin_key: SecretKey, remote: bool) -> Result<Self> {
pub fn new(
resource: Arc<HydraDoomNode>,
admin_key: SecretKey,
remote: bool,
network: Network,
) -> Result<Self> {
let status = resource.status.as_ref().ok_or(anyhow!("no status found"))?;

let (local_connection, remote_connection) = ConnectionInfo::from_resource(status)?;
Expand All @@ -77,7 +82,7 @@ impl NodeClient {
} else {
local_connection
},
tx_builder: TxBuilder::new(admin_key),
tx_builder: TxBuilder::new(admin_key, network),
};

Ok(node)
Expand All @@ -95,7 +100,7 @@ impl NodeClient {

let new_game_tx = self
.tx_builder
.new_game(player, utxos, Network::Testnet)
.new_game(player, utxos)
.context("failed to build transaction")?; // TODO: pass in network
debug!("new game tx: {}", hex::encode(&new_game_tx.tx_bytes));

Expand All @@ -113,13 +118,12 @@ impl NodeClient {
Ok(tx_hash)
}

//TODO: don't hardcode network
pub async fn start_game(&self) -> Result<Vec<u8>> {
let utxos = self.fetch_utxos().await.context("failed to fetch UTxOs")?;

let start_game_tx = self
.tx_builder
.start_game(utxos, Network::Testnet)
.start_game(utxos)
.context("failed to build transaction")?;

debug!("start game tx: {}", hex::encode(&start_game_tx.tx_bytes));
Expand All @@ -138,13 +142,12 @@ impl NodeClient {
Ok(tx_hash)
}

//TODO: don't hardcode network
pub async fn add_player(&self, player: Player) -> Result<Vec<u8>> {
let utxos = self.fetch_utxos().await.context("failed to fetch UTxOs")?;

let add_player_tx = self
.tx_builder
.add_player(player, utxos, Network::Testnet)
.add_player(player, utxos)
.context("failed to build transaction")?;

debug!("add player tx: {}", hex::encode(&add_player_tx.tx_bytes));
Expand All @@ -169,7 +172,7 @@ impl NodeClient {

let cleanup_tx = self
.tx_builder
.cleanup_game(utxos, Network::Testnet)
.cleanup_game(utxos)
.context("failed to build transaction")?;

debug!("cleanup tx: {}", hex::encode(&cleanup_tx.tx_bytes));
Expand All @@ -195,7 +198,7 @@ impl NodeClient {

let end_game_tx = self
.tx_builder
.end_game(None, utxos, Network::Testnet)
.end_game(None, utxos)
.context("failed to build transaction")?;

debug!("end_game_tx tx: {}", hex::encode(&end_game_tx.tx_bytes));
Expand Down
Loading

0 comments on commit 91d1b34

Please sign in to comment.