Skip to content

Commit a5954c4

Browse files
authored
fix: autodiscovery crashes with mixed resource types (#637)
* fix: autodiscovery now uses glob patterns instead of explicit paths Fixes autodiscovery crash when projects contain both HelmChart manifests and Support Bundle specs (or other mixed resource types). ## Problem Autodiscovery was storing explicit file paths in the config instead of glob patterns. This caused the extraction phase to use strict validation (explicit path mode) instead of lenient filtering (glob pattern mode). When mixed resource types existed (e.g., HelmChart + Support Bundle), the strict validation would fail: - DiscoverSupportBundlesFromManifests() processes all paths in config.Manifests - It tries to validate helmchart.yaml as a SupportBundle - Error: "file does not contain kind: SupportBundle" ## Root Cause The extraction layer (`extractAllPathsAndMetadata`) was designed for glob patterns, not explicit paths. Non-autodiscovery configs use patterns like "./**/*.yaml" which trigger lenient filtering. Autodiscovery was storing explicit paths like "./manifests/helmchart.yaml" which trigger strict validation. ## Solution Make autodiscovery store glob patterns (`./**`) instead of explicit file paths. This: - ✅ Eliminates code path duplication (both use same extraction logic) - ✅ Fixes the bug (glob patterns use lenient filtering) - ✅ Maintains discovery counts for user display - ✅ Follows the design pattern of the system Discovery still happens twice (once for counting/display, once for extraction), but this is conceptually correct and has negligible performance impact. ## Changes - Autodiscovery block now stores patterns, not paths - Added comprehensive test with all resource types (Chart, Preflight, Support Bundle, HelmChart manifest) - Test reproduces the bug before the fix, passes after ## Testing - Added TestLint_AutodiscoveryWithMixedManifests - All existing lint tests still pass - All pkg/lint2 tests still pass * test: add comprehensive autodiscovery test coverage Adds 5 additional tests to thoroughly cover autodiscovery scenarios. ## Phase 1: Critical Edge Cases - TestLint_AutodiscoveryEmptyProject: Validates graceful handling of empty directories - TestLint_AutodiscoveryChartWithoutHelmChart: Validates error when chart lacks HelmChart manifest - TestLint_AutodiscoveryMultipleCharts: Tests scaling with multiple charts ## Phase 2: Robustness - TestLint_AutodiscoveryHiddenDirectories: Confirms .git, .github, etc. are ignored - TestLint_AutodiscoveryBothYamlExtensions: Tests both .yaml and .yml file discovery All tests pass, bringing total autodiscovery test coverage to 6 tests: 1. Mixed manifests (bug reproduction + fix validation) 2. Empty project 3. Chart without HelmChart 4. Multiple charts 5. Hidden directories 6. Both YAML extensions This provides comprehensive coverage of autodiscovery functionality. * refactor: clean up redundant comments in autodiscovery code Remove redundant 'Use recursive wildcard pattern' comments since the parent comment (lines 177-178) already explains the glob pattern approach. Add more meaningful comment explaining why Support Bundles and HelmChart manifests are combined into config.Manifests.
1 parent 9270622 commit a5954c4

File tree

2 files changed

+663
-13
lines changed

2 files changed

+663
-13
lines changed

cli/cmd/lint.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,38 +150,44 @@ func (r *runners) runLint(cmd *cobra.Command, args []string) error {
150150
fmt.Fprintf(r.w, "No .replicated config found. Auto-discovering lintable resources in current directory...\n\n")
151151
r.w.Flush()
152152

153-
// Auto-discover Helm charts
153+
// Auto-discover Helm charts (for counting and display)
154154
chartPaths, err := lint2.DiscoverChartPaths(filepath.Join(".", "**"))
155155
if err != nil {
156156
return errors.Wrap(err, "failed to discover helm charts")
157157
}
158-
for _, chartPath := range chartPaths {
159-
config.Charts = append(config.Charts, tools.ChartConfig{Path: chartPath})
160-
}
161158

162-
// Auto-discover Preflight specs
159+
// Auto-discover Preflight specs (for counting and display)
163160
preflightPaths, err := lint2.DiscoverPreflightPaths(filepath.Join(".", "**"))
164161
if err != nil {
165162
return errors.Wrap(err, "failed to discover preflight specs")
166163
}
167-
for _, preflightPath := range preflightPaths {
168-
config.Preflights = append(config.Preflights, tools.PreflightConfig{Path: preflightPath})
169-
}
170164

171-
// Auto-discover Support Bundle specs
165+
// Auto-discover Support Bundle specs (for counting and display)
172166
sbPaths, err := lint2.DiscoverSupportBundlePaths(filepath.Join(".", "**"))
173167
if err != nil {
174168
return errors.Wrap(err, "failed to discover support bundle specs")
175169
}
176-
// Convert to manifests glob patterns for compatibility
177-
config.Manifests = append(config.Manifests, sbPaths...)
178170

179-
// Auto-discover HelmChart manifests (needed for chart validation)
171+
// Auto-discover HelmChart manifests (for counting and display)
180172
helmChartPaths, err := lint2.DiscoverHelmChartPaths(filepath.Join(".", "**"))
181173
if err != nil {
182174
return errors.Wrap(err, "failed to discover HelmChart manifests")
183175
}
184-
config.Manifests = append(config.Manifests, helmChartPaths...)
176+
177+
// Store glob patterns (not explicit paths) for extraction phase
178+
// This matches non-autodiscovery behavior and uses lenient filtering
179+
if len(chartPaths) > 0 {
180+
config.Charts = []tools.ChartConfig{{Path: "./**"}}
181+
}
182+
if len(preflightPaths) > 0 {
183+
config.Preflights = []tools.PreflightConfig{
184+
{Path: "./**"},
185+
}
186+
}
187+
// Both Support Bundles and HelmChart manifests go into config.Manifests
188+
if len(sbPaths) > 0 || len(helmChartPaths) > 0 {
189+
config.Manifests = []string{"./**"}
190+
}
185191

186192
// Print what was discovered
187193
fmt.Fprintf(r.w, "Discovered resources:\n")

0 commit comments

Comments
 (0)