Skip to content

Commit

Permalink
feat: json compare host analyser (#1582)
Browse files Browse the repository at this point in the history
* feat: json compore host analyser

Signed-off-by: Evans Mungai <evans@replicated.com>

* Add missing json compare host analyser file

Signed-off-by: Evans Mungai <evans@replicated.com>

* Generate schemas

Signed-off-by: Evans Mungai <evans@replicated.com>

* Fix failing tests

Signed-off-by: Evans Mungai <evans@replicated.com>

* Ensure json compare analyser always has a title

Signed-off-by: Evans Mungai <evans@replicated.com>

---------

Signed-off-by: Evans Mungai <evans@replicated.com>
  • Loading branch information
banjoh authored Jul 24, 2024
1 parent 0020c11 commit 1444c01
Show file tree
Hide file tree
Showing 12 changed files with 454 additions and 11 deletions.
57 changes: 57 additions & 0 deletions config/crds/troubleshoot.sh_analyzers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2348,6 +2348,63 @@ spec:
required:
- outcomes
type: object
jsonCompare:
properties:
annotations:
additionalProperties:
type: string
type: object
checkName:
type: string
collectorName:
type: string
exclude:
type: BoolString
fileName:
type: string
jsonPath:
type: string
outcomes:
items:
properties:
fail:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
pass:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
warn:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
type: object
type: array
path:
type: string
strict:
type: BoolString
value:
type: string
required:
- outcomes
type: object
kernelConfigs:
properties:
annotations:
Expand Down
57 changes: 57 additions & 0 deletions config/crds/troubleshoot.sh_hostcollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,63 @@ spec:
required:
- outcomes
type: object
jsonCompare:
properties:
annotations:
additionalProperties:
type: string
type: object
checkName:
type: string
collectorName:
type: string
exclude:
type: BoolString
fileName:
type: string
jsonPath:
type: string
outcomes:
items:
properties:
fail:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
pass:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
warn:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
type: object
type: array
path:
type: string
strict:
type: BoolString
value:
type: string
required:
- outcomes
type: object
kernelConfigs:
properties:
annotations:
Expand Down
57 changes: 57 additions & 0 deletions config/crds/troubleshoot.sh_hostpreflights.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,63 @@ spec:
required:
- outcomes
type: object
jsonCompare:
properties:
annotations:
additionalProperties:
type: string
type: object
checkName:
type: string
collectorName:
type: string
exclude:
type: BoolString
fileName:
type: string
jsonPath:
type: string
outcomes:
items:
properties:
fail:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
pass:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
warn:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
type: object
type: array
path:
type: string
strict:
type: BoolString
value:
type: string
required:
- outcomes
type: object
kernelConfigs:
properties:
annotations:
Expand Down
57 changes: 57 additions & 0 deletions config/crds/troubleshoot.sh_supportbundles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19187,6 +19187,63 @@ spec:
required:
- outcomes
type: object
jsonCompare:
properties:
annotations:
additionalProperties:
type: string
type: object
checkName:
type: string
collectorName:
type: string
exclude:
type: BoolString
fileName:
type: string
jsonPath:
type: string
outcomes:
items:
properties:
fail:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
pass:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
warn:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
type: object
type: array
path:
type: string
strict:
type: BoolString
value:
type: string
required:
- outcomes
type: object
kernelConfigs:
properties:
annotations:
Expand Down
2 changes: 2 additions & 0 deletions pkg/analyze/host_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func GetHostAnalyzer(analyzer *troubleshootv1beta2.HostAnalyze) (HostAnalyzer, b
return &AnalyzeHostTextAnalyze{analyzer.TextAnalyze}, true
case analyzer.KernelConfigs != nil:
return &AnalyzeHostKernelConfigs{analyzer.KernelConfigs}, true
case analyzer.JsonCompare != nil:
return &AnalyzeHostJsonCompare{analyzer.JsonCompare}, true
default:
return nil, false
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/analyze/host_json_compare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package analyzer

import (
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
)

type AnalyzeHostJsonCompare struct {
hostAnalyzer *troubleshootv1beta2.JsonCompare
}

func (a *AnalyzeHostJsonCompare) Title() string {
return jsonCompareTitle(a.hostAnalyzer)
}

func (a *AnalyzeHostJsonCompare) IsExcluded() (bool, error) {
return isExcluded(a.hostAnalyzer.Exclude)
}

func (a *AnalyzeHostJsonCompare) Analyze(
getCollectedFileContents func(string) ([]byte, error), findFiles getChildCollectedFileContents,
) ([]*AnalyzeResult, error) {
result, err := analyzeJsonCompare(a.hostAnalyzer, getCollectedFileContents, a.Title())
if err != nil {
return nil, err
}
result.Strict = a.hostAnalyzer.Strict.BoolOrDefaultFalse()
return []*AnalyzeResult{result}, nil
}
19 changes: 13 additions & 6 deletions pkg/analyze/json_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ type AnalyzeJsonCompare struct {
}

func (a *AnalyzeJsonCompare) Title() string {
title := a.analyzer.CheckName
return jsonCompareTitle(a.analyzer)
}

func jsonCompareTitle(analyser *troubleshootv1beta2.JsonCompare) string {
title := analyser.CheckName
if title == "" {
title = analyser.CollectorName
}
if title == "" {
title = a.analyzer.CollectorName
title = "Json Compare"
}

return title
Expand All @@ -34,15 +41,15 @@ func (a *AnalyzeJsonCompare) IsExcluded() (bool, error) {
}

func (a *AnalyzeJsonCompare) Analyze(getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
result, err := a.analyzeJsonCompare(a.analyzer, getFile)
result, err := analyzeJsonCompare(a.analyzer, getFile, a.Title())
if err != nil {
return nil, err
}
result.Strict = a.analyzer.Strict.BoolOrDefaultFalse()
return []*AnalyzeResult{result}, nil
}

func (a *AnalyzeJsonCompare) analyzeJsonCompare(analyzer *troubleshootv1beta2.JsonCompare, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
func analyzeJsonCompare(analyzer *troubleshootv1beta2.JsonCompare, getCollectedFileContents func(string) ([]byte, error), title string) (*AnalyzeResult, error) {
fullPath := filepath.Join(analyzer.CollectorName, analyzer.FileName)
collected, err := getCollectedFileContents(fullPath)
if err != nil {
Expand Down Expand Up @@ -97,7 +104,7 @@ func (a *AnalyzeJsonCompare) analyzeJsonCompare(analyzer *troubleshootv1beta2.Js
}

result := &AnalyzeResult{
Title: a.Title(),
Title: title,
IconKey: "kubernetes_text_analyze",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
}
Expand Down Expand Up @@ -173,7 +180,7 @@ func (a *AnalyzeJsonCompare) analyzeJsonCompare(analyzer *troubleshootv1beta2.Js
}

return &AnalyzeResult{
Title: a.Title(),
Title: title,
IconKey: "kubernetes_text_analyze",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
IsFail: true,
Expand Down
6 changes: 1 addition & 5 deletions pkg/analyze/json_compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,11 +808,7 @@ func Test_jsonCompare(t *testing.T) {
return test.fileContents, nil
}

a := AnalyzeJsonCompare{
analyzer: &test.analyzer,
}

actual, err := a.analyzeJsonCompare(&test.analyzer, getCollectedFileContents)
actual, err := analyzeJsonCompare(&test.analyzer, getCollectedFileContents, test.analyzer.CollectorName)
if !test.isError {
req.NoError(err)
req.Equal(test.expectResult, *actual)
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/troubleshoot/v1beta2/hostanalyzer_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,5 @@ type HostAnalyze struct {
HostOS *HostOSAnalyze `json:"hostOS,omitempty" yaml:"hostOS,omitempty"`
TextAnalyze *TextAnalyze `json:"textAnalyze,omitempty" yaml:"textAnalyze,omitempty"`
KernelConfigs *KernelConfigsAnalyze `json:"kernelConfigs,omitempty" yaml:"kernelConfigs,omitempty"`
JsonCompare *JsonCompare `json:"jsonCompare,omitempty" yaml:"jsonCompare,omitempty"`
}
5 changes: 5 additions & 0 deletions pkg/apis/troubleshoot/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1444c01

Please sign in to comment.