ArgoCD ApplicationSet CRD

发布时间 2023-12-13 17:39:17作者: 小吉猫

ApplicationSet 概述

ApplicationSet controller是一个 Kubernetes controller,添加了对 ApplicationSet CustomResourceDefinition (CRD) 的支持。该controller/CRD 实现了跨大量集群和 monorepos 内管理 Argo CD Application 的自动化和更大的灵活性,此外,它还使多租户 Kubernetes 集群上的自助服务使用成为可能。

ApplicationSet 功能

1. 能够使用单个 Kubernetes 清单通过 Argo CD 部署到多个 Kubernetes 集群
2. 能够使用单个 Kubernetes 清单通过 Argo CD 从一个或多个 Git 存储库部署多个应用程序
3. 改进了对 monorepos 的支持:在 Argo CD 的context中,monorepo 是在单个 Git 存储库中定义的多个 Argo CD Application资源
4. 在多租户集群内,提高单个集群租户使用 Argo CD 部署应用程序的能力(无需特权集群管理员参与启用目标clusters/namespaces)

ApplicationSet controller 工作模式

ApplicationSet controller的唯一职责是创建、更新和删除 Argo CD 命名空间内的Application资源。controller的唯一工作是确保Application资源与定义的声明性 ApplicationSet 资源保持一致。因此,ApplicationSet controller:
1. 不创建/修改/删除 Kubernetes 资源(Application CR 除外)
2. 不会连接到部署了Argo CD 的集群以外的集群
3. 除了argocd名称空间外,不与其他命称空间交互
Argo CD 本身负责生成子Application资源的实际部署,例如 Deployments、Services 和 ConfigMaps。
ApplicationSet控制器可被视作Application CRD资源的工厂,它将ApplicationSet资源作为输入,而输出的是一个或多个Application资源配置

 

在此图中ApplicationSet定义了一个资源,ApplicationSet controller负责创建相应的Application资源。然后生成的Application资源由 Argo CD 管理:也就是说,Argo CD 负责实际部署资源。
Argo CD 根据Application spec字段中定义的 Git 存储库的内容生成Application的 Kubernetes 资源, e.g. Deployments, Service和其他资源。
Applications的创建、更新或删除将对 Argo CD 命名空间中存在的Application产生直接影响。同样,cluster events(使用Cluster generator时添加/删除 Argo CD cluster secrets)或 Git 中的更改(使用 Git generator时)将在构建Application资源时用作 ApplicationSet controller的输入。
Argo CD 和 ApplicationSet controller协同工作,确保存在一组一致的Application资源,并跨目标集群部署。

ApplicationSet generators 

1. List generator
2. Cluster generator
3. Git generator
4. Matrix generator
5. Merge generator
6. SCM Provider generator
7. Pull Request generator
8. Cluster Decision Resource generator
9. Plugin generator

ApplicationSet CRD

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: guestbook
  namespace: argocd
spec:
  # See docs for available generators and their specs.
  generators:                 # 定义负责生成参数的生成器,这些参数会被用于渲染template字段中定义的模板
  - list:
      elements:
      - cluster: engineering-dev
        url: https://1.2.3.4
      - cluster: engineering-prod
        url: https://2.4.6.8
      - cluster: finance-preprod
        url: https://9.8.7.6
  # Determines whether go templating will be used in the `template` field below.
  goTemplate: false
  # Optional list of go templating options, see https://pkg.go.dev/text/template#Template.Option
  # This is only relevant if `goTemplate` is true
  goTemplateOptions: ["missingkey="]
  # These fields are identical to the Application spec.
  template:                   # Application资源模板,配置格式与Application规范相同,但它含有一些参数化的配置,通过将这些参数替换为generators生成的“值”完成模板的实例化
    metadata:
      name: '{{cluster}}-guestbook'
    spec:
      project: my-project
      source:
        repoURL: https://github.com/infra-team/cluster-deployments.git
        targetRevision: HEAD
        path: guestbook/{{cluster}}
      destination:
        server: '{{url}}'
        namespace: guestbook
  # This sync policy pertains to the ApplicationSet, not to the Applications it creates.
  syncPolicy:                 # 资源同步策略,仅支持内嵌一个布尔型字段preserveResourcesOnDeletion
    # Determines whether the controller will delete Applications when an ApplicationSet is deleted.
    preserveResourcesOnDeletion: false
  # Alpha feature to determine the order in which ApplicationSet applies changes.
  strategy:
  # This field lets you define fields which should be ignored when applying Application resources. This is helpful if you
  # want to use ApplicationSets to create apps, but also want to allow users to modify those apps without having their
  # changes overwritten by the ApplicationSet.
  ignoreApplicationDifferences:
  - jsonPointers:
    - /spec/source/targetRevision
  - name: some-app
    jqPathExpressions:
    - .spec.source.helm.values

参考文档

https://argo-cd.readthedocs.io/en/stable/operator-manual/applicationset/