Argo CD ApplicationSet SCM Provider Generator 全解析:从 GitHub 到 AWS CodeCommit 的仓库自动发现与 GitOps 落地
2026/9/13 0:31:47 网站建设 项目流程

Argo CD ApplicationSet SCM Provider Generator 全解析:从 GitHub 到 AWS CodeCommit 的仓库自动发现与 GitOps 落地

【免费下载链接】argo-cdDeclarative Continuous Deployment for Kubernetes项目地址: https://gitcode.com/GitHub_Trending/ar/argo-cd

SCM Provider Generator 是 Argo CD ApplicationSet 内置的生成器,它通过调用 GitHub、GitLab、Gitea、Bitbucket(Server/Cloud)、Azure DevOps、AWS CodeCommit 等 SCM 托管平台(SCMaaS)的 API,自动发现组织(Organization/Group/Workspace/Project)内符合条件的仓库,并为每个仓库(或每个分支)生成一个 Application。这天然契合"一个微服务一个仓库"的 GitOps 布局模式:新仓库在 SCM 平台创建并打上标签后,无需任何人工干预即可被 Argo CD 接管。读完本文,你将掌握scmProvider生成器的全部配置项、六类平台的具体用法、过滤器(Filters)的组合逻辑、模板参数与values插值技巧,以及源码层面的实现原理与安全边界。

SCM Provider Generator 的基本形态与 cloneProtocol

SCM Provider Generator 在 ApplicationSet 的spec.generators中声明,核心字段是cloneProtocol,用于指定最终生成 Application 时使用的克隆协议:

apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: myapps spec: generators: - scmProvider: # Which protocol to clone using. cloneProtocol: ssh # See below for provider specific options. github: # ...

cloneProtocol决定 SCM URL 的形态:默认值为 provider 相关,但只要 provider 支持 SSH 就默认使用 SSH;并不是所有 provider 都支持所有协议(各 provider 支持的协议见下文各小节)。从源码看,该字段在 pkg/apis/application/v1alpha1/applicationset_types.go 中定义为可选字符串;provider 端对协议的解析逻辑位于各 provider 实现的ListRepos方法中,例如 GitHub 的实现在 github.go:ssh使用GetSSHURL()https使用GetCloneURL(),空值同样回退到 SSH,其余值会直接报错unknown clone protocol for GitHub

该生成器还支持requeueAfterSeconds字段,用于控制 ApplicationSet 控制器多久重新扫描一次 SCM 平台。若未显式指定,scm_provider.go 中的GetRequeueAfter会返回默认值DefaultSCMProviderRequeueAfter,即 30 分钟(DefaultSCMProviderRequeueAfter = 30 * time.Minute,见 scm_provider.go)。

[!NOTE] 使用 SCM 生成器前务必了解其安全影响:只有管理员才能创建/更新/删除 ApplicationSets,以避免泄露 Secret;若带 SCM 生成器的 ApplicationSet 的project字段被模板化,只有管理员才能创建仓库/分支,以避免越权管理超出范围的资源。相关安全分析详见 Security.md。

代理配置:为 SCM API 请求单独设置 Proxy

如果 ApplicationSet 控制器需要通过 HTTP/HTTPS 代理访问 SCM 平台 API(GitHub、GitLab、Gitea、Bitbucket Server 等),请使用专门的 SCM 代理参数,而不是通用的 kubectl 代理参数:

argocd-applicationset-controller \ --scm-proxy-url=http://proxy.corp.example.com:3128 \ --scm-no-proxy=internal.gitlab.corp.example.com,10.0.0.0/8

上述参数也可以等价地通过环境变量设置:

  • ARGOCD_APPLICATIONSET_CONTROLLER_SCM_PROXY_URL
  • ARGOCD_APPLICATIONSET_CONTROLLER_SCM_NO_PROXY

从源码看,这两个配置通过WithProxyURL/WithNoProxyList注入到SCMConfig(见 scm_provider.go),并在newSCMHTTPClient中基于http.DefaultTransport克隆出一个携带代理回调的 Transport(见 scm_provider.go);GitLab 等 provider 还会在构造 HTTP 客户端时把代理回调一并应用到自己的 Transport 上(见 gitlab.go)。

