Kustomize Input Type
The Kustomize input type allows you to use Kustomize to manage and customize Kubernetes manifests within Kapitan. This input type is particularly useful when you need to:
- Apply patches to existing Kubernetes manifests
- Set namespaces for resources
- Manage multiple environments with overlays
- Customize resources without modifying the original manifests
Configuration
The Kustomize input type supports the following configuration options:
kapitan:
compile:
- output_path: manifests
input_type: kustomize
input_paths:
- path/to/kustomize/overlay
namespace: my-namespace # Optional: Set namespace for all resources
patches: # Optional: Apply patches to resources
patch-name:
target:
kind: Deployment
name: my-deployment
namespace: my-namespace
patch:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
namespace: my-namespace
spec:
template:
spec:
containers:
- name: my-container
image: nginx:1.19
Configuration Options
Option | Type | Description |
---|---|---|
output_path |
string | Path where compiled manifests will be written |
input_type |
string | Must be set to kustomize |
input_paths |
list | List of paths to Kustomize overlays |
namespace |
string | Optional: Namespace to set for all resources |
patches |
object | Optional: Dictionary of patches to apply |
Examples
Basic Usage
Here's a simple example that uses Kustomize to manage a deployment:
# inventory/targets/my-app.yml
classes:
- common
parameters:
target_name: my-app
kapitan:
compile:
- output_path: manifests
input_type: kustomize
input_paths:
- kubernetes/base
namespace: my-app
With the following directory structure:
kubernetes/
base/
kustomization.yaml
deployment.yaml
# kubernetes/base/kustomization.yaml
resources:
- deployment.yaml
# kubernetes/base/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
template:
spec:
containers:
- name: my-app
image: nginx:latest
Using Patches
Here's an example that uses patches to customize a deployment:
# inventory/targets/my-app.yml
classes:
- common
parameters:
target_name: my-app
kapitan:
compile:
- output_path: manifests
input_type: kustomize
input_paths:
- kubernetes/base
namespace: my-app
patches:
update-image:
target:
kind: Deployment
name: my-app
namespace: my-app
patch:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
namespace: my-app
spec:
template:
spec:
containers:
- name: my-app
image: nginx:1.19
Multiple Environments
You can use Kustomize overlays to manage different environments:
# inventory/targets/my-app-prod.yml
classes:
- common
parameters:
target_name: my-app-prod
kapitan:
compile:
- output_path: manifests
input_type: kustomize
input_paths:
- kubernetes/overlays/prod
namespace: my-app-prod
With the following directory structure:
kubernetes/
base/
kustomization.yaml
deployment.yaml
overlays/
prod/
kustomization.yaml
patches/
replicas.yaml
# kubernetes/overlays/prod/kustomization.yaml
resources:
- ../../base
patches:
- path: patches/replicas.yaml
# kubernetes/overlays/prod/patches/replicas.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
Best Practices
-
Use Base and Overlays: Organize your Kustomize resources using base and overlays for better maintainability.
-
Keep Patches Small: Create small, focused patches that modify specific aspects of resources.
-
Use Namespaces: Always specify namespaces in your patches to avoid conflicts.
-
Version Control: Keep your Kustomize resources in version control for better tracking and collaboration.
-
Documentation: Document your Kustomize overlays and patches for better maintainability.
Troubleshooting
Common Issues
-
Patch Not Applied: Ensure that the target resource exists and the patch format is correct.
-
Namespace Issues: Make sure to specify the namespace in both the target and patch.
-
Resource Not Found: Verify that the input paths are correct and the resources exist.
Debugging
To debug Kustomize issues:
- Check the generated kustomization.yaml in the temporary directory
- Verify that all paths are correct
- Ensure that patches are properly formatted
- Check that target resources exist