Skip to content

Commit

Permalink
Add dry_run config option for Agent.
Browse files Browse the repository at this point in the history
  • Loading branch information
yorik committed Oct 18, 2024
1 parent 05d6807 commit 5cfbeb6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
7 changes: 7 additions & 0 deletions core/lib/config/src/configs/prover_autoscaler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub struct ProverAutoscalerAgentConfig {
pub namespaces: Vec<String>,
/// Watched cluster name. Also can be set via flag.
pub cluster_name: Option<String>,
/// If dry-run enabled don't do any k8s updates, just report success.
#[serde(default = "ProverAutoscalerAgentConfig::default_dry_run")]
pub dry_run: bool,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Default)]
Expand Down Expand Up @@ -101,6 +104,10 @@ impl ProverAutoscalerAgentConfig {
pub fn default_namespaces() -> Vec<String> {
vec!["prover-blue".to_string(), "prover-red".to_string()]
}

pub fn default_dry_run() -> bool {
true
}
}

impl ProverAutoscalerScalerConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ message ProverAutoscalerAgentConfig {
optional uint32 http_port = 2; // required
repeated string namespaces = 3; // optional
optional string cluster_name = 4; // optional
optional bool dry_run = 5; // optional
}

message ProtocolVersion {
Expand Down
4 changes: 3 additions & 1 deletion core/lib/protobuf_config/src/prover_autoscaler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use anyhow::Context as _;
use anyhow::Context;
use time::Duration;
use zksync_config::configs::{self, prover_autoscaler::Gpu};
use zksync_protobuf::{read_optional, repr::ProtoRepr, required, ProtoFmt};
Expand Down Expand Up @@ -42,6 +42,7 @@ impl ProtoRepr for proto::ProverAutoscalerAgentConfig {
.context("http_port")?,
namespaces: self.namespaces.to_vec(),
cluster_name: Some("".to_string()),
dry_run: self.dry_run.unwrap_or(Self::Type::default_dry_run()),
})
}

Expand All @@ -51,6 +52,7 @@ impl ProtoRepr for proto::ProverAutoscalerAgentConfig {
http_port: Some(this.http_port.into()),
namespaces: this.namespaces.clone(),
cluster_name: this.cluster_name.clone(),
dry_run: Some(this.dry_run),
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions prover/crates/bin/prover_autoscaler/src/k8s/scaler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ use kube::api::{Api, Patch, PatchParams};
#[derive(Clone)]
pub struct Scaler {
pub client: kube::Client,
dry_run: bool,
}

impl Scaler {
pub fn new(client: kube::Client, dry_run: bool) -> Self {
Self { client, dry_run }
}

pub async fn scale(&self, namespace: &str, name: &str, size: i32) -> anyhow::Result<()> {
let deployments: Api<api::apps::v1::Deployment> =
Api::namespaced(self.client.clone(), namespace);
Expand All @@ -18,6 +23,16 @@ impl Scaler {
"replicas": size
}
});

if self.dry_run {
tracing::info!(
"Dry run of scaled deployment/{} to {} replica(s).",
name,
size
);
return Ok(());
}

let pp = PatchParams::default();
deployments.patch(name, &pp, &Patch::Merge(patch)).await?;
tracing::info!("Scaled deployment/{} to {} replica(s).", name, size);
Expand Down
2 changes: 1 addition & 1 deletion prover/crates/bin/prover_autoscaler/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async fn main() -> anyhow::Result<()> {
// TODO: maybe get cluster name from curl -H "Metadata-Flavor: Google"
// http://metadata.google.internal/computeMetadata/v1/instance/attributes/cluster-name
let watcher = Watcher::new(client.clone(), cluster, agent_config.namespaces);
let scaler = Scaler { client };
let scaler = Scaler::new(client, agent_config.dry_run);
tasks.push(tokio::spawn(watcher.clone().run()));
tasks.push(tokio::spawn(agent::run_server(
agent_config.http_port,
Expand Down

0 comments on commit 5cfbeb6

Please sign in to comment.