[!NOTE]--scm-proxy-url只影响出站的 SCM API 请求,不影响Kubernetes API Server 的连接。Kubernetes API 流量需要使用标准的--proxy-url(kubectl 的标准参数)来代理。

GitHub:扫描组织仓库(github.com 与 GitHub Enterprise)

GitHub 模式使用 GitHub API 扫描指定组织下的仓库,既支持 github.com,也支持 GitHub Enterprise:

apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: myapps spec: generators: - scmProvider: github: # The GitHub organization to scan. organization: myorg # For GitHub Enterprise: api: https://git.example.com/ # If true, scan every branch of every repository. If false, scan only the default branch. Defaults to false. allBranches: true # Exclude repos that are archived excludeArchivedRepos: true # Reference to a Secret containing an access token. (optional) tokenRef: secretName: github-token key: token # (optional) use a GitHub App to access the API instead of a PAT. appSecretName: gh-app-repo-creds template: # ...

各字段说明:

  • organization必填,要扫描的 GitHub 组织名。如果有多个组织,请配置多个生成器(每个 generator 对应一个组织)。
  • api:使用 GitHub Enterprise 时填写其访问 URL。
  • allBranches:默认false,此时模板只对每个仓库的默认分支求值;设为true后,每个仓库的每个分支都会传给过滤器。开启该选项后建议配合branchMatch过滤器使用。
  • tokenRefSecret的名称和键,内含用于请求的 GitHub 访问令牌。不指定时将以匿名身份请求,速率限制更低,且只能看到公开仓库。
  • appSecretNameSecret名称,内含 GitHub App 的密钥,格式遵循 repo-creds 格式(可参考 github_app_auth 相关实现)。
  • excludeArchivedRepos:排除已归档(archived)的仓库,默认false

GitHub 模式的标签过滤使用仓库的topics(主题标签)作为标签来源(见 github.go,Labels直接取自githubRepo.Topics)。支持的克隆协议为sshhttps

从实现细节看,GitHub provider 基于go-githubSDK:分页拉取组织下仓库(每页 100 个,自动翻页直到resp.NextPage == 0),excludeArchivedRepos在枚举阶段直接跳过GetArchived() == true的仓库;RepoHasPath通过GetContentsAPI 探测仓库中是否存在指定路径,404 视为"路径不存在"而非错误(见 github.go)。此外,若通过appSecretName指定了 GitHub App,控制器会优先使用 GitHub App 认证(见 scm_provider.go),否则才回退到tokenRef的 PAT。

GitLab:扫描 Group(含子组、共享项目与自签名 TLS)

GitLab 模式使用 GitLab API 扫描 gitlab.com 或自托管 GitLab 中的 Group:

apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: myapps spec: generators: - scmProvider: gitlab: # The base GitLab group to scan. You can either use the group id or the full namespaced path. group: "8675309" # For self-hosted GitLab: api: https://gitlab.example.com/ # If true, scan every branch of every repository. If false, scan only the default branch. Defaults to false. allBranches: true # If true, recurses through subgroups. If false, it searches only in the base group. Defaults to false. includeSubgroups: true # If true and includeSubgroups is also true, include Shared Projects, which is gitlab API default. # If false only search Projects under the same path. Defaults to true. includeSharedProjects: false # Include repos that are archived includeArchivedRepos: true # filter projects by topic. A single topic is supported by Gitlab API. Defaults to "" (all topics). topic: "my-topic" # Reference to a Secret containing an access token. (optional) tokenRef: secretName: gitlab-token key: token # If true, skips validating the SCM provider's TLS certificate - useful for self-signed certificates. insecure: false # Reference to a ConfigMap containing trusted CA certs - useful for self-signed certificates. (optional) caRef: configMapName: argocd-tls-certs-cm key: gitlab-ca template: # ...

