mirror of
https://github.com/replicatedhq/troubleshoot.git
synced 2026-04-15 07:16:34 +00:00
Template messages in outcome block for textAnalyzer
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
package analyzer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
|
||||
@@ -169,7 +171,11 @@ func analyzeRegexGroups(pattern string, collected []byte, outcomes []*troublesho
|
||||
|
||||
if isMatch {
|
||||
result.IsFail = true
|
||||
result.Message = outcome.Fail.Message
|
||||
tplMessage, err := templateRegExGroup(outcome.Fail.Message, foundMatches)
|
||||
if err != nil {
|
||||
return result, errors.Wrap(err, "failed to template message in outcome.Fail block")
|
||||
}
|
||||
result.Message = tplMessage
|
||||
result.URI = outcome.Fail.URI
|
||||
|
||||
return result, nil
|
||||
@@ -182,7 +188,11 @@ func analyzeRegexGroups(pattern string, collected []byte, outcomes []*troublesho
|
||||
|
||||
if isMatch {
|
||||
result.IsWarn = true
|
||||
result.Message = outcome.Warn.Message
|
||||
tplMessage, err := templateRegExGroup(outcome.Warn.Message, foundMatches)
|
||||
if err != nil {
|
||||
return result, errors.Wrap(err, "failed to template message in outcome.Warn block")
|
||||
}
|
||||
result.Message = tplMessage
|
||||
result.URI = outcome.Warn.URI
|
||||
|
||||
return result, nil
|
||||
@@ -195,7 +205,11 @@ func analyzeRegexGroups(pattern string, collected []byte, outcomes []*troublesho
|
||||
|
||||
if isMatch {
|
||||
result.IsPass = true
|
||||
result.Message = outcome.Pass.Message
|
||||
tplMessage, err := templateRegExGroup(outcome.Pass.Message, foundMatches)
|
||||
if err != nil {
|
||||
return result, errors.Wrap(err, "failed to template message in outcome.Pass block")
|
||||
}
|
||||
result.Message = tplMessage
|
||||
result.URI = outcome.Pass.URI
|
||||
|
||||
return result, nil
|
||||
@@ -206,6 +220,20 @@ func analyzeRegexGroups(pattern string, collected []byte, outcomes []*troublesho
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// templateRegExGroup takes a tpl and replaces the variables using matches.
|
||||
func templateRegExGroup(tpl string, matches map[string]string) (string, error) {
|
||||
t, err := template.New("").Parse(tpl)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var msg bytes.Buffer
|
||||
err = t.Execute(&msg, matches)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return msg.String(), nil
|
||||
}
|
||||
|
||||
func compareRegex(conditional string, foundMatches map[string]string) (bool, error) {
|
||||
if conditional == "" {
|
||||
return true, nil
|
||||
|
||||
@@ -477,6 +477,41 @@ func Test_textAnalyze(t *testing.T) {
|
||||
"text-collector-1/cfile-1.txt": []byte("value: 2\nother: 10"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Outcome message is templated with regex groups",
|
||||
analyzer: troubleshootv1beta2.TextAnalyze{
|
||||
Outcomes: []*troubleshootv1beta2.Outcome{
|
||||
{
|
||||
Pass: &troubleshootv1beta2.SingleOutcome{
|
||||
When: `Error == ""`,
|
||||
Message: "No error found",
|
||||
},
|
||||
},
|
||||
{
|
||||
Fail: &troubleshootv1beta2.SingleOutcome{
|
||||
Message: "There is a prefix not found error in {{ .SubName }} in namespace {{ .Namespace }}",
|
||||
},
|
||||
},
|
||||
},
|
||||
CollectorName: "text-collector-templated-regex-message",
|
||||
FileName: "cfile-1.txt",
|
||||
RegexGroups: `"name":\s*"(?P<SubName>.*?)".*namespace":\s*"(?P<Namespace>.*?)".*error":\s*.*"(?P<Error>prefix not found.*?)"`,
|
||||
},
|
||||
expectResult: []AnalyzeResult{
|
||||
{
|
||||
IsPass: false,
|
||||
IsWarn: false,
|
||||
IsFail: true,
|
||||
Title: "text-collector-templated-regex-message",
|
||||
Message: "There is a prefix not found error in wrong-eventtypeprefix in namespace default",
|
||||
IconKey: "kubernetes_text_analyze",
|
||||
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg?w=13&h=16",
|
||||
},
|
||||
},
|
||||
files: map[string][]byte{
|
||||
"text-collector-templated-regex-message/cfile-1.txt": []byte(`{"level":"ERROR","timestamp":"2022-05-17T20:37:41Z","caller":"controller/controller.go:317","message":"Reconciler error","context":{"name":"wrong-eventtypeprefix","namespace":"default","error":"prefix not found"}}`),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
||||
Reference in New Issue
Block a user