<pre><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: ns1

resources:
  - r1a.yaml
  - r1b.yaml
  - r1c.yaml
  - r1d.yaml
  - r1e.yaml
  - r2.yaml # needs to be placed in namespace ns2
</code></pre>
<p>Let's assume above situation. The problem is objects specified in <code>r2.yaml</code> would be place in <code>ns1</code> even if <code>ns2</code> is explicitely referenced in <code>metadata.namespace</code>.</p>
<p>How do I have to deal with this? Or how can I solve this (as I assume there a multiple options)?</p>

<p>I've looked into this and I came up with one idea.</p>
<pre><code>├── base
│   ├── [nginx.yaml]  Deployment nginx ns: default
|   ├── [nginx2.yaml]  Deployment nginx ns: default
|   ├── [nginx3.yaml]  Deployment nginx ns: default
|   ├── [nginx4.yaml]  Deployment nginx ns: default
|   ├── [nginx5.yaml]  Deployment nginx ns: nginx
│   └── [kustomization.yaml]  Kustomization 
└── prod
    ├── [kustomization.yaml]  Kustomization 
    └── [patch.yaml] patching namespace
</code></pre>
<p>You need to have 2 directories, in this setup are: <code>base</code> and <code>prod</code>. In <code>base</code> directory you should use your base YAMLs and <code>kustomization.yaml</code> file. In my scenario I have 6 YAMLs: <code>nginx/1/2/3/4.yaml</code> based on <a href="https://kubernetes.io/docs/tasks/run-application/run-stateless-application-deployment/#creating-and-exploring-an-nginx-deployment" rel="nofollow noreferrer">Kubernetes Documentation</a>, and <code>nginx5.yaml</code> which looks the same but with additional <code>spec.namespace: nginx</code>.</p>
<p><strong>In <code>base</code> directory:</strong></p>
<pre><code>$ cat kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- nginx1.yaml
- nginx2.yaml
- nginx3.yaml
- nginx4.yaml
- nginx5.yaml
</code></pre>
<p>And 5 YAMLs with nginx.</p>
<p><strong>In <code>Prod</code> directory:</strong></p>
<p>You should have 2 files. <code>kustomization.yaml</code> and <code>patch.yaml</code>.</p>
<pre><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: ns1
bases:
- ../base
patchesJson6902:
- target:
    group: apps
    version: v1
    kind: Deployment
    name: nginx-deployment-5
  path: patch.yaml

$ cat patch.yaml
- op: replace
  path: /metadata/namespace
  value: nginx
</code></pre>
<p>When you will use <code>kustomize build .</code> in <code>prod</code> directory, all <code>nginx-deployment/-2/-3/-4</code> will be in the <code>namespace: ns1</code> and <code>nginx-deployment-5</code> will be in <code>namespace: nginx</code>.</p>
<pre><code>~/prod (project)$ kustomize build .
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment-5
  namespace: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.14.2
        name: nginx
        ports:
        - containerPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: ns1
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.14.2
        name: nginx
        ports:
        - containerPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment-2
  namespace: ns1
spec:
</code></pre>
<p><strong>Useful links</strong>:</p>
<ul>
<li><a href="https://kubectl.docs.kubernetes.io/guides/extending_kustomize/builtins/" rel="nofollow noreferrer">Kustomize Builtin Plugins</a></li>
<li><a href="https://kubernetes.io/docs/tasks/manage-kubernetes-objects/kustomization/#customizing" rel="nofollow noreferrer">Customizong</a></li>
<li><a href="https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/patches/" rel="nofollow noreferrer">Kustomization Patches</a></li>
</ul>
