Kustomize metadata.name for a specific kind:

10/13/2020

I'am new to Kustomize, and i would like to know:

  • How to "patch" metadata.name in a specific yaml like kind: RedisInstance.

I have tried to set a nameSuffix to a specific yaml (kind: RedisInstance), but it didn't worked.

Kustomization.yaml

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: "true"
  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

This is my patch:

7-memorystore-patch.yaml

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

memorystore.yaml

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
-- Canna
kubernetes
kustomize
yaml

1 Answer

10/13/2020

I don't think you can do it with the patchesStrategicMerge: the name field is used to match resources to which the patch should be applied, so either:

  • you specify the old name, the resource gets matched but the name is not changed
  • you specify the new name and the resource does not get matched

The good news is that it can be done with the patchesJson6902:

Kustomization.yaml

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

redisInstanceNamePatch.yaml

- op: replace
  path: /metadata/name
  value: NewName

All of the fields of the target, i.e. group, version, kind and name are mandatory

You may specify multiple operations in the same patch file

-- Mafor
Source: StackOverflow