From 627c8e58cf306de45114530f2f1fa696dace75e9 Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Sat, 16 Nov 2024 13:46:41 -0500 Subject: [PATCH] Add automatic image updates --- cluster/flux-system-automation.yaml | 25 ++++++++ .../documentation-deployment.yaml | 2 +- docs/technical/auto-image-updates.md | 63 +++++++++++++++++++ mkdocs.yaml | 1 + 4 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 cluster/flux-system-automation.yaml create mode 100644 docs/technical/auto-image-updates.md diff --git a/cluster/flux-system-automation.yaml b/cluster/flux-system-automation.yaml new file mode 100644 index 0000000..b22bea2 --- /dev/null +++ b/cluster/flux-system-automation.yaml @@ -0,0 +1,25 @@ +apiVersion: image.toolkit.fluxcd.io/v1beta2 +kind: ImageUpdateAutomation +metadata: + name: flux-system + namespace: flux-system +spec: + git: + checkout: + ref: + branch: main + commit: + author: + email: fluxcdbot@users.noreply.github.com + name: fluxcdbot + messageTemplate: '{{range .Changed.Changes}}{{print .OldValue}} -> {{println + .NewValue}}{{end}}' + push: + branch: main + interval: 15m0s + sourceRef: + kind: GitRepository + name: flux-system + update: + path: ./cluster + strategy: Setters diff --git a/cluster/static-site-hosts/documentation-deployment.yaml b/cluster/static-site-hosts/documentation-deployment.yaml index a6138cb..b22a359 100644 --- a/cluster/static-site-hosts/documentation-deployment.yaml +++ b/cluster/static-site-hosts/documentation-deployment.yaml @@ -14,7 +14,7 @@ spec: spec: containers: - name: documentation-site - image: git.clortox.com/infrastructure/gluttony-cluster/documentation:0.0.1 + image: git.clortox.com/infrastructure/gluttony-cluster/documentation:0.0.1 # {"$imagepolicy": "flux-system:gluttony-cluster-documentation"} ports: - name: http containerPort: 80 diff --git a/docs/technical/auto-image-updates.md b/docs/technical/auto-image-updates.md new file mode 100644 index 0000000..7530c95 --- /dev/null +++ b/docs/technical/auto-image-updates.md @@ -0,0 +1,63 @@ +# Automatic Image Updates + +If you want flux to automatically update container images, this is how. +Based on [this](https://fluxcd.io/flux/guides/image-update/) documentation. + + +## Make the deployment + +Start by making a normal deployment how you would normally. Just note not to use `latest` as your tag. +(You shouldn't be anyways). + +## Add an image scanner + +Create an image scanner to go pull new image versions. + +```bash +flux create image repository MYNAME \ + --image=git.clortox.com/infrastructure/my-cool-image \ + --internal=5m \ + --export > MYNAME-registry.yaml +``` + +Next make the `ImagePolicy` to choose what semver to filter for. + +```bash +flux create image policy MYNAME \ + --image-ref=MYNAME \ + --select-semver=0.0.x \ + --export > MYNAME-policy.yaml +``` + +## Configure Image Updates + +At this point flux can scan peridoically and see that new images are released. +Now lets automatically pull them down, and update the git repo. + +First, we have to tell flux where it should apply updates in our manifests. +To do that, add the following comment to the end of the line in your deployment + +```yaml +image: git.clortox.com/infrastructure/gluttony-cluster/documentation:0.0.1 # {"$imagepolicy": "flux-system:MYNAME"} +``` + +The syntax here is `NAMESPACE:IMAGE_POLICY_NAME`. + +After this, lets make the updater for the entire system. + +```bash +flux create image update flux-system \ + --interval=15m \ + --git-repo-ref=flux-system \ + --git-repo-path="./cluster" \ + --checkout-branch=main \ + --push-branch=main \ + --author-name=fluxcdbot \ + --author-email=fluxcdbot@users.noreply.github.com \ + --commit-template="{{range .Changed.Changes}}{{print .OldValue}} -> {{println .NewValue}}{{end}}" \ + --export > ./flux-system-automation.yaml +``` + +> [!NOTE] +> +> `flux-system` is the default source name. Check it with `flux get source git` diff --git a/mkdocs.yaml b/mkdocs.yaml index ae7b4e6..19cd5d9 100644 --- a/mkdocs.yaml +++ b/mkdocs.yaml @@ -41,6 +41,7 @@ nav: - NFS: technical/nfs-storage.md - Seal-a-secret: technical/seal-a-secret.md - Networking: technical/inter-pod-networking.md + - Automatic Image Updates: technical/auto-image-updates.md theme: name: material