Skip to content

Commit db871e6

Browse files
banjohDexterYan
andauthored
feat: node metrics analyser (#1520)
* 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 <yanshaocong@gmail.com>
1 parent dc44038 commit db871e6

17 files changed

+1303
-12
lines changed

config/crds/troubleshoot.sh_analyzers.yaml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,66 @@ spec:
10451045
- collectorName
10461046
- outcomes
10471047
type: object
1048+
nodeMetrics:
1049+
properties:
1050+
annotations:
1051+
additionalProperties:
1052+
type: string
1053+
type: object
1054+
checkName:
1055+
type: string
1056+
collectorName:
1057+
type: string
1058+
exclude:
1059+
type: BoolString
1060+
filters:
1061+
properties:
1062+
pvc:
1063+
properties:
1064+
nameRegex:
1065+
type: string
1066+
namespace:
1067+
type: string
1068+
type: object
1069+
type: object
1070+
outcomes:
1071+
items:
1072+
properties:
1073+
fail:
1074+
properties:
1075+
message:
1076+
type: string
1077+
uri:
1078+
type: string
1079+
when:
1080+
type: string
1081+
type: object
1082+
pass:
1083+
properties:
1084+
message:
1085+
type: string
1086+
uri:
1087+
type: string
1088+
when:
1089+
type: string
1090+
type: object
1091+
warn:
1092+
properties:
1093+
message:
1094+
type: string
1095+
uri:
1096+
type: string
1097+
when:
1098+
type: string
1099+
type: object
1100+
type: object
1101+
type: array
1102+
strict:
1103+
type: BoolString
1104+
required:
1105+
- collectorName
1106+
- outcomes
1107+
type: object
10481108
nodeResources:
10491109
properties:
10501110
annotations:

config/crds/troubleshoot.sh_preflights.yaml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,66 @@ spec:
10451045
- collectorName
10461046
- outcomes
10471047
type: object
1048+
nodeMetrics:
1049+
properties:
1050+
annotations:
1051+
additionalProperties:
1052+
type: string
1053+
type: object
1054+
checkName:
1055+
type: string
1056+
collectorName:
1057+
type: string
1058+
exclude:
1059+
type: BoolString
1060+
filters:
1061+
properties:
1062+
pvc:
1063+
properties:
1064+
nameRegex:
1065+
type: string
1066+
namespace:
1067+
type: string
1068+
type: object
1069+
type: object
1070+
outcomes:
1071+
items:
1072+
properties:
1073+
fail:
1074+
properties:
1075+
message:
1076+
type: string
1077+
uri:
1078+
type: string
1079+
when:
1080+
type: string
1081+
type: object
1082+
pass:
1083+
properties:
1084+
message:
1085+
type: string
1086+
uri:
1087+
type: string
1088+
when:
1089+
type: string
1090+
type: object
1091+
warn:
1092+
properties:
1093+
message:
1094+
type: string
1095+
uri:
1096+
type: string
1097+
when:
1098+
type: string
1099+
type: object
1100+
type: object
1101+
type: array
1102+
strict:
1103+
type: BoolString
1104+
required:
1105+
- collectorName
1106+
- outcomes
1107+
type: object
10481108
nodeResources:
10491109
properties:
10501110
annotations:

config/crds/troubleshoot.sh_supportbundles.yaml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,66 @@ spec:
10761076
- collectorName
10771077
- outcomes
10781078
type: object
1079+
nodeMetrics:
1080+
properties:
1081+
annotations:
1082+
additionalProperties:
1083+
type: string
1084+
type: object
1085+
checkName:
1086+
type: string
1087+
collectorName:
1088+
type: string
1089+
exclude:
1090+
type: BoolString
1091+
filters:
1092+
properties:
1093+
pvc:
1094+
properties:
1095+
nameRegex:
1096+
type: string
1097+
namespace:
1098+
type: string
1099+
type: object
1100+
type: object
1101+
outcomes:
1102+
items:
1103+
properties:
1104+
fail:
1105+
properties:
1106+
message:
1107+
type: string
1108+
uri:
1109+
type: string
1110+
when:
1111+
type: string
1112+
type: object
1113+
pass:
1114+
properties:
1115+
message:
1116+
type: string
1117+
uri:
1118+
type: string
1119+
when:
1120+
type: string
1121+
type: object
1122+
warn:
1123+
properties:
1124+
message:
1125+
type: string
1126+
uri:
1127+
type: string
1128+
when:
1129+
type: string
1130+
type: object
1131+
type: object
1132+
type: array
1133+
strict:
1134+
type: BoolString
1135+
required:
1136+
- collectorName
1137+
- outcomes
1138+
type: object
10791139
nodeResources:
10801140
properties:
10811141
annotations:

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ require (
252252
gopkg.in/yaml.v3 v3.0.1 // indirect
253253
helm.sh/helm/v3 v3.14.3
254254
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
255+
k8s.io/kubelet v0.29.3
255256
k8s.io/metrics v0.29.3
256257
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
257258
periph.io/x/host/v3 v3.8.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,8 @@ k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 h1:aVUu9fTY98ivBPKR9Y5w/A
15731573
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00/go.mod h1:AsvuZPBlUDVuCdzJ87iajxtXuR9oktsTctW/R9wwouA=
15741574
k8s.io/kubectl v0.29.0 h1:Oqi48gXjikDhrBF67AYuZRTcJV4lg2l42GmvsP7FmYI=
15751575
k8s.io/kubectl v0.29.0/go.mod h1:0jMjGWIcMIQzmUaMgAzhSELv5WtHo2a8pq67DtviAJs=
1576+
k8s.io/kubelet v0.29.3 h1:X9h0ZHzc+eUeNTaksbN0ItHyvGhQ7Z0HPjnQD2oHdwU=
1577+
k8s.io/kubelet v0.29.3/go.mod h1:jDiGuTkFOUynyBKzOoC1xRSWlgAZ9UPcTYeFyjr6vas=
15761578
k8s.io/metrics v0.29.3 h1:nN+eavbMQ7Kuif2tIdTr2/F2ec2E/SIAWSruTZ+Ye6U=
15771579
k8s.io/metrics v0.29.3/go.mod h1:kb3tGGC4ZcIDIuvXyUE291RwJ5WmDu0tB4wAVZM6h2I=
15781580
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=

pkg/analyze/analyzer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ func getAnalyzer(analyzer *troubleshootv1beta2.Analyze) Analyzer {
248248
return &AnalyzeGoldpinger{analyzer: analyzer.Goldpinger}
249249
case analyzer.Event != nil:
250250
return &AnalyzeEvent{analyzer: analyzer.Event}
251+
case analyzer.NodeMetrics != nil:
252+
return &AnalyzeNodeMetrics{analyzer: analyzer.NodeMetrics}
251253
default:
252254
return nil
253255
}

pkg/analyze/comparison.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package analyzer
2+
3+
import "fmt"
4+
5+
type ComparisonOperator int
6+
7+
const (
8+
Unknown ComparisonOperator = iota
9+
Equal
10+
NotEqual
11+
GreaterThan
12+
GreaterThanOrEqual
13+
LessThan
14+
LessThanOrEqual
15+
)
16+
17+
func ParseComparisonOperator(s string) (ComparisonOperator, error) {
18+
switch s {
19+
case "=", "==", "===":
20+
return Equal, nil
21+
case "!=", "!==":
22+
return NotEqual, nil
23+
case "<":
24+
return LessThan, nil
25+
case ">":
26+
return GreaterThan, nil
27+
case "<=":
28+
return LessThanOrEqual, nil
29+
case ">=":
30+
return GreaterThanOrEqual, nil
31+
}
32+
33+
return Unknown, fmt.Errorf("unknown operator: %s", s)
34+
}

pkg/analyze/comparison_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package analyzer
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestParseComparisonOperator(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
input string
13+
want ComparisonOperator
14+
wantErr bool
15+
}{
16+
{
17+
name: "equal",
18+
input: "=",
19+
want: Equal,
20+
},
21+
{
22+
name: "equal",
23+
input: "==",
24+
want: Equal,
25+
},
26+
{
27+
name: "equal",
28+
input: "===",
29+
want: Equal,
30+
},
31+
{
32+
name: "not equal",
33+
input: "!=",
34+
want: NotEqual,
35+
},
36+
{
37+
name: "not equal",
38+
input: "!==",
39+
want: NotEqual,
40+
},
41+
{
42+
name: "less than",
43+
input: "<",
44+
want: LessThan,
45+
},
46+
{
47+
name: "greater than",
48+
input: ">",
49+
want: GreaterThan,
50+
},
51+
{
52+
name: "less than or equal",
53+
input: "<=",
54+
want: LessThanOrEqual,
55+
},
56+
{
57+
name: "greater than or equal",
58+
input: ">=",
59+
want: GreaterThanOrEqual,
60+
},
61+
{
62+
name: "invalid operator 1",
63+
input: "",
64+
wantErr: true,
65+
},
66+
{
67+
name: "invalid operator 2",
68+
input: "gibberish",
69+
wantErr: true,
70+
},
71+
}
72+
for _, tt := range tests {
73+
t.Run(tt.name, func(t *testing.T) {
74+
got, err := ParseComparisonOperator(tt.input)
75+
assert.Equal(t, tt.want, got, "ParseOperator() = %v, want %v", got, tt.want)
76+
assert.Equalf(t, tt.wantErr, err != nil, "ParseOperator() error = %v, wantErr %v", err, tt.wantErr)
77+
})
78+
}
79+
}

0 commit comments

Comments
 (0)