Kustomize技术问题:如何设置待补丁资源metadata.name及从变量赋值
1. How to target a resource by metadata.name for patching
When you need to apply a patch to a specific resource, Kustomize makes it straightforward to target by metadata.name using the target field in your kustomization.yaml. Here's exactly how to do it:
- Define your patch with a
targetblock that specifies the resource'skindandmetadata.name. This ensures the patch only applies to the exact resource you intend, avoiding accidental changes to similar resources.
Example: Patch a specific Deployment
Suppose you want to bump the replica count of a Deployment named my-web-app:
# kustomization.yaml patches: - target: kind: Deployment name: my-web-app patch: |- apiVersion: apps/v1 kind: Deployment metadata: name: my-web-app spec: replicas: 3
Using a separate patch file
If you prefer keeping patches in individual files, reference the file while still targeting by name:
# kustomization.yaml patches: - target: kind: Deployment name: my-web-app path: ./deployment-replica-patch.yaml
The patch file itself just needs to include the resource's metadata.name to match, but the target block adds an extra layer of specificity that's helpful in complex kustomizations.
2. Dynamically setting metadata.name for base resources (like Namespaces)
This is a common pain point when working with resources where the name isn't fixed upfront—think ephemeral test namespaces or environment-specific resources you can't hardcode. I've used two reliable methods to handle this:
Method 1: Command-line variable expansion (simple, direct input)
If you want to pass the name straight from your terminal, use Kustomize's built-in variable expansion (requires Kustomize v4.1.0 or later):
- In your base resource file (e.g.,
namespace.yaml), use a variable placeholder for the name:
apiVersion: v1 kind: Namespace metadata: name: $(NAMESPACE_NAME)
- In your base
kustomization.yaml, declare the variable with an optional default value:
# base/kustomization.yaml resources: - namespace.yaml # Apply common labels to all resources (including the dynamic namespace) commonLabels: environment: $(ENV) vars: - name: NAMESPACE_NAME value: default-namespace # Fallback if no value is provided - name: ENV value: dev
- Build with your desired values passed via command line (don't forget the
--enable-var-expansionflag—it's required):
kustomize build ./base --var="NAMESPACE_NAME=test-ns-456" --var="ENV=test" --enable-var-expansion
This replaces the placeholders with your input values, and still applies all Kustomize transforms like commonLabels to the final resource.
Method 2: Replacements (pull values from other resources)
If you want to source the name from another resource (like a generated ConfigMap for environment configs), use Kustomize's replacements feature:
- In your base, define a placeholder Namespace (we'll replace this name later):
# base/namespace.yaml apiVersion: v1 kind: Namespace metadata: name: placeholder-namespace
- In your overlay (or base), generate a ConfigMap with the dynamic namespace name, then define the replacement:
# overlay/kustomization.yaml bases: - ../base # Apply common labels to all resources commonLabels: app: my-app # Generate a ConfigMap with your desired namespace name configMapGenerator: - name: namespace-config literals: - namespace=prod-ns-2024 # Replace the placeholder namespace name with the value from the ConfigMap replacements: - source: kind: ConfigMap name: namespace-config apiVersion: v1 fieldpath: data.namespace targets: - select: kind: Namespace name: placeholder-namespace fieldpaths: - metadata.name
When you build the overlay, Kustomize will swap the placeholder name with the value from the ConfigMap, while retaining all your commonLabels and other transforms.
内容的提问来源于stack exchange,提问作者DevLounge

