- Published on
How to Exclude Resources from Specific Environments Using Kustomize Delete Patching
- Authors
- Name
- Alexander Arana Escobedo
Introduction
Sometimes when you're working with Kustomize, you start with a base configuration and apply patches for different environments like test or production.
Letβs say youβve written a new deployment YAML file that you want to try out in the test environment first, without it being applied in production just yet.
However, since this YAML file is part of your base, it would automatically be included in all environments, including production.
Thatβs where delete patching comes in handy π.
Project Structure Overview & Implementation Guide
Before diving into code, hereβs what the file layout looks like:
hulk-app/
βββ base/ # Shared resources for all environments
β βββ deployment.yaml # Defines the base Deployment (placeholders used)
β βββ kustomization.yaml # Base kustomization that includes deployment.yaml
βββ overlays/ # Environment-specific kustomizations
β βββ test/
β β βββ kustomization.yaml # Kustomization for test environment
β β βββ deployment-patch.yaml # Patch that provides values for replicas and image
β βββ production/
β βββ kustomization.yaml # Kustomization for production environment
β βββ delete-deployment.yaml # Delete patch to exclude deployment from production
base/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: hulk-app
spec:
replicas: none # REQUIRED: provided by the patch
selector:
matchLabels:
app: hulk
template:
metadata:
labels:
app: hulk
spec:
containers:
- name: hulk
image: none # REQUIRED: provided by the patch
ports:
- containerPort: 80
base/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: green
resources:
- deployment.yaml
overlays/test/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: green
resources:
- ../../base
patches:
- target:
kind: Deployment
name: hulk-app
path: deployment-patch.yaml
overlays/test/deployment-patch.yaml
- op: replace
path: /spec/replicas
value: 2
- op: replace
path: /spec/template/spec/containers/0/image
value: nginx:1.25
overlays/production/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: green
resources:
- ../../base
patches:
- target:
kind: Deployment
name: hulk-app
path: delete-deployment.yaml
overlays/production/delete-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: hulk-app
$patch: delete
While there are other ways to control environment-specific resources in Kustomize, delete patching offers a clean, declarative, and environment-driven way to manage what gets deployed. You maintain a shared base and only include what you need in each environment.
π‘ Tip: Validate Kustomize Locally
You can run and validate your Kustomize overlays locally using the following command:
kubectl kustomize overlays/test
or for production:
kubectl kustomize overlays/production
This command will output the final rendered YAML so you can verify that patches and deletions are applied as expectedβwithout needing to deploy anything to your cluster. Itβs a great way to catch mistakes early and ensure your Kustomize logic behaves the way you intend.
I hope this post helps you manage your Kustomize environments more effectively! If you have any questions, feel free to reach out! π
Alexander Arana.E