From 02b2c986edf1389c448cce464f994dd400e60622 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sat, 30 Mar 2024 00:09:34 +0100 Subject: [PATCH] feat: add base things Signed-off-by: kjuulh --- .drone.yml | 192 ++ .gitignore | 2 + Cargo.lock | 2044 +++++++++++++++++++ Cargo.toml | 12 + README.md | 1 + ci/Cargo.toml | 16 + ci/src/main.rs | 43 + crates/cuddle-rust-cli-plan/.gitignore | 1 + crates/cuddle-rust-cli-plan/Cargo.toml | 15 + crates/cuddle-rust-cli-plan/src/main.rs | 86 + cuddle.yaml | 24 + renovate.json | 3 + scripts/render.sh | 19 + templates/deployment.yaml | 20 + templates/kustomize/base/deployment.yaml | 22 + templates/kustomize/base/kustomization.yaml | 24 + templates/kustomize/base/service.yaml | 17 + 17 files changed, 2541 insertions(+) create mode 100644 .drone.yml create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 ci/Cargo.toml create mode 100644 ci/src/main.rs create mode 100644 crates/cuddle-rust-cli-plan/.gitignore create mode 100644 crates/cuddle-rust-cli-plan/Cargo.toml create mode 100644 crates/cuddle-rust-cli-plan/src/main.rs create mode 100644 cuddle.yaml create mode 100644 renovate.json create mode 100755 scripts/render.sh create mode 100644 templates/deployment.yaml create mode 100644 templates/kustomize/base/deployment.yaml create mode 100644 templates/kustomize/base/kustomization.yaml create mode 100644 templates/kustomize/base/service.yaml diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..ff95a16 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,192 @@ +kind: pipeline +name: default +type: docker + +steps: + - name: build ci + image: rustlang/rust:nightly + volumes: + - name: ci + path: /mnt/ci + environment: + PKG_CONFIG_SYSROOT_DIR: "/" + CI_PREFIX: "/mnt/ci" + commands: + - set -e + - apt update + - apt install musl-tools pkg-config libssl-dev openssl build-essential musl-dev -y + - rustup target add x86_64-unknown-linux-musl + - cargo build --target=x86_64-unknown-linux-musl -p ci --bin ci + - mv target/x86_64-unknown-linux-musl/debug/ci "$CI_PREFIX/ci" + + - name: load_secret + image: debian:buster-slim + volumes: + - name: ssh + path: /root/.ssh/ + environment: + SSH_KEY: + from_secret: gitea_id_ed25519 + commands: + - mkdir -p $HOME/.ssh/ + - echo "$SSH_KEY" | base64 -d > $HOME/.ssh/id_ed25519 + - chmod -R 600 ~/.ssh + - | + cat >$HOME/.ssh/config < eyre::Result<()> { + let client = dagger_sdk::connect().await?; + + let service = RustService::from(client) + .with_arch(Architecture::Amd64) + .with_os(Os::Linux) + .with_apt(&[ + "clang", + "libssl-dev", + "libz-dev", + "libgit2-dev", + "git", + "openssh-client", + ]) + .with_apt_release(&["git", "openssh-client"]) + .with_docker_cli() + .with_cuddle_cli() + .with_kubectl() + .with_apt_ca_certificates() + .with_crates(["ci", "crates/*"]) + .with_mold("2.3.3") + .with_bin_name("cuddle-rust-cli-plan") + .with_deployment(false) + .to_owned(); + + let service = Arc::new(Mutex::new(service)); + + CuddleCI::default() + .with_pull_request(service.clone()) + .with_main(service.clone()) + .execute(std::env::args()) + .await?; + + Ok(()) +} diff --git a/crates/cuddle-rust-cli-plan/.gitignore b/crates/cuddle-rust-cli-plan/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/crates/cuddle-rust-cli-plan/.gitignore @@ -0,0 +1 @@ +/target diff --git a/crates/cuddle-rust-cli-plan/Cargo.toml b/crates/cuddle-rust-cli-plan/Cargo.toml new file mode 100644 index 0000000..9f886b3 --- /dev/null +++ b/crates/cuddle-rust-cli-plan/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "cuddle-rust-cli-plan" +version = "0.1.0" +edition = "2021" + +[dependencies] +tokio.workspace = true + +dagger-sdk = {git = "https://github.com/kjuulh/dagger.git", branch = "feat/with-send-sync"} +eyre = { version = "0.6.12" } + +dagger-components = { git = "https://git.front.kjuulh.io/kjuulh/dagger-components", branch = "main" } +dagger-rust = { git = "https://git.front.kjuulh.io/kjuulh/dagger-components", branch = "main" } +cuddle-ci = { git = "https://git.front.kjuulh.io/kjuulh/dagger-components", branch = "main" } +async-trait = "0.1.79" diff --git a/crates/cuddle-rust-cli-plan/src/main.rs b/crates/cuddle-rust-cli-plan/src/main.rs new file mode 100644 index 0000000..abc78e4 --- /dev/null +++ b/crates/cuddle-rust-cli-plan/src/main.rs @@ -0,0 +1,86 @@ +use std::sync::Arc; + +use async_trait::async_trait; +use cuddle_ci::cuddle_file::CuddleFile; +use cuddle_ci::cuddle_releaser::CuddleReleaser; +use cuddle_ci::rust_service::architecture::{Architecture, Os}; +use cuddle_ci::rust_service::RustService; +use cuddle_ci::rust_service::{extensions::*, RustServiceContext}; +use cuddle_ci::{Context, CuddleCI, MainAction, PullRequestAction}; +use tokio::sync::Mutex; + +#[tokio::main] +async fn main() -> eyre::Result<()> { + let client = dagger_sdk::connect().await?; + let cuddle_file = CuddleFile::from_cuddle_file().await?; + + let service = RustService::from(client.clone()) + .with_arch(Architecture::Amd64) + .with_os(Os::Linux) + .with_apt(&["libssl-dev", "libz-dev", "libpq-dev", "protobuf-compiler"]) + .with_apt_release(&["libssl-dev", "libz-dev", "libpq-dev"]) + .with_apt_ca_certificates() + .with_crates(["crates/*"]) + .with_mold("2.3.3") + .with_bin_name(cuddle_file.vars.service) + .with_deployment(false) + .to_owned(); + + let service = Arc::new(Mutex::new(service)); + + let render = Arc::new(Mutex::new(RustServiceRender::default())); + + let deployment = Arc::new(Mutex::new(CuddleReleaser::new(client).await?)); + CuddleCI::default() + .with_pull_request(service.clone()) + //.with_pull_request(render.clone()) + //.with_pull_request(deployment.clone()) + .with_main(service.clone()) + .with_main(render.clone()) + .with_main(deployment.clone()) + .execute(std::env::args()) + .await?; + + Ok(()) +} + +#[derive(Default)] +struct RustServiceRender {} + +#[async_trait] +impl MainAction for RustServiceRender { + async fn execute_main(&self, ctx: &mut Context) -> eyre::Result<()> { + let image_tag = ctx + .get_image_tag()? + .ok_or(eyre::anyhow!("failed to find image_tag"))?; + + cuddle_ci::cuddle_x::well_known::render(vec![ + "--cluster", + "clank-prod", + "--image_tag", + &image_tag, + ]) + .await?; + + Ok(()) + } +} + +#[async_trait] +impl PullRequestAction for RustServiceRender { + async fn execute_pull_request(&self, ctx: &mut Context) -> eyre::Result<()> { + let image_tag = ctx + .get_image_tag()? + .ok_or(eyre::anyhow!("failed to find image_tag"))?; + + cuddle_ci::cuddle_x::well_known::render(vec![ + "--cluster", + "clank-dev", + "--image_tag", + &image_tag, + ]) + .await?; + + Ok(()) + } +} diff --git a/cuddle.yaml b/cuddle.yaml new file mode 100644 index 0000000..258a707 --- /dev/null +++ b/cuddle.yaml @@ -0,0 +1,24 @@ +# yaml-language-server: $schema=https://git.front.kjuulh.io/kjuulh/cuddle/raw/branch/main/schemas/base.json + +base: "git@git.front.kjuulh.io:kjuulh/cuddle-base.git" + +vars: + service: "cuddle-rust-cli-plan" + registry: kasperhermansen + + clusters: + clank-prod: + replicas: "3" + namespace: prod + +scripts: + render: + type: shell + args: + cluster: + name: cluster + type: flag + image_tag: + name: image_tag + type: flag + diff --git a/renovate.json b/renovate.json new file mode 100644 index 0000000..7190a60 --- /dev/null +++ b/renovate.json @@ -0,0 +1,3 @@ +{ + "$schema": "https://docs.renovatebot.com/renovate-schema.json" +} diff --git a/scripts/render.sh b/scripts/render.sh new file mode 100755 index 0000000..ad2cc04 --- /dev/null +++ b/scripts/render.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +set -eou pipefail + +echo "rendering folder" + +cuddle render folder \ + --source $TMP/kustomize \ + --destination $TMP/rendered/kustomize \ + --extra-var cluster=$CLUSTER \ + --extra-var image_tag=$IMAGE_TAG + +echo "rendering kustomize" + +cuddle render kustomize \ + --kustomize-folder $TMP/rendered/kustomize/base \ + --destination $TMP/k8s + +echo "done" diff --git a/templates/deployment.yaml b/templates/deployment.yaml new file mode 100644 index 0000000..7b9ecdf --- /dev/null +++ b/templates/deployment.yaml @@ -0,0 +1,20 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: "{{ service }}" +spec: + selector: + matchLabels: + app: "{{ service }}" + template: + spec: + containers: + - name: "{{ service }}" + image: "deployment:latest" + ports: + - containerPort: 3000 + name: external_http + - containerPort: 3001 + name: internal_http + - containerPort: 3002 + name: internal_grpc diff --git a/templates/kustomize/base/deployment.yaml b/templates/kustomize/base/deployment.yaml new file mode 100644 index 0000000..2b29ba7 --- /dev/null +++ b/templates/kustomize/base/deployment.yaml @@ -0,0 +1,22 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: "{{ service }}" +spec: + selector: + matchLabels: + app: "{{ service }}" + template: + spec: + containers: + - name: "{{ service }}" + command: [{{ service }}] + args: ["serve"] + image: "deployment:latest" + ports: + - containerPort: 3000 + name: external-http + - containerPort: 3001 + name: internal-http + - containerPort: 3002 + name: internal-grpc diff --git a/templates/kustomize/base/kustomization.yaml b/templates/kustomize/base/kustomization.yaml new file mode 100644 index 0000000..1681e3b --- /dev/null +++ b/templates/kustomize/base/kustomization.yaml @@ -0,0 +1,24 @@ + +{% set_global cluster_vars = filter_by_prefix(prefix=["clusters", cluster]) %} + +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization + +resources: +- deployment.yaml +- service.yaml + +commonLabels: + app: "{{ service }}" + cluster: "{{ cluster }}" + +namespace: "{{ cluster_vars.namespace }}" + +replicas: + - name: "{{ service }}" + count: {{ cluster_vars.replicas }} + +images: + - name: "deployment" + newName: "{{ registry }}/{{ service }}" + newTag: "{{ image_tag }}" diff --git a/templates/kustomize/base/service.yaml b/templates/kustomize/base/service.yaml new file mode 100644 index 0000000..beeccf8 --- /dev/null +++ b/templates/kustomize/base/service.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Service +metadata: + name: "{{ service }}" +spec: + type: LoadBalancer + ports: + - name: external-http + port: 3000 + targetPort: 3000 + - name: internal-http + port: 3001 + targetPort: 3001 + - name: internal-grpc + port: 3002 + targetPort: 3002 +