Skip to content

Commit

Permalink
feat: [sc-106625] http analyzer for in-cluster (#1566)
Browse files Browse the repository at this point in the history
* http analzyer for in-cluster
* make check-schemas
  • Loading branch information
nvanthao authored Jun 19, 2024
1 parent 18ec6ad commit edfa01c
Show file tree
Hide file tree
Showing 11 changed files with 450 additions and 32 deletions.
49 changes: 49 additions & 0 deletions config/crds/troubleshoot.sh_analyzers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,55 @@ spec:
required:
- collectorName
type: object
http:
properties:
annotations:
additionalProperties:
type: string
type: object
checkName:
type: string
collectorName:
type: string
exclude:
type: BoolString
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
strict:
type: BoolString
required:
- outcomes
type: object
imagePullSecret:
properties:
annotations:
Expand Down
49 changes: 49 additions & 0 deletions config/crds/troubleshoot.sh_preflights.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,55 @@ spec:
required:
- collectorName
type: object
http:
properties:
annotations:
additionalProperties:
type: string
type: object
checkName:
type: string
collectorName:
type: string
exclude:
type: BoolString
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
strict:
type: BoolString
required:
- outcomes
type: object
imagePullSecret:
properties:
annotations:
Expand Down
49 changes: 49 additions & 0 deletions config/crds/troubleshoot.sh_supportbundles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,55 @@ spec:
required:
- collectorName
type: object
http:
properties:
annotations:
additionalProperties:
type: string
type: object
checkName:
type: string
collectorName:
type: string
exclude:
type: BoolString
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
strict:
type: BoolString
required:
- outcomes
type: object
imagePullSecret:
properties:
annotations:
Expand Down
2 changes: 2 additions & 0 deletions pkg/analyze/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ func getAnalyzer(analyzer *troubleshootv1beta2.Analyze) Analyzer {
return &AnalyzeEvent{analyzer: analyzer.Event}
case analyzer.NodeMetrics != nil:
return &AnalyzeNodeMetrics{analyzer: analyzer.NodeMetrics}
case analyzer.HTTP != nil:
return &AnalyzeHTTPAnalyze{analyzer: analyzer.HTTP}
default:
return nil
}
Expand Down
69 changes: 37 additions & 32 deletions pkg/analyze/host_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,41 @@ func (a *AnalyzeHostHTTP) Analyze(
if hostAnalyzer.CollectorName != "" {
name = filepath.Join("host-collectors/http", hostAnalyzer.CollectorName+".json")
}
contents, err := getCollectedFileContents(name)

return analyzeHTTPResult(hostAnalyzer, name, getCollectedFileContents, a.Title())
}

func compareHostHTTPConditionalToActual(conditional string, result *httpResult) (res bool, err error) {
if conditional == "error" {
return result.Error != nil, nil
}

parts := strings.Split(conditional, " ")
if len(parts) != 3 {
return false, fmt.Errorf("Failed to parse conditional: got %d parts", len(parts))
}

if parts[0] != "statusCode" {
return false, errors.New(`Conditional must begin with keyword "statusCode"`)
}

if parts[1] != "=" && parts[1] != "==" && parts[1] != "===" {
return false, errors.New(`Only supported operator is "=="`)
}

i, err := strconv.Atoi(parts[2])
if err != nil {
return false, err
}

if result.Response == nil {
return false, err
}
return result.Response.Status == i, nil
}

func analyzeHTTPResult(analyzer *troubleshootv1beta2.HTTPAnalyze, fileName string, getCollectedFileContents getCollectedFileContents, title string) ([]*AnalyzeResult, error) {
contents, err := getCollectedFileContents(fileName)
if err != nil {
return nil, errors.Wrap(err, "failed to get collected file")
}
Expand All @@ -49,10 +83,10 @@ func (a *AnalyzeHostHTTP) Analyze(
}

result := &AnalyzeResult{
Title: a.Title(),
Title: title,
}

for _, outcome := range hostAnalyzer.Outcomes {
for _, outcome := range analyzer.Outcomes {
if outcome.Fail != nil {
if outcome.Fail.When == "" {
result.IsFail = true
Expand Down Expand Up @@ -122,32 +156,3 @@ func (a *AnalyzeHostHTTP) Analyze(

return []*AnalyzeResult{result}, nil
}

func compareHostHTTPConditionalToActual(conditional string, result *httpResult) (res bool, err error) {
if conditional == "error" {
return result.Error != nil, nil
}

parts := strings.Split(conditional, " ")
if len(parts) != 3 {
return false, fmt.Errorf("Failed to parse conditional: got %d parts", len(parts))
}

if parts[0] != "statusCode" {
return false, errors.New(`Conditional must begin with keyword "statusCode"`)
}

if parts[1] != "=" && parts[1] != "==" && parts[1] != "===" {
return false, errors.New(`Only supported operator is "=="`)
}

i, err := strconv.Atoi(parts[2])
if err != nil {
return false, err
}

if result.Response == nil {
return false, err
}
return result.Response.Status == i, nil
}
30 changes: 30 additions & 0 deletions pkg/analyze/http_analyze.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package analyzer

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

type AnalyzeHTTPAnalyze struct {
analyzer *troubleshootv1beta2.HTTPAnalyze
}

func (a *AnalyzeHTTPAnalyze) Title() string {
checkName := a.analyzer.CheckName
if checkName == "" {
checkName = a.analyzer.CollectorName
}

return checkName
}

func (a *AnalyzeHTTPAnalyze) IsExcluded() (bool, error) {
return isExcluded(a.analyzer.Exclude)
}

func (a *AnalyzeHTTPAnalyze) Analyze(getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
fileName := "result.json"
if a.analyzer.CollectorName != "" {
fileName = a.analyzer.CollectorName + ".json"
}
return analyzeHTTPResult(a.analyzer, fileName, getFile, a.Title())
}
1 change: 1 addition & 0 deletions pkg/apis/troubleshoot/v1beta2/analyzer_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,5 @@ type Analyze struct {
Goldpinger *GoldpingerAnalyze `json:"goldpinger,omitempty" yaml:"goldpinger,omitempty"`
Event *EventAnalyze `json:"event,omitempty" yaml:"event,omitempty"`
NodeMetrics *NodeMetricsAnalyze `json:"nodeMetrics,omitempty" yaml:"nodeMetrics,omitempty"`
HTTP *HTTPAnalyze `json:"http,omitempty" yaml:"http,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.

76 changes: 76 additions & 0 deletions schemas/analyzer-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,82 @@
}
}
},
"http": {
"type": "object",
"required": [
"outcomes"
],
"properties": {
"annotations": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"checkName": {
"type": "string"
},
"collectorName": {
"type": "string"
},
"exclude": {
"oneOf": [{"type": "string"},{"type": "boolean"}]
},
"outcomes": {
"type": "array",
"items": {
"type": "object",
"properties": {
"fail": {
"type": "object",
"properties": {
"message": {
"type": "string"
},
"uri": {
"type": "string"
},
"when": {
"type": "string"
}
}
},
"pass": {
"type": "object",
"properties": {
"message": {
"type": "string"
},
"uri": {
"type": "string"
},
"when": {
"type": "string"
}
}
},
"warn": {
"type": "object",
"properties": {
"message": {
"type": "string"
},
"uri": {
"type": "string"
},
"when": {
"type": "string"
}
}
}
}
}
},
"strict": {
"oneOf": [{"type": "string"},{"type": "boolean"}]
}
}
},
"imagePullSecret": {
"type": "object",
"required": [
Expand Down
Loading

0 comments on commit edfa01c

Please sign in to comment.