From aa3435ee7b0da62ef2109f7eddc815e9e0341c73 Mon Sep 17 00:00:00 2001 From: Nikolai Rodionov Date: Tue, 14 Feb 2023 21:16:34 +0000 Subject: [PATCH] Add docker images (#5) --- .dockerignore | 1 + .github/workflows/container-stable.yaml | 44 ++++++++++++++++++++---- .github/workflows/container-version.yaml | 41 +++++++++++++++++++--- Dockerfile | 10 ++++++ Makefile | 2 ++ dockerfiles/Dockerfile-argo | 11 ++++++ dockerfiles/Dockerfile-helmfile | 16 +++++++++ scripts/download_for_arch.sh | 15 ++++++++ src/connectors/helmfile.rs | 12 ++++--- src/main.rs | 7 ++-- 10 files changed, 140 insertions(+), 19 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 Makefile create mode 100644 dockerfiles/Dockerfile-argo create mode 100644 dockerfiles/Dockerfile-helmfile create mode 100755 scripts/download_for_arch.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +target diff --git a/.github/workflows/container-stable.yaml b/.github/workflows/container-stable.yaml index 9189fff..f3e145b 100644 --- a/.github/workflows/container-stable.yaml +++ b/.github/workflows/container-stable.yaml @@ -5,8 +5,6 @@ on: push: branches: - main - paths: - - "src/**" jobs: containerization: @@ -26,15 +24,13 @@ jobs: - name: Set up Docker Buildx id: buildx uses: docker/setup-buildx-action@master - - name: Login to GitHub Container Registry uses: docker/login-action@v1 with: registry: ghcr.io username: ${{ github.repository_owner }} password: ${{ secrets.CR_PAT }} - - - name: Build + - name: Build base uses: docker/build-push-action@v2 with: builder: ${{ steps.buildx.outputs.name }} @@ -43,8 +39,42 @@ jobs: platforms: linux/amd64,linux/arm64 push: true tags: | - ghcr.io/allanger/${{ env.GITHUB_REPOSITORY }}:stable - ghcr.io/allanger/${{ env.GITHUB_REPOSITORY }}:latest + ghcr.io/${{ github.repository }}-base:latest + labels: | + action_id=${{ github.action }} + action_link=${{ env.LINK }} + actor=${{ github.actor }} + sha=${{ github.sha }} + ref=${{ github.ref }} + - name: Build helmfile + uses: docker/build-push-action@v2 + with: + builder: ${{ steps.buildx.outputs.name }} + context: ./dockerfiles + file: ./dockerfiles/Dockerfile-helmfile + platforms: linux/arm64 + push: true + tags: | + ghcr.io/${{ github.repository }}-helmfile:latest + ghcr.io/${{ github.repository }}-helmfile:stable + labels: | + action_id=${{ github.action }} + action_link=${{ env.LINK }} + actor=${{ github.actor }} + sha=${{ github.sha }} + ref=${{ github.ref }} + + - name: Build argo + uses: docker/build-push-action@v2 + with: + builder: ${{ steps.buildx.outputs.name }} + context: ./dockerfiles + file: ./dockerfiles/Dockerfile-argo + platforms: linux/amd64,linux/arm64 + push: true + tags: | + ghcr.io/${{ github.repository }}-argo:latest + ghcr.io/${{ github.repository }}-argo:stable labels: | action_id=${{ github.action }} action_link=${{ env.LINK }} diff --git a/.github/workflows/container-version.yaml b/.github/workflows/container-version.yaml index d40f07e..288ad56 100644 --- a/.github/workflows/container-version.yaml +++ b/.github/workflows/container-version.yaml @@ -1,5 +1,5 @@ --- -name: "Version container" +name: "Versioned container" on: push: @@ -27,15 +27,13 @@ jobs: - name: Set up Docker Buildx id: buildx uses: docker/setup-buildx-action@master - - name: Login to GitHub Container Registry uses: docker/login-action@v1 with: registry: ghcr.io username: ${{ github.repository_owner }} password: ${{ secrets.CR_PAT }} - - - name: Build + - name: Build base uses: docker/build-push-action@v2 with: builder: ${{ steps.buildx.outputs.name }} @@ -44,7 +42,40 @@ jobs: platforms: linux/amd64,linux/arm64 push: true tags: | - ghcr.io/allanger/${{ env.GITHUB_REPOSITORY }}:${{ env.TAG }} + ghcr.io/${{ github.repository }}-base:${{ env.TAG }} + labels: | + action_id=${{ github.action }} + action_link=${{ env.LINK }} + actor=${{ github.actor }} + sha=${{ github.sha }} + ref=${{ github.ref }} + - name: Build helmfile + uses: docker/build-push-action@v2 + with: + builder: ${{ steps.buildx.outputs.name }} + context: ./dockerfiles + file: ./dockerfiles/Dockerfile-helmfile + platforms: linux/arm64 + push: true + tags: | + ghcr.io/${{ github.repository }}-helmfile:${{ env.TAG }} + labels: | + action_id=${{ github.action }} + action_link=${{ env.LINK }} + actor=${{ github.actor }} + sha=${{ github.sha }} + ref=${{ github.ref }} + + - name: Build argo + uses: docker/build-push-action@v2 + with: + builder: ${{ steps.buildx.outputs.name }} + context: ./dockerfiles + file: ./dockerfiles/Dockerfile-argo + platforms: linux/amd64,linux/arm64 + push: true + tags: | + ghcr.io/${{ github.repository }}-argo:${{ env.TAG }} labels: | action_id=${{ github.action }} action_link=${{ env.LINK }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0587e3d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM rust:1.66.1-alpine3.17 as builder +WORKDIR /src +RUN apk update && apk add --no-cache gcc musl-dev +COPY ./ . +RUN cargo build --release + +FROM alpine:3.17.1 +COPY --from=builder /src/target/release/cdh /bin/cdh +WORKDIR /workdir +ENTRYPOINT ["/bin/cdh"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..acf3e73 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +build: + cargo build --release diff --git a/dockerfiles/Dockerfile-argo b/dockerfiles/Dockerfile-argo new file mode 100644 index 0000000..3d25b9b --- /dev/null +++ b/dockerfiles/Dockerfile-argo @@ -0,0 +1,11 @@ +FROM ghcr.io/allanger/dumb-downloader as builder +ARG ARGOCD_VERSION=v2.5.10 +ENV RUST_LOG=info +RUN mkdir /out +RUN dudo -l "https://github.com/argoproj/argo-cd/releases/download/{{ version }}/argocd-{{ os }}-{{ arch }}" -i /tmp/argocd -p $ARGOCD_VERSION +RUN mv /tmp/argocd /out/argocd && chmod +x /out/argocd + +FROM ghcr.io/allanger/check-da-helm-base +COPY --from=builder /out/ /usr/bin +RUN apk update --no-cache && apk add --no-cache jq bash +ENTRYPOINT ["cdh"] diff --git a/dockerfiles/Dockerfile-helmfile b/dockerfiles/Dockerfile-helmfile new file mode 100644 index 0000000..c32a215 --- /dev/null +++ b/dockerfiles/Dockerfile-helmfile @@ -0,0 +1,16 @@ +FROM ghcr.io/allanger/dumb-downloader as builder +RUN apt-get update -y && apt-get install tar -y +ARG HELM_VERSION=v3.10.3 +ARG HELMFILE_VERSION=0.150.0 +ENV RUST_LOG=info +RUN dudo -l "https://github.com/helmfile/helmfile/releases/download/v{{ version }}/helmfile_{{ version }}_{{ os }}_{{ arch }}.tar.gz" -i /tmp/helmfile -p $HELMFILE_VERSION +RUN dudo -l "https://get.helm.sh/helm-{{ version }}-{{ os }}-{{ arch }}.tar.gz" -i /tmp/helm.tar.gz -p $HELM_VERSION +RUN tar -xf /tmp/helm.tar.gz -C /tmp && rm -f /tmp/helm.tar.gz +RUN mkdir /out && for bin in `find /tmp | grep helm`; do cp $bin /out/; done +RUN chmod +x /out/helm +RUN chmod +x /out/helmfile + +FROM ghcr.io/allanger/check-da-helm-base +COPY --from=builder /out/ /usr/bin +RUN apk update --no-cache && apk add --no-cache jq bash +ENTRYPOINT ["cdh"] diff --git a/scripts/download_for_arch.sh b/scripts/download_for_arch.sh new file mode 100755 index 0000000..09287e9 --- /dev/null +++ b/scripts/download_for_arch.sh @@ -0,0 +1,15 @@ +#!/bin/sh +case $(uname -m) in + "arm64"|"aarch64") + PLATFORM="arm64" + ;; + "x86_64") + PLATFORM="amd64" + ;; + *) + echo "Unsuported target" + exit 1 + ;; +esac +echo "Downloading $2 from $1" +curl -LJO $1 $2 diff --git a/src/connectors/helmfile.rs b/src/connectors/helmfile.rs index bae5885..ca495ae 100644 --- a/src/connectors/helmfile.rs +++ b/src/connectors/helmfile.rs @@ -8,13 +8,15 @@ use std::{borrow::Borrow, io::Result, process::Command}; pub(crate) struct Helmfile { path: String, + env: String, } impl Connector for Helmfile { fn get_app(&self) -> Result> { let cmd: String = format!( - "helmfile -f {} list --output json | jq '[.[] | {{chart: .name, version: .version}}]'", - self.path + "helmfile -f {} -e {} list --output json | jq '[.[] | {{chart: .name, version: .version}}]'", + self.path, + self.env ) .to_string(); @@ -35,7 +37,7 @@ impl Connector for Helmfile { } } fn sync_repos(&self) -> Result<()> { - let cmd: String = format!("helmfile -f {} sync", self.path); + let cmd: String = format!("helmfile -f {} -e {} sync", self.path, self.env); Command::new("bash") .arg("-c") .arg(cmd) @@ -47,7 +49,7 @@ impl Connector for Helmfile { type ConnectorType = Helmfile; } impl Helmfile { - pub(crate) fn init(path: String) -> Self { - Self { path: path } + pub(crate) fn init(path: String, env: String) -> Self { + Self {path, env} } } diff --git a/src/main.rs b/src/main.rs index 64424e9..7939b39 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,6 +43,9 @@ struct Args { /// Path to the helmfile #[clap(short, long, value_parser, default_value = "./")] path: String, + /// Pass an environment to the helmfile + #[arg(long, required = false, default_value = "default")] + helmfile_environment: String, /// Should execution be failed if you have outdated charts #[clap(short, long, action, default_value_t = false, env = "OUTDATED_FAIL")] outdated_fail: bool, @@ -88,7 +91,7 @@ fn main() { let charts = match args.kind { Kinds::Argo => Argo::init().get_app(), Kinds::Helm => Helm::init().get_app(), - Kinds::Helmfile => Helmfile::init(args.path.clone()).get_app(), + Kinds::Helmfile => Helmfile::init(args.path.clone(), args.helmfile_environment.clone()).get_app(), } .unwrap(); @@ -97,7 +100,7 @@ fn main() { let res = match args.kind { Kinds::Argo => Argo::init().sync_repos(), Kinds::Helm => Helm::init().sync_repos(), - Kinds::Helmfile => Helmfile::init(args.path).sync_repos(), + Kinds::Helmfile => Helmfile::init(args.path, args.helmfile_environment).sync_repos(), }; match res { Ok(_) => info!("helm repos are synced"),