<p>I have what I would consider a common use case but I am really struggling to find a solution:</p>
<p>I want to reuse a variable in <code>Kustomize</code> patches in our deployments.   Specifically, we are using commit IDs to reference image tags (Use Case A) and k8s Jobs related to the deployments (Use Case B).</p>
<p>We use a setup where for each ArgoCD app we have a <code>/base/</code> folder and <code>/overlays/[environment-name]</code>, this base is patched with a <code>kustomization.yaml</code>.</p>
<h3>Use Case A:</h3>
<p>A very straightforward usage - in <code>/overlays/[environment-name]</code> we have a <code>kustomization.yaml</code> which uses:</p>
<pre><code>images:
- name: our-aws-repo-url
  newName: our-aws-repo-url
  newTag: commit-id
</code></pre>
<p>Works like a charm since we can re-use this both for the Deployment itself as well as its related Jobs all with one commit reference.</p>
<h3>Use Case B:</h3>
<p>The problem:</p>
<p>We use N Jobs to e.g. do migrations for 0 downtime deployments where we run alembic containers that run the migration and we have a <code>waitforit</code> <code>initContainer</code> that listens for the Job to get completed i.e. when the migration was successful in order to deploy.</p>
<p>The problem is now that I need to touch 4 files in one service's overlay to patch the id everywhere (which we use to recognize the Job):</p>
<ul>
<li>deployment.yaml like so:</li>
</ul>
<pre><code>- image: groundnuty/k8s-wait-for:v1.4
  imagePullPolicy: IfNotPresent
  args: 
   - &quot;job&quot;
   - &quot;job-commit-id&quot;
</code></pre>
<ul>
<li>job.yaml itself to change re-trigger of Job for new deployment/potential migration:</li>
</ul>
<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: job-commit-id

</code></pre>
<ul>
<li>kustomization.yaml as described in Use Case A.</li>
</ul>
<p>What I think should be possible instead is to:</p>
<ol>
<li>define variable <code>commit-id</code> somehow in kustomization.yaml and</li>
<li>for Use Case A &amp; B do something like:</li>
</ol>
<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: job-${commit-id}

</code></pre>
<pre><code>- image: groundnuty/k8s-wait-for:v1.4
  imagePullPolicy: IfNotPresent
  args: 
   - &quot;job&quot;
   - &quot;job-${commit-id}&quot;
</code></pre>
<pre><code>images:
- name: our-aws-repo-url
  newName: our-aws-repo-url
  newTag: ${commit-id}
</code></pre>
<p>Goal: when developers do PRs for releases, they should only touch one reference to the commit ID to prevent typos etc. (also easier to review instead of checking commit ID in N places)</p>
<p>Caveat: I am sure there is also another way to do migrations instead of Jobs but this is generally a common thing: how to re-use a variable inside kustomize.</p>
<p>I know I can reference ENV variable in kustomize but I want to reuse a variable within the manifests.</p>

tech4242
<blockquote>
<p>but I want to reuse a variable within the manifests.</p>
</blockquote>
<p>This is not how you typically work with Kustomize. Its a good thing that things are <em>declarative</em> and <em>explicit</em> when working with Kustomize.</p>
<blockquote>
<p>when developers do PRs for releases, they should only touch one reference to the commit ID to prevent typos etc. (also easier to review instead of checking commit ID in N places)</p>
</blockquote>
<p>yes and no.</p>
<p>That there is a change in four places should not be seen as a problem, in my opinion. That there is <em>human toil</em> to update four places <strong>is the problem</strong>.</p>
<p>The solution to human toil is typically <strong>automation</strong>. By using <a href="https://github.com/mikefarah/yq" rel="nofollow noreferrer">yq</a> in an automated pipeline (e.g. Jenkins - or a shellscript) can you automate your manifest-update to take a single parameter - this can optionally be automated directly for each build after you have a git &quot;commit id&quot; available. The pipeline need to run four <code>yq</code>-commands to update the four Yaml fields. See e.g. <a href="https://mikefarah.gitbook.io/yq/operators/assign-update" rel="nofollow noreferrer">assign operation</a> and <a href="https://mikefarah.gitbook.io/yq/usage/github-action" rel="nofollow noreferrer">github action - pipeline example</a>. No other variables are needed.</p>
