<p>I am using <a href="https://kustomize.io/" rel="nofollow noreferrer">https://kustomize.io/</a> have below is my <code>kustomization.yaml</code> file,</p>
<p>I have multiple docker images and during deployment all having same <code>tag</code>. I can manually change all the <code>tag</code> value and I can run it <code>kubectl apply -k .</code> through command prompt.</p>
<p>Question is, I don't want to change this file manually, I want to send <code>tag</code> value as a command line argument within <code>kubectl apply -k .</code> command. Is there way to do that? Thanks.</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
images:
- name: foo/bar
  newTag: "36"
- name: zoo/too
  newTag: "36"</code></pre>
</div>
</div>
</p>

user584018
<p>It my opinion using <em>&quot;<strong>file</strong>&quot;</em> approach is the correct way and the best solution even while working with your test scenario.</p>
<p>By the <em>&quot;<strong>correct way</strong>&quot;</em> I mean this is how you should work with kustomize - keeping your environment specific data into separate directory.</p>
<blockquote>
<p>kustomize supports the best practice of storing one’s entire configuration in a version control system.</p>
</blockquote>
<ol>
<li>Before <code>kustomize build .</code> you can change those values using:</li>
</ol>
<pre><code>kustomize edit set image foo/bar=12.5

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
...
images:
- name: foo/bar
  newName: &quot;12.5&quot;

</code></pre>
<ol start="2">
<li>Using <code>envsubst</code> approach:</li>
</ol>
<ul>
<li><code>deployment.yaml</code> and <code>kustomization.yaml</code> in <code>base</code> directory:</li>
</ul>
<pre><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml


apiVersion: apps/v1
kind: Deployment
metadata:
  name: the-deployment
spec:
  selector:
    matchLabels:
      run: my-nginx
  replicas: 1  
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: test
        image: foo/bar:1.2
</code></pre>
<p>directory tree with test overlay:</p>
<pre><code>├── base
│   ├── deployment.yaml
│   └── kustomization.yaml
└── overlays
    └── test
        └── kustomization2.yaml

</code></pre>
<ul>
<li>Create new <code>kustomization2.yaml</code> with variables in <code>overlay/test</code> directory:</li>
</ul>
<pre><code>cd overlays/test
cat kustomization2.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
images:
- name: foo/bar
  newTag: &quot;${IMAGE_TAG}&quot;
</code></pre>
<pre><code>export IMAGE_TAG=&quot;2.2.11&quot; ; envsubst &lt; kustomization2.yaml &gt; kustomization.yaml ; kustomize build .


output:

apiVersion: apps/v1
kind: Deployment
metadata:
...
    spec:
      containers:
      - image: foo/bar:2.2.11
        name: test
</code></pre>
<p>Files in directory after <code>envsubst</code> :</p>
<pre><code>
.
├── base
│   ├── deployment.yaml
│   └── kustomization.yaml
└── overlays
    └── test
        ├── kustomization2.yaml
        └── kustomization.yaml
</code></pre>
<ol start="3">
<li>You can always pipe the result from <code>kustomize build .</code> into <code>kubectl</code> to change the image on the fly :</li>
</ol>
<pre><code>kustomize build . | kubectl set image -f -  test=nginx3:4 --local -o yaml 

Output:
apiVersion: apps/v1
kind: Deployment
metadata:
...
    spec:
      containers:
      - image: nginx3:4
        name: test
</code></pre>
<p><strong>Note</strong>:</p>
<blockquote>
<p><a href="https://kubectl.docs.kubernetes.io/faq/kustomize/eschewedfeatures/#build-time-side-effects-from-cli-args-or-env-variables" rel="nofollow noreferrer">build in solution</a></p>
<p>Build-time side effects from CLI args or env variables</p>
<p>Changing kustomize build configuration output as a result of additional &gt;arguments or flags to build, or by consulting shell environment variable &gt;values in build code, would frustrate that goal.</p>
<p>kustomize insteads offers kustomization file edit commands. Like any shell &gt;command, they can accept environment variable arguments.</p>
<p>For example, to set the tag used on an image to match an environment &gt;variable, run
<code>kustomize edit set image nginx:$MY_NGINX_VERSION</code>
as part of some encapsulating work flow executed before kustomize build</p>
</blockquote>
