<p><strong>Background:</strong> There are Cluster A and Cluster B in Azure AKS. Create a pod called <strong>Agent</strong> running linux container in cluster A in namespace <strong>test</strong> (which is <strong>non-default</strong> namespace). In the linux container, pwsh and kubectl are installed.</p>
<p><strong>Operation:</strong> Get into the pod/Agent in cluster A (kubectl exec -it pod/agent -- bash), and get-credential of Cluster B, configfile will be setup with cluster name and user name, but <strong>NO namespace</strong>.<br />
When connect to cluster B from pod/Agent, then execute <code>kubectl get pods</code>, the resource within namespace <strong>test</strong> is returned instead of the resources within namespace <strong>default</strong>.<br />
Since, there is no namespace called <strong>test</strong> in cluster B, so no resource is returned.</p>
<p>So I wonder where the namespace <strong>test</strong> is defined/setup in the pod/Agent as the default namespace.</p>
<p>Spent some time try to dive in kubectl code in github, without luck..</p>
<p>I also tried to use alias, but it only works for bash/sh, not for pwsh, since I don't want to change command name kubectl, if I do alias kubectl='kubectl -n default', pwsh would stuck into a loop.</p>
<p>Any answer is appreciated.</p>

<p>From <a href="https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/#directly-accessing-the-rest-api-1" rel="nofollow noreferrer">docs</a>:</p>
<blockquote>
<p>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.</p>
</blockquote>
<p>Simple test from a pod:</p>
<pre><code>root@ubuntu:/# strace -eopenat kubectl get pod 2&gt;&amp;1 | grep namespace
openat(AT_FDCWD, &quot;/var/run/secrets/kubernetes.io/serviceaccount/namespace&quot;, O_RDONLY|O_CLOEXEC) = 6
Error from server (Forbidden): pods is forbidden: User &quot;system:serviceaccount:default:default&quot; cannot list resource &quot;pods&quot; in API group &quot;&quot; in the namespace &quot;default&quot;
</code></pre>
<p>Directory <code>/run/secrets/kubernetes.io/serviceaccount</code> is by default always mounted to pod and contains serviceaccount token to access Kube API.</p>
