From 9abae620353038bd6778ce0214fbb9edeab91714 Mon Sep 17 00:00:00 2001 From: Nikolai Rodionov Date: Sat, 25 Feb 2023 19:28:00 +0100 Subject: [PATCH] Test drone --- .drone.yml | 23 ++++ .../index.md | 125 +++++++++++++++++- 2 files changed, 147 insertions(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index d323f85..d12e3c4 100644 --- a/.drone.yml +++ b/.drone.yml @@ -42,6 +42,29 @@ steps: repo: git.badhouseplants.net/allanger/badhouseplants-net tags: ${DRONE_COMMIT_SHA} +- name: Deploy a preview ApplicationSet + image: alpine/k8s + when: + branch: + exclude: + - main + environment: + KUBECONFIG_CONTENT: + from_secret: KUBECONFIG_CONTENT + ARGO_APP_BRANCH: ${DRONE_BRANCH} + ARGO_APP_HOSTNAME: "${DRONE_BRANCH}-dev.badhouseplants.net" + ARGO_APP_IMAGE_TAG: ${DRONE_COMMIT_SHA} + commands: + - mkdir $HOME/.kube + - echo $KUBECONFIG_CONTENT | base64 -d > $HOME/.kube/config + - apk update --no-cache && apk add yq gettext + - export ARGO_APP_CHART_VERSION=`cat chart/Chart.yaml | yq '.version' + - kubectl get -f applicationset.yaml -o yaml > /tmp/old_appset.yaml + - yq 'del(.spec.generators[].list.elements[] | select(.name == ""))' /tmp/old_application.set > /tmp/clean_appset.yaml + - envsubst < ./kube/template.yaml > /tmp/elements.yaml + - yq '.spec.generators[].list.elements + load("/tmp/elements.yaml")' /tmp/clean_appset.yaml > /tmp/new_appset.yaml + - kubectl apply -f /tmp/new_appset.yaml + --- # ---------------------------------------------- # -- Upload a newer version of my CV diff --git a/content/posts/argocd-dynamic-environment-per-branch-part-1/index.md b/content/posts/argocd-dynamic-environment-per-branch-part-1/index.md index cafba77..f9016aa 100644 --- a/content/posts/argocd-dynamic-environment-per-branch-part-1/index.md +++ b/content/posts/argocd-dynamic-environment-per-branch-part-1/index.md @@ -198,4 +198,127 @@ FROM git.badhouseplants.net/badhouseplants/hugo-builder WORKDIR /src COPY . /src RUN hugo -``` \ No newline at end of file +``` + +### How to deploy + +Previously I was using the same helmfile that I use for everything else in my k8s cluster. It was fine for static envs, but when I need to deploy them dynamically, it's not an option anymore. And here `ArgoCD` enters the room. I'm creating an `ApplicationSet` that looks like that: +```YAML +apiVersion: argoproj.io/v1alpha1 +kind: ApplicationSet +metadata: + name: badhouseplants-net + namespace: argo-system +spec: + generators: + - list: + elements: + - name: application # just not to lose a backward compability with the prevouos setup + app: badhouseplants + branch: main + chart_version: 0.3.6 + # Image that is lates now, we'll get there later + value: | + hugo: + image: + tag: latest + # And this is an example of environemnt that I want to be created. + - name: dynamic-charts + app: badhouseplants + branch: dynamic-charts + chart_version: 0.3.6 + value: | + istio: + hosts: + - dynamic-charts-dev.badhouseplants.net + hugo: + image: + tag: 5d742a71731320883db698432303c92aee4d68a1 + baseURL: https://dynamic-charts-dev.badhouseplants.net/ + buildDrafts: true + template: + metadata: + name: "{{ app }}-{{ name }}" + namespace: argo-system + spec: + project: "default" + source: + helm: + valueFiles: + - values.yaml + values: "{{ value }}" + repoURL: https://git.badhouseplants.net/api/packages/allanger/helm + targetRevision: "{{ chart_version }}" + chart: badhouseplants-net + destination: + server: "https://kubernetes.default.svc" + namespace: "{{ app }}-{{ name }}" + syncPolicy: + syncOptions: + - CreateNamespace=true + +``` + +But storing I don't like an idea of storing something like that in the repository. So in the git I'm putting something like that. +```YAML +apiVersion: argoproj.io/v1alpha1 +kind: ApplicationSet +metadata: + name: badhouseplants-net + namespace: argo-system +spec: + generators: + - list: + elements: + - name: application + app: badhouseplants + branch: main + chart_version: 0.3.6 + value: | + hugo: + image: + tag: $ARGO_IMAGE_TAG +... +``` + +Since I'm not using latest anymore, I need to add use a new tag every time a new image is pushed. But let's test with the preview env first: +```YAML +# ./kube/template.yaml +... +- name: $ARGO_APP_BRANCH + app: badhouseplants + branch: $ARGO_APP_BRANCH + chart_version: $ARGO_APP_CHART_VERSION + value: | + istio: + hosts: + - $ARGO_APP_HOSTNAME + hugo: + image: + tag: $ARGO_APP_IMAGE_TAG + baseURL: https://$ARGO_APP_HOSTNAME/ + buildDrafts: true +... +``` + +And the logic that I would like to have in my setup would be +- In the git repo there is only application set with the main instance only (production) +- After a new image is pushed to registry, I'm getting this application set as `yaml` and appending new generator to it. +- Applying a new `ApplicationSet` and syncing application using the `argo` cli tool + +First, let's set environment variables: +``` +- $ARGO_APP_BRANCH = $DRONE_BRANCH | I don't want to use it directly, in case if I want to stop using Drone +- $ARGO_APP_CHART_VERSION should be taken from the `./chart/Chart.yaml` file. `cat chart/Chart.yaml | yq '.version'` +- $ARGO_APP_HOSTNAME, I want it to look like that: "$DRONE_BRANCH-dev.badhouseplants.net" +- $ARGO_APP_IMAGE_TAG = $DRONE_COMMIT_SHA +``` + +So after setting all these variables, I can use `envsubst < ./kube/template.yaml` to create a correct generator. After that I only need to append it to one that is already in k8s. *And not to append if it's already there*. + +So let's design out pipeline: + +1. Get an application set is currently deployed to k8s + ```BASH + kubectl get application-set + ```