各字段说明:

  • group必填,要扫描的基础 GitLab Group,既可使用 group id,也可使用完整的命名空间路径(namespaced path)。多个基础 Group 需配置多个生成器。
  • api:自托管 GitLab 时填写访问 URL。
  • allBranches:默认false,只对默认分支求值;true时每个分支都会传给过滤器,建议配合branchMatch
  • includeSubgroups:默认false,只扫描基础 Group 直属的仓库;true时递归遍历所有子组(subgroups)中的仓库。
  • includeSharedProjects:当includeSubgroupstrue时生效,控制是否包含共享项目(Shared Projects)。GitLab API 默认包含共享项目,因此该字段默认true;设为false时只搜索同一路径下的项目。大多数场景建议显式设为false
  • includeArchivedRepos:包含已归档仓库,默认false(GitLab API 默认不返回归档项目,源码中通过Archived: gitlab.Ptr(true)显式打开,见 gitlab.go)。
  • topic:按 topic 过滤项目。GitLab API 只支持单个 topic,默认""(不过滤)。
  • tokenRefSecret名称和键,内含 GitLab 访问令牌。不指定时匿名请求,速率限制低且只能访问公开仓库。
  • insecure:默认false。跳过对 SCM 证书有效性的校验,适用于自签名 TLS 证书。
  • caRef:可选ConfigMap名称和键,内含需要信任的 GitLab 证书,同样适用于自签名 TLS 证书,可直接引用 Argo CD 存放受信证书的 ConfigMap。

GitLab 的标签过滤同样使用仓库的topics。支持的克隆协议为sshhttps

自签名 TLS 证书的推荐配置

相比把insecure设为true,更推荐为 GitLab 显式配置自签名 TLS 证书:

  1. 将自签名证书挂载到 applicationset-controller 上;
  2. 通过环境变量ARGOCD_APPLICATIONSET_CONTROLLER_SCM_ROOT_CA_PATH或参数--scm-root-ca-path显式指定挂载证书的路径,控制器会读取该证书用于创建 SCM/PR Provider 的 GitLab 客户端;
  3. 更便捷的方式是在 argocd-cmd-params-cm ConfigMap 中设置applicationsetcontroller.scm.root.ca.path

设置完成后务必重启 ApplicationSet 控制器使其生效。从源码看,该根 CA 路径连同insecurecaRef提供的证书会统一组装进 TLS 客户端配置utils.GetTlsConfig(scmRootCAPath, insecure, caCerts)(见 gitlab.go);同时 GitLab 客户端还包了一层retryablehttp以实现请求重试(见 gitlab.go)。

Gitea:扫描实例中的组织

Gitea 模式使用 Gitea API 扫描你实例中的组织(organization):

apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: myapps spec: generators: - scmProvider: gitea: # The Gitea owner to scan. owner: myorg # The Gitea instance url api: https://gitea.mydomain.com/ # If true, scan every branch of every repository. If false, scan only the default branch. Defaults to false. allBranches: true # Exclude repos that are archived excludeArchivedRepos: true # Reference to a Secret containing an access token. (optional) tokenRef: secretName: gitea-token key: token template: # ...

各字段说明:

  • owner必填,要扫描的 Gitea 组织名。多个组织需配置多个生成器。
  • api:所使用的 Gitea 实例 URL。
  • allBranches:默认false,只对默认分支求值;true时每个分支都传给过滤器,建议配合branchMatch
  • tokenRefSecret名称和键,内含 Gitea 访问令牌。不指定时匿名请求。
  • insecure:允许自签名 TLS 证书。
  • excludeArchivedRepos:排除已归档仓库,默认false

注意:Gitea 目前不支持标签过滤(对应的 provider 实现中Labels相关能力未接通)。支持的克隆协议为sshhttps

Bitbucket Server:扫描 Project 中的仓库(REST API 1.0)

Bitbucket Server 模式使用 Bitbucket Server API(1.0)扫描 Project 下的仓库。注意Bitbucket Server 不同于 Bitbucket Cloud(API 2.0)

apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: myapps spec: generators: - scmProvider: bitbucketServer: project: myproject # URL of the Bitbucket Server. Required. api: https://mycompany.bitbucket.org # If true, scan every branch of every repository. If false, scan only the default branch. Defaults to false. allBranches: true # Credentials for Basic authentication (App Password). Either basicAuth or bearerToken # authentication is required to access private repositories basicAuth: # The username to authenticate with username: myuser # Reference to a Secret containing the password or personal access token. passwordRef: secretName: mypassword key: password # Credentials for Bearer Token (App Token) authentication. Either basicAuth or bearerToken # authentication is required to access private repositories bearerToken: # Reference to a Secret containing the bearer token. tokenRef: secretName: repotoken key: token # If true, skips validating the SCM provider's TLS certificate - useful for self-signed certificates. insecure: true # Reference to a ConfigMap containing trusted CA certs - useful for self-signed certificates. (optional) caRef: configMapName: argocd-tls-certs-cm key: bitbucket-ca # Support for filtering by labels is TODO. Bitbucket server labels are not supported for PRs, but they are for repos template: # ...

各字段说明:

  • project必填,Bitbucket Project 名称。
  • api必填,访问 Bitbucket REST API 的 URL。
  • allBranches:默认false,只对默认分支求值;true时每个分支都传给过滤器,建议配合branchMatch

访问私有仓库必须提供认证凭据,二选一:

  • Basic Auth(目前唯一正式支持的认证方式)
    • username:认证用户名,只需对目标仓库有读权限;
    • passwordRefSecret名称和键,内含密码或个人访问令牌。
  • Bitbucket App Token(Bearer Token):使用bearerToken段:
    • tokenRefSecret名称和键,内含 App Token。

针对 Bitbucket Server 自签名证书,可使用以下选项:

  • insecure:默认false。跳过证书有效性校验,适用于自签名 TLS 证书。
  • caRef:可选ConfigMap名称和键,内含需要信任的 Bitbucket Server 证书,可直接引用 Argo CD 存放受信证书的 ConfigMap。

从源码看,Bitbucket Server 的三种认证方式(BearerToken/BasicAuth/ 无认证)在 scm_provider.go 中被分别实例化为不同的 provider 构造函数;标签过滤对 Bitbucket Server 仍是 TODO(Bitbucket Server 的标签虽然支持仓库但不支持 PR)。

支持的克隆协议为sshhttps

Azure DevOps:按团队项目发现仓库

Azure DevOps 模式使用 Azure DevOps API 在指定的组织(organization)内、基于团队项目(team project)查找符合条件的仓库。默认的 Azure DevOps URL 是https://dev.azure.com,可通过azureDevOps.api字段覆盖:

apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: myapps spec: generators: - scmProvider: azureDevOps: # The Azure DevOps organization. organization: myorg # URL to Azure DevOps. Optional. Defaults to https://dev.azure.com. api: https://dev.azure.com # If true, scan every branch of eligible repositories. If false, check only the default branch of the eligible repositories. Defaults to false. allBranches: true # The team project within the specified Azure DevOps organization. teamProject: myProject # Reference to a Secret containing the Azure DevOps Personal Access Token (PAT) used for accessing Azure DevOps. accessTokenRef: secretName: azure-devops-scm key: accesstoken template: # ...

各字段说明:

  • organization必填,Azure DevOps 组织名。
  • teamProject必填,指定organization内团队项目(team project)的名称。
  • accessTokenRef必填Secret名称和键,内含用于请求的 Azure DevOps 个人访问令牌(PAT)。
  • api:可选,Azure DevOps URL,未设置时使用https://dev.azure.com
  • allBranches:可选,默认falsetrue时扫描符合条件仓库的每个分支,false时只检查默认分支。

Bitbucket Cloud:扫描 Workspace(API V2)

Bitbucket 模式使用 Bitbucket API V2 扫描 bitbucket.org 上的 workspace:

apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: myapps spec: generators: - scmProvider: bitbucket: # The workspace id (slug). owner: "example-owner" # The user to use for basic authentication with an app password. user: "example-user" # If true, scan every branch of every repository. If false, scan only the main branch. Defaults to false. allBranches: true # Reference to a Secret containing an app password. appPasswordRef: secretName: appPassword key: password template: # ...

各字段说明:

  • owner:查询仓库时使用的 workspace ID(slug)。
  • user:用于向 bitbucket.org 的 Bitbucket API V2 进行认证的用户。
  • allBranches:默认false,只对主分支求值;true时每个分支都传给过滤器,建议配合branchMatch
  • appPasswordRefSecret名称和键,内含 Bitbucket app password。

Bitbucket Cloud不支持标签过滤。支持的克隆协议为sshhttps

AWS CodeCommit(Alpha):跨账号与跨区域扫描

AWS CodeCommit 模式使用 AWS ResourceGroupsTagging API 和 AWS CodeCommit API 跨 AWS 账号与区域扫描仓库:

apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: myapps spec: generators: - scmProvider: awsCodeCommit: # AWS region to scan repos. # default to the environmental region from ApplicationSet controller. region: us-east-1 # AWS role to assume to scan repos. # default to the environmental role from ApplicationSet controller. role: arn:aws:iam::111111111111:role/argocd-application-set-discovery # If true, scan every branch of every repository. If false, scan only the main branch. Defaults to false. allBranches: true # AWS resource tags to filter repos with. # default to no tagFilters, to include all repos in the region. tagFilters: - key: organization value: platform-engineering - key: argo-ready template: # ...

各字段说明:

  • region:可选,要扫描仓库的 AWS 区域。默认使用 ApplicationSet 控制器当前所在区域。
  • role:可选,扫描仓库时要扮演(assume)的 AWS 角色。默认使用 ApplicationSet 控制器当前角色。
  • allBranches:可选,true时扫描符合条件仓库的每个分支,false时只检查默认分支,默认false
  • tagFilters:可选,用于过滤 AWS CodeCommit 仓库的标签过滤器列表,语义与 AWS ResourceGroupsTagging API 的TagFilters一致(key+ 可选value)。默认不加任何过滤,包含区域内全部仓库。

AWS CodeCommit 模式不支持以下特性:

  • 标签过滤(label filtering);
  • shashort_shashort_sha_7模板参数。

支持的克隆协议为sshhttpshttps-fips(FIPS 端点,见 aws_codecommit.go 中的prefixGitURLHTTPSFIPS)。

AWS IAM 权限考量

要调用 AWS API 发现 AWS CodeCommit 仓库,ApplicationSet 控制器必须配置有效的环境级 AWS 配置(如当前区域和凭据)。AWS 配置可通过所有标准方式提供,例如实例元数据服务(IMDS)、配置文件、环境变量或 IRSA(IAM Roles for Service Accounts)。

根据awsCodeCommit属性中是否提供role,AWS IAM 权限要求有所不同:

在 ApplicationSet 控制器同一 AWS 账号内发现仓库

不指定role时,ApplicationSet 控制器将使用自身 AWS 身份扫描 AWS CodeCommit 仓库。这适用于所有仓库与 Argo CD 位于同一 AWS 账号的简单场景。

由于直接使用控制器的 AWS 身份进行仓库发现,必须为其授予以下 AWS 权限:

  • tag:GetResources
  • codecommit:ListRepositories
  • codecommit:GetRepository
  • codecommit:GetFolder
  • codecommit:ListBranches
跨 AWS 账号与区域发现仓库

指定role后,ApplicationSet 控制器会先扮演该角色,再用其进行仓库发现,从而支持从不同 AWS 账号和区域发现仓库的复杂场景:

  • ApplicationSet 控制器的 AWS 身份需要被授予sts:AssumeRole权限;
  • 所有被扮演的 AWS 角色都必须具备仓库发现相关权限:tag:GetResourcescodecommit:ListRepositoriescodecommit:GetRepositorycodecommit:GetFoldercodecommit:ListBranches

从源码看,AWS provider 通过createAWSDiscoveryClients组装 AWS SDK 客户端,并使用 STS 的AssumeRole凭据提供者(stscreds)实现角色切换(见 aws_codecommit.go),底层同时依赖 ResourceGroupsTaggingAPI 的GetResources与 CodeCommit 的ListRepositories等接口完成发现。

Filters:精确挑选要生成 Application 的仓库

过滤器用于决定为哪些仓库生成 Application。规则要点:

  • 每个过滤器(filter)可以声明一个或多个条件,同一过滤器内所有条件必须全部满足
  • 存在多个过滤器时,仓库命中任意一个过滤器即可被包含;
  • 不指定过滤器时,处理全部仓库。
apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: myapps spec: generators: - scmProvider: filters: # Include any repository starting with "myapp" AND including a Kustomize config AND labeled with "deploy-ok" ... - repositoryMatch: ^myapp pathsExist: [kubernetes/kustomization.yaml] labelMatch: deploy-ok # ... OR include any repository starting with "otherapp" AND a Helm folder and doesn't have file disabledrepo.txt. - repositoryMatch: ^otherapp pathsExist: [helm] pathsDoNotExist: [disabledrepo.txt] template: # ...

过滤器字段:

  • repositoryMatch:与仓库名匹配的正则表达式。
  • pathsExist:仓库内必须存在的路径数组,可以是文件或目录。
  • pathsDoNotExist:仓库内必须不存在的路径数组,可以是文件或目录。
  • labelMatch:与仓库标签匹配的正则表达式。只要任意一个标签匹配,仓库即被包含(Gitea 用仓库标签、GitHub/GitLab 用 topics 作为标签来源)。
  • branchMatch:与分支名匹配的正则表达式,通常配合allBranches: true使用。

从源码看,过滤器的实现非常严谨,值得深入理解其两阶段执行模型(见 utils.go):

  1. 编译阶段compileFilters):所有正则(repositoryMatchlabelMatchbranchMatch)被预编译为regexp.Regexp,同时按作用对象将过滤器归类为FilterTypeRepo(仓库级:repositoryMatch/labelMatch)或FilterTypeBranch(分支级:pathsExist/pathsDoNotExist/branchMatch),见 types.go;
  2. 执行阶段ListRepos):先按FilterTypeRepo过滤器在"仓库"维度筛一遍(任一命中即保留),再对保留仓库调用GetBranches展开分支,最后按FilterTypeBranch过滤器在"分支"维度筛选(见 utils.go)。路径存在性检查通过各 provider 的RepoHasPath完成,GitHub 实现中 404 视为"路径不存在"(见 github.go)。

这套两阶段模型也解释了为什么pathsExist这类条件默认走分支维度:路径是否存在于不同分支上可能不同,branchMatch同理。仓库级过滤先于分支展开执行,可以显著减少不必要的 API 调用。

Template:生成器输出参数与模板使用

与所有生成器一致,SCM Provider 会为每个仓库生成一组参数,供ApplicationSet资源模板使用:

apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: myapps spec: goTemplate: true goTemplateOptions: ["missingkey=error"] generators: - scmProvider: # ... template: metadata: name: '{{ .repository }}' spec: source: repoURL: '{{ .url }}' targetRevision: '{{ .branch }}' path: kubernetes/ project: default destination: server: https://kubernetes.default.svc namespace: default

每个仓库生成的模板参数如下:

参数含义
organization仓库所在组织的名称
repository仓库名称
repository_id仓库的 ID
url仓库的克隆 URL(由cloneProtocol决定)
branch仓库的默认分支(或当前匹配的分支)
sha该分支对应的 Git commit SHA
short_sha缩写后的 Git commit SHA(8 个字符;若sha长度不足 8,则取sha的长度)
short_sha_7缩写后的 Git commit SHA(7 个字符;若sha长度不足 7,则取sha的长度)
labels逗号分隔的仓库标签列表:Gitea 为仓库标签,GitLab/GitHub 为 topics。Bitbucket Cloud、Bitbucket Server、Azure DevOps 不支持
branchNormalizedbranch的值经过规范化处理,只包含小写字母、数字、-.

