Skip to content

Commit

Permalink
Add strict mode (#142)
Browse files Browse the repository at this point in the history
Add profile.rs to handle "strict" mode.
  • Loading branch information
stephanzlatarev authored Jan 26, 2025
1 parent 2fc97b6 commit 752bf2b
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 26 deletions.
51 changes: 37 additions & 14 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ WORKDIR /app

RUN apt update && apt install -y lld clang

FROM chef as planner
FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

# Build Stage
FROM chef as builder
FROM chef AS builder
ARG CARGO_FLAGS="--release"

COPY --from=planner /app/recipe.json recipe.json
Expand All @@ -20,8 +20,9 @@ COPY . .
ARG FEATURES=""
RUN cargo build ${CARGO_FLAGS} --features="${FEATURES}"

###########################################################

FROM debian:bookworm-slim as k8s_controller
FROM debian:bookworm-slim AS k8s_controller
ARG APP=/app

RUN apt-get update \
Expand All @@ -30,7 +31,8 @@ RUN apt-get update \

ENV TZ=Etc/UTC

RUN mkdir -p ${APP}
RUN mkdir -m 777 ${APP} \
&& mkdir -m 777 /logs

WORKDIR ${APP}

Expand All @@ -39,7 +41,9 @@ COPY --from=builder /app/target/*/k8s_controller ${APP}/k8s_controller
ENTRYPOINT ["./k8s_controller"]
LABEL service=k8s_controller

FROM aiarena/arenaclient-bot-base:v0.6.1 as bot_controller
###########################################################

FROM aiarena/arenaclient-bot-base:v0.6.1 AS bot_controller
ARG APP=/app

RUN apt-get update \
Expand All @@ -48,7 +52,10 @@ RUN apt-get update \

ENV TZ=Etc/UTC

RUN mkdir -p ${APP}
RUN mkdir -m 777 ${APP} \
&& mkdir -m 777 /bots \
&& mkdir -m 777 /logs \
&& chmod -R 777 /root

WORKDIR ${APP}

Expand All @@ -57,22 +64,30 @@ COPY --from=builder /app/target/*/bot_controller ${APP}/bot_controller
ENTRYPOINT ["./bot_controller"]
LABEL service=bot_controller

FROM aiarena/arenaclient-sc2-base:4.10-bookworm as sc2_controller
ARG APP=/app
###########################################################

FROM aiarena/arenaclient-sc2-base:4.10-bookworm AS sc2_controller
ARG APP=/app

RUN apt-get update \
&& apt-get install -y ca-certificates tzdata p7zip-full \
&& rm -rf /var/lib/apt/lists/*

RUN mkdir -m 777 ${APP} \
&& mkdir -m 777 /logs \
&& chmod -R 777 /root \
&& chmod -R 777 /tmp

WORKDIR ${APP}

COPY --from=builder /app/target/*/sc2_controller ${APP}/sc2_controller

ENTRYPOINT ["./sc2_controller"]
LABEL service=sc2_controller

FROM debian:bookworm-slim as proxy_controller
###########################################################

FROM debian:bookworm-slim AS proxy_controller
ARG APP=/app

USER root
Expand All @@ -83,14 +98,17 @@ RUN apt-get update \
&& apt-get upgrade --assume-yes --quiet=2 \
&& apt-get install --assume-yes --no-install-recommends --no-show-upgraded


RUN apt-get update \
&& apt-get install -y ca-certificates tzdata p7zip-full \
&& rm -rf /var/lib/apt/lists/*

ENV TZ=Etc/UTC

RUN mkdir -p ${APP}
RUN mkdir -m 777 ${APP} \
&& mkdir -m 777 /bots \
&& mkdir -m 777 /logs \
&& mkdir -m 777 /replays \
&& chmod -R 777 /tmp

WORKDIR ${APP}

Expand All @@ -99,8 +117,9 @@ COPY --from=builder /app/target/*/proxy_controller ${APP}/proxy_controller
ENTRYPOINT ["./proxy_controller"]
LABEL service=proxy_controller

###########################################################

FROM aiarena/arenaclient-bot-base:v0.6.1 as combined
FROM aiarena/arenaclient-bot-base:v0.6.1 AS combined
ARG APP=/app

USER root
Expand All @@ -111,7 +130,6 @@ RUN apt-get update \
&& apt-get upgrade --assume-yes --quiet=2 \
&& apt-get install --assume-yes --no-install-recommends --no-show-upgraded


RUN apt-get update \
&& apt-get install -y ca-certificates tzdata p7zip-full supervisor \
&& rm -rf /var/lib/apt/lists/*
Expand All @@ -120,7 +138,12 @@ RUN mkdir -p /var/log/supervisor

ENV TZ=Etc/UTC

RUN mkdir -p ${APP}
RUN mkdir -m 777 ${APP} \
&& mkdir -m 777 /bots \
&& mkdir -m 777 /logs \
&& mkdir -m 777 /replays \
&& chmod -R 777 /root \
&& chmod -R 777 /tmp

WORKDIR ${APP}
COPY --from=sc2_controller /root/StarCraftII/ /root/StarCraftII/
Expand Down
22 changes: 10 additions & 12 deletions k8s_controller/src/k8s_processor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{io::BufReader, time::Instant};

use crate::{arenaclient::Arenaclient, k8s_config::K8sConfig};
use crate::{arenaclient::Arenaclient, k8s_config::K8sConfig, profile::Profile};
use anyhow::Context;
use common::api::api_reference::aiarena::aiarena_api_client::AiArenaApiClient;
use futures_util::future::join;
Expand All @@ -16,7 +16,6 @@ use tracing::{debug, error, info, trace};

pub async fn process(settings: K8sConfig) {
let interval = Duration::from_secs(settings.interval_seconds);
let job_yaml = include_str!("../templates/ac-job.yaml");
let mut arenaclients = match load_arenaclient_details(&settings.arenaclients_json_path) {
Ok(c) => c,
Err(e) => {
Expand Down Expand Up @@ -63,10 +62,8 @@ pub async fn process(settings: K8sConfig) {
}
}

let mut job_data: Job = serde_yaml::from_str(job_yaml).unwrap();

let res = join(
schedule_jobs(&settings, &jobs, &mut job_data, &arenaclients),
schedule_jobs(&settings, &jobs, &arenaclients),
delete_old_jobs(&settings, &jobs, &arenaclients),
)
.await;
Expand All @@ -84,7 +81,6 @@ pub async fn process(settings: K8sConfig) {
async fn schedule_jobs(
settings: &K8sConfig,
jobs: &Api<Job>,
job_data: &mut Job,
arenaclients: &[Arenaclient],
) -> anyhow::Result<()> {
for ac in arenaclients
Expand All @@ -96,6 +92,8 @@ async fn schedule_jobs(
debug!("Getting new match for AC {:?}", ac.name);
match api_client.get_match().await {
Ok(new_match) => {
let mut job_data: Job = Profile::get(&new_match).job_descriptor;

let new_name = if settings.job_prefix.is_empty() {
format!("{}-{}", ac.name.replace('_', "-"), new_match.id)
} else {
Expand All @@ -107,22 +105,22 @@ async fn schedule_jobs(
)
};
debug!("Setting job name:{:?}", &new_name);
set_job_name(job_data, &new_name);
set_job_name(&mut job_data, &new_name);
debug!("Setting API token");
set_api_token(job_data, &ac.token)?;
set_api_token(&mut job_data, &ac.token)?;
debug!("Setting job labels");
set_job_labels(job_data, &ac.name, new_match.id)?;
set_job_labels(&mut job_data, &ac.name, new_match.id)?;
debug!("Setting configmap name");
let new_configmap_name = if settings.job_prefix.is_empty() {
"arenaclient-config".to_string()
} else {
format!("{}-{}", settings.job_prefix, "arenaclient-config")
};
set_config_configmap_name(job_data, &new_configmap_name)?;
set_config_configmap_name(&mut job_data, &new_configmap_name)?;
if let Some(version) = &settings.version {
debug!("Setting image tags");
set_image_tags(
job_data,
&mut job_data,
&[
"proxy-controller",
"bot-controller-1",
Expand All @@ -138,7 +136,7 @@ async fn schedule_jobs(
new_match.id, &ac.name
);
debug!("Creating job");
jobs.create(&PostParams::default(), job_data).await?;
jobs.create(&PostParams::default(), &mut job_data).await?;
debug!("Job created");
}
Err(e) => {
Expand Down
1 change: 1 addition & 0 deletions k8s_controller/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod arenaclient;
// mod old;
mod k8s_config;
mod k8s_processor;
mod profile;
mod state;
// #[cfg(feature = "swagger")]
// mod docs;
Expand Down
42 changes: 42 additions & 0 deletions k8s_controller/src/profile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use common::models::aiarena::aiarena_match::AiArenaMatch;
use k8s_openapi::api::batch::v1::Job;
use tracing::debug;

// Profiles are used to run matches in a specific Kubernetes configuration that depends on the given match.
// For example, certain competitions may require less or more time for the match to complete;
// certain bots may require more memory to run; and
// certain matches may be run with a debug profile with verbose logs for the bot author to review.

pub struct Profile {
pub job_descriptor: Job,
}

impl Profile {
pub fn get(arena_match: &AiArenaMatch) -> Self {
let profile_name = select_profile(arena_match);

debug!("Using job profile: {:?}", profile_name);

Self {
job_descriptor: serde_yaml::from_str(load_template(profile_name)).unwrap(),
}
}
}

fn select_profile(arena_match: &AiArenaMatch) -> &str {
match arena_match.map.name.as_str() {
// Map "DefendersLandingAIE" activates "strict" profile.
// This map is used until API /api/arenaclient/v2/next-match/ gives the competition for each match.
"DefendersLandingAIE" => "strict",

// Use the default template for all other maps
_ => "default",
}
}

fn load_template(profile_name: &str) -> &str {
match profile_name {
"strict" => include_str!("../templates/ac-job-strict.yaml"),
_ => include_str!("../templates/ac-job.yaml"),
}
}
Loading

0 comments on commit 752bf2b

Please sign in to comment.