From 1406a21785b3b095be27b648eecc2ff1d11aea48 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 21 Jul 2024 15:54:12 -0400 Subject: [PATCH] Creating an example PipelineRun. --- Cargo.lock | 1 + Cargo.toml | 1 + example_pipeline_run.json | 70 +++++++++++++++++++++++++++++++++++++++ example_pipeline_run.yaml | 44 ++++++++++++++++++++++++ src/crd_pipeline_run.rs | 24 ++++++++++++++ src/main.rs | 16 +++++++++ 6 files changed, 156 insertions(+) create mode 100644 example_pipeline_run.json create mode 100644 example_pipeline_run.yaml create mode 100644 src/crd_pipeline_run.rs diff --git a/Cargo.lock b/Cargo.lock index fb04ecc..1eed226 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1905,6 +1905,7 @@ dependencies = [ "http-body-util", "k8s-openapi", "kube", + "schemars", "serde", "serde_json", "sha2", diff --git a/Cargo.toml b/Cargo.toml index 84aa584..4d29113 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ http-body-util = "0.1.2" k8s-openapi = { version = "0.22.0", default-features = false, features = ["v1_30"] } # default client, config, rustls-tls kube = { version = "0.92.1", default-features = false, features = ["client", "config", "rustls-tls", "derive", "runtime"] } +schemars = "0.8.21" serde = { version = "1.0.204", features = ["derive"] } # default std serde_json = { version = "1.0.120", default-features = false, features = ["std"] } diff --git a/example_pipeline_run.json b/example_pipeline_run.json new file mode 100644 index 0000000..b4e351a --- /dev/null +++ b/example_pipeline_run.json @@ -0,0 +1,70 @@ +{ + "apiVersion": "tekton.dev/v1beta1", + "kind": "PipelineRun", + "metadata": { + "name": "minimal-test", + "namespace": "lighthouse" + }, + "spec": { + "pipelineSpec": { + "tasks": [ + { + "name": "echo-variable", + "taskSpec": { + "metadata": {}, + "stepTemplate": { + "image": "alpine:3.18", + "name": "", + "resources": { + "requests": { + "cpu": "10m", + "memory": "600Mi" + } + } + }, + "steps": [ + { + "image": "alpine:3.18", + "script": "#!/usr/bin/env sh\necho \"The variable: $(params.LOREM)\"\n" + } + ] + }, + "params": [ + { + "name": "LOREM", + "value": "$(tasks.set-variable.results.ipsum)" + } + ] + }, + { + "name": "set-variable", + "taskSpec": { + "metadata": {}, + "stepTemplate": { + "image": "alpine:3.18", + "name": "", + "resources": { + "requests": { + "cpu": "10m", + "memory": "600Mi" + } + } + }, + "results": [ + { + "name": "ipsum" + } + ], + "steps": [ + { + "image": "alpine:3.18", + "script": "#!/usr/bin/env sh\necho -n \"dolar\" > \"$(results.ipsum.path)\"\n" + } + ] + } + } + ] + }, + "timeout": "240h0m0s" + } +} diff --git a/example_pipeline_run.yaml b/example_pipeline_run.yaml new file mode 100644 index 0000000..0c766a8 --- /dev/null +++ b/example_pipeline_run.yaml @@ -0,0 +1,44 @@ +apiVersion: tekton.dev/v1beta1 +kind: PipelineRun +metadata: + name: minimal-test + namespace: lighthouse +spec: + pipelineSpec: + tasks: + - name: echo-variable + taskSpec: + metadata: {} + stepTemplate: + image: alpine:3.18 + name: "" + resources: + requests: + cpu: 10m + memory: 600Mi + steps: + - image: alpine:3.18 + script: | + #!/usr/bin/env sh + echo "The variable: $(params.LOREM)" + params: + - name: LOREM + value: $(tasks.set-variable.results.ipsum) + - name: set-variable + taskSpec: + metadata: {} + stepTemplate: + image: alpine:3.18 + name: "" + resources: + requests: + cpu: 10m + memory: 600Mi + results: + - name: ipsum + steps: + - image: alpine:3.18 + script: | + #!/usr/bin/env sh + echo -n "dolar" > "$(results.ipsum.path)" + timeout: 240h0m0s diff --git a/src/crd_pipeline_run.rs b/src/crd_pipeline_run.rs new file mode 100644 index 0000000..1caf879 --- /dev/null +++ b/src/crd_pipeline_run.rs @@ -0,0 +1,24 @@ +use kube::CustomResource; +use schemars::JsonSchema; +use serde::Deserialize; +use serde::Serialize; +use serde_json::Value; + +/// A single execution of a Pipeline. +#[derive(CustomResource, Serialize, Deserialize, Clone, Debug, JsonSchema)] +#[kube( + group = "tekton.dev", + version = "v1beta1", + kind = "PipelineRun", + singular = "pipelinerun", + plural = "pipelineruns" +)] +#[kube(namespaced)] +pub struct PipelineRunSpec { + /// Contents of the Pipeline + #[serde(default, skip_serializing_if = "Option::is_none")] + pub pipelineSpec: Option, + + #[serde(default, skip_serializing_if = "Option::is_none")] + pub timeout: Option, +} diff --git a/src/main.rs b/src/main.rs index 2d6502f..369f525 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,8 @@ use axum::routing::get; use axum::routing::post; use axum::Json; use axum::Router; +use kube::api::PostParams; +use kube::Api; use kube::Client; use serde::Serialize; use tokio::signal; @@ -15,12 +17,17 @@ use tower_http::trace::TraceLayer; use tracing_subscriber::layer::SubscriberExt; use tracing_subscriber::util::SubscriberInitExt; +use self::crd_pipeline_run::PipelineRun; use self::webhook::hook; use self::webhook::verify_signature; +use kube::CustomResourceExt; +mod crd_pipeline_run; mod hook_push; mod webhook; +const EXAMPLE_PIPELINE_RUN: &'static str = include_str!("../example_pipeline_run.json"); + #[tokio::main] async fn main() -> Result<(), Box> { tracing_subscriber::registry() @@ -36,6 +43,15 @@ async fn main() -> Result<(), Box> { .await .expect("Set KUBECONFIG to a valid kubernetes config."); + let jobs: Api = Api::namespaced(kubernetes_client, "lighthouse"); + // let jobs: Api = Api::default_namespaced(kubernetes_client); + tracing::info!("Using crd: {}", serde_json::to_string(&PipelineRun::crd())?); + + let test_run: PipelineRun = serde_json::from_str(EXAMPLE_PIPELINE_RUN)?; + + let pp = PostParams::default(); + let created_run = jobs.create(&pp, &test_run).await?; + let app = Router::new() .route("/hook", post(hook)) .layer(middleware::from_fn(verify_signature))