Skip to content

Commit a0df5dc

Browse files
committed
Fix textAnalyze analyzer to auto-match exec collector nested paths
- Auto-detect exec output files (*-stdout.txt, *-stderr.txt, *-errors.json) - Convert simple filenames to wildcard patterns automatically - Preserve existing wildcard patterns - Fixes 'No matching file' errors for exec + textAnalyze workflows
1 parent 79d74d3 commit a0df5dc

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-1
lines changed

pkg/analyze/text_analyze.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,23 @@ func analyzeTextAnalyze(
3737
analyzer *troubleshootv1beta2.TextAnalyze, getCollectedFileContents getChildCollectedFileContents, title string,
3838
) ([]*AnalyzeResult, error) {
3939
fullPath := filepath.Join(analyzer.CollectorName, analyzer.FileName)
40+
41+
// Auto-handle exec collector output files which are nested deeper than expected
42+
// Exec collectors store files in: {collectorName}/{namespace}/{podName}/{fileName}
43+
// But textAnalyze expects: {collectorName}/{fileName}
44+
// If the fileName looks like exec output and doesn't already have wildcards, make it work automatically
45+
if isLikelyExecOutput(analyzer.FileName) && !containsWildcards(analyzer.FileName) && !containsWildcards(fullPath) {
46+
fullPath = filepath.Join(analyzer.CollectorName, "*", "*", analyzer.FileName)
47+
}
48+
4049
excludeFiles := []string{}
4150
for _, excludeFile := range analyzer.ExcludeFiles {
42-
excludeFiles = append(excludeFiles, filepath.Join(analyzer.CollectorName, excludeFile))
51+
excludePath := filepath.Join(analyzer.CollectorName, excludeFile)
52+
// Apply same logic to exclude files
53+
if isLikelyExecOutput(excludeFile) && !containsWildcards(excludeFile) && !containsWildcards(excludePath) {
54+
excludePath = filepath.Join(analyzer.CollectorName, "*", "*", excludeFile)
55+
}
56+
excludeFiles = append(excludeFiles, excludePath)
4357
}
4458

4559
collected, err := getCollectedFileContents(fullPath, excludeFiles)
@@ -108,6 +122,20 @@ func analyzeTextAnalyze(
108122
}, nil
109123
}
110124

125+
// isLikelyExecOutput checks if a filename looks like exec collector output
126+
func isLikelyExecOutput(fileName string) bool {
127+
return strings.HasSuffix(fileName, "-stdout.txt") ||
128+
strings.HasSuffix(fileName, "-stderr.txt") ||
129+
strings.HasSuffix(fileName, "-errors.json")
130+
}
131+
132+
// containsWildcards checks if a path contains glob wildcards
133+
func containsWildcards(path string) bool {
134+
return strings.Contains(path, "*") ||
135+
strings.Contains(path, "?") ||
136+
strings.Contains(path, "[")
137+
}
138+
111139
func analyzeRegexPattern(pattern string, collected []byte, outcomes []*troubleshootv1beta2.Outcome, checkName string) (*AnalyzeResult, error) {
112140
re, err := regexp.Compile(pattern)
113141
if err != nil {

pkg/analyze/text_analyze_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,106 @@ func Test_textAnalyze(t *testing.T) {
776776
"text-collector-1/cfile-2.txt": []byte("Yes it all succeeded"),
777777
},
778778
},
779+
{
780+
name: "exec collector auto-path matching for stdout",
781+
analyzer: troubleshootv1beta2.TextAnalyze{
782+
Outcomes: []*troubleshootv1beta2.Outcome{
783+
{
784+
Pass: &troubleshootv1beta2.SingleOutcome{
785+
Message: "Command output found",
786+
},
787+
},
788+
{
789+
Fail: &troubleshootv1beta2.SingleOutcome{
790+
Message: "Command output not found",
791+
},
792+
},
793+
},
794+
CollectorName: "netbox-branch-check",
795+
FileName: "netbox-branch-check-stdout.txt", // Simple filename, but file is nested deeper
796+
RegexPattern: "success",
797+
},
798+
expectResult: []AnalyzeResult{
799+
{
800+
IsPass: true,
801+
IsWarn: false,
802+
IsFail: false,
803+
Title: "netbox-branch-check",
804+
Message: "Command output found",
805+
IconKey: "kubernetes_text_analyze",
806+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
807+
},
808+
},
809+
files: map[string][]byte{
810+
// File is stored in exec-style nested path: {collector}/{namespace}/{pod}/{collector}-stdout.txt
811+
"netbox-branch-check/netbox-enterprise/netbox-enterprise-858bcb8d4-cdgk7/netbox-branch-check-stdout.txt": []byte("operation success completed"),
812+
},
813+
},
814+
{
815+
name: "exec collector auto-path matching for stderr",
816+
analyzer: troubleshootv1beta2.TextAnalyze{
817+
Outcomes: []*troubleshootv1beta2.Outcome{
818+
{
819+
Pass: &troubleshootv1beta2.SingleOutcome{
820+
Message: "No errors in stderr",
821+
When: "false",
822+
},
823+
},
824+
{
825+
Fail: &troubleshootv1beta2.SingleOutcome{
826+
Message: "Error found in stderr",
827+
When: "true",
828+
},
829+
},
830+
},
831+
CollectorName: "my-exec-collector",
832+
FileName: "my-exec-collector-stderr.txt",
833+
RegexPattern: "error",
834+
},
835+
expectResult: []AnalyzeResult{
836+
{
837+
IsPass: false,
838+
IsWarn: false,
839+
IsFail: true,
840+
Title: "my-exec-collector",
841+
Message: "Error found in stderr",
842+
IconKey: "kubernetes_text_analyze",
843+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
844+
},
845+
},
846+
files: map[string][]byte{
847+
"my-exec-collector/default/my-pod-12345/my-exec-collector-stderr.txt": []byte("connection error occurred"),
848+
},
849+
},
850+
{
851+
name: "exec collector no auto-match when wildcards already present",
852+
analyzer: troubleshootv1beta2.TextAnalyze{
853+
Outcomes: []*troubleshootv1beta2.Outcome{
854+
{
855+
Pass: &troubleshootv1beta2.SingleOutcome{
856+
Message: "Found with existing wildcard",
857+
},
858+
},
859+
},
860+
CollectorName: "test-collector",
861+
FileName: "*/test-collector-stdout.txt", // Already has wildcard, should not be modified
862+
RegexPattern: "output",
863+
},
864+
expectResult: []AnalyzeResult{
865+
{
866+
IsPass: true,
867+
IsWarn: false,
868+
IsFail: false,
869+
Title: "test-collector",
870+
Message: "Found with existing wildcard",
871+
IconKey: "kubernetes_text_analyze",
872+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
873+
},
874+
},
875+
files: map[string][]byte{
876+
"test-collector/something/test-collector-stdout.txt": []byte("some output here"),
877+
},
878+
},
779879
}
780880

781881
for _, test := range tests {

0 commit comments

Comments
 (0)