Skip to content

Commit

Permalink
feat: node metrics analyser (#1520)
Browse files Browse the repository at this point in the history
* feat: node metrics analyser

The analyser only checks PVC usage at the moment. More analysers
can be added on a need to have basis

* Add tests

* Fix flaky test by waiting for goldpinger pods to start

* Fix how outcomes get checked

* Fix catch all outcome condition

* Fix test

* feat: node metrics analyser

The analyser only checks PVC usage at the moment. More analysers
can be added on a need to have basis

* Add tests

* Fix flaky test by waiting for goldpinger pods to start

* Fix how outcomes get checked

* Fix catch all outcome condition

* Fix test

* Regenerate schemas

* Fix failing test

---------

Co-authored-by: Dexter Yan <[email protected]>
  • Loading branch information
banjoh and DexterYan authored Apr 9, 2024
1 parent dc44038 commit db871e6
Show file tree
Hide file tree
Showing 17 changed files with 1,303 additions and 12 deletions.
60 changes: 60 additions & 0 deletions config/crds/troubleshoot.sh_analyzers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,66 @@ spec:
- collectorName
- outcomes
type: object
nodeMetrics:
properties:
annotations:
additionalProperties:
type: string
type: object
checkName:
type: string
collectorName:
type: string
exclude:
type: BoolString
filters:
properties:
pvc:
properties:
nameRegex:
type: string
namespace:
type: string
type: object
type: object
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:
- collectorName
- outcomes
type: object
nodeResources:
properties:
annotations:
Expand Down
60 changes: 60 additions & 0 deletions config/crds/troubleshoot.sh_preflights.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,66 @@ spec:
- collectorName
- outcomes
type: object
nodeMetrics:
properties:
annotations:
additionalProperties:
type: string
type: object
checkName:
type: string
collectorName:
type: string
exclude:
type: BoolString
filters:
properties:
pvc:
properties:
nameRegex:
type: string
namespace:
type: string
type: object
type: object
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:
- collectorName
- outcomes
type: object
nodeResources:
properties:
annotations:
Expand Down
60 changes: 60 additions & 0 deletions config/crds/troubleshoot.sh_supportbundles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,66 @@ spec:
- collectorName
- outcomes
type: object
nodeMetrics:
properties:
annotations:
additionalProperties:
type: string
type: object
checkName:
type: string
collectorName:
type: string
exclude:
type: BoolString
filters:
properties:
pvc:
properties:
nameRegex:
type: string
namespace:
type: string
type: object
type: object
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:
- collectorName
- outcomes
type: object
nodeResources:
properties:
annotations:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
helm.sh/helm/v3 v3.14.3
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/kubelet v0.29.3
k8s.io/metrics v0.29.3
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
periph.io/x/host/v3 v3.8.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,8 @@ k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 h1:aVUu9fTY98ivBPKR9Y5w/A
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00/go.mod h1:AsvuZPBlUDVuCdzJ87iajxtXuR9oktsTctW/R9wwouA=
k8s.io/kubectl v0.29.0 h1:Oqi48gXjikDhrBF67AYuZRTcJV4lg2l42GmvsP7FmYI=
k8s.io/kubectl v0.29.0/go.mod h1:0jMjGWIcMIQzmUaMgAzhSELv5WtHo2a8pq67DtviAJs=
k8s.io/kubelet v0.29.3 h1:X9h0ZHzc+eUeNTaksbN0ItHyvGhQ7Z0HPjnQD2oHdwU=
k8s.io/kubelet v0.29.3/go.mod h1:jDiGuTkFOUynyBKzOoC1xRSWlgAZ9UPcTYeFyjr6vas=
k8s.io/metrics v0.29.3 h1:nN+eavbMQ7Kuif2tIdTr2/F2ec2E/SIAWSruTZ+Ye6U=
k8s.io/metrics v0.29.3/go.mod h1:kb3tGGC4ZcIDIuvXyUE291RwJ5WmDu0tB4wAVZM6h2I=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
Expand Down
2 changes: 2 additions & 0 deletions pkg/analyze/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ func getAnalyzer(analyzer *troubleshootv1beta2.Analyze) Analyzer {
return &AnalyzeGoldpinger{analyzer: analyzer.Goldpinger}
case analyzer.Event != nil:
return &AnalyzeEvent{analyzer: analyzer.Event}
case analyzer.NodeMetrics != nil:
return &AnalyzeNodeMetrics{analyzer: analyzer.NodeMetrics}
default:
return nil
}
Expand Down
34 changes: 34 additions & 0 deletions pkg/analyze/comparison.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package analyzer

import "fmt"

type ComparisonOperator int

const (
Unknown ComparisonOperator = iota
Equal
NotEqual
GreaterThan
GreaterThanOrEqual
LessThan
LessThanOrEqual
)

func ParseComparisonOperator(s string) (ComparisonOperator, error) {
switch s {
case "=", "==", "===":
return Equal, nil
case "!=", "!==":
return NotEqual, nil
case "<":
return LessThan, nil
case ">":
return GreaterThan, nil
case "<=":
return LessThanOrEqual, nil
case ">=":
return GreaterThanOrEqual, nil
}

return Unknown, fmt.Errorf("unknown operator: %s", s)
}
79 changes: 79 additions & 0 deletions pkg/analyze/comparison_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package analyzer

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseComparisonOperator(t *testing.T) {
tests := []struct {
name string
input string
want ComparisonOperator
wantErr bool
}{
{
name: "equal",
input: "=",
want: Equal,
},
{
name: "equal",
input: "==",
want: Equal,
},
{
name: "equal",
input: "===",
want: Equal,
},
{
name: "not equal",
input: "!=",
want: NotEqual,
},
{
name: "not equal",
input: "!==",
want: NotEqual,
},
{
name: "less than",
input: "<",
want: LessThan,
},
{
name: "greater than",
input: ">",
want: GreaterThan,
},
{
name: "less than or equal",
input: "<=",
want: LessThanOrEqual,
},
{
name: "greater than or equal",
input: ">=",
want: GreaterThanOrEqual,
},
{
name: "invalid operator 1",
input: "",
wantErr: true,
},
{
name: "invalid operator 2",
input: "gibberish",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseComparisonOperator(tt.input)
assert.Equal(t, tt.want, got, "ParseOperator() = %v, want %v", got, tt.want)
assert.Equalf(t, tt.wantErr, err != nil, "ParseOperator() error = %v, wantErr %v", err, tt.wantErr)
})
}
}
Loading

0 comments on commit db871e6

Please sign in to comment.