@@ -23,11 +23,13 @@ import (
23
23
"testing"
24
24
"time"
25
25
26
+ dto "github.com/prometheus/client_model/go"
26
27
"k8s.io/api/core/v1"
27
28
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
29
"k8s.io/apimachinery/pkg/types"
29
30
"k8s.io/apimachinery/pkg/util/clock"
30
31
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
32
+ "k8s.io/kubernetes/pkg/scheduler/metrics"
31
33
"k8s.io/kubernetes/pkg/scheduler/util"
32
34
)
33
35
@@ -1066,7 +1068,7 @@ func TestPodTimestamp(t *testing.T) {
1066
1068
tests := []struct {
1067
1069
name string
1068
1070
operations []operation
1069
- operants []* podInfo
1071
+ operands []* podInfo
1070
1072
expected []* podInfo
1071
1073
}{
1072
1074
{
@@ -1075,7 +1077,7 @@ func TestPodTimestamp(t *testing.T) {
1075
1077
addPodActiveQ ,
1076
1078
addPodActiveQ ,
1077
1079
},
1078
- operants : []* podInfo {pInfo2 , pInfo1 },
1080
+ operands : []* podInfo {pInfo2 , pInfo1 },
1079
1081
expected : []* podInfo {pInfo1 , pInfo2 },
1080
1082
},
1081
1083
{
@@ -1084,7 +1086,7 @@ func TestPodTimestamp(t *testing.T) {
1084
1086
updatePodActiveQ ,
1085
1087
updatePodActiveQ ,
1086
1088
},
1087
- operants : []* podInfo {pInfo2 , pInfo1 },
1089
+ operands : []* podInfo {pInfo2 , pInfo1 },
1088
1090
expected : []* podInfo {pInfo1 , pInfo2 },
1089
1091
},
1090
1092
{
@@ -1094,7 +1096,7 @@ func TestPodTimestamp(t *testing.T) {
1094
1096
addPodUnschedulableQ ,
1095
1097
moveAllToActiveQ ,
1096
1098
},
1097
- operants : []* podInfo {pInfo2 , pInfo1 , nil },
1099
+ operands : []* podInfo {pInfo2 , pInfo1 , nil },
1098
1100
expected : []* podInfo {pInfo1 , pInfo2 },
1099
1101
},
1100
1102
{
@@ -1106,7 +1108,7 @@ func TestPodTimestamp(t *testing.T) {
1106
1108
flushBackoffQ ,
1107
1109
moveAllToActiveQ ,
1108
1110
},
1109
- operants : []* podInfo {pInfo2 , pInfo1 , pInfo1 , nil , nil },
1111
+ operands : []* podInfo {pInfo2 , pInfo1 , pInfo1 , nil , nil },
1110
1112
expected : []* podInfo {pInfo1 , pInfo2 },
1111
1113
},
1112
1114
}
@@ -1117,7 +1119,7 @@ func TestPodTimestamp(t *testing.T) {
1117
1119
var podInfoList []* podInfo
1118
1120
1119
1121
for i , op := range test .operations {
1120
- op (queue , test .operants [i ])
1122
+ op (queue , test .operands [i ])
1121
1123
}
1122
1124
1123
1125
for i := 0 ; i < len (test .expected ); i ++ {
@@ -1135,3 +1137,146 @@ func TestPodTimestamp(t *testing.T) {
1135
1137
})
1136
1138
}
1137
1139
}
1140
+
1141
+ // TestPendingPodsMetric tests Prometheus metrics related with pending pods
1142
+ func TestPendingPodsMetric (t * testing.T ) {
1143
+ total := 50
1144
+ timestamp := time .Now ()
1145
+ var pInfos = make ([]* podInfo , 0 , total )
1146
+ for i := 1 ; i <= total ; i ++ {
1147
+ p := & podInfo {
1148
+ pod : & v1.Pod {
1149
+ ObjectMeta : metav1.ObjectMeta {
1150
+ Name : fmt .Sprintf ("test-pod-%d" , i ),
1151
+ Namespace : fmt .Sprintf ("ns%d" , i ),
1152
+ UID : types .UID (fmt .Sprintf ("tp-%d" , i )),
1153
+ },
1154
+ },
1155
+ timestamp : timestamp ,
1156
+ }
1157
+ pInfos = append (pInfos , p )
1158
+ }
1159
+ tests := []struct {
1160
+ name string
1161
+ operations []operation
1162
+ operands [][]* podInfo
1163
+ expected []int64
1164
+ }{
1165
+ {
1166
+ name : "add pods to activeQ and unschedulableQ" ,
1167
+ operations : []operation {
1168
+ addPodActiveQ ,
1169
+ addPodUnschedulableQ ,
1170
+ },
1171
+ operands : [][]* podInfo {
1172
+ pInfos [:30 ],
1173
+ pInfos [30 :],
1174
+ },
1175
+ expected : []int64 {30 , 0 , 20 },
1176
+ },
1177
+ {
1178
+ name : "add pods to all kinds of queues" ,
1179
+ operations : []operation {
1180
+ addPodActiveQ ,
1181
+ backoffPod ,
1182
+ addPodBackoffQ ,
1183
+ addPodUnschedulableQ ,
1184
+ },
1185
+ operands : [][]* podInfo {
1186
+ pInfos [:15 ],
1187
+ pInfos [15 :40 ],
1188
+ pInfos [15 :40 ],
1189
+ pInfos [40 :],
1190
+ },
1191
+ expected : []int64 {15 , 25 , 10 },
1192
+ },
1193
+ {
1194
+ name : "add pods to unschedulableQ and then move all to activeQ" ,
1195
+ operations : []operation {
1196
+ addPodUnschedulableQ ,
1197
+ moveAllToActiveQ ,
1198
+ },
1199
+ operands : [][]* podInfo {
1200
+ pInfos [:total ],
1201
+ {nil },
1202
+ },
1203
+ expected : []int64 {int64 (total ), 0 , 0 },
1204
+ },
1205
+ {
1206
+ name : "make some pods subject to backoff, add pods to unschedulableQ, and then move all to activeQ" ,
1207
+ operations : []operation {
1208
+ backoffPod ,
1209
+ addPodUnschedulableQ ,
1210
+ moveAllToActiveQ ,
1211
+ },
1212
+ operands : [][]* podInfo {
1213
+ pInfos [:20 ],
1214
+ pInfos [:total ],
1215
+ {nil },
1216
+ },
1217
+ expected : []int64 {int64 (total - 20 ), 20 , 0 },
1218
+ },
1219
+ {
1220
+ name : "make some pods subject to backoff, add pods to unschedulableQ/activeQ, move all to activeQ, and finally flush backoffQ" ,
1221
+ operations : []operation {
1222
+ backoffPod ,
1223
+ addPodUnschedulableQ ,
1224
+ addPodActiveQ ,
1225
+ moveAllToActiveQ ,
1226
+ flushBackoffQ ,
1227
+ },
1228
+ operands : [][]* podInfo {
1229
+ pInfos [:20 ],
1230
+ pInfos [:40 ],
1231
+ pInfos [40 :],
1232
+ {nil },
1233
+ {nil },
1234
+ },
1235
+ expected : []int64 {int64 (total ), 0 , 0 },
1236
+ },
1237
+ }
1238
+
1239
+ resetMetrics := func () {
1240
+ metrics .ActivePods .Set (0 )
1241
+ metrics .BackoffPods .Set (0 )
1242
+ metrics .UnschedulablePods .Set (0 )
1243
+ }
1244
+
1245
+ for _ , test := range tests {
1246
+ t .Run (test .name , func (t * testing.T ) {
1247
+ resetMetrics ()
1248
+ queue := NewPriorityQueueWithClock (nil , clock .NewFakeClock (timestamp ))
1249
+ for i , op := range test .operations {
1250
+ for _ , pInfo := range test .operands [i ] {
1251
+ op (queue , pInfo )
1252
+ }
1253
+ }
1254
+
1255
+ var activeNum , backoffNum , unschedulableNum float64
1256
+ metricProto := & dto.Metric {}
1257
+ if err := metrics .ActivePods .Write (metricProto ); err != nil {
1258
+ t .Errorf ("error writing ActivePods metric: %v" , err )
1259
+ }
1260
+ activeNum = metricProto .Gauge .GetValue ()
1261
+ if int64 (activeNum ) != test .expected [0 ] {
1262
+ t .Errorf ("ActivePods: Expected %v, got %v" , test .expected [0 ], activeNum )
1263
+ }
1264
+
1265
+ if err := metrics .BackoffPods .Write (metricProto ); err != nil {
1266
+ t .Errorf ("error writing BackoffPods metric: %v" , err )
1267
+ }
1268
+ backoffNum = metricProto .Gauge .GetValue ()
1269
+ if int64 (backoffNum ) != test .expected [1 ] {
1270
+ t .Errorf ("BackoffPods: Expected %v, got %v" , test .expected [1 ], backoffNum )
1271
+ }
1272
+
1273
+ if err := metrics .UnschedulablePods .Write (metricProto ); err != nil {
1274
+ t .Errorf ("error writing UnschedulablePods metric: %v" , err )
1275
+ }
1276
+ unschedulableNum = metricProto .Gauge .GetValue ()
1277
+ if int64 (unschedulableNum ) != test .expected [2 ] {
1278
+ t .Errorf ("UnschedulablePods: Expected %v, got %v" , test .expected [2 ], unschedulableNum )
1279
+ }
1280
+ })
1281
+ }
1282
+ }
0 commit comments