Skip to content

Instantly share code, notes, and snippets.

@welteki
Last active June 29, 2022 13:59
Show Gist options
  • Save welteki/3651a526a805c6f97b8a0d0827cdc447 to your computer and use it in GitHub Desktop.
Save welteki/3651a526a805c6f97b8a0d0827cdc447 to your computer and use it in GitHub Desktop.
Automatically update OpenFaaS functions with the Argo CD Image Updater

Automatically update OpenFaaS functions with the Argo CD Image Updater

A short walk-through of how to use the Argo CD Image Updater to automatically update your OpenFaaS functions.

In this walk-through we bootstrap a new cluster using Argo CD with the app of apps pattern to deploy OpenFaaS and a set of OpenFaaS functions. We will then configure the Argo CD Image Updater to update our functions.

View the example on GitHub

Bootstrap a new cluster

Create a local cluster with kind

kind create cluster

We deploy Argo CD using arkade

arkade install argocd

Arkade will print out the instruction to port forward argocd and login using the argocd CLI. You can always retrieve these instructions later by running arkade info argocd

You can also use arkade to get the argocd CLI

arkade get argocd  

Create the applications

We create two Helm Charts one for our Argo CD applications and one for our functions.

The Argo CD Image Updater only works with applications of either Kustomize or Helm type so functions have to be packaged as a Helm Chart. For more details on how to create the functions Chart:

chart
├── applications
│   ├── Chart.yaml
│   ├── templates
│   │   ├── namespaces.yaml
│   │   ├── openfaas-functions-app.yaml
│   │   └── openfaas-operator-app.yaml
│   └── values.yaml
└── functions
    ├── Chart.yaml
    ├── templates
    │   ├── email-notify-func.yaml
    │   └── marketing-list-func.yaml
    └── values.yaml

The applications Chart has templates for two applications. The openfaas-operator-app for the deployment of OpenFaaS and the openfaas-functions-app for the deployment of our functions.

The template for the openfaas-operator application:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: openfaas-operator
  namespace: argocd
spec:
  destination:
    namespace: openfaas
    server: {{ .Values.spec.destination.server }}
  project: default
  source:
    helm:
      parameters:
      - name: operator.create
        value: "true"
      - name: generateBasicAuth
        value: "true"
      - name: functionNamespace
        value: openfaas-fn
    path: chart/openfaas
    repoURL: https://github.com/openfaas/faas-netes.git
  syncPolicy:
    automated:
      selfHeal: true

You can view the full example on GitHub

The parent app can be created and synced via the CLI:

argocd app create applications \
    --dest-namespace argocd \
    --dest-server https://kubernetes.default.svc \
    --repo https://github.com/welteki/openfaas-argocd-example.git \
    --path chart/applications

You can check the Argo CD UI to see if the apps were synced successfully or use kubectl to verify if OpenFaaS and the functions are running.

kubectl get pods -n openfaas

NAME                              READY   STATUS    RESTARTS          AGE
alertmanager-c4df79ff7-2fxcs      1/1     Running   2 (4h42m ago)     39h
basic-auth-plugin-588f588-rkgmz   1/1     Running   2 (4h42m ago)     39h
gateway-55fd54cb76-xf4n7          2/2     Running   3 (4h42m ago)     39h
nats-67d8f684f8-x46zj             1/1     Running   2 (4h42m ago)     39h
prometheus-cd4844fc7-c6j2b        1/1     Running   2 (4h42m ago)     39h
queue-worker-5795ff9bb5-tkwpv     1/1     Running   3 (4h42m ago)     39h
kubectl get functions -n openfaas-fn

NAME             IMAGE
email-notify     welteki/email-notify:0.1.0
marketing-list   welteki/marketing-list:0.1.0

Install the Argo CD Image Updater

For the different installation methods see: Argo CD Image Updater - Getting Started

We install the updater as Kubernetes workload in the Argo CD namespace.

kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj-labs/argocd-image-updater/stable/manifests/install.yaml

Configure the functions application

The openfaas-functions application needs to be correctly annotated for the Argo CD image Updater to know which application and images it should inspect and update.

The following annotation were added to the openfaas-functions application template

annotations:
    argocd-image-updater.argoproj.io/image-list: marketingList=welteki/marketing-list:~0.1
    argocd-image-updater.argoproj.io/marketingList.helm.image-spec: marketingList.image

For a detailed explanation of the configuration options see: Argo CD Image Updater - Configuration

The Argo CD Image Updater in action

We use envsubst-style templates in the YAML stack to make the image tag configurable.

  marketing-list:
    lang: node17
    handler: ./marketing-list
    image: ${REGISTRY:-docker.io}/${REPO:-welteki}/marketing-list:${TAG:-dev}

To build and push a new version of the images run

TAG=0.1.4 faas-cli publish -f stack.yml

The logs for the Argo Image Updater show that it is considering 1 annotated application for update. Once it queries the registry for updated images it will detect the new version of our function image and update the openfaas-functions application to use the new version of the marketing-list function.

time="2022-06-14T20:33:41Z" level=info msg="Starting image update cycle, considering 1 annotated application(s) for update"
time="2022-06-14T20:33:43Z" level=info msg="Setting new image to welteki/marketing-list:0.1.4" alias=marketingList application=openfaas-functions image_name=welteki/marketing-list image_tag=0.1.3 registry=
time="2022-06-14T20:33:43Z" level=info msg="Successfully updated image 'welteki/marketing-list:0.1.3' to 'welteki/marketing-list:0.1.4', but pending spec update (dry run=false)" alias=marketingList application=openfaas-functions image_name=welteki/marketing-list image_tag=0.1.3 registry=
time="2022-06-14T20:33:43Z" level=info msg="Committing 1 parameter update(s) for application openfaas-functions" application=openfaas-functions
time="2022-06-14T20:33:43Z" level=info msg="Successfully updated the live application spec" application=openfaas-functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment