-
Notifications
You must be signed in to change notification settings - Fork 32
fix: Preserve CRD location when patching charts #160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yxxhero
merged 1 commit into
helmfile:master
from
aditmeno:fix/preserve-crd-location-issue-2291
Nov 24, 2025
Merged
fix: Preserve CRD location when patching charts #160
yxxhero
merged 1 commit into
helmfile:master
from
aditmeno:fix/preserve-crd-location-issue-2291
Nov 24, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Fixes #2291
Problem:
When using strategicMergePatches with charts that have CRDs in
templates/crds/ (like KEDA), chartify relocates all CRDs to the
root crds/ directory. This changes how Helm manages these CRDs,
causing helm diff to show CRDs being removed even though patches
don't reference CRDs.
Root Cause:
The Patch() function unconditionally moves all CRDs to crds/ for
Helm 3/4, regardless of their original location. This breaks charts
that intentionally place CRDs in templates/crds/ for:
- Conditional rendering ({{- if .Values.crds.install }})
- Template features (.Release.Namespace, labels, annotations)
- CRD updates during helm upgrade
Helm treats these locations differently:
- templates/crds/: Regular resources, updated on upgrade, deleted on uninstall
- crds/: Install-only, immutable on upgrade, not deleted on uninstall
Solution:
Detect if CRDs originally came from templates/crds/ subdirectory
and preserve that location. This maintains the chart author's intent
and ensures helm diff behaves correctly.
Implementation:
- Added detection logic to check if any generated files are in templates/crds/
- When detected, CRDs are placed back in templates/crds/ after patching
- Otherwise, standard Helm 3/4 behavior (crds/) is preserved
- Cross-platform compatible using filepath.ToSlash() for path normalization
Testing:
Added comprehensive test suite covering:
- CRDs from templates/crds/ preserved in original location
- CRDs from root crds/ go to standard crds/ location
- Mixed resources without CRDs in templates/
- KEDA real-world scenario validation
- Cross-platform path handling
This preserves backward compatibility while fixing the issue for
affected charts.
Signed-off-by: Aditya Menon <amenon@canarytechnologies.com>
aditmeno
added a commit
to aditmeno/helmfile
that referenced
this pull request
Nov 24, 2025
Add comprehensive integration test to validate that strategicMergePatches do not cause CRDs to be incorrectly relocated or removed. ## Changes 1. **Integration Test** (test/integration/test-cases/issue-2291.sh) - Verifies CRDs in templates/crds/ are correctly templated - Validates strategicMergePatches don't trigger CRD relocation - Ensures helm diff doesn't show CRDs as being removed - Confirms patches are applied only to intended resources 2. **Test Chart** (test/integration/test-cases/issue-2291/input/) - Minimal chart with CRD in templates/crds/ directory - Uses conditional rendering with .Values.crds.install - Includes Deployment resource to be patched - Strategic merge patch for DNS configuration 3. **Test Suite Integration** (test/integration/run.sh) - Added issue-2291 test to main test suite 4. **Chartify Dependency** (go.mod) - Added replace directive to use chartify PR helmfile#160 - Points to fix/preserve-crd-location-issue-2291 branch - Allows testing the fix before upstream merge ## Test Validation The test validates the complete workflow: - Step 1: Template chart and verify CRD is included - Step 2: Apply chart with strategicMergePatches - Step 3: Verify CRD was installed to cluster - Step 4: Run diff - should NOT show CRD removal (regression check) - Step 5: Verify strategic merge patch applied to Deployment ## Related - Issue: helmfile#2291 - Chartify PR: helmfile/chartify#160 The test will fail without the chartify fix, and pass with it, providing regression protection for this critical bug. Signed-off-by: Aditya Menon <amenon@canarytechnologies.com>
aditmeno
added a commit
to aditmeno/helmfile
that referenced
this pull request
Nov 24, 2025
Add comprehensive integration test to validate that strategicMergePatches do not cause CRDs to be incorrectly relocated or removed. ## Changes 1. **Integration Test** (test/integration/test-cases/issue-2291.sh) - Verifies CRDs in templates/crds/ are correctly templated - Validates strategicMergePatches don't trigger CRD relocation - Ensures helm diff doesn't show CRDs as being removed - Confirms patches are applied only to intended resources 2. **Test Chart** (test/integration/test-cases/issue-2291/input/) - Minimal chart with CRD in templates/crds/ directory - Uses conditional rendering with .Values.crds.install - Includes Deployment resource to be patched - Strategic merge patch for DNS configuration 3. **Test Suite Integration** (test/integration/run.sh) - Added issue-2291 test to main test suite 4. **Chartify Dependency** (go.mod) - Added replace directive to use chartify PR helmfile#160 - Points to fix/preserve-crd-location-issue-2291 branch - Allows testing the fix before upstream merge ## Test Validation The test validates the complete workflow: - Step 1: Template chart and verify CRD is included - Step 2: Apply chart with strategicMergePatches - Step 3: Verify CRD was installed to cluster - Step 4: Run diff - should NOT show CRD removal (regression check) - Step 5: Verify strategic merge patch applied to Deployment ## Related - Issue: helmfile#2291 - Chartify PR: helmfile/chartify#160 The test will fail without the chartify fix, and pass with it, providing regression protection for this critical bug. Signed-off-by: Aditya Menon <amenon@canarytechnologies.com>
aditmeno
added a commit
to aditmeno/helmfile
that referenced
this pull request
Nov 24, 2025
Add comprehensive integration test to validate that strategicMergePatches do not cause CRDs to be incorrectly relocated or removed. 1. **Integration Test** (test/integration/test-cases/issue-2291.sh) - Verifies CRDs in templates/crds/ are correctly templated - Validates strategicMergePatches don't trigger CRD relocation - Ensures helm diff doesn't show CRDs as being removed - Confirms patches are applied only to intended resources 2. **Test Chart** (test/integration/test-cases/issue-2291/input/) - Minimal chart with CRD in templates/crds/ directory - Uses conditional rendering with .Values.crds.install - Includes Deployment resource to be patched - Strategic merge patch for DNS configuration 3. **Test Suite Integration** (test/integration/run.sh) - Added issue-2291 test to main test suite 4. **Chartify Dependency** (go.mod) - Added replace directive to use chartify PR helmfile#160 - Points to fix/preserve-crd-location-issue-2291 branch - Allows testing the fix before upstream merge The test validates the complete workflow: - Step 1: Template chart and verify CRD is included - Step 2: Apply chart with strategicMergePatches - Step 3: Verify CRD was installed to cluster - Step 4: Run diff - should NOT show CRD removal (regression check) - Step 5: Verify strategic merge patch applied to Deployment - Issue: helmfile#2291 - Chartify PR: helmfile/chartify#160 The test will fail without the chartify fix, and pass with it, providing regression protection for this critical bug. Signed-off-by: Aditya Menon <amenon@canarytechnologies.com>
aditmeno
added a commit
to aditmeno/helmfile
that referenced
this pull request
Nov 24, 2025
Add comprehensive integration test to validate that strategicMergePatches do not cause CRDs to be incorrectly relocated or removed. 1. **Integration Test** (test/integration/test-cases/issue-2291.sh) - Verifies CRDs in templates/crds/ are correctly templated - Validates strategicMergePatches don't trigger CRD relocation - Ensures helm diff doesn't show CRDs as being removed - Confirms patches are applied only to intended resources 2. **Test Chart** (test/integration/test-cases/issue-2291/input/) - Minimal chart with CRD in templates/crds/ directory - Uses conditional rendering with .Values.crds.install - Includes Deployment resource to be patched - Strategic merge patch for DNS configuration 3. **Test Suite Integration** (test/integration/run.sh) - Added issue-2291 test to main test suite 4. **Chartify Dependency** (go.mod) - Added replace directive to use chartify PR helmfile#160 - Points to fix/preserve-crd-location-issue-2291 branch - Allows testing the fix before upstream merge The test validates the complete workflow: - Step 1: Template chart and verify CRD is included - Step 2: Apply chart with strategicMergePatches - Step 3: Verify CRD was installed to cluster - Step 4: Run diff - should NOT show CRD removal (regression check) - Step 5: Verify strategic merge patch applied to Deployment - Issue: helmfile#2291 - Chartify PR: helmfile/chartify#160 The test will fail without the chartify fix, and pass with it, providing regression protection for this critical bug. Signed-off-by: Aditya Menon <amenon@canarytechnologies.com>
aditmeno
added a commit
to aditmeno/helmfile
that referenced
this pull request
Nov 24, 2025
Add comprehensive integration test to validate that strategicMergePatches do not cause CRDs to be incorrectly relocated or removed. 1. **Integration Test** (test/integration/test-cases/issue-2291.sh) - Verifies CRDs in templates/crds/ are correctly templated - Validates strategicMergePatches don't trigger CRD relocation - Ensures helm diff doesn't show CRDs as being removed - Confirms patches are applied only to intended resources 2. **Test Chart** (test/integration/test-cases/issue-2291/input/) - Minimal chart with CRD in templates/crds/ directory - Uses conditional rendering with .Values.crds.install - Includes Deployment resource to be patched - Strategic merge patch for DNS configuration 3. **Test Suite Integration** (test/integration/run.sh) - Added issue-2291 test to main test suite 4. **Chartify Dependency** (go.mod) - Added replace directive to use chartify PR helmfile#160 - Points to fix/preserve-crd-location-issue-2291 branch - Allows testing the fix before upstream merge The test validates the complete workflow: - Step 1: Template chart and verify CRD is included - Step 2: Apply chart with strategicMergePatches - Step 3: Verify CRD was installed to cluster - Step 4: Run diff - should NOT show CRD removal (regression check) - Step 5: Verify strategic merge patch applied to Deployment - Issue: helmfile#2291 - Chartify PR: helmfile/chartify#160 The test will fail without the chartify fix, and pass with it, providing regression protection for this critical bug. Signed-off-by: Aditya Menon <amenon@canarytechnologies.com>
aditmeno
added a commit
to aditmeno/helmfile
that referenced
this pull request
Nov 24, 2025
Add comprehensive integration test to validate that strategicMergePatches do not cause CRDs to be incorrectly relocated or removed. 1. **Integration Test** (test/integration/test-cases/issue-2291.sh) - Verifies CRDs in templates/crds/ are correctly templated - Validates strategicMergePatches don't trigger CRD relocation - Ensures helm diff doesn't show CRDs as being removed - Confirms patches are applied only to intended resources 2. **Test Chart** (test/integration/test-cases/issue-2291/input/) - Minimal chart with CRD in templates/crds/ directory - Uses conditional rendering with .Values.crds.install - Includes Deployment resource to be patched - Strategic merge patch for DNS configuration 3. **Test Suite Integration** (test/integration/run.sh) - Added issue-2291 test to main test suite 4. **Chartify Dependency** (go.mod) - Added replace directive to use chartify PR helmfile#160 - Points to fix/preserve-crd-location-issue-2291 branch - Allows testing the fix before upstream merge The test validates the complete workflow: - Step 1: Template chart and verify CRD is included - Step 2: Apply chart with strategicMergePatches - Step 3: Verify CRD was installed to cluster - Step 4: Run diff - should NOT show CRD removal (regression check) - Step 5: Verify strategic merge patch applied to Deployment - Issue: helmfile#2291 - Chartify PR: helmfile/chartify#160 The test will fail without the chartify fix, and pass with it, providing regression protection for this critical bug. Signed-off-by: Aditya Menon <amenon@canarytechnologies.com>
aditmeno
added a commit
to aditmeno/helmfile
that referenced
this pull request
Nov 24, 2025
Add comprehensive integration test to validate that strategicMergePatches do not cause CRDs to be incorrectly relocated or removed. 1. **Integration Test** (test/integration/test-cases/issue-2291.sh) - Verifies CRDs in templates/crds/ are correctly templated - Validates strategicMergePatches don't trigger CRD relocation - Ensures helm diff doesn't show CRDs as being removed - Confirms patches are applied only to intended resources 2. **Test Chart** (test/integration/test-cases/issue-2291/input/) - Minimal chart with CRD in templates/crds/ directory - Uses conditional rendering with .Values.crds.install - Includes Deployment resource to be patched - Strategic merge patch for DNS configuration 3. **Test Suite Integration** (test/integration/run.sh) - Added issue-2291 test to main test suite 4. **Chartify Dependency** (go.mod) - Added replace directive to use chartify PR helmfile#160 - Points to fix/preserve-crd-location-issue-2291 branch - Allows testing the fix before upstream merge The test validates the complete workflow: - Step 1: Template chart and verify CRD is included - Step 2: Apply chart with strategicMergePatches - Step 3: Verify CRD was installed to cluster - Step 4: Run diff - should NOT show CRD removal (regression check) - Step 5: Verify strategic merge patch applied to Deployment - Issue: helmfile#2291 - Chartify PR: helmfile/chartify#160 The test will fail without the chartify fix, and pass with it, providing regression protection for this critical bug. Signed-off-by: Aditya Menon <amenon@canarytechnologies.com>
Contributor
Author
|
@yxxhero PR is ready for review |
Contributor
Member
|
@aditmeno LGTM. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes helmfile/helmfile#2291
This PR fixes an issue where
strategicMergePatchescauses CRDs fromtemplates/crds/to be relocated to the rootcrds/directory, changing how Helm manages them and causinghelm diffto show CRDs being removed.Problem
When using strategicMergePatches with charts like KEDA that place CRDs in
templates/crds/:templates/crds/(managed as regular resources)crds/directory (Helm special handling)helm diffshows CRDs being removed, even though patches don't reference CRDsWhy This Matters
Chart authors intentionally use
templates/crds/instead of rootcrds/for:{{- if .Values.crds.install }}.Release.Namespace, labels, annotationshelm upgradeHelm's Different Treatment
templates/crds/crds/Root Cause
In
patch.go:342-364, CRDs were unconditionally relocated:Solution
Detect if CRDs originally came from
templates/crds/and preserve that location:Changes
patch.goLines 48-62: Added CRD source location detection logic
templates/crds/subdirectoryfilepath.ToSlash()for cross-platform compatibilityLines 342-368: Modified CRD placement logic
templates/crds/location when detectedcrds/location for Helm 3/4Comments: Added detailed explanations referencing issue #2291
patch_test.go(new file)Added comprehensive test coverage:
TestPatch_PreserveCRDLocation: End-to-end scenariostemplates/crds/stay intemplates/crds/crds/go tocrds/TestPatch_CRDLocationDetection: Detection logic validationtemplates/crds/filescrds/or other pathsTestPatch_KEDA_RealWorld: Real-world KEDA scenariotemplates/crds/)Testing
Test Results
All tests pass and cover:
Verification
A complete reproduction test case demonstrating the issue is available with:
Run verification:
Backward Compatibility
✅ No breaking changes
crds/→ same behavior as beforetemplates/crds/→ now correctly preservedImpact
Affected Charts
Any chart with CRDs in
templates/subdirectories:Benefits
helm diffshowing incorrect CRD deletionsExample: KEDA Chart
Before this fix:
After this fix:
Related Issues
Review Guide
Key areas for reviewers:
Detection Logic (
patch.go:48-62)templates/crds/subdirectoryPreservation Logic (
patch.go:342-368)Test Coverage (
patch_test.go)Documentation
Ready for review ✅
This fix resolves a critical issue where patches unintentionally change CRD management behavior, making strategicMergePatches safe to use with charts like KEDA.