<p>I'am new to Kustomize, and i would like to know:</p>
<ul>
<li>How to &quot;patch&quot; metadata.name in a specific yaml like kind: RedisInstance.</li>
</ul>
<p>I have tried to set a nameSuffix to a specific yaml (kind: RedisInstance), but it didn't worked.</p>
<p>Kustomization.yaml</p>
<pre><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: community-organization-uat-product

bases:
- ../../../../common/base/custom/gcp
- ../../../../common/base/custom/gcp/organization

commonAnnotations:
  cnrm.cloud.google.com/managed: &quot;true&quot;
  cnrm.cloud.google.com/project-id: community-organization-uat-product

patchesStrategicMerge:
- 1-cXXXXX-patch.yaml
- 3-mXXXXX-patch.yaml
- 4-mXXXXX-patch.yaml
- 5-cXXXXX-patch.yaml
- 6-mXXXXX-patch.yaml
- 7-memorystore-patch.yaml
</code></pre>
<p>This is my patch:</p>
<p>7-memorystore-patch.yaml</p>
<pre><code>apiVersion: redis.cnrm.cloud.google.com/v1beta1
kind: RedisInstance
metadata:
  name: memorystore-redis
spec:
  displayName: memorystore-redis-organization-community-uat
  memorySizeGb: 2
  redisVersion: REDIS_5_0
  reservedIpRange: 10.1XXXXX/29
</code></pre>
<p>memorystore.yaml</p>
<pre><code>apiVersion: redis.cnrm.cloud.google.com/v1beta1
kind: RedisInstance
metadata:
  name: memorystore-redis
spec:
  displayName: Value defined by Kustomize
  region: XXXX
  connectMode: PRIVATE_SERVICE_ACCESS
  locationId: usXXXX
  memorySizeGb: Value defined by Kustomize
  redisVersion: REDIS_5_0
  reservedIpRange: Value defined by Kustomize
  tier: BASIC
</code></pre>

<p>I don't think you can do it with the <code>patchesStrategicMerge</code>: the <code>name</code> field is used to match resources to which the patch should be applied, so either:</p>
<ul>
<li>you specify the old name, the resource gets matched but the name is not changed</li>
<li>you specify the new name and the resource does not get matched</li>
</ul>
<p>The good news is that it can be done with the <a href="https://kubectl.docs.kubernetes.io/pages/reference/kustomize.html#patchesjson6902" rel="noreferrer">patchesJson6902</a>:</p>
<p><strong>Kustomization.yaml</strong></p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

# ...

patchesJson6902:
  - target:
      group: redis.cnrm.cloud.google.com
      version: v1beta1
      kind: RedisInstance
      name: memorystore-redis
    path: redisInstanceNamePatch.yaml
</code></pre>
<p><strong>redisInstanceNamePatch.yaml</strong></p>
<pre class="lang-yaml prettyprint-override"><code>- op: replace
  path: /metadata/name
  value: NewName
</code></pre>
<blockquote>
<p>All of the fields of the <strong>target</strong>, i.e. group, version, kind and name are mandatory</p>
</blockquote>
<blockquote>
<p>You may specify multiple operations in the same patch file</p>
</blockquote>
