kubernetes - argocd 应用程序在 CI 管道中创建(GitHub Actions、Tekton...)抛出 "PermissionDenied desc = permission denied: applications, create, default/myapp"

标签 kubernetes continuous-deployment argocd tekton gitops

在我们的 Tekton 管道中,我们希望使用 ArgoCD CLI 根据构建的应用程序动态执行 argocd 应用程序创建argocd 应用程序同步。我们创建了一个新用户as described in the docs通过将 accounts.tekton: apiKey 添加到 argocd-cm ConfigMap:

kubectl patch configmap argocd-cm -n argocd -p '{"data": {"accounts.tekton": "apiKey"}}'

然后我们为 tekton 用户创建了一个 token :

argocd account generate-token --account tekton

使用此 token 作为密码用户名 tekton,我们进行了argocd登录,如下

argocd login $(kubectl get service argocd-server -n argocd --output=jsonpath='{.status.loadBalancer.ingress[0].hostname}') --username=tekton --password="$TOKEN";

现在,在我们的 Tekton 管道中(但我们猜测,考虑到非管理员用户的使用情况,这对于所有其他 CI 来说都是相同的),如果我们运行 argocd app create,我们会收到以下错误>:

$ argocd app create microservice-api-spring-boot --repo https://gitlab.com/jonashackt/microservice-api-spring-boot-config.git --path deployment --dest-server https://kubernetes.default.svc --dest-namespace default --revision argocd --sync-policy auto
error rpc error: code = PermissionDenied desc = permission denied: applications, create, default/microservice-api-spring-boot, sub: tekton, iat: 2022-02-03T16:36:48Z

最佳答案

Argo's useraccounts docs中提到了该问题:

When you create local users, each of those users will need additional RBAC rules set up, otherwise they will fall back to the default policy specified by policy.default field of the argocd-rbac-cm ConfigMap.

但是这些额外的 RBAC 规则可以设置为最简单的 using ArgoCD Projects 。并有了这样一个AppProject您甚至不需要创建像 tekton 这样的用户在 ConfigMap argocd-cm 中。 ArgoCD项目有能力定义Project roles :

Projects include a feature called roles that enable automated access to a project's applications. These can be used to give a CI pipeline a restricted set of permissions. For example, a CI system may only be able to sync a single app (but not change its source or destination).

有两种解决方案如何配置 AppProject 、角色和权限,包括。角色 token :

  1. 使用 argocd命令行界面
  2. 使用 list YAML 文件

1.) 使用argocd CLI 创建AppProject 、角色和权限,包括。角色 token

所以让我们动手创建一个 ArgoCD AppProject使用argocd CLI 称为 apps2deploy :

argocd proj create apps2deploy -d https://kubernetes.default.svc,default --src "*"

我们用 --src "*" 创建它作为任何 git 存储库的通配符 ( as described here )。

现在我们创建一个项目rolecreate-sync通过:

argocd proj role create apps2deploy create-sync --description "project role to create and sync apps from a CI/CD pipeline"

您可以使用 argocd proj role list apps2deploy 检查新角色是否已创建。 .

然后我们需要为新的项目角色create-sync创建一个 token ,可以通过以下方式创建:

argocd proj role create-token apps2deploy create-sync

此 token 需要用于 argocd login我们的 Tekton/CI 管道内的命令。还有一个--token-only命令的参数,因此我们可以通过创建环境变量

ARGOCD_AUTH_TOKEN=$(argocd proj role create-token apps2deploy create-sync --token-only)

ARGOCD_AUTH_TOKEN将被 argo login 自动使用.

现在我们需要向该角色授予权限,以便它能够从 Tekton 或任何其他 CI 管道中在 ArgoCD 中创建和同步我们的应用程序。 As described in the docs we therefore add policies to our roles使用argocd命令行界面:

argocd proj role add-policy apps2deploy create-sync --action get --permission allow --object "*"
argocd proj role add-policy apps2deploy create-sync --action create --permission allow --object "*"
argocd proj role add-policy apps2deploy create-sync --action sync --permission allow --object "*"
argocd proj role add-policy apps2deploy create-sync --action update --permission allow --object "*"
argocd proj role add-policy apps2deploy create-sync --action delete --permission allow --object "*"

