<p>I would like to set the <code>name</code> field in a <code>Namespace</code> resource and also replace the <code>namespace</code> field in a <code>Deployment</code> resource with the same value, for example <code>my-namespace</code>.</p>
<p>Here is <code>kustomization.json</code>:</p>
<pre><code>namespace: &lt;NAMESPACE&gt;
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
patchesJson6902:
- patch: |-
    - op: replace
      path: /metadata/name
      value: &lt;NAMESPACE&gt;
  target:
    kind: Namespace
    name: system
    version: v1
resources:
- manager.yaml
</code></pre>
<p>and <code>manager.yaml</code>:</p>
<pre><code>apiVersion: v1
kind: Namespace
metadata:
  labels:
    control-plane: controller-manager
  name: system
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: controller-manager
  namespace: system
spec:
  selector:
    matchLabels:
      control-plane: controller-manager
  replicas: 1
  template:
    metadata:
      labels:
        control-plane: controller-manager
    spec:
      containers:
      - command:
        - /manager
        args:
        - --enable-leader-election
        image: controller:latest
        name: manager
</code></pre>
<p>I tried using <code>kustomize edit set namespace my-namespace &amp;&amp; kustomize build</code>, but it only changes the <code>namespace</code> field in the <code>Deployment</code> object.</p>
<p>Is there a way to change both field without using <code>sed</code>, in 'pure' <code>kustomize</code> and without having to change manually  value in <code>kustomization.json</code>?</p>

Fabrice Jammes
<p><strong>Is there a way to change both field without using <code>sed</code>, in 'pure' <code>kustomize</code> and without having to change manually value in <code>kustomization.json</code>?</strong></p>
<p>I managed to achieve somewhat similar with the following configuration:</p>
<pre class="lang-yaml prettyprint-override"><code>kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: my-namespace
resources:
- deployment.yaml
</code></pre>
<pre class="lang-yaml prettyprint-override"><code>depyloment.yaml
---
apiVersion: v1
kind: Namespace
metadata:
  name: nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  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
</code></pre>
<p>And here is the output of the command that you used:</p>
<pre class="lang-sh prettyprint-override"><code>➜  kustomize kustomize edit set namespace my-namespace7 &amp;&amp; kustomize build .  
</code></pre>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Namespace
metadata:
  name: my-namespace7
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: my-namespace7
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.14.2
        name: nginx
        ports:
        - containerPort: 80
</code></pre>
<p>What is happening here is that once you set the <code>namespace</code> globally in <code>kustomization.yaml</code> it will apply it to your targets which looks to me that looks an easier way to achieve what you want.</p>
<p>I cannot test your config without <code>manager_patch.yaml</code> content. If you wish to go with your way further you will have update the question with the file content.</p>
