<p>Kustomize directory structure</p>
<pre><code>├── base
│   ├── deployment.yaml
│   └── kustomization.yaml
└── overlays
    └── prod
        ├── kustomization.yaml
        ├── namespace-a
        │   ├── deployment-a1
        │   │   ├── kustomization.yaml
        │   │   └── patch.yaml
        │   ├── deployment-a2
        │   │   ├── kustomization.yaml
        │   │   └── patch.yaml
        │   ├── kustomization.yaml
        │   └── namespace.yaml
        ├── namespace-b
        │   ├── deployment-b1
        │   │   ├── kustomization.yaml
        │   │   └── patch.yaml
        │   ├── deployment-b2
        │   │   ├── kustomization.yaml
        │   │   └── patch.yaml
        │   ├── kustomization.yaml
        │   └── namespace.yaml
        └── namespace-c

</code></pre>
<p>As you can see above, I have <code>prod</code> environment with <code>namesapce-a</code> and <code>namespace-b</code> and few more.
To  create deployment for all, I can simply run the below command:</p>
<pre><code>    &gt; kustomize overlays/prod
</code></pre>
<p>Which works flawlessly, both namespaces are created along with other deployment files for all deployments.</p>
<p>To create a deployment for only namespace-a:</p>
<pre><code>    &gt; kustomize overlays/prod/namespace-a
</code></pre>
<p>That also works. :)</p>
<p>But that's not where the story ends for me at-least.</p>
<p>I would like to keep the current functionality and be able to deploy <code>deployment-a1, deployment-a2 ...</code></p>
<pre><code>    &gt; kustomize overlays/prod/namespace-a/deployment-a1
</code></pre>
<p>If I put the namespace.yaml inside <code>deployment-a1</code> folder and add it in <code>kustomization.yaml</code>
then the above command works but previous 2 fails with error because now we have 2 namespace files with same name.</p>
<p>I have 2 queries.</p>
<ol>
<li>Can this directory structure be improved?</li>
<li>How can I create namesapce with single deployment without breaking the other functionality?</li>
</ol>
<p>Full code can be seen <a href="https://github.com/deepak-gc/kustomize-namespace-issue" rel="nofollow noreferrer">here</a></p>

<p>In your particular case, in the most ideal scenario, all the required namespaces should already be created before running the <code>kustomize</code> command.
However, I know that you would like to create namespaces dynamically as needed.</p>
<p>Using a Bash script as some kind of wrapper can definitely help with this approach, but I'm not sure if you want to use this.</p>
<p>Below, I'll show you how this can work, and you can choose if it's right for you.</p>
<hr />
<p>First, I created a <code>kustomize-wrapper</code> script that requires two arguments:</p>
<ol>
<li>The name of the Namespace you want to use.</li>
<li>Path to the directory containing the <code>kustomization.yaml</code> file.</li>
</ol>
<p><strong>kustomize-wrapper.sh</strong></p>
<pre><code>$ cat kustomize-wrapper.sh
#!/bin/bash

if [ -z &quot;$1&quot; ] || [ -z &quot;$2&quot; ]; then
    echo &quot;Pass required arguments !&quot;
    echo &quot;Usage: $0 NAMESPACE KUSTOMIZE_PATH&quot;
    exit 1
else
    NAMESPACE=$1
    KUSTOMIZE_PATH=$2
fi

echo &quot;Creating namespace&quot;
sed -i &quot;s/name:.*/name: ${NAMESPACE}/&quot; namespace.yaml
kubectl apply -f namespace.yaml

echo &quot;Setting namespace: ${NAMESPACE} in the kustomization.yaml file&quot;
sed -i &quot;s/namespace:.*/namespace: ${NAMESPACE}/&quot; base/kustomization.yaml

echo &quot;Deploying resources in the ${NAMESPACE}&quot;
kustomize build ${KUSTOMIZE_PATH} | kubectl apply -f -
</code></pre>
<p>As you can see, this script creates a namespace using the <code>namespace.yaml</code> file as the template. It then sets the same namespace in the <code>base/kustomization.yaml</code> file  and finally runs the <code>kustomize</code> command with the path you provided as the second argument.</p>
<p><strong>namespace.yaml</strong></p>
<pre><code>$ cat namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
  name:
</code></pre>
<p><strong>base/kustomization.yaml</strong></p>
<pre><code>$ cat base/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace:

resources:
- deployment.yaml
</code></pre>
<p><strong>Directory structure</strong></p>
<pre><code>$ tree
.
├── base
│   ├── deployment.yaml
│   └── kustomization.yaml
├── kustomize-wrapper.sh
├── namespace.yaml
└── overlays
    └── prod
        ├── deployment-a1
        │   ├── kustomization.yaml
        │   └── patch.yaml
        ├── deployment-a2
        │   ├── kustomization.yaml
        │   └── patch.yaml
        └── kustomization.yaml
    
    
    
</code></pre>
<p>We can check if it works as expected.</p>
<p>Creating the <code>namespace-a</code> Namespace along with <code>app-deployment-a1</code> and <code>app-deployment-a2</code> Deployments:</p>
<pre><code>$ ./kustomize-wrapper.sh namespace-a overlays/prod
Creating namespace
namespace/namespace-a created
Setting namespace: namespace-a in the kustomization.yaml file
deployment.apps/app-deployment-a1 created
deployment.apps/app-deployment-a2 created
</code></pre>
<p>Creating only the <code>namespace-a</code> Namespace and <code>app-deployment-a1</code> Deployment:</p>
<pre><code>$ ./kustomize-wrapper.sh namespace-a overlays/prod/deployment-a1
Creating namespace
namespace/namespace-a created
Setting namespace: namespace-a in the kustomization.yaml file
deployment.apps/app-deployment-a1 created
</code></pre>
