<p>I would like to transform a <code>ClusterRoleBinding</code> in a <code>RoleBinding</code> using <code>kustomize-v4.0.5</code>, and also set the namespace field for the <code>RoleBinding</code> and in an additional <code>Deployment</code> resource with the same value.</p>
<p>I succeed in doing that using files below:</p>
<pre><code>cat &lt;&lt;EOF &gt; kustomization.yaml 
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
patchesJson6902:
- patch: |-
    - op: replace
      path: /kind
      value: RoleBinding
    - op: add
      path: /metadata/namespace
      value: 
        &lt;NAMESPACE&gt;
  target:
    group: rbac.authorization.k8s.io
    kind: ClusterRoleBinding
    name: manager-rolebinding
    version: v1
resources:
- role_binding.yaml 
- service_account.yaml
namespace: &lt;NAMESPACE&gt;
EOF

cat &lt;&lt;EOF &gt; role_binding.yaml 
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: manager-rolebinding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: manager-role
subjects:
- kind: ServiceAccount
  name: controller-manager
  namespace: 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
EOF

cat &lt;&lt;EOF &gt; service_account.yaml 
apiVersion: v1
kind: ServiceAccount
metadata:
  name: controller-manager
  namespace: system
EOF
</code></pre>
<p>However, in above example, I have to hardcode <code>&lt;NAMESPACE&gt;</code> in several places of my <code>kustomization.yaml</code>. Is there a way to change namespace value for these field without using sed, in 'pure' kustomize and without having to change manually values in kustomization.json?</p>

Fabrice Jammes
<p>This is a community wiki answer. Feel free to expand it.</p>
<p>I have analyzed your issue and came to the following conclusions.</p>
<p>TL;DR: Unfortunately, the answer is: &quot;not possible like you want it&quot;. The current workaround you are using with <code>sed</code> is the way to go. At the end of the day, even if a bit atypical, it is a practical solution.</p>
<p>First of all, the whole point of Kustomize is to apply different configurations from files or rather directories containing files to customize for multiple environments or the likes of environments. As such, if you know which values you would like to apply, than you would only have to include them in the corresponding overlay directory and apply whichever you would like to. For example, as part of the &quot;development&quot; and &quot;production&quot; overlays <a href="https://kubectl.docs.kubernetes.io/guides/introduction/kustomize/#2-create-variants-using-overlays" rel="nofollow noreferrer">included here</a>. That means hardcoding the namespace for each overlay.</p>
<p>But there is that question: &quot;where do you get the namespace value from&quot;? And, as a consequence, how dynamic it is - if not dynamic at all, simply one of a set of values, it is just a matter of using the approach I just described.</p>
<p>Let's assume it is fully dynamic:</p>
<p>There is a command for dynamic substitution of values: <code>kustomize edit set</code> but unfortunately it only takes these parameters: <code>image</code>, <code>label</code>, <code>nameprefix</code>, <code>namespace</code>, <code>namesuffix</code>, <code>replicas</code> so we cannot use it here (See the help for that command for more information). This is also an indication that dynamic substitution for arbitrary values has not been implemented yet.</p>
<p>I have also investigated other approaches and I can think of no &quot;pure&quot; Kustomize solution.</p>
