Skip to content

Commit edfa01c

Browse files
authored
feat: [sc-106625] http analyzer for in-cluster (#1566)
* http analzyer for in-cluster * make check-schemas
1 parent 18ec6ad commit edfa01c

11 files changed

+450
-32
lines changed

config/crds/troubleshoot.sh_analyzers.yaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,55 @@ spec:
673673
required:
674674
- collectorName
675675
type: object
676+
http:
677+
properties:
678+
annotations:
679+
additionalProperties:
680+
type: string
681+
type: object
682+
checkName:
683+
type: string
684+
collectorName:
685+
type: string
686+
exclude:
687+
type: BoolString
688+
outcomes:
689+
items:
690+
properties:
691+
fail:
692+
properties:
693+
message:
694+
type: string
695+
uri:
696+
type: string
697+
when:
698+
type: string
699+
type: object
700+
pass:
701+
properties:
702+
message:
703+
type: string
704+
uri:
705+
type: string
706+
when:
707+
type: string
708+
type: object
709+
warn:
710+
properties:
711+
message:
712+
type: string
713+
uri:
714+
type: string
715+
when:
716+
type: string
717+
type: object
718+
type: object
719+
type: array
720+
strict:
721+
type: BoolString
722+
required:
723+
- outcomes
724+
type: object
676725
imagePullSecret:
677726
properties:
678727
annotations:

config/crds/troubleshoot.sh_preflights.yaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,55 @@ spec:
673673
required:
674674
- collectorName
675675
type: object
676+
http:
677+
properties:
678+
annotations:
679+
additionalProperties:
680+
type: string
681+
type: object
682+
checkName:
683+
type: string
684+
collectorName:
685+
type: string
686+
exclude:
687+
type: BoolString
688+
outcomes:
689+
items:
690+
properties:
691+
fail:
692+
properties:
693+
message:
694+
type: string
695+
uri:
696+
type: string
697+
when:
698+
type: string
699+
type: object
700+
pass:
701+
properties:
702+
message:
703+
type: string
704+
uri:
705+
type: string
706+
when:
707+
type: string
708+
type: object
709+
warn:
710+
properties:
711+
message:
712+
type: string
713+
uri:
714+
type: string
715+
when:
716+
type: string
717+
type: object
718+
type: object
719+
type: array
720+
strict:
721+
type: BoolString
722+
required:
723+
- outcomes
724+
type: object
676725
imagePullSecret:
677726
properties:
678727
annotations:

config/crds/troubleshoot.sh_supportbundles.yaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,55 @@ spec:
704704
required:
705705
- collectorName
706706
type: object
707+
http:
708+
properties:
709+
annotations:
710+
additionalProperties:
711+
type: string
712+
type: object
713+
checkName:
714+
type: string
715+
collectorName:
716+
type: string
717+
exclude:
718+
type: BoolString
719+
outcomes:
720+
items:
721+
properties:
722+
fail:
723+
properties:
724+
message:
725+
type: string
726+
uri:
727+
type: string
728+
when:
729+
type: string
730+
type: object
731+
pass:
732+
properties:
733+
message:
734+
type: string
735+
uri:
736+
type: string
737+
when:
738+
type: string
739+
type: object
740+
warn:
741+
properties:
742+
message:
743+
type: string
744+
uri:
745+
type: string
746+
when:
747+
type: string
748+
type: object
749+
type: object
750+
type: array
751+
strict:
752+
type: BoolString
753+
required:
754+
- outcomes
755+
type: object
707756
imagePullSecret:
708757
properties:
709758
annotations:

pkg/analyze/analyzer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ func getAnalyzer(analyzer *troubleshootv1beta2.Analyze) Analyzer {
250250
return &AnalyzeEvent{analyzer: analyzer.Event}
251251
case analyzer.NodeMetrics != nil:
252252
return &AnalyzeNodeMetrics{analyzer: analyzer.NodeMetrics}
253+
case analyzer.HTTP != nil:
254+
return &AnalyzeHTTPAnalyze{analyzer: analyzer.HTTP}
253255
default:
254256
return nil
255257
}

pkg/analyze/host_http.go

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,41 @@ func (a *AnalyzeHostHTTP) Analyze(
3838
if hostAnalyzer.CollectorName != "" {
3939
name = filepath.Join("host-collectors/http", hostAnalyzer.CollectorName+".json")
4040
}
41-
contents, err := getCollectedFileContents(name)
41+
42+
return analyzeHTTPResult(hostAnalyzer, name, getCollectedFileContents, a.Title())
43+
}
44+
45+
func compareHostHTTPConditionalToActual(conditional string, result *httpResult) (res bool, err error) {
46+
if conditional == "error" {
47+
return result.Error != nil, nil
48+
}
49+
50+
parts := strings.Split(conditional, " ")
51+
if len(parts) != 3 {
52+
return false, fmt.Errorf("Failed to parse conditional: got %d parts", len(parts))
53+
}
54+
55+
if parts[0] != "statusCode" {
56+
return false, errors.New(`Conditional must begin with keyword "statusCode"`)
57+
}
58+
59+
if parts[1] != "=" && parts[1] != "==" && parts[1] != "===" {
60+
return false, errors.New(`Only supported operator is "=="`)
61+
}
62+
63+
i, err := strconv.Atoi(parts[2])
64+
if err != nil {
65+
return false, err
66+
}
67+
68+
if result.Response == nil {
69+
return false, err
70+
}
71+
return result.Response.Status == i, nil
72+
}
73+
74+
func analyzeHTTPResult(analyzer *troubleshootv1beta2.HTTPAnalyze, fileName string, getCollectedFileContents getCollectedFileContents, title string) ([]*AnalyzeResult, error) {
75+
contents, err := getCollectedFileContents(fileName)
4276
if err != nil {
4377
return nil, errors.Wrap(err, "failed to get collected file")
4478
}
@@ -49,10 +83,10 @@ func (a *AnalyzeHostHTTP) Analyze(
4983
}
5084

5185
result := &AnalyzeResult{
52-
Title: a.Title(),
86+
Title: title,
5387
}
5488

55-
for _, outcome := range hostAnalyzer.Outcomes {
89+
for _, outcome := range analyzer.Outcomes {
5690
if outcome.Fail != nil {
5791
if outcome.Fail.When == "" {
5892
result.IsFail = true
@@ -122,32 +156,3 @@ func (a *AnalyzeHostHTTP) Analyze(
122156

123157
return []*AnalyzeResult{result}, nil
124158
}
125-
126-
func compareHostHTTPConditionalToActual(conditional string, result *httpResult) (res bool, err error) {
127-
if conditional == "error" {
128-
return result.Error != nil, nil
129-
}
130-
131-
parts := strings.Split(conditional, " ")
132-
if len(parts) != 3 {
133-
return false, fmt.Errorf("Failed to parse conditional: got %d parts", len(parts))
134-
}
135-
136-
if parts[0] != "statusCode" {
137-
return false, errors.New(`Conditional must begin with keyword "statusCode"`)
138-
}
139-
140-
if parts[1] != "=" && parts[1] != "==" && parts[1] != "===" {
141-
return false, errors.New(`Only supported operator is "=="`)
142-
}
143-
144-
i, err := strconv.Atoi(parts[2])
145-
if err != nil {
146-
return false, err
147-
}
148-
149-
if result.Response == nil {
150-
return false, err
151-
}
152-
return result.Response.Status == i, nil
153-
}

pkg/analyze/http_analyze.go

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

pkg/apis/troubleshoot/v1beta2/analyzer_shared.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,5 @@ type Analyze struct {
292292
Goldpinger *GoldpingerAnalyze `json:"goldpinger,omitempty" yaml:"goldpinger,omitempty"`
293293
Event *EventAnalyze `json:"event,omitempty" yaml:"event,omitempty"`
294294
NodeMetrics *NodeMetricsAnalyze `json:"nodeMetrics,omitempty" yaml:"nodeMetrics,omitempty"`
295+
HTTP *HTTPAnalyze `json:"http,omitempty" yaml:"http,omitempty"`
295296
}

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.

schemas/analyzer-troubleshoot-v1beta2.json

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,82 @@
993993
}
994994
}
995995
},
996+
"http": {
997+
"type": "object",
998+
"required": [
999+
"outcomes"
1000+
],
1001+
"properties": {
1002+
"annotations": {
1003+
"type": "object",
1004+
"additionalProperties": {
1005+
"type": "string"
1006+
}
1007+
},
1008+
"checkName": {
1009+
"type": "string"
1010+
},
1011+
"collectorName": {
1012+
"type": "string"
1013+
},
1014+
"exclude": {
1015+
"oneOf": [{"type": "string"},{"type": "boolean"}]
1016+
},
1017+
"outcomes": {
1018+
"type": "array",
1019+
"items": {
1020+
"type": "object",
1021+
"properties": {
1022+
"fail": {
1023+
"type": "object",
1024+
"properties": {
1025+
"message": {
1026+
"type": "string"
1027+
},
1028+
"uri": {
1029+
"type": "string"
1030+
},
1031+
"when": {
1032+
"type": "string"
1033+
}
1034+
}
1035+
},
1036+
"pass": {
1037+
"type": "object",
1038+
"properties": {
1039+
"message": {
1040+
"type": "string"
1041+
},
1042+
"uri": {
1043+
"type": "string"
1044+
},
1045+
"when": {
1046+
"type": "string"
1047+
}
1048+
}
1049+
},
1050+
"warn": {
1051+
"type": "object",
1052+
"properties": {
1053+
"message": {
1054+
"type": "string"
1055+
},
1056+
"uri": {
1057+
"type": "string"
1058+
},
1059+
"when": {
1060+
"type": "string"
1061+
}
1062+
}
1063+
}
1064+
}
1065+
}
1066+
},
1067+
"strict": {
1068+
"oneOf": [{"type": "string"},{"type": "boolean"}]
1069+
}
1070+
}
1071+
},
9961072
"imagePullSecret": {
9971073
"type": "object",
9981074
"required": [

0 commit comments

Comments
 (0)