Skip to content

Commit

Permalink
feat: adds new yamlCompare and jsonCompare analyzers (#598)
Browse files Browse the repository at this point in the history
* feat: adds new yamlCompare analyzer

* feat: adds new jsonCompare analyzer

* outcome when for yamlCompare and jsonCompare
  • Loading branch information
Craig O'Donnell authored Jun 17, 2022
1 parent 1de5a79 commit 354a996
Show file tree
Hide file tree
Showing 8 changed files with 1,415 additions and 3 deletions.
30 changes: 30 additions & 0 deletions pkg/analyze/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,36 @@ func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileCont
}
return results, nil
}
if analyzer.YamlCompare != nil {
isExcluded, err := isExcluded(analyzer.YamlCompare.Exclude)
if err != nil {
return nil, err
}
if isExcluded {
return nil, nil
}
result, err := analyzeYamlCompare(analyzer.YamlCompare, getFile)
if err != nil {
return nil, err
}
result.Strict = analyzer.YamlCompare.Strict.BoolOrDefaultFalse()
return []*AnalyzeResult{result}, nil
}
if analyzer.JsonCompare != nil {
isExcluded, err := isExcluded(analyzer.JsonCompare.Exclude)
if err != nil {
return nil, err
}
if isExcluded {
return nil, nil
}
result, err := analyzeJsonCompare(analyzer.JsonCompare, getFile)
if err != nil {
return nil, err
}
result.Strict = analyzer.JsonCompare.Strict.BoolOrDefaultFalse()
return []*AnalyzeResult{result}, nil
}
if analyzer.Postgres != nil {
isExcluded, err := isExcluded(analyzer.Postgres.Exclude)
if err != nil {
Expand Down
112 changes: 112 additions & 0 deletions pkg/analyze/json_compare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package analyzer

import (
"encoding/json"
"path/filepath"
"reflect"
"strconv"

"github.com/pkg/errors"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
iutils "github.com/replicatedhq/troubleshoot/pkg/interfaceutils"
)

func analyzeJsonCompare(analyzer *troubleshootv1beta2.JsonCompare, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
fullPath := filepath.Join(analyzer.CollectorName, analyzer.FileName)
collected, err := getCollectedFileContents(fullPath)
if err != nil {
return nil, errors.Wrapf(err, "failed to read collected file name: %s", fullPath)
}

var actual interface{}
err = json.Unmarshal(collected, &actual)
if err != nil {
return nil, errors.Wrap(err, "failed to parse collected data as json")
}

if analyzer.Path != "" {
actual, err = iutils.GetAtPath(actual, analyzer.Path)
if err != nil {
return nil, errors.Wrapf(err, "failed to get object at path: %s", analyzer.Path)
}
}

var expected interface{}
err = json.Unmarshal([]byte(analyzer.Value), &expected)
if err != nil {
return nil, errors.Wrap(err, "failed to parse expected value as json")
}

title := analyzer.CheckName
if title == "" {
title = analyzer.CollectorName
}

result := &AnalyzeResult{
Title: title,
IconKey: "kubernetes_text_analyze",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
}

equal := reflect.DeepEqual(actual, expected)

for _, outcome := range analyzer.Outcomes {
if outcome.Fail != nil {
when := false
if outcome.Fail.When != "" {
when, err = strconv.ParseBool(outcome.Fail.When)
if err != nil {
return nil, errors.Wrapf(err, "failed to process when statement: %s", outcome.Fail.When)
}
}

if when == equal {
result.IsFail = true
result.Message = outcome.Fail.Message
result.URI = outcome.Fail.URI

return result, nil
}
} else if outcome.Warn != nil {
when := false
if outcome.Warn.When != "" {
when, err = strconv.ParseBool(outcome.Warn.When)
if err != nil {
return nil, errors.Wrapf(err, "failed to process when statement: %s", outcome.Warn.When)
}
}

if when == equal {
result.IsWarn = true
result.Message = outcome.Warn.Message
result.URI = outcome.Warn.URI

return result, nil
}
} else if outcome.Pass != nil {
when := true // default to passing when values are equal
if outcome.Pass.When != "" {
when, err = strconv.ParseBool(outcome.Pass.When)
if err != nil {
return nil, errors.Wrapf(err, "failed to process when statement: %s", outcome.Pass.When)
}
}

if when == equal {
result.IsPass = true
result.Message = outcome.Pass.Message
result.URI = outcome.Pass.URI

return result, nil
}
}
}

return &AnalyzeResult{
Title: title,
IconKey: "kubernetes_text_analyze",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
IsFail: true,
Message: "Invalid analyzer",
}, nil
}
Loading

0 comments on commit 354a996

Please sign in to comment.