webhook_bridge/src/kubernetes.rs

70 lines
2.6 KiB
Rust
Raw Normal View History

use kube::api::PostParams;
use kube::Api;
use kube::CustomResourceExt;
use tracing::debug;
use crate::crd_pipeline_run::PipelineParam;
use crate::crd_pipeline_run::PipelineRun;
use crate::discovery::PipelineTemplate;
use crate::hook_push::HookPush;
use crate::hook_push::PipelineParamters;
pub(crate) async fn run_pipelines(
hook: HookPush,
pipelines: Vec<PipelineTemplate>,
kubernetes_client: kube::Client,
) -> Result<(), Box<dyn std::error::Error>> {
let jobs: Api<PipelineRun> = Api::namespaced(kubernetes_client, "lighthouse");
// let jobs: Api<PipelineRun> = Api::default_namespaced(kubernetes_client);
tracing::info!("Using crd: {}", serde_json::to_string(&PipelineRun::crd())?);
for mut pipeline in pipelines {
debug!("Kicking off {}", pipeline.name);
if pipeline.pipeline.spec.params.is_none() {
pipeline.pipeline.spec.params = Some(Vec::new());
}
if let Some(param_list) = pipeline.pipeline.spec.params.as_mut() {
param_list.push(PipelineParam {
name: Some("JOB_NAME".to_owned()),
value: Some(serde_json::Value::String(pipeline.name)),
});
param_list.push(PipelineParam {
name: Some("REPO_OWNER".to_owned()),
value: Some(serde_json::Value::String(
hook.get_repo_owner()?.into_owned(),
)),
});
param_list.push(PipelineParam {
name: Some("REPO_NAME".to_owned()),
value: Some(serde_json::Value::String(
hook.get_repo_name()?.into_owned(),
)),
});
let hook_repo_url = hook.get_repo_url()?;
param_list.push(PipelineParam {
name: Some("REPO_URL".to_owned()),
value: pipeline
.clone_uri
.map(|uri| serde_json::Value::String(uri))
.or_else(|| Some(serde_json::Value::String(hook_repo_url.into_owned()))),
});
param_list.push(PipelineParam {
name: Some("PULL_BASE_SHA".to_owned()),
value: Some(serde_json::Value::String(
hook.get_pull_base_sha()?.into_owned(),
)),
});
param_list.push(PipelineParam {
name: Some("PULL_BASE_REF".to_owned()),
value: Some(serde_json::Value::String(
hook.get_pull_base_ref()?.into_owned(),
)),
});
}
let pp = PostParams::default();
let created_run = jobs.create(&pp, &pipeline.pipeline).await?;
}
Ok(())
}