)
深入解析 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用于列出某个应用下资源上所有可用的 actionsLists 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 controllers 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 Proxys 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 Rediss 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 servers 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 servers name label differs from the default, for example when installing via the Helm chart (default argocd-server)输出格式解析默认情况下不指定-o命令通过tabwriter输出一张五列对齐表格表头为GROUP KIND NAME ACTION DISABLED这些列对应源码中定义的结构体 DisplayedActiontype 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以缩进 JSONjson.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 GroupCLI 会直接报错并提示使用--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对无脚本的类型返回空列表属于正常现象而非错误。DISABLEDtrue的动作由 Lua discovery 脚本根据 live 资源当前状态判定如已暂停的 Deployment 上pause被禁用不代表动作不存在条件恢复后即可执行。多命名空间部署在 namespaced 安装中-N/--app-namespace指定的是 Application CR 所在的命名空间与资源自身的--namespace是两个维度勿混淆。本文所有行为均以当前仓库源码为准CLI 入口位于 cmd/argocd/commands/app_actions.go服务端位于 server/application/application.goactions 自定义配置位于 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),仅供参考