Skip to content

Commit

Permalink
Generate the crd as yaml using a build.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
film42 committed Mar 24, 2022
1 parent 28948c3 commit ca6dcf8
Show file tree
Hide file tree
Showing 14 changed files with 5,065 additions and 37 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.git
target
*/target
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
FROM rust:1.58-buster as builder
ADD . /app
WORKDIR /app
RUN cargo build --release
RUN cd /app/docbot-controller && cargo build --release

FROM debian:buster-slim
RUN apt-get update \
&& apt-get install -y libssl1.1 ca-certificates\
&& rm -rf /var/lib/apt/lists/*
COPY --from=builder /app/target/release/docbot /srv/docbot/docbot
COPY --from=builder /app/docbot-controller/target/release/docbot-controller /srv/docbot/docbot-controller
WORKDIR /srv/docbot
ENTRYPOINT ["/srv/docbot/docbot"]
ENTRYPOINT ["/srv/docbot/docbot-controller"]
3,622 changes: 3,597 additions & 25 deletions deploymenthooks.apps.mx.com.yaml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docbot-controller/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
18 changes: 17 additions & 1 deletion Cargo.lock → docbot-controller/Cargo.lock

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

33 changes: 33 additions & 0 deletions docbot-controller/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "docbot-controller"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tokio = { version = "1.15.0", features = ["full"] }
futures = "0.3.19"
serde = "1"
serde_json = "1.0"
serde_yaml = "0.8"
schemars = "0.8"
docbot-crd = { path = "../docbot-crd" }

k8s-openapi = { version = "0.13", default-features = false, features = ["v1_17", "schemars"] } # Kube-rs depends on k8s-openapi
kube = { version = "0.65", default-features = true, features = ["derive"] } # Library for talking to Kubernetes API
kube-runtime = "0.65"

[build-dependencies]
tokio = { version = "1.15.0", features = ["full"] }
futures = "0.3.19"
serde = "1"
serde_json = "1.0"
serde_yaml = "0.8"
schemars = "0.8"
docbot-crd = { path = "../docbot-crd" }

k8s-openapi = { version = "0.13", default-features = false, features = ["v1_17", "schemars"] } # Kube-rs depends on k8s-openapi
kube = { version = "0.65", default-features = true, features = ["derive"] } # Library for talking to Kubernetes API
kube-runtime = "0.65"

15 changes: 15 additions & 0 deletions docbot-controller/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use docbot_crd::DeploymentHook;
use kube::CustomResourceExt;
use std::fs::File;
use std::io::Write;
use std::path::Path;

fn main() {
let crate_dir = std::env::var_os("CARGO_MANIFEST_DIR").unwrap();
let schema = serde_yaml::to_string(&DeploymentHook::crd()).unwrap();
let crd_schema_path = Path::new(&crate_dir)
.join("..")
.join("deploymenthooks.apps.mx.com.yaml");
let mut f = File::create(&crd_schema_path).unwrap();
f.write_all(schema.as_bytes()).unwrap();
}
2 changes: 1 addition & 1 deletion src/cache.rs → docbot-controller/src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::crd::DeploymentHook;
use docbot_crd::DeploymentHook;
use k8s_openapi::api::apps::v1::Deployment;
use kube::{api::ListParams, client::Client, Api};
use std::collections::BTreeMap;
Expand Down
2 changes: 1 addition & 1 deletion src/job.rs → docbot-controller/src/job.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::crd::DeploymentHook;
use docbot_crd::DeploymentHook;
use k8s_openapi::api::batch::v1::{Job, JobSpec};
use k8s_openapi::api::core::v1::PodTemplate;
use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down
6 changes: 1 addition & 5 deletions src/main.rs → docbot-controller/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::cache::DeploymentHookCache;
use crd::DeploymentHook;
use docbot_crd::DeploymentHook;
use futures::{StreamExt, TryStreamExt};
use k8s_openapi::api::apps::v1::Deployment;
use k8s_openapi::api::batch::v1::Job;
Expand All @@ -12,7 +12,6 @@ use kube::{
};

mod cache;
mod crd;
mod job;

// Helper to print namspace/name in a nice way since we do that a lot.
Expand Down Expand Up @@ -150,9 +149,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let cache = cache::DeploymentHookCache::default();
cache.refresh(&client).await?;

use kube::CustomResourceExt;
println!("{}", serde_yaml::to_string(&DeploymentHook::crd()).unwrap());

// Refresh the cache every minute
tokio::spawn({
let cache = cache.clone();
Expand Down
1 change: 1 addition & 0 deletions docbot-crd/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
Loading

0 comments on commit ca6dcf8

Please sign in to comment.