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

feat(prover): Add cluster name autodetection #3227

Merged
merged 1 commit into from
Nov 5, 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
2 changes: 0 additions & 2 deletions prover/crates/bin/prover_autoscaler/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ pub struct ProverAutoscalerAgentConfig {
/// List of namespaces to watch.
#[serde(default = "ProverAutoscalerAgentConfig::default_namespaces")]
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,
Expand Down
32 changes: 31 additions & 1 deletion prover/crates/bin/prover_autoscaler/src/k8s/watcher.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
use std::{collections::HashMap, sync::Arc};

use anyhow::Context;
use chrono::{DateTime, Utc};
use futures::{stream, StreamExt, TryStreamExt};
use k8s_openapi::api;
use kube::{
api::{Api, ResourceExt},
runtime::{watcher, WatchStreamExt},
};
use reqwest::{
header::{HeaderMap, HeaderValue},
Method,
};
use tokio::sync::Mutex;
use zksync_utils::http_with_retries::send_request_with_retries;

use crate::cluster_types::{Cluster, Deployment, Namespace, Pod, ScaleEvent};

Expand All @@ -17,13 +23,37 @@ pub struct Watcher {
pub cluster: Arc<Mutex<Cluster>>,
}

async fn get_cluster_name() -> anyhow::Result<String> {
let mut headers = HeaderMap::new();
headers.insert("Metadata-Flavor", HeaderValue::from_static("Google"));
let url = "http://metadata.google.internal/computeMetadata/v1/instance/attributes/cluster-name";
let response = send_request_with_retries(url, 5, Method::GET, Some(headers), None).await;
response
.map_err(|err| anyhow::anyhow!("Failed fetching response from url: {url}: {err:?}"))?
.text()
.await
.context("Failed to read response as text")
}

impl Watcher {
pub fn new(client: kube::Client, cluster_name: String, namespaces: Vec<String>) -> Self {
pub async fn new(
client: kube::Client,
cluster_name: Option<String>,
namespaces: Vec<String>,
) -> Self {
let mut ns = HashMap::new();
namespaces.into_iter().for_each(|n| {
ns.insert(n, Namespace::default());
});

let cluster_name = match cluster_name {
Some(c) => c,
None => get_cluster_name()
.await
.expect("Load cluster_name from GCP"),
};
tracing::info!("Agent cluster name is {cluster_name}");

Self {
client,
cluster: Arc::new(Mutex::new(Cluster {
Expand Down
10 changes: 3 additions & 7 deletions prover/crates/bin/prover_autoscaler/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,16 @@ async fn main() -> anyhow::Result<()> {

match opt.job {
AutoscalerType::Agent => {
let cluster = opt
.cluster_name
.context("cluster_name is required for Agent")?;
tracing::info!("Starting ProverAutoscaler Agent for cluster {}", cluster);
tracing::info!("Starting ProverAutoscaler Agent");
let agent_config = general_config.agent_config.context("agent_config")?;
let exporter_config = PrometheusExporterConfig::pull(agent_config.prometheus_port);
tasks.push(tokio::spawn(exporter_config.run(stop_receiver.clone())));

let _ = rustls::crypto::ring::default_provider().install_default();
let client = kube::Client::try_default().await?;

// 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 watcher =
Watcher::new(client.clone(), opt.cluster_name, agent_config.namespaces).await;
let scaler = Scaler::new(client, agent_config.dry_run);
tasks.push(tokio::spawn(watcher.clone().run()));
tasks.push(tokio::spawn(agent::run_server(
Expand Down
Loading