Skip to content

Commit 1444c01

Browse files
authored
feat: json compare host analyser (#1582)
* feat: json compore host analyser Signed-off-by: Evans Mungai <[email protected]> * Add missing json compare host analyser file Signed-off-by: Evans Mungai <[email protected]> * Generate schemas Signed-off-by: Evans Mungai <[email protected]> * Fix failing tests Signed-off-by: Evans Mungai <[email protected]> * Ensure json compare analyser always has a title Signed-off-by: Evans Mungai <[email protected]> --------- Signed-off-by: Evans Mungai <[email protected]>
1 parent 0020c11 commit 1444c01

12 files changed

+454
-11
lines changed

config/crds/troubleshoot.sh_analyzers.yaml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,6 +2348,63 @@ spec:
23482348
required:
23492349
- outcomes
23502350
type: object
2351+
jsonCompare:
2352+
properties:
2353+
annotations:
2354+
additionalProperties:
2355+
type: string
2356+
type: object
2357+
checkName:
2358+
type: string
2359+
collectorName:
2360+
type: string
2361+
exclude:
2362+
type: BoolString
2363+
fileName:
2364+
type: string
2365+
jsonPath:
2366+
type: string
2367+
outcomes:
2368+
items:
2369+
properties:
2370+
fail:
2371+
properties:
2372+
message:
2373+
type: string
2374+
uri:
2375+
type: string
2376+
when:
2377+
type: string
2378+
type: object
2379+
pass:
2380+
properties:
2381+
message:
2382+
type: string
2383+
uri:
2384+
type: string
2385+
when:
2386+
type: string
2387+
type: object
2388+
warn:
2389+
properties:
2390+
message:
2391+
type: string
2392+
uri:
2393+
type: string
2394+
when:
2395+
type: string
2396+
type: object
2397+
type: object
2398+
type: array
2399+
path:
2400+
type: string
2401+
strict:
2402+
type: BoolString
2403+
value:
2404+
type: string
2405+
required:
2406+
- outcomes
2407+
type: object
23512408
kernelConfigs:
23522409
properties:
23532410
annotations:

config/crds/troubleshoot.sh_hostcollectors.yaml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,63 @@ spec:
588588
required:
589589
- outcomes
590590
type: object
591+
jsonCompare:
592+
properties:
593+
annotations:
594+
additionalProperties:
595+
type: string
596+
type: object
597+
checkName:
598+
type: string
599+
collectorName:
600+
type: string
601+
exclude:
602+
type: BoolString
603+
fileName:
604+
type: string
605+
jsonPath:
606+
type: string
607+
outcomes:
608+
items:
609+
properties:
610+
fail:
611+
properties:
612+
message:
613+
type: string
614+
uri:
615+
type: string
616+
when:
617+
type: string
618+
type: object
619+
pass:
620+
properties:
621+
message:
622+
type: string
623+
uri:
624+
type: string
625+
when:
626+
type: string
627+
type: object
628+
warn:
629+
properties:
630+
message:
631+
type: string
632+
uri:
633+
type: string
634+
when:
635+
type: string
636+
type: object
637+
type: object
638+
type: array
639+
path:
640+
type: string
641+
strict:
642+
type: BoolString
643+
value:
644+
type: string
645+
required:
646+
- outcomes
647+
type: object
591648
kernelConfigs:
592649
properties:
593650
annotations:

config/crds/troubleshoot.sh_hostpreflights.yaml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,63 @@ spec:
588588
required:
589589
- outcomes
590590
type: object
591+
jsonCompare:
592+
properties:
593+
annotations:
594+
additionalProperties:
595+
type: string
596+
type: object
597+
checkName:
598+
type: string
599+
collectorName:
600+
type: string
601+
exclude:
602+
type: BoolString
603+
fileName:
604+
type: string
605+
jsonPath:
606+
type: string
607+
outcomes:
608+
items:
609+
properties:
610+
fail:
611+
properties:
612+
message:
613+
type: string
614+
uri:
615+
type: string
616+
when:
617+
type: string
618+
type: object
619+
pass:
620+
properties:
621+
message:
622+
type: string
623+
uri:
624+
type: string
625+
when:
626+
type: string
627+
type: object
628+
warn:
629+
properties:
630+
message:
631+
type: string
632+
uri:
633+
type: string
634+
when:
635+
type: string
636+
type: object
637+
type: object
638+
type: array
639+
path:
640+
type: string
641+
strict:
642+
type: BoolString
643+
value:
644+
type: string
645+
required:
646+
- outcomes
647+
type: object
591648
kernelConfigs:
592649
properties:
593650
annotations:

config/crds/troubleshoot.sh_supportbundles.yaml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19187,6 +19187,63 @@ spec:
1918719187
required:
1918819188
- outcomes
1918919189
type: object
19190+
jsonCompare:
19191+
properties:
19192+
annotations:
19193+
additionalProperties:
19194+
type: string
19195+
type: object
19196+
checkName:
19197+
type: string
19198+
collectorName:
19199+
type: string
19200+
exclude:
19201+
type: BoolString
19202+
fileName:
19203+
type: string
19204+
jsonPath:
19205+
type: string
19206+
outcomes:
19207+
items:
19208+
properties:
19209+
fail:
19210+
properties:
19211+
message:
19212+
type: string
19213+
uri:
19214+
type: string
19215+
when:
19216+
type: string
19217+
type: object
19218+
pass:
19219+
properties:
19220+
message:
19221+
type: string
19222+
uri:
19223+
type: string
19224+
when:
19225+
type: string
19226+
type: object
19227+
warn:
19228+
properties:
19229+
message:
19230+
type: string
19231+
uri:
19232+
type: string
19233+
when:
19234+
type: string
19235+
type: object
19236+
type: object
19237+
type: array
19238+
path:
19239+
type: string
19240+
strict:
19241+
type: BoolString
19242+
value:
19243+
type: string
19244+
required:
19245+
- outcomes
19246+
type: object
1919019247
kernelConfigs:
1919119248
properties:
1919219249
annotations:

pkg/analyze/host_analyzer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ func GetHostAnalyzer(analyzer *troubleshootv1beta2.HostAnalyze) (HostAnalyzer, b
5454
return &AnalyzeHostTextAnalyze{analyzer.TextAnalyze}, true
5555
case analyzer.KernelConfigs != nil:
5656
return &AnalyzeHostKernelConfigs{analyzer.KernelConfigs}, true
57+
case analyzer.JsonCompare != nil:
58+
return &AnalyzeHostJsonCompare{analyzer.JsonCompare}, true
5759
default:
5860
return nil, false
5961
}

pkg/analyze/host_json_compare.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package analyzer
2+
3+
import (
4+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
5+
)
6+
7+
type AnalyzeHostJsonCompare struct {
8+
hostAnalyzer *troubleshootv1beta2.JsonCompare
9+
}
10+
11+
func (a *AnalyzeHostJsonCompare) Title() string {
12+
return jsonCompareTitle(a.hostAnalyzer)
13+
}
14+
15+
func (a *AnalyzeHostJsonCompare) IsExcluded() (bool, error) {
16+
return isExcluded(a.hostAnalyzer.Exclude)
17+
}
18+
19+
func (a *AnalyzeHostJsonCompare) Analyze(
20+
getCollectedFileContents func(string) ([]byte, error), findFiles getChildCollectedFileContents,
21+
) ([]*AnalyzeResult, error) {
22+
result, err := analyzeJsonCompare(a.hostAnalyzer, getCollectedFileContents, a.Title())
23+
if err != nil {
24+
return nil, err
25+
}
26+
result.Strict = a.hostAnalyzer.Strict.BoolOrDefaultFalse()
27+
return []*AnalyzeResult{result}, nil
28+
}

pkg/analyze/json_compare.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,16 @@ type AnalyzeJsonCompare struct {
2121
}
2222

2323
func (a *AnalyzeJsonCompare) Title() string {
24-
title := a.analyzer.CheckName
24+
return jsonCompareTitle(a.analyzer)
25+
}
26+
27+
func jsonCompareTitle(analyser *troubleshootv1beta2.JsonCompare) string {
28+
title := analyser.CheckName
29+
if title == "" {
30+
title = analyser.CollectorName
31+
}
2532
if title == "" {
26-
title = a.analyzer.CollectorName
33+
title = "Json Compare"
2734
}
2835

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

3643
func (a *AnalyzeJsonCompare) Analyze(getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
37-
result, err := a.analyzeJsonCompare(a.analyzer, getFile)
44+
result, err := analyzeJsonCompare(a.analyzer, getFile, a.Title())
3845
if err != nil {
3946
return nil, err
4047
}
4148
result.Strict = a.analyzer.Strict.BoolOrDefaultFalse()
4249
return []*AnalyzeResult{result}, nil
4350
}
4451

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

99106
result := &AnalyzeResult{
100-
Title: a.Title(),
107+
Title: title,
101108
IconKey: "kubernetes_text_analyze",
102109
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
103110
}
@@ -173,7 +180,7 @@ func (a *AnalyzeJsonCompare) analyzeJsonCompare(analyzer *troubleshootv1beta2.Js
173180
}
174181

175182
return &AnalyzeResult{
176-
Title: a.Title(),
183+
Title: title,
177184
IconKey: "kubernetes_text_analyze",
178185
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
179186
IsFail: true,

pkg/analyze/json_compare_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -808,11 +808,7 @@ func Test_jsonCompare(t *testing.T) {
808808
return test.fileContents, nil
809809
}
810810

811-
a := AnalyzeJsonCompare{
812-
analyzer: &test.analyzer,
813-
}
814-
815-
actual, err := a.analyzeJsonCompare(&test.analyzer, getCollectedFileContents)
811+
actual, err := analyzeJsonCompare(&test.analyzer, getCollectedFileContents, test.analyzer.CollectorName)
816812
if !test.isError {
817813
req.NoError(err)
818814
req.Equal(test.expectResult, *actual)

pkg/apis/troubleshoot/v1beta2/hostanalyzer_shared.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,5 @@ type HostAnalyze struct {
152152
HostOS *HostOSAnalyze `json:"hostOS,omitempty" yaml:"hostOS,omitempty"`
153153
TextAnalyze *TextAnalyze `json:"textAnalyze,omitempty" yaml:"textAnalyze,omitempty"`
154154
KernelConfigs *KernelConfigsAnalyze `json:"kernelConfigs,omitempty" yaml:"kernelConfigs,omitempty"`
155+
JsonCompare *JsonCompare `json:"jsonCompare,omitempty" yaml:"jsonCompare,omitempty"`
155156
}

pkg/apis/troubleshoot/v1beta2/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)