commit 938f55896275fc4a19fff1a3d9d441d4ed26b0a4 Author: Gabriel Simmer Date: Sat Jun 29 10:39:37 2024 +0100 Initial commit of madness diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..a3857de --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,151 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "chrono" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +dependencies = [ + "num-traits", + "serde", +] + +[[package]] +name = "infra-rs" +version = "0.1.0" +dependencies = [ + "k8s-openapi", +] + +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + +[[package]] +name = "k8s-openapi" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19501afb943ae5806548bc3ebd7f3374153ca057a38f480ef30adfde5ef09755" +dependencies = [ + "base64", + "chrono", + "serde", + "serde-value", + "serde_json", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "ordered-float" +version = "2.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68f19d67e5a2795c94e73e0bb1cc1a7edeb2e28efd39e2e1c9b7a40c1108b11c" +dependencies = [ + "num-traits", +] + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] +name = "serde" +version = "1.0.203" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde-value" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3a1a3341211875ef120e117ea7fd5228530ae7e7036a779fdc9117be6b3282c" +dependencies = [ + "ordered-float", + "serde", +] + +[[package]] +name = "serde_derive" +version = "1.0.203" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.118" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d947f6b3163d8857ea16c4fa0dd4840d52f3041039a85decd46867eb1abef2e4" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "2.0.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "901fa70d88b9d6c98022e23b4136f9f3e54e4662c3bc1bd1d84a42a9a0f0c1e9" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..9dca517 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "infra-rs" +version = "0.1.0" +edition = "2021" + +[dependencies] +k8s-openapi = { version = "0.22.0", features = ["v1_30"] } diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..153d416 --- /dev/null +++ b/LICENSE @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. \ No newline at end of file diff --git a/README.org b/README.org new file mode 100644 index 0000000..c19d6fc --- /dev/null +++ b/README.org @@ -0,0 +1,17 @@ +#+title: infra-rs + +Is this easier than just writing YAML? No. Is it fun? Maybe. + +** About + +Generates Kubernetes manifests using Rust code. The bulk of the work (should) be done in the =infra_rs= module, which defines some fairly generic structs we can translate to the OpenAPI spec generated types from [[https://docs.rs/k8s-openapi/latest/k8s_openapi/][k8s_openapi]]. This /probably/ shouldn't be used in any serious way, unless your seriously silly. + +This is still very much a work in progress, but what use is mad science if it isn't shared for others to learn from. See =manifests/vaultwarden.rs= for an example deployment of Vaultwarden. + +** Running + +=cargo run= should be all you need. + +** Contributing + +Maybe don't if you value sanity. diff --git a/src/infra_rs/deployment.rs b/src/infra_rs/deployment.rs new file mode 100644 index 0000000..0523935 --- /dev/null +++ b/src/infra_rs/deployment.rs @@ -0,0 +1,125 @@ +use std::collections::BTreeMap; + +use k8s_openapi::{ + api::{ + apps::v1 as api, + core::v1::{ + ConfigMapEnvSource, Container, ContainerPort, EmptyDirVolumeSource, EnvFromSource, + PodSpec, PodTemplateSpec, SecretEnvSource, Volume, VolumeMount, + }, + }, + apimachinery::pkg::apis::meta::v1::{LabelSelector, ObjectMeta}, +}; + +#[derive(Default)] +pub struct Deployment { + pub name: String, + pub image: String, + pub ports: Vec, + pub data_dir: Option, + pub env: Vec, +} + +#[derive(Default)] +pub struct DeploymentEnv { + pub secret_ref: Option, + pub config_map_ref: Option, +} + +pub fn new(deployment: Deployment) -> api::Deployment { + let ports: Vec = deployment + .ports + .iter() + .map(|port| ContainerPort { + container_port: *port, + name: Some(format!("{}-{}", deployment.name, *port)), + ..Default::default() + }) + .collect(); + let envs: Vec = deployment + .env + .iter() + .map(|envsource| EnvFromSource { + secret_ref: if let Some(secret_ref) = &envsource.secret_ref { + Some(SecretEnvSource { + name: Some(secret_ref.clone()), + ..Default::default() + }) + } else { + None + }, + config_map_ref: if let Some(config_map_ref) = &envsource.config_map_ref { + Some(ConfigMapEnvSource { + name: Some(config_map_ref.clone()), + ..Default::default() + }) + } else { + None + }, + ..Default::default() + }) + .collect(); + + let spec = api::DeploymentSpec { + selector: LabelSelector { + match_labels: Some(BTreeMap::from([( + String::from("app"), + deployment.name.clone(), + )])), + ..Default::default() + }, + template: PodTemplateSpec { + metadata: Some(ObjectMeta { + name: Some(deployment.name.clone()), + namespace: Some(deployment.name.clone()), + labels: Some(BTreeMap::from([( + String::from("app"), + deployment.name.clone(), + )])), + ..Default::default() + }), + spec: Some(PodSpec { + containers: vec![Container { + name: deployment.name.clone(), + image: Some(deployment.image.clone()), + ports: Some(ports), + volume_mounts: if deployment.data_dir.is_some() { + Some(vec![VolumeMount { + mount_path: deployment.data_dir.clone().unwrap(), + name: format!("{}-data", deployment.name), + ..Default::default() + }]) + } else { + None + }, + env_from: Some(envs), + ..Default::default() + }], + volumes: if deployment.data_dir.is_some() { + Some(vec![Volume { + name: format!("{}-data", deployment.name), + empty_dir: Some(EmptyDirVolumeSource { + ..Default::default() + }), + ..Default::default() + }]) + } else { + None + }, + ..Default::default() + }), + ..Default::default() + }, + ..Default::default() + }; + + api::Deployment { + metadata: ObjectMeta { + name: Some(deployment.name.clone()), + namespace: Some(deployment.name.clone()), + ..Default::default() + }, + spec: Some(spec), + ..Default::default() + } +} diff --git a/src/infra_rs/mod.rs b/src/infra_rs/mod.rs new file mode 100644 index 0000000..9862902 --- /dev/null +++ b/src/infra_rs/mod.rs @@ -0,0 +1 @@ +pub mod deployment; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..fa585f7 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,10 @@ +use k8s_openapi::serde_json; + +mod infra_rs; +mod manifests; + +fn main() { + let test = manifests::vaultwarden::render(); + + println!("{}", serde_json::to_string(&test).unwrap()); +} diff --git a/src/manifests/mod.rs b/src/manifests/mod.rs new file mode 100644 index 0000000..c3069ac --- /dev/null +++ b/src/manifests/mod.rs @@ -0,0 +1 @@ +pub mod vaultwarden; diff --git a/src/manifests/vaultwarden.rs b/src/manifests/vaultwarden.rs new file mode 100644 index 0000000..162a0a8 --- /dev/null +++ b/src/manifests/vaultwarden.rs @@ -0,0 +1,22 @@ +use k8s_openapi::api::apps::v1 as api; + +use crate::infra_rs::deployment::{self, Deployment, DeploymentEnv}; + +pub fn render() -> api::Deployment { + deployment::new(Deployment { + name: String::from("vaultwarden"), + image: String::from("vaultwarden/server:testing"), + ports: vec![80], + data_dir: Some(String::from("/data")), + env: vec![ + DeploymentEnv { + secret_ref: Some(String::from("vaultwarden")), + ..Default::default() + }, + DeploymentEnv { + config_map_ref: Some(String::from("vaultwarden-env")), + ..Default::default() + }, + ], + }) +}