<p>I'm working with a Pod (Shiny Proxy) that talks to Kubernetes API to start other pods. I'm wanting to make this generic, and so don't want to hardcode the namespace (because I intend to have multiple of these, deployed probably as an OpenShift Template or similar).</p>
<p>I am using Kustomize to set the namespace on all objects. Here's what my kustomization.yaml looks like for my overlay:</p>
<pre class="lang-yaml prettyprint-override"><code>bases:
- ../../base

namespace: shiny
commonAnnotations:
  technical_contact: A Local Developer &lt;somedev@example.invalid&gt;
</code></pre>
<p>Running Shiny Proxy and having it start the pods I need it to (I have service accounts and RBAC already sorted) works, so long as as in the configuration for Shiny Proxy I specify (hard-code) the namespace that the new pods should be generated in. The default namespace that Shiny Proxy will use is (unfortunately) 'default', which is inappropriate for my needs.</p>
<p>Currently for the configuration I'm using a ConfigMap (perhaps I should move to a Kustomize ConfigMapGenerator)</p>
<p>The ConfigMap in question is currently like the following:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: shiny-proxy
data:
  application_yml: |
      ...
      container-backend: kubernetes
      kubernetes:
        url: https://kubernetes.default.svc.cluster.local:443
        namespace: shiny
      ...
</code></pre>
<p>The above works, but 'shiny' is hardcoded; I would like to be able to do something like the following:</p>
<pre><code>        namespace: { .metadata.namespace }
</code></pre>
<p>But this doesn't appear to work in a ConfigMap, and I don't see anything in the documentation that would lead to believe that it would, or that a similar thing appears possible within the ConfigMap machinery.</p>
<p>Looking over the Kustomize documentation doesn't fill me with clarity either, particularly as the configuration file is essentially plain-text (and not a YAML document as far as the ConfigMap is concerned). I've seen some use of Vars, but <a href="https://github.com/kubernetes-sigs/kustomize/issues/741" rel="nofollow noreferrer">https://github.com/kubernetes-sigs/kustomize/issues/741</a> leads to believe that's a non-starter.</p>
<p>Is there a nice declarative way of handling this? Or should I be looking to have the templating smarts happen within the container, which seems kinda wrong to me, but I am still new to Kubernetes (and OpenShift)</p>
<p>I'm using CodeReady Containers 1.24 (OpenShift 4.7.2) which is essentially Kubernetes 1.20 (IIRC). I'm preferring to keep this fairly well aligned with Kubernetes without getting too OpenShift specific, but this is still early days.</p>
<p>Thanks,
Cameron</p>

<p>If you don't want to hard-code a specific data in your manifest file, you can consider using <a href="https://kubectl.docs.kubernetes.io/guides/extending_kustomize/" rel="nofollow noreferrer">Kustomize plugins</a>. In this case, the <a href="https://github.com/kubernetes-sigs/kustomize/blob/master/plugin/someteam.example.com/v1/sedtransformer/SedTransformer" rel="nofollow noreferrer">sedtransformer plugin</a> may be useful. This is an  example plugin, maintained and tested by the kustomize maintainers, but not built-in to kustomize.<br />
As you can see in the <a href="https://kubectl.docs.kubernetes.io/guides/extending_kustomize/" rel="nofollow noreferrer">Kustomize plugins guide</a>:</p>
<blockquote>
<p>Kustomize offers a plugin framework allowing people to write their own resource generators and transformers.</p>
</blockquote>
<p>For more information on creating and using Kustomize plugins, see <a href="https://kubectl.docs.kubernetes.io/guides/extending_kustomize/" rel="nofollow noreferrer">Extending Kustomize</a>.</p>
<hr />
<p>I will create an example to illustrate how you can use the <code>sedtransformer</code> plugin in your case.</p>
<p>Suppose I have a <code>shiny-proxy</code> <code>ConfigMap</code>:<br />
<strong>NOTE:</strong> I don't specify a namespace, I use <code>namespace: NAMESPACE</code> instead.</p>
<pre><code>$ cat cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: shiny-proxy
data:
  application_yml: |
      container-backend: kubernetes
      kubernetes:
        url: https://kubernetes.default.svc.cluster.local:443
        namespace: NAMESPACE
      something_else:
        something: something
</code></pre>
<p>To use the <code>sedtransformer</code> plugin, we first need to create the plugin’s configuration file which contains a YAML configuration object:<br />
<strong>NOTE:</strong> In <code>argsOneLiner:</code> I specify that <code>NAMESPACE</code> should be replaced with <code>shiny</code>.</p>
<pre><code>$ cat sedTransformer.yaml
apiVersion: someteam.example.com/v1
kind: SedTransformer
metadata:
  name: sedtransformer
argsOneLiner: s/NAMESPACE/shiny/g
</code></pre>
<p>Next, we need to put the <a href="https://github.com/kubernetes-sigs/kustomize/blob/master/plugin/someteam.example.com/v1/sedtransformer/SedTransformer" rel="nofollow noreferrer">SedTransformer</a> Bash script in the right place.</p>
<blockquote>
<p>When loading, kustomize will first look for an executable file called
<code>$XDG_CONFIG_HOME/kustomize/plugin/${apiVersion}/LOWERCASE(${kind})/${kind}</code></p>
</blockquote>
<p>I create the necessary directories and download the <code>SedTransformer</code> script from the Github:<br />
<strong>NOTE:</strong> The downloaded script need to be executable.</p>
<pre><code>$ mkdir -p $HOME/.config/kustomize/plugin/someteam.example.com/v1/sedtransformer
$ cd $HOME/.config/kustomize/plugin/someteam.example.com/v1/sedtransformer
$ wget https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/plugin/someteam.example.com/v1/sedtransformer/SedTransformer
$ chmod a+x SedTransformer
</code></pre>
<p>Finally, we can check if it works as expected:<br />
<strong>NOTE:</strong> To use this plugin, you need to provide the <code>--enable-alpha-plugins</code> flag.</p>
<pre><code>$ tree
.
├── cm.yaml
├── kustomization.yaml
└── sedTransformer.yaml

0 directories, 3 files

$ cat kustomization.yaml
resources:
 - cm.yaml

transformers:
  - sedTransformer.yaml
  
$ kustomize build --enable-alpha-plugins .
apiVersion: v1
data:
  application_yml: |
    container-backend: kubernetes
    kubernetes:
      url: https://kubernetes.default.svc.cluster.local:443
      namespace: shiny
    something_else:
      something: something
kind: ConfigMap
metadata:
  name: shiny-proxy
</code></pre>
<p>Using the <a href="https://github.com/kubernetes-sigs/kustomize/blob/master/plugin/someteam.example.com/v1/sedtransformer/SedTransformer" rel="nofollow noreferrer">sedtransformer plugin</a> can be especially useful if you want to replace <code>NAMESPACE</code> in a number of places.</p>