查看 argocd proj role get apps2deploy create-sync 的角色策略,它应该看起来像这样:

$ argocd proj role get apps2deploy create-sync
Role Name:     create-sync
Description:   project role to create and sync apps from a CI/CD pipeline
Policies:
p, proj:apps2deploy:create-sync, projects, get, apps2deploy, allow
p, proj:apps2deploy:create-sync, applications, get, apps2deploy/*, allow
p, proj:apps2deploy:create-sync, applications, create, apps2deploy/*, allow
p, proj:apps2deploy:create-sync, applications, update, apps2deploy/*, allow
p, proj:apps2deploy:create-sync, applications, delete, apps2deploy/*, allow
p, proj:apps2deploy:create-sync, applications, sync, apps2deploy/*, allow
JWT Tokens:
ID          ISSUED-AT                                EXPIRES-AT
1644166189  2022-02-06T17:49:49+01:00 (2 hours ago)  <none>

最后,我们应该完成一切设置以成功 argocd app create 。我们需要做的就是添加 --project apps2deploy参数:

argocd app create microservice-api-spring-boot --repo https://gitlab.com/jonashackt/microservice-api-spring-boot-config.git --path deployment --project apps2deploy --dest-server https://kubernetes.default.svc --dest-namespace default --revision argocd --sync-policy auto

2.) 使用 list YAML 创建 AppProject 、角色和权限,包括。角色 token

由于解决方案 1.) 中所有基于 CLI 的步骤相当多,我们还可以 using a manifest YAML file 。这是一个例子argocd-appproject-apps2deploy.yml其配置与解决方案 a) 中的配置完全相同:

apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
  name: apps2deploy
  namespace: argocd
spec:
  destinations:
    - namespace: default
      server: https://kubernetes.default.svc
  sourceRepos:
    - '*'
  roles:
    - description: project role to create and sync apps from a CI/CD pipeline
      name: create-sync
      policies:
      - p, proj:apps2deploy:create-sync, applications, get, apps2deploy/*, allow
      - p, proj:apps2deploy:create-sync, applications, create, apps2deploy/*, allow
      - p, proj:apps2deploy:create-sync, applications, update, apps2deploy/*, allow
      - p, proj:apps2deploy:create-sync, applications, delete, apps2deploy/*, allow
      - p, proj:apps2deploy:create-sync, applications, sync, apps2deploy/*, allow

只剩下2步就能成功 argocd app create来自 Tekton(或其他 CI 管道)内部。我们需要apply list 与

kubectl apply -f argocd-appproject-apps2deploy.yml

我们需要创建一个角色 token ,最好将其直接分配给 ARGOCD_AUTH_TOKEN对于argocd login命令(也需要在之后完成):

ARGOCD_AUTH_TOKEN=$(argocd proj role create-token apps2deploy create-sync --token-only)

同上argocd app create解决方案 1 中提到的命令。)现在应该可以工作:

argocd app create microservice-api-spring-boot --repo https://gitlab.com/jonashackt/microservice-api-spring-boot-config.git --path deployment --project apps2deploy --dest-server https://kubernetes.default.svc --dest-namespace default --revision argocd --sync-policy auto

关于kubernetes - argocd 应用程序在 CI 管道中创建(GitHub Actions、Tekton...)抛出 "PermissionDenied desc = permission denied: applications, create, default/myapp",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71052421/

相关文章:

kubernetes - 标签上的Kubernetes AntiAffinity-通过Node-Labels传播副本

go - Client-Go,如何监视 Kubernetes 中新创建的 Pod

go - Travis CI 找不到 GitHub 部署的发布文件

kubernetes - Helm 图表最佳实践 : latest tag or not

ssl - 带有谷歌管理证书的 argocd ssl 证书

single-sign-on - Argo CD - SSO 登录后无法登录 UI

ssl - 内部将域名解析为 Kubernetes 服务

continuous-integration - Bamboo - 创建新分支时自动创建部署触发器

kubernetes - ArgoCD policy.csv 中的 'p' 和 'g' 是什么?

docker - 如何从外部浏览器访问 minikube 仪表板,部署在 gcloud 计算引擎上