Skip to content

Commit

Permalink
Add $PATH variable; tolerate any path suffix for creating jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
kklingenberg committed Dec 4, 2023
1 parent 067640f commit d01a5de
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "k8s-job-dispatcher"
version = "0.3.1"
version = "0.4.0"
edition = "2021"

[dependencies]
Expand Down
9 changes: 6 additions & 3 deletions src/jq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn jq_extensions() -> impl Iterator<Item = (String, usize, Native)> {

/// Compile a filter.
pub fn compile(filter: &str) -> Result<Filter> {
let mut defs = ParseCtx::new(vec!["ENV".to_string()]);
let mut defs = ParseCtx::new(vec!["ENV".to_string(), "PATH".to_string()]);
defs.insert_natives(jaq_core::core());
defs.insert_natives(jq_extensions());
defs.insert_defs(jaq_std::std());
Expand All @@ -62,10 +62,13 @@ pub fn compile(filter: &str) -> Result<Filter> {

/// Execute a compiled filter against an input, and produce the first
/// serde_json value.
pub fn first_result(filter: &Filter, input: Value) -> Option<Result<Value>> {
pub fn first_result(filter: &Filter, input: Value, path: &str) -> Option<Result<Value>> {
let inputs = RcIter::new(core::iter::empty());
let mut outputs = filter
.run((Ctx::new([jq_env()], &inputs), Val::from(input)))
.run((
Ctx::new([jq_env(), Val::str(path.to_string())], &inputs),
Val::from(input),
))
.map(|r| r.map(Value::from).map_err(|e| anyhow!(e.to_string())));
let first_result = outputs.next();
if outputs.next().is_some() {
Expand Down
19 changes: 15 additions & 4 deletions src/k8s_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
use crate::jq;
use crate::state;

use actix_web::{error, get, post, web, HttpResponse, Responder, Result};
use actix_web::{error, get, routes, web, HttpResponse, Responder, Result};
use k8s_openapi::api::batch::v1::{Job, JobStatus};
use kube::{core::params::PostParams, Error};
use serde::Serialize;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tracing::{debug, info};

Expand All @@ -18,14 +18,25 @@ struct JobSummary {
status: Option<JobStatus>,
}

/// A container for the create_job path information.
#[derive(Deserialize)]
struct PathInfo {
path: Option<String>,
}

/// Create a K8s job by converting the request body to a job manifest.
#[routes]
#[post("/job")]
#[post("/job/{path:.*}")]
async fn create_job(
path: web::Path<PathInfo>,
body: web::Json<Value>,
state: web::Data<state::AppState>,
) -> Result<impl Responder> {
debug!("Job creation request: {:?}", body);
let raw_manifest = jq::first_result(&state.filter, body.into_inner())
let path = format!("/job/{}", path.path.clone().unwrap_or_default());
let path = path.strip_suffix('/').map(String::from).unwrap_or(path);
debug!("Job creation request at {:?}: {:?}", path, body);
let raw_manifest = jq::first_result(&state.filter, body.into_inner(), &path)
.ok_or_else(|| error::ErrorBadRequest("Filter didn't produce results"))?
.map_err(|e| error::ErrorBadRequest(format!("Filter failed: {:?}", e)))?;
debug!("Job raw manifest: {:?}", raw_manifest);
Expand Down

0 comments on commit d01a5de

Please sign in to comment.