1.9. 为 Argo CD push 模型创建自定义服务帐户


通过在 hub 集群上创建 ManagedServiceAccount 资源,在受管集群中创建服务帐户。使用 ClusterPermission 资源为服务帐户授予特定权限。

创建用于 Argo CD push 模型的 customzied 服务帐户包括以下优点:

  • 应用程序管理器附加组件在每个受管集群上运行。默认情况下,Argo CD 控制器使用服务帐户 Application Manager 将这些资源推送到受管集群。
  • 应用程序管理器服务帐户具有大量权限,因为应用程序订阅附加组件使用应用程序管理器服务在受管集群中部署应用程序。如果您需要一组有限的权限,请不要使用 Application manager 服务帐户。
  • 您可以指定希望 Argo CD 推送模型使用的不同服务帐户。当 Argo CD 控制器将资源从集中式 hub 集群推送到受管集群时,您可以使用与默认应用程序管理器不同的服务帐户。通过使用不同的服务帐户,您可以控制授予此服务帐户的权限。
  • 服务帐户必须存在于受管集群上。为便于创建具有关联权限的服务帐户,请在集中式 hub 集群上使用 ManagedServiceAccount 资源和新的 ClusterPermission 资源。

完成所有以下流程后,您可以为受管服务帐户授予集群权限。具有集群权限,您的受管服务帐户具有在受管集群中部署应用程序资源所需的权限。完成以下步骤:

1.9.1. 创建受管服务帐户

hub 上的 ManagedServiceAccount 自定义资源提供了一种便捷的方式,可在受管集群中创建 ServiceAccount。当在 hub 集群上的 < managed-cluster& gt; 命名空间中创建 ManagedServiceAccount 自定义资源时,会在受管集群中创建一个 ServiceAccount

要创建受管服务帐户,请参阅启用 ManagedServiceAccount 附加组件

1.9.2. 在 GitOpsCluster 资源中使用受管服务帐户

GitOpsCluster 资源使用放置将所选受管集群导入到 Argo CD 中,包括创建 Argo CD 集群 secret,其中包含用于访问集群的信息。默认情况下,Argo CD 集群 secret 使用应用程序管理器服务帐户来访问受管集群。

  1. 要将 GitOpsCluster 资源更新为使用受管服务帐户,请将 ManagedServiceAccountRef 属性添加到受管服务帐户的名称。
  2. 将以下示例保存为 gitops.yaml 文件,以创建 GitOpsCluster 自定义资源:

    apiVersion: apps.open-cluster-management.io/v1beta1
    kind: GitOpsCluster
    metadata:
      name: argo-acm-importer
      namespace: openshift-gitops
    spec:
      managedServiceAccountRef: <managed-sa-sample>
      argoServer:
        cluster: notused
        argoNamespace: openshift-gitops
      placementRef:
        kind: Placement
        apiVersion: cluster.open-cluster-management.io/v1beta1
        name: all-openshift-clusters
        namespace: openshift-gitops
    Copy to Clipboard Toggle word wrap
  3. 运行 oc apply -f gitops.yaml 以应用该文件。
  4. 进入 openshift-gitops 命名空间,并验证是否有新的 Argo CD 集群 secret,名称为 < managed cluster-managed-sa-sample-cluster-secret>。运行以下命令:

    oc get secrets -n openshift-gitops <managed cluster-managed-sa-sample-cluster-secret>
    Copy to Clipboard Toggle word wrap
  5. 请查看以下输出来验证:

    NAME                                        TYPE     DATA   AGE
    <managed cluster-managed-sa-sample-cluster-secret>   Opaque   3      4m2s
    Copy to Clipboard Toggle word wrap

1.9.3. 创建 Argo CD 应用程序

使用推送模型从 Argo CD 控制台部署 Argo CD 应用程序。Argo CD 应用程序使用受管服务帐户 < managed-sa-sample > 部署。

  1. 登录到 Argo CD 控制台。
  2. 单击 Create a new application
  3. 选择集群 URL。
  4. 进入 Argo CD 应用程序,验证它具有给定权限,如角色和集群角色,您要传播到 < managed cluster >。

1.9.4. 使用策略创建受管服务帐户和集群权限

当使用 ManagedServiceAccountRef 更新 GitOpsCluster 资源时,此 GitOpsCluster 放置中的每个受管集群都需要具有服务帐户。如果您有多个受管集群,则为每个受管集群创建受管服务帐户和集群权限会非常繁琐。您可以使用策略为所有受管集群创建受管服务帐户和集群权限来简单这个过程

当您将 ManagedServiceAccountClusterPermission 资源应用到 hub 集群时,此策略的放置会绑定到本地集群。在 GitOpsCluster 资源放置中,将这些资源复制到所有受管集群的受管集群。