从源码看,这些参数在 scm_provider.go 的GenerateParams中逐仓库组装,short_sha/short_sha_7的截断逻辑分别是min(len(repo.SHA), 8)min(len(repo.SHA), 7)branchNormalized则通过utils.SanitizeName(repo.Branch)生成。相关行为在 scm_provider_test.go 中有完整测试覆盖:例如 SHA 为0bc57212c3cbbec69d20b34c507284bd300def5bshort_sha0bc57212short_sha_70bc5721;当 SHA 仅为59d0(不足 8 位)时,short_shashort_sha_7都直接返回59d0

通过 values 字段传入额外的键值对

你可以在任意 SCM 生成器上通过values字段传入额外的、任意的字符串键值对。通过values字段添加的值会以values.(field)的形式出现在模板参数中。

下面的示例传递了一个name参数值,它由organizationrepository插值生成,用于产出不同的模板名称:

apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: myapps spec: goTemplate: true goTemplateOptions: ["missingkey=error"] generators: - scmProvider: bitbucketServer: project: myproject api: https://mycompany.bitbucket.org allBranches: true basicAuth: username: myuser passwordRef: secretName: mypassword key: password values: name: "{{.organization}}-{{.repository}}" template: metadata: name: '{{ .values.name }}' spec: source: repoURL: '{{ .url }}' targetRevision: '{{ .branch }}' path: kubernetes/ project: default destination: server: https://kubernetes.default.svc namespace: default

[!NOTE]values.前缀总会自动加到generators.scmProvider.values提供的值前面。在template中使用时必须带上这个前缀(即写成{{ .values.name }})。

values中可以插值 SCM 生成器产出的上述所有字段(如organizationrepositorybranch等)。从源码看,该插值在 scm_provider.go 中通过appendTemplatedValues完成,且会遵循 ApplicationSet 上配置的goTemplate/goTemplateOptions语义;测试用例 "Value interpolation" 也验证了values.should_i_force_push_to能被正确解析为"main?"这类跨字段插值(见 scm_provider_test.go)。

总结:从仓库发现到 Application 生成的全链路

回顾整个流程,SCM Provider Generator 在 ApplicationSet 控制器中的执行链路可以概括为:

  1. 配置解析:读取spec.generators[].scmProvider下的平台配置、过滤器、cloneProtocolrequeueAfterSecondsvalues(类型定义见 applicationset_types.go);
  2. Provider 实例化:根据配置的平台(GitHub/GitLab/Gitea/Bitbucket Server/Azure DevOps/Bitbucket Cloud/AWS CodeCommit)及认证方式(PAT/App Password/Bearer Token/GitHub App/AWS 角色),在 scm_provider.go 中创建对应的SCMProviderService实现;
  3. 仓库发现与过滤:调用ListRepos拉取仓库列表,按仓库级过滤器筛选,展开分支后按分支级过滤器筛选(utils.go);
  4. 参数生成:为每个命中的(仓库,分支)组合生成organizationrepositoryurlbranchshalabels等模板参数,并合并values插值;
  5. 模板渲染:用这些参数渲染template,产出 Application 资源,交给 Argo CD 完成同步。

在落地实践时,还有几点值得注意:SCM 发现默认每 30 分钟重跑一次(可用requeueAfterSeconds调整);生产环境建议为所有平台配置显式认证令牌而非匿名访问;GitHub/GitLab 的标签过滤依赖仓库 topics 维护;跨平台使用branchNormalized可安全生成符合 DNS/资源命名规范的 Application 名称;而 AWS CodeCommit 场景需要提前规划好跨账号 IAM 信任关系。掌握了这些细节,你就可以用一份 ApplicationSet 声明,驱动 Argo CD 自动接管组织中成百上千个仓库的持续部署。

【免费下载链接】argo-cdDeclarative Continuous Deployment for Kubernetes项目地址: https://gitcode.com/GitHub_Trending/ar/argo-cd

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询