ARTICLE · INTELLIGENCE

战地情报 · 详情页

来自尧图项目组的一线实战观察与深度解析

Argo CD `argocd proj remove-source-namespace` 命令详解:从 AppProject 中安全移除源命名空间

Argo CD `argocd proj remove-source-namespace` 命令详解:从 AppProject 中安全移除源命名空间 Argo CDargocd proj remove-source-namespace命令详解从 AppProject 中安全移除源命名空间【免费下载链接】argo-cdDeclarative Continuous Deployment for Kubernetes项目地址: https://gitcode.com/GitHub_Trending/ar/argo-cd导读argocd proj remove-source-namespace是 Argo CD 命令行工具中用于管理 AppProject应用项目源命名空间source namespace授权的核心命令之一。它允许运维人员从一个 AppProject 的.spec.sourceNamespaces列表中移除指定命名空间从而撤销在该命名空间中创建的 Application 可以引用此项目的权限。阅读本文后你将掌握该命令的完整语法、所有可用参数、底层源码实现原理以及 source namespace 机制在 Argo CD 多命名空间部署中的安全边界与最佳实践。命令概览它管理的是什么在 Argo CD 中AppProject 是 Application 的逻辑分组与权限边界。其中sourceNamespaces字段决定了哪些命名空间中创建的 Application 允许引用这个 AppProjectSourceNamespaces defines the namespaces application resources are allowed to be created insourceNamespaces定义了允许 Application 资源被创建在哪些命名空间中。当你在 Argo CD 控制平面命名空间默认argocd之外的其他命名空间中声明式地创建 Application 时该 Application 的.spec.project所指向的 AppProject必须在其.spec.sourceNamespaces列表中包含这个命名空间否则 Argo CD 会判定为权限违规并拒绝处理该 Application详见 app-any-namespace 文档。remove-source-namespace命令正是用来撤销这种授权的argocd proj remove-source-namespace PROJECT NAMESPACE [flags]位置参数含义PROJECT要修改的 AppProject 名称NAMESPACE要从该项目的sourceNamespaces列表中移除的 Kubernetes 命名空间使用示例最简单的用法是直接指定项目名和命名空间# Remove source NAMESPACE in PROJECT argocd proj remove-source-namespace PROJECT NAMESPACE实际场景示例——从project-one中移除namespace-oneargocd proj remove-source-namespace project-one namespace-one命令执行成功后位于namespace-one中的 Application 将不再被允许引用project-oneArgo CD 会在下一次协调reconcile时把这些 Application 视为权限违规并停止处理。命令专属选项remove-source-namespace命令本身没有额外的业务参数仅包含标准帮助选项-h, --help help for remove-source-namespace命令的所有行为通过两个位置参数控制其余行为由父命令argocd根命令的全局参数决定。从父命令继承的全局选项该命令继承argocdCLI 的全部全局参数这些参数控制着客户端如何连接、认证 Argo CD 服务器。完整列表如下--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-crt-key string Server certificate key 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)关键全局参数速查参数作用--server/--argocd-context指定 Argo CD API 服务器地址或已配置的上下文名称--auth-token使用令牌认证或设置ARGOCD_AUTH_TOKEN环境变量--core跳过 API 服务器直接与 Kubernetes 通信--insecure/--plaintext跳过证书校验 / 禁用 TLS--port-forward通过端口转发连接随机的 argocd-server 端口--config指定 CLI 配置文件路径默认~/.config/argocd/config--grpc-web/--grpc-web-root-path当服务器位于不支持 HTTP2 的代理之后时启用 gRPC-web--redis-name/--redis-haproxy-name/--repo-server-name/--controller-name/--server-name当使用 Helm Chart 安装导致组件名称 label 与默认值不同时覆盖组件名查找源码实现解析移除操作的完整流程该命令的实现位于 cmd/argocd/commands/project.go 的NewProjectRemoveSourceNamespace函数。整个执行流程可以拆解为以下步骤1. 参数校验if len(args) ! 2 { c.HelpFunc()(c, args) os.Exit(1) }命令要求必须恰好传入两个位置参数项目名 命名空间否则打印帮助信息并以退出码 1 结束。Use字段定义的规范用法为remove-source-namespace PROJECT NAMESPACE。2. 建立客户端连接并获取项目conn, projIf : headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDieWithContext(ctx) defer utilio.Close(conn) proj, err : projIf.Get(ctx, projectpkg.ProjectQuery{Name: projName}) errors.CheckError(err)CLI 通过 gRPC 客户端调用 ProjectService 的Get方法按名称读取当前 AppProject 的完整定义包括spec.sourceNamespaces列表。这与 argocd proj 命令族使用完全相同的客户端路径。3. 查找并移除目标命名空间index : -1 for i, item : range proj.Spec.SourceNamespaces { if item srcNamespace item ! * { index i break } } if index -1 { fmt.Printf(Source namespace %s does not exist in project or cannot be removed\n, srcNamespace) } else { proj.Spec.SourceNamespaces append(proj.Spec.SourceNamespaces[:index], proj.Spec.SourceNamespaces[index1:]...) _, err projIf.Update(ctx, projectpkg.ProjectUpdateRequest{Project: proj}) errors.CheckError(err) }这里有两个值得注意的实现细节通配符*无法通过此命令移除查找条件item srcNamespace item ! *意味着即使你传入*也永远不会命中匹配。这是刻意的安全设计——*通常表示允许所有命名空间移除它需要更审慎的决策且命令会输出Source namespace * does not exist in project or cannot be removed提示这与add-source-namespace命令中检测到已有*时拒绝添加的逻辑对称呼应。幂等行为如果目标命名空间根本不在列表中命令不会报错而是打印提示信息Source namespace xxx does not exist in project or cannot be removed并正常退出。这一设计使脚本化调用shell 循环批量清理更安全。原地切片删除通过append(slice[:index], slice[index1:]...)原地删除目标元素然后调用ProjectService.Update将整个更新后的项目写回 API 服务器。4. 与 add-source-namespace 的对照移除命令与添加命令 NewProjectAddSourceNamespace 构成一对完整的管理闭环操作命令行为特点添加argocd proj add-source-namespace PROJECT NAMESPACE若列表已有*或同名项则跳过并提示已允许移除argocd proj remove-source-namespace PROJECT NAMESPACE若*或不存在则提示无法移除/不存在两者的底层都复用proj.Spec.SourceNamespaces切片 ProjectUpdateRequest更新的模式属于典型的读-改-写read-modify-write流程。底层原理SourceNamespaces 如何生效字段定义SourceNamespaces定义于 pkg/apis/application/v1alpha1/types.go// SourceNamespaces defines the namespaces application resources are allowed to be created in SourceNamespaces []string json:sourceNamespaces,omitempty protobuf:bytes,12,opt,namesourceNamespaces它是一个字符串数组支持 shell 风格通配符。例如team-one-*可以匹配team-one-frontend、team-one-backend等命名空间。校验规则在 pkg/apis/application/v1alpha1/app_project_types.go 的ValidateProject中sourceNamespaces列表必须是唯一的重复条目会导致校验失败srcNamespaces : make(map[string]bool) for _, ns : range proj.Spec.SourceNamespaces { if _, ok : srcNamespaces[ns]; ok { return status.Errorf(codes.InvalidArgument, source namespace %s already added, ns) } srcNamespaces[ns] true }这保证了 CLI 命令操作的是一个语义上无冗余的列表。权限判定真正执行授权判断的是 IsAppNamespacePermittedfunc (proj AppProject) IsAppNamespacePermitted(app *Application, controllerNs string) bool { if app.Namespace || app.Namespace controllerNs { return true } return glob.MatchStringInList(proj.Spec.SourceNamespaces, app.Namespace, glob.REGEXP) }两个关键结论可以从源码直接得出控制平面命名空间永远豁免位于 Argo CD 控制平面命名空间默认argocd的 Application 无论sourceNamespaces如何配置都被允许——这是向后兼容设计参见 app-any-namespace 文档 的说明。正则/通配匹配非控制平面命名空间的 Application 必须匹配sourceNamespaces列表中的某一项支持通配符否则被拒绝处理。因此执行remove-source-namespace后被移除命名空间中已存在的 Application 会立即失去合法性Argo CD 将停止对其的协调操作。安全最佳实践与注意事项结合 app-any-namespace 文档 与 projects 文档使用该命令时应注意最小权限原则不要将用户可控的命名空间加入任何高权限 AppProject如default的sourceNamespaces同样在移除授权时应优先移除不再需要的命名空间。default项目的配置中默认包含sourceNamespaces: []空列表见 projects.md。严禁授予控制平面命名空间永远不要在sourceNamespaces中授予argocd命名空间——这会允许用户创建具有提升权限的 Application由于 RBAC 对控制平面应用的兼容性规范化foo/argocd/*形式的策略也无法单独限制它们。通配符的治理由于列表支持team-one-*这类通配符移除时需注意精确匹配语义——移除操作按字符串精确匹配不含*时一个具体的命名空间条目与通配符条目互不影响。多命名空间协同AppProject 默认创建在argocd命名空间namespace: argocd因此即使项目本身在控制平面中其sourceNamespaces依然控制着其他命名空间中的 Application 能否引用它。级联影响预判移除前应检查目标命名空间中是否有正在使用该项目的 ApplicationCLI/UI 中以namespace/name形式显示避免误删导致应用停摆。相关命令与延伸阅读argocd proj — 项目管理的命令族入口argocd proj add-source-namespace — 添加源命名空间的对应命令Projects 文档 — AppProject 的完整管理指南创建、授权、默认项目配置App in Any Namespace 文档 — sourceNamespaces 的完整语义、示例与安全边界project.yaml 示例 — AppProject 声明式配置参考在声明式管理GitOps场景下直接编辑 AppProject 的sourceNamespaces字段并提交到 Git 是更可审计的方式而argocd proj remove-source-namespace命令则适合快速应急撤销、脚本化批量清理以及交互式排障场景——两者应结合使用共同维护 Argo CD 多命名空间部署的权限边界。【免费下载链接】argo-cdDeclarative Continuous Deployment for Kubernetes项目地址: https://gitcode.com/GitHub_Trending/ar/argo-cd创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
RELATED READING

延伸阅读

更多一线实战笔记与深度复盘,助您持续精进