Generate names for pipeline runs.
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
use std::borrow::Borrow;
|
||||
use std::borrow::Cow;
|
||||
|
||||
use kube::api::PostParams;
|
||||
use kube::Api;
|
||||
use kube::CustomResourceExt;
|
||||
use regex::Regex;
|
||||
use tracing::debug;
|
||||
use tracing::info;
|
||||
|
||||
use crate::crd_pipeline_run::PipelineParam;
|
||||
use crate::crd_pipeline_run::PipelineRun;
|
||||
@@ -16,7 +21,7 @@ pub(crate) async fn run_pipelines(
|
||||
) -> 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())?);
|
||||
tracing::debug!("Using crd: {}", serde_json::to_string(&PipelineRun::crd())?);
|
||||
|
||||
for mut pipeline in pipelines {
|
||||
debug!("Kicking off {}", pipeline.name);
|
||||
@@ -26,7 +31,7 @@ pub(crate) async fn run_pipelines(
|
||||
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)),
|
||||
value: Some(serde_json::Value::String(pipeline.name.clone())),
|
||||
});
|
||||
param_list.push(PipelineParam {
|
||||
name: Some("REPO_OWNER".to_owned()),
|
||||
@@ -61,9 +66,55 @@ pub(crate) async fn run_pipelines(
|
||||
)),
|
||||
});
|
||||
}
|
||||
if pipeline.pipeline.metadata.generate_name.is_none()
|
||||
&& pipeline.pipeline.metadata.name.is_some()
|
||||
{
|
||||
std::mem::swap(
|
||||
&mut pipeline.pipeline.metadata.generate_name,
|
||||
&mut pipeline.pipeline.metadata.name,
|
||||
);
|
||||
if let Some(ref mut name) = pipeline.pipeline.metadata.generate_name {
|
||||
let mut new_name_stub = sanitize_kubernetes_identifier(format!(
|
||||
"{}-{}-{}",
|
||||
name,
|
||||
hook.get_repo_name()?,
|
||||
hook.get_repo_owner()?
|
||||
))?
|
||||
.into_owned();
|
||||
new_name_stub.truncate(63);
|
||||
(*name) = new_name_stub + "-";
|
||||
debug!("Using generate name: {}", name);
|
||||
}
|
||||
}
|
||||
let pp = PostParams::default();
|
||||
let created_run = jobs.create(&pp, &pipeline.pipeline).await?;
|
||||
info!("Created PipelineRun {:?}", created_run.metadata.name);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn sanitize_kubernetes_identifier<'a, O: Into<Cow<'a, str>>>(
|
||||
original: O,
|
||||
) -> Result<Cow<'a, str>, Box<dyn std::error::Error>> {
|
||||
let validate_identifier_regex =
|
||||
Regex::new(r"[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*")?;
|
||||
let original = original.into();
|
||||
if !validate_identifier_regex.is_match(original.borrow()) {
|
||||
return Ok(original);
|
||||
}
|
||||
|
||||
let mut clean_identifier = String::with_capacity(original.len());
|
||||
for c in original.chars() {
|
||||
match c {
|
||||
'a'..='z' | 'A'..='Z' | '0'..='9' | '.' | '-' => {
|
||||
clean_identifier.push(c);
|
||||
}
|
||||
_ => {
|
||||
clean_identifier.push('-');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Ok(Cow::Owned(clean_identifier))
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#![forbid(unsafe_code)]
|
||||
use std::borrow::Borrow;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -23,6 +24,7 @@ use self::discovery::discover_matching_push_triggers;
|
||||
use self::discovery::discover_webhook_bridge_config;
|
||||
use self::gitea_client::GiteaClient;
|
||||
use self::hook_push::HookPush;
|
||||
use self::hook_push::PipelineParamters;
|
||||
use self::in_repo_config::InRepoConfig;
|
||||
use self::kubernetes::run_pipelines;
|
||||
use self::webhook::hook;
|
||||
@@ -58,11 +60,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let gitea_api_token = std::env::var("WEBHOOK_BRIDGE_OAUTH_TOKEN")?;
|
||||
let gitea = GiteaClient::new(gitea_api_root, gitea_api_token);
|
||||
|
||||
let webhook_payload: HookPush = serde_json::from_str(EXAMPLE_WEBHOOK_PAYLOAD)?;
|
||||
let repo_tree = gitea
|
||||
.get_tree(
|
||||
"talexander",
|
||||
"webhook_bridge",
|
||||
"b8444344c4821e87a894cd195f8bec39cd501f68",
|
||||
webhook_payload.get_pull_base_sha()?,
|
||||
)
|
||||
.await?;
|
||||
let in_repo_config = discover_webhook_bridge_config(&gitea, &repo_tree).await?;
|
||||
@@ -70,8 +73,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
discover_matching_push_triggers(&gitea, &repo_tree, "refs/heads/main", &in_repo_config)
|
||||
.await?;
|
||||
|
||||
let webhook_payload: HookPush = serde_json::from_str(EXAMPLE_WEBHOOK_PAYLOAD)?;
|
||||
|
||||
run_pipelines(webhook_payload, pipelines, kubernetes_client).await?;
|
||||
|
||||
// let app = Router::new()
|
||||
|
||||
Reference in New Issue
Block a user