深入解析 Argo CDargocd app actions list命令:列出资源上可执行的操作(Actions)
【免费下载链接】argo-cdDeclarative Continuous Deployment for Kubernetes项目地址: https://gitcode.com/GitHub_Trending/ar/argo-cd
在 Argo CD 中,除了同步(Sync)之外,用户经常需要对线上资源执行一些运维动作,例如重启 Deployment、暂停/恢复、调整副本数等。这些动作以“资源操作”(Resource Actions)的形式定义在资源自定义配置(Resource Customizations)中,而argocd app actions list就是查询某个应用下哪些资源支持哪些操作的 CLI 入口。本文以该命令的官方参考文档为主体,完整覆盖其用法与全部参数,并结合 CLI 源码 与 服务端实现 说明其底层工作流程,帮助你准确理解输出结果的含义、过滤参数的语义,以及如何与argocd app actions run配合完成实际操作。
命令概述
argocd app actions list用于列出某个应用下资源上所有可用的 actions(Lists available actions on a resource)。其基本调用形式为:
argocd app actions list APPNAME [flags]官方示例:
# List all the available actions for an application argocd app actions list APPNAME该命令属于argocd app actions命令组(Manage Resource actions)下的子命令,与之配套的还有argocd app actions run,用于真正执行某个已列出的操作。完整的命令组文档可参考 argocd app actions 命令参考,run子命令可参考 argocd app actions run 命令参考。
完整参数说明
本命令专属选项
以下选项完整继承自命令参考文档:
-N, --app-namespace string Namespace of the application --group string Group -h, --help help for list --kind string Kind --namespace string Namespace -o, --out string Output format. One of: yaml, json --resource-name string Name of resource结合 CLI 实现,各参数含义如下:
| 参数 | 含义 | 说明 |
|---|---|---|
APPNAME | 应用名 | 位置参数,必需且仅接受 1 个;支持namespace/name的限定名形式,由argo.ParseFromQualifiedName解析 |
-N, --app-namespace | 应用所在命名空间 | 用于多命名空间(namespaced)安装时指定 Application 所在的 K8s 命名空间 |
--group | 资源 API Group | 按组过滤资源,例如apps、argoproj.io |
--kind | 资源 Kind | 例如Deployment、StatefulSet、Application |
--namespace | 资源所在命名空间 | 注意这里是资源的命名空间,与应用命名空间(-N)区分 |
--resource-name | 资源名 | 精确到某一个资源 |
-o, --out | 输出格式 | 取值为yaml、json;不指定时默认输出对齐表格 |
从父命令继承的选项
以下全局选项由argocd根命令继承,所有 Argo CD CLI 命令均可用,在此完整保留原文档内容:
--argocd-context string The name of the Argo-CD server context to use --auth-token string Authentication token; set this or the ARGOCD_AUTH_TOKEN environment variable --client-crt string Client certificate file --client-crt-key string Client certificate key file --config string Path to Argo CD config (default "/home/user/.config/argocd/config") --controller-name string Name of the Argo CD Application controller; set this or the ARGOCD_APPLICATION_CONTROLLER_NAME environment variable when the controller's name label differs from the default, for example when installing via the Helm chart (default "argocd-application-controller") --core If set to true then CLI talks directly to Kubernetes instead of talking to Argo CD API server --grpc-web Enables gRPC-web protocol. Useful if Argo CD server is behind proxy which does not support HTTP2. --grpc-web-root-path string Enables gRPC-web protocol. Useful if Argo CD server is behind proxy which does not support HTTP2. Set web root. -H, --header strings Sets additional header to all requests made by Argo CD CLI. (Can be repeated multiple times to add multiple headers, also supports comma separated headers) --http-retry-max int Maximum number of retries to establish http connection to Argo CD server --insecure Skip server certificate and domain verification --kube-context string Directs the command to the given kube-context --logformat string Set the logging format. One of: json|text (default "json") --loglevel string Set the logging level. One of: debug|info|warn|error (default "info") --plaintext Disable TLS --port-forward Connect to a random argocd-server port using port forwarding --port-forward-namespace string Namespace name which should be used for port forwarding --prompts-enabled Force optional interactive prompts to be enabled or disabled, overriding local configuration. If not specified, the local configuration value will be used, which is false by default. --redis-compress string Enable this if the application controller is configured with redis compression enabled. (possible values: gzip, none) (default "gzip") --redis-haproxy-name string Name of the Redis HA Proxy; set this or the ARGOCD_REDIS_HAPROXY_NAME environment variable when the HA Proxy's name label differs from the default, for example when installing via the Helm chart (default "argocd-redis-ha-haproxy") --redis-name string Name of the Redis deployment; set this or the ARGOCD_REDIS_NAME environment variable when the Redis's name label differs from the default, for example when installing via the Helm chart (default "argocd-redis") --repo-server-name string Name of the Argo CD Repo server; set this or the ARGOCD_REPO_SERVER_NAME environment variable when the server's name label differs from the default, for example when installing via the Helm chart (default "argocd-repo-server") --server string Argo CD server address --server-crt string Server certificate file --server-name string Name of the Argo CD API server; set this or the ARGOCD_SERVER_NAME environment variable when the server's name label differs from the default, for example when installing via the Helm chart (default "argocd-server")输出格式解析
默认情况下(不指定-o),命令通过tabwriter输出一张五列对齐表格,表头为:
GROUP KIND NAME ACTION DISABLED这些列对应源码中定义的结构体 DisplayedAction:
type DisplayedAction struct { Group string Kind string Name string Action string Disabled bool }其中DISABLED列(布尔值)表示该动作在当前资源状态下是否被禁用——例如一个已处于暂停状态的 Deployment,其pause动作会显示为true而resume为false。指定输出格式时:
argocd app actions list my-app -o yaml:以 YAML 数组形式输出DisplayedAction列表,便于管道处理;argocd app actions list my-app -o json:以缩进 JSON(json.MarshalIndent)输出,便于脚本解析。
输出分支逻辑见 源码 L118-L134。
客户端执行流程:从 APPNAME 到 ListResourceActions
从 CLI 源码 可以还原出完整的调用流程:
- 解析应用名:
argo.ParseFromQualifiedName(args[0], appNamespace)将APPNAME(或其namespace/name限定形式)与应用命名空间拆开。 - 建立 gRPC 连接:通过
headless.NewClientOrDie(...).NewApplicationClientOrDieWithContext(ctx)获取 Application 服务客户端。 - 拉取可操作资源列表:调用 getActionableResourcesForApplication,它做两件事:
- 通过
ManagedResourcesRPC 获取应用管理的全部资源; - 通过
GetRPC 获取 Application 对象本身,并将其以ResourceDiff形式追加到资源列表中——因此 actions list 的结果中也会包含 Application 资源自身定义的动作(argoproj.io/Application的自定义 actions)。
- 通过
- 过滤:
util.FilterResources(...)依据--group、--kind、--namespace、--resource-name对资源做过滤。注意源码中第一个参数传的是command.Flags().Changed("group"),意味着只有当用户显式指定了--group时,Group 过滤才按精确匹配生效,否则 Kind/Name/Namespace 的组合即可定位资源。 - 逐资源请求动作清单:对每个过滤后的资源,按其 GVK 组装
ApplicationResourceRequest并调用ListResourceActionsRPC(源码 L96-L105),收集每个动作的name与disabled状态,组装为DisplayedAction输出。
服务端实现:Actions 是如何被发现和返回的
ListResourceActionsRPC 的 gRPC 定义位于 server/application/application.proto,服务端入口实现在 Server.ListResourceActions,核心步骤为:
- 获取线上资源:通过
getUnstructuredLiveResourceOrApp按请求中的 Group/Kind/Name 定位 live 对象;若请求的正是 Application 自身(argoproj.io/Application),则直接走 Application informer 路径,并对该应用做 RBACget校验。 - 读取资源覆盖配置:
s.settingsMgr.GetResourceOverrides()加载全部 Resource Customizations。 - 执行 Lua 发现脚本:getAvailableActions 通过内嵌 Lua 虚拟机依次调用
GetResourceActionDiscovery取出该资源类型的 action discovery 脚本,再ExecuteResourceActionDiscovery执行,得到动作列表(含name、disabled、iconClass、displayName、params等字段)。若该资源类型没有定义 discovery 脚本,则返回空列表。
也就是说,Actions 不是内置功能,而是由resource_customizations/目录中按 API Group/Kind 组织的自定义配置驱动的。以 Deployment 为例,resource_customizations/apps/Deployment/actions/action_test.yaml 的discoveryTests段展示了 discovery 脚本对同一 Kind 不同状态输入的判定结果:
discoveryTests: - inputPath: testdata/deployment.yaml result: - name: restart disabled: false iconClass: "fa fa-fw fa-redo" displayName: "" - name: pause disabled: false iconClass: "fa fa-fw fa-pause-circle" displayName: "" - name: resume disabled: true iconClass: "fa fa-fw fa-play-circle" displayName: "" - name: scale disabled: false iconClass: "fa fa-fw fa-plus-circle" displayName: "" params: - name: replicas这段测试数据恰好解释了argocd app actions list输出中DISABLED列的来历:resume在正常运行状态下为true(不可用),而scale动作带有replicas参数声明。同目录下还有 StatefulSet、DaemonSet、Rollout、postgresql.cnpg.io/Cluster等类型的 actions 配置与测试,覆盖了 Argo CD 内置支持动作的主要资源类型。
权限模型
服务端对两个 RPC 都执行 RBAC 校验:
ListResourceActions以rbac.ActionGet权限请求 live 资源(见 L2577-L2581);- 执行动作时构造动作权限串
actions/action/<group>/<kind>/<action>做校验(见 RunResourceActionV2 L2689-L2690)。
因此要列出或执行动作,账号需要同时具备对应用的get权限以及对应动作的 action 权限;服务端测试用例 server/application/application_test.go 中专门验证了无权限上下文(noRoleCtx)下调用ListResourceActions/RunResourceAction会被拒绝。
与argocd app actions run的配合
典型工作流是:先用list查询可用动作,再用run执行。例如:
# 第一步:查看 guestbook 应用中 Deployment 上有哪些动作 argocd app actions list guestbook --kind Deployment # 第二步:对指定资源执行 restart 动作 argocd app actions run guestbook restart --kind Deployment \ --resource-name guestbook --namespace defaultrun子命令的关键约束(来自其参考文档与 源码 L154-L170):
--kind为必填参数(MarkFlagRequired("kind"));--group、--namespace、--resource-name均为可选过滤器;--all:当过滤条件匹配到多个资源时,是否在全部匹配资源上执行该动作;- 官方说明明确指出:动作只能作用于在 Git 中有表示的资源,不能作用于子资源(child resources);
- 若过滤出的多个资源分属不同 API Group,CLI 会直接报错并提示使用
--group显式指定(源码 L188-L193)。
执行链路上,CLI 优先调用RunResourceActionV2(支持动作参数),若服务端返回Unimplemented则回退到已废弃的RunResourceAction(源码 L199-L228)。服务端侧的 V2 实现会先校验目标资源是否被 AppProject 允许(verifyResourcePermitted),对需要创建资源的动作先做DryRun: All预检,再实际执行 patch/create,最后记录ResourceActionRan事件与审计日志(L2738-L2789)。
常见问题与适用前提
- 列表为空:说明该资源的 Group/Kind 在 Resource Customizations 中没有定义 actions 发现脚本(
getAvailableActions对无脚本的类型返回空列表),属于正常现象而非错误。 DISABLED=true的动作:由 Lua discovery 脚本根据 live 资源当前状态判定(如已暂停的 Deployment 上pause被禁用),不代表动作不存在,条件恢复后即可执行。- 多命名空间部署:在 namespaced 安装中,
-N/--app-namespace指定的是 Application CR 所在的命名空间,与资源自身的--namespace是两个维度,勿混淆。 - 本文所有行为均以当前仓库源码为准:CLI 入口位于 cmd/argocd/commands/app_actions.go,服务端位于 server/application/application.go,actions 自定义配置位于 resource_customizations/ 目录。
相关命令参考
- argocd app actions(命令组)
- argocd app actions run(执行动作)
- argocd admin settings resource-overrides list-actions(管理员侧查看资源动作定义)
【免费下载链接】argo-cdDeclarative Continuous Deployment for Kubernetes项目地址: https://gitcode.com/GitHub_Trending/ar/argo-cd
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考