Skip to main content

ApplicationSets


Table of Contents

  1. Introduction to ApplicationSets
  2. List Generator
  3. Git Generator
  4. Cluster Generator
  5. Matrix and Merge
  6. Hands-on exercises

1 - Introduction to ApplicationSets

Definition

ApplicationSet is a controller that automatically generates Argo CD Applications from templates.

Use cases

ScenarioSolution
Multi-environmentList Generator
Monorepo with multiple appsGit Generator
Multi-clusterCluster Generator
Complex combinationsMatrix Generator

Base structure

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: my-appset
namespace: argocd
spec:
generators:
- list:
elements:
- env: dev
- env: prod
template:
metadata:
name: 'my-app-{{env}}'
spec:
project: default
source:
repoURL: https://github.com/org/gitops.git
path: 'apps/my-app/overlays/{{env}}'
destination:
server: https://kubernetes.default.svc
namespace: '{{env}}'

🔝 Back to table of contents


2 - List Generator

Static definition

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: environments
namespace: argocd
spec:
generators:
- list:
elements:
- env: dev
namespace: development
replicas: "1"
- env: staging
namespace: staging
replicas: "2"
- env: prod
namespace: production
replicas: "5"
template:
metadata:
name: 'myapp-{{env}}'
spec:
project: default
source:
repoURL: https://github.com/org/gitops.git
path: 'apps/myapp/overlays/{{env}}'
targetRevision: HEAD
destination:
server: https://kubernetes.default.svc
namespace: '{{namespace}}'
syncPolicy:
automated:
prune: true

Result

3 generated Applications:

  • myapp-dev → namespace: development
  • myapp-staging → namespace: staging
  • myapp-prod → namespace: production

🔝 Back to table of contents


3 - Git Generator

Directories

# Structure du repo :
# apps/
# ├── frontend/
# ├── backend/
# └── worker/

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: apps
namespace: argocd
spec:
generators:
- git:
repoURL: https://github.com/org/gitops.git
revision: HEAD
directories:
- path: apps/*
template:
metadata:
name: '{{path.basename}}'
spec:
project: default
source:
repoURL: https://github.com/org/gitops.git
path: '{{path}}'
destination:
server: https://kubernetes.default.svc
namespace: default

Available variables

VariableDescription
{{path}}Full path (apps/frontend)
{{path.basename}}Folder name (frontend)
{{path[0]}}First segment (apps)

Files

# Lire des fichiers JSON/YAML pour générer des apps
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: apps-from-files
spec:
generators:
- git:
repoURL: https://github.com/org/gitops.git
revision: HEAD
files:
- path: "config/**/config.json"
template:
metadata:
name: '{{name}}'
spec:
source:
path: '{{path}}'
destination:
namespace: '{{namespace}}'
// config/frontend/config.json
{
"name": "frontend",
"path": "apps/frontend",
"namespace": "web"
}

🔝 Back to table of contents


4 - Cluster Generator

All clusters

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: monitoring
namespace: argocd
spec:
generators:
- clusters: {}
template:
metadata:
name: 'monitoring-{{name}}'
spec:
source:
repoURL: https://github.com/org/gitops.git
path: infrastructure/monitoring
destination:
server: '{{server}}'
namespace: monitoring

With labels

spec:
generators:
- clusters:
selector:
matchLabels:
env: production

Available variables

VariableDescription
{{name}}Cluster name
{{server}}K8s server URL
{{metadata.labels.X}}Cluster labels

Multi-cluster example

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: nginx-all-clusters
spec:
generators:
- clusters:
selector:
matchLabels:
env: production
template:
metadata:
name: 'nginx-{{name}}'
spec:
project: default
source:
repoURL: https://github.com/org/gitops.git
path: apps/nginx
destination:
server: '{{server}}'
namespace: web
syncPolicy:
automated:
prune: true
selfHeal: true

🔝 Back to table of contents


5 - Matrix and Merge

Matrix Generator

Cartesian product of two generators.

spec:
generators:
- matrix:
generators:
- list:
elements:
- env: dev
- env: prod
- clusters:
selector:
matchLabels:
region: us
template:
metadata:
name: 'myapp-{{env}}-{{name}}'

Merge Generator

Combines results with override.

spec:
generators:
- merge:
mergeKeys:
- env
generators:
- list:
elements:
- env: dev
replicas: "1"
- env: prod
replicas: "5"
- list:
elements:
- env: prod
replicas: "10" # Override
template:
# env=prod aura replicas=10

Complex example

# Déployer sur tous les clusters prod, 
# avec des configs différentes par région
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: regional-apps
spec:
generators:
- matrix:
generators:
- git:
repoURL: https://github.com/org/gitops.git
directories:
- path: apps/*
- clusters:
selector:
matchLabels:
env: production
template:
metadata:
name: '{{path.basename}}-{{name}}'
spec:
source:
path: '{{path}}'
destination:
server: '{{server}}'

🔝 Back to table of contents


6 - Hands-on exercises

Exercise 1: Multi-environment

# appset-multi-env.yaml
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: guestbook-envs
namespace: argocd
spec:
generators:
- list:
elements:
- env: dev
- env: staging
- env: prod
template:
metadata:
name: 'guestbook-{{env}}'
spec:
project: default
source:
repoURL: https://github.com/argoproj/argocd-example-apps.git
path: guestbook
destination:
server: https://kubernetes.default.svc
namespace: '{{env}}'
syncPolicy:
automated:
prune: true
syncOptions:
- CreateNamespace=true
kubectl apply -f appset-multi-env.yaml
argocd app list

Quiz

Q1. What is the difference between List and Git Generator?

Answer
  • List Generator: Elements defined statically in the ApplicationSet
  • Git Generator: Elements generated dynamically from the Git repo structure (folders or files)

The Git Generator is more dynamic because it automatically adapts when you add/remove folders.

Q2. How do you deploy to all production clusters?

Answer

Use the Cluster Generator with a selector:

generators:
- clusters:
selector:
matchLabels:
env: production

🔝 Back to table of contents


Key takeaways

  • ApplicationSet generates Applications automatically
  • List: static definition
  • Git: based on the repo structure
  • Cluster: based on registered clusters
  • Matrix: cartesian product of two generators
  • Variables: {{path}}, {{name}}, {{server}}

← Previous chapter | Next chapter: Multi-cluster →