对于 pod 内的命令 kubectl,默认命名空间是 defined/setup

Where the default namespace is defined/setup for command kubectl inside of a pod

背景:Azure AKS中有Cluster A和Cluster B。在集群 A 的命名空间 test(即 非默认 命名空间)。在linux容器中,安装了pwsh和kubectl。

操作:进入集群A中的pod/Agent(kubectl exec -it pod/agent -- bash),得到-集群 B 的凭据,配置文件将使用集群名称和用户名进行设置,但 NO 命名空间.
当从 pod/Agent 连接到集群 B 时,然后执行 kubectl get pods,返回命名空间 test 中的资源,而不是命名空间 default[ 中的资源=36=].
因为,集群 B 中没有名为 test 的命名空间,所以没有资源返回。

所以我想知道命名空间test在pod/Agent中作为默认命名空间的defined/setup在哪里。

花了一些时间尝试研究 github 中的 kubectl 代码,但运气不佳..

我也尝试过使用别名,但它只适用于 bash/sh,不适用于 pwsh,因为我不想更改命令名称 kubectl,如果我使用 alias kubectl='kubectl -n default' , pwsh 会陷入循环。

如有任何答复,我们将不胜感激。

来自docs

Finally, the default namespace to be used for namespaced API operations is placed in a file at /var/run/secrets/kubernetes.io/serviceaccount/namespace in each container.

从 pod 进行简单测试:

root@ubuntu:/# strace -eopenat kubectl get pod 2>&1 | grep namespace
openat(AT_FDCWD, "/var/run/secrets/kubernetes.io/serviceaccount/namespace", O_RDONLY|O_CLOEXEC) = 6
Error from server (Forbidden): pods is forbidden: User "system:serviceaccount:default:default" cannot list resource "pods" in API group "" in the namespace "default"

目录 /run/secrets/kubernetes.io/serviceaccount 默认情况下始终挂载到 pod 并包含用于访问 Kube 的 serviceaccount 令牌 API。