<p>In the following scenario I have my containers defined in ../base/.</p>

<p>In this /dev/ directory I want to start all the deployments and statefulsets in namespace dev.  </p>

<p>The rub is that I also want to run the local-path-storage CSI in the local-path-storage namespace.  kustomize will override it and create it in the "dev" namespace.</p>

<pre><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: dev
bases:
  - ../base    
resources:
  - local-path-storage.yaml
</code></pre>

<p>How can I undo the namespace override for just local-path-storage.yaml?</p>

<p>This functionality doesn't exist in Kustomize yet. There's an <a href="https://github.com/kubernetes-sigs/kustomize/issues/880" rel="nofollow noreferrer">open issue</a> addressing this, but no open PRs at the time of this writing.</p>
<p>The quickest solution here is to remove the <code>namespace</code> setting in the <code>dev/kustomize.yaml</code> and hand-set the namespace in all resources in <code>dev</code>.</p>
<p>Another option, shamelessly copied from the issue I cited earlier, is to create a transformer to get around this:</p>
<pre><code>#!/usr/bin/env /usr/bin/python3

import sys
import yaml

with open(sys.argv[1], &quot;r&quot;) as stream:
    try:
        data = yaml.safe_load(stream)
    except yaml.YAMLError as exc:
        print(&quot;Error parsing NamespaceTransformer input&quot;, file=sys.stderr)

# See kubectl api-resources --namespaced=false
denylist = [
    &quot;ComponentStatus&quot;,
    &quot;Namespace&quot;,
    &quot;Node&quot;,
    &quot;PersistentVolume&quot;,
    &quot;MutatingWebhookConfiguration&quot;,
    &quot;ValidatingWebhookConfiguration&quot;,
    &quot;CustomResourceDefinition&quot;,
    &quot;APIService&quot;,
    &quot;MeshPolicy&quot;,
    &quot;TokenReview&quot;,
    &quot;SelfSubjectAccessReview&quot;,
    &quot;SelfSubjectRulesReview&quot;,
    &quot;SubjectAccessReview&quot;,
    &quot;CertificateSigningRequest&quot;,
    &quot;ClusterIssuer&quot;,
    &quot;BGPConfiguration&quot;,
    &quot;ClusterInformation&quot;,
    &quot;FelixConfiguration&quot;,
    &quot;GlobalBGPConfig&quot;,
    &quot;GlobalFelixConfig&quot;,
    &quot;GlobalNetworkPolicy&quot;,
    &quot;GlobalNetworkSet&quot;,
    &quot;HostEndpoint&quot;,
    &quot;IPPool&quot;,
    &quot;PodSecurityPolicy&quot;,
    &quot;NodeMetrics&quot;,
    &quot;PodSecurityPolicy&quot;,
    &quot;ClusterRoleBinding&quot;,
    &quot;ClusterRole&quot;,
    &quot;ClusterRbacConfig&quot;,
    &quot;PriorityClass&quot;,
    &quot;StorageClass&quot;,
    &quot;VolumeAttachment&quot;,
]

try:
    for yaml_input in yaml.safe_load_all(sys.stdin):
        if yaml_input['kind'] not in denylist:
            if &quot;namespace&quot; not in yaml_input[&quot;metadata&quot;]:
                yaml_input[&quot;metadata&quot;][&quot;namespace&quot;] = data[&quot;namespace&quot;]
        print(&quot;---&quot;)
        print(yaml.dump(yaml_input, default_flow_style=False))
except yaml.YAMLError as exc:
    print(&quot;Error parsing YAML input\n\n%s\n\n&quot; % input, file=sys.stderr)
</code></pre>
