Published on

How to Exclude Resources from Specific Environments Using Kustomize Delete Patching

Authors
  • avatar
    Name
    Alexander Arana Escobedo
    Twitter

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