使用策略创建 ManagedServiceAccountClusterPermission 资源包含以下属性:

  • 更新策略中 ManagedServiceAccountClusterPermission 对象模板会导致在所有受管集群中更新所有 ManagedServiceAccountClusterPermission 资源。
  • 直接更新到 ManagedServiceAccountClusterPermission 资源将恢复回原始状态,因为它由策略强制执行。
  • 如果 GitOpsCluster 放置更改的放置决定,策略会管理受管集群命名空间中资源的创建和删除。

    1. 要为 YAML 创建策略以创建受管服务帐户和集群权限,请使用以下内容创建一个 YAML:
    apiVersion: policy.open-cluster-management.io/v1
    kind: Policy
    metadata:
      name: policy-gitops
      namespace: openshift-gitops
      annotations:
        policy.open-cluster-management.io/standards: NIST-CSF
        policy.open-cluster-management.io/categories: PR.PT Protective Technology
        policy.open-cluster-management.io/controls: PR.PT-3 Least Functionality
    spec:
      remediationAction: enforce
      disabled: false
      policy-templates:
    
        - objectDefinition:
            apiVersion: policy.open-cluster-management.io/v1
            kind: ConfigurationPolicy
            metadata:
              name: policy-gitops-sub
            spec:
              pruneObjectBehavior: None
              remediationAction: enforce
              severity: low
              object-templates-raw: |
                {{ range $placedec := (lookup "cluster.open-cluster-management.io/v1beta1" "PlacementDecision" "openshift-gitops" "" "cluster.open-cluster-management.io/placement=aws-app-placement").items }}
                {{ range $clustdec := $placedec.status.decisions }}
                - complianceType: musthave
                  objectDefinition:
                    apiVersion: authentication.open-cluster-management.io/v1alpha1
                    kind: ManagedServiceAccount
                    metadata:
                      name: <managed-sa-sample>
                      namespace: {{ $clustdec.clusterName }}
                    spec:
                      rotation: {}
                - complianceType: musthave
                  objectDefinition:
                    apiVersion: rbac.open-cluster-management.io/v1alpha1
                    kind: ClusterPermission
                    metadata:
                      name: <clusterpermission-msa-subject-sample>
                      namespace: {{ $clustdec.clusterName }}
                    spec:
                      roles:
                      - namespace: default
                        rules:
                        - apiGroups: ["apps"]
                          resources: ["deployments"]
                          verbs: ["get", "list", "create", "update", "delete"]
                        - apiGroups: [""]
                          resources: ["configmaps", "secrets", "pods", "podtemplates", "persistentvolumeclaims", "persistentvolumes"]
                          verbs: ["get", "update", "list", "create", "delete"]
                        - apiGroups: ["storage.k8s.io"]
                          resources: ["*"]
                          verbs: ["list"]
                      - namespace: mortgage
                        rules:
                        - apiGroups: ["apps"]
                          resources: ["deployments"]
                          verbs: ["get", "list", "create", "update", "delete"]
                        - apiGroups: [""]
                          resources: ["configmaps", "secrets", "pods", "services", "namespace"]
                          verbs: ["get", "update", "list", "create", "delete"]
                      clusterRole:
                        rules:
                        - apiGroups: ["*"]
                          resources: ["*"]
                          verbs: ["get", "list"]
                      roleBindings:
                      - namespace: default
                        roleRef:
                          kind: Role
                        subject:
                          apiGroup: authentication.open-cluster-management.io
                          kind: ManagedServiceAccount
                          name: <managed-sa-sample>
                      - namespace: mortgage
                        roleRef:
                          kind: Role
                        subject:
                          apiGroup: authentication.open-cluster-management.io
                          kind: ManagedServiceAccount
                          name: <managed-sa-sample>
                      clusterRoleBinding:
                        subject:
                          apiGroup: authentication.open-cluster-management.io
                          kind: ManagedServiceAccount
                          name: <managed-sa-sample>
                {{ end }}
                {{ end }}
    ---
    apiVersion: policy.open-cluster-management.io/v1
    kind: PlacementBinding
    metadata:
      name: binding-policy-gitops
      namespace: openshift-gitops
    placementRef:
      name: lc-app-placement
      kind: Placement
      apiGroup: cluster.open-cluster-management.io
    subjects:
      - name: policy-gitops
        kind: Policy
        apiGroup: policy.open-cluster-management.io
    ---
    apiVersion: cluster.open-cluster-management.io/v1beta1
    kind: Placement
    metadata:
      name: lc-app-placement
      namespace: openshift-gitops
    spec:
      numberOfClusters: 1
      predicates:
      - requiredClusterSelector:
          labelSelector:
            matchLabels:
              name: <your-local-cluster-name>
    Copy to Clipboard Toggle word wrap
    1. 将 YAML 文件保存到名为 policy.yaml 的文件中。
    2. 运行 oc apply -f policy.yaml
    3. 在策略的对象模板中,它会迭代 GitOpsCluster 关联的放置决策,并应用以下 ManagedServiceAccountClusterPermission 模板:
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat