Skip to content

Commit d86d580

Browse files
committed
add ut for loadaware scheduler plugin and npd plugin
1 parent 0802c8a commit d86d580

File tree

22 files changed

+2005
-115
lines changed

22 files changed

+2005
-115
lines changed

cmd/katalyst-controller/app/options/npd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ limitations under the License.
1717
package options
1818

1919
import (
20-
cliflag "k8s.io/component-base/cli/flag"
2120
"time"
2221

22+
cliflag "k8s.io/component-base/cli/flag"
23+
2324
"github.com/kubewharf/katalyst-core/pkg/config/controller"
2425
)
2526

cmd/katalyst-scheduler/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ limitations under the License.
1717
package main
1818

1919
import (
20-
"github.com/kubewharf/katalyst-core/pkg/scheduler/plugins/loadaware"
2120
"os"
2221

2322
"github.com/spf13/cobra"
2423
"k8s.io/component-base/logs"
2524

2625
"github.com/kubewharf/katalyst-core/cmd/katalyst-scheduler/app"
26+
"github.com/kubewharf/katalyst-core/pkg/scheduler/plugins/loadaware"
2727
"github.com/kubewharf/katalyst-core/pkg/scheduler/plugins/nodeovercommitment"
2828
"github.com/kubewharf/katalyst-core/pkg/scheduler/plugins/noderesourcetopology"
2929
"github.com/kubewharf/katalyst-core/pkg/scheduler/plugins/qosawarenoderesources"

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ require (
161161
)
162162

163163
replace (
164-
github.com/kubewharf/katalyst-api => github.com/WangZzzhe/katalyst-api v0.0.0-20240626083651-4a90fe53af11
164+
github.com/kubewharf/katalyst-api => github.com/WangZzzhe/katalyst-api v0.0.0-20240719035252-ac200da4db6c
165165
k8s.io/api => k8s.io/api v0.24.6
166166
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.24.6
167167
k8s.io/apimachinery => k8s.io/apimachinery v0.24.6

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWX
8484
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
8585
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
8686
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
87-
github.com/WangZzzhe/katalyst-api v0.0.0-20240626083651-4a90fe53af11 h1:4RUG7QfX0hBwtHtI3Nll6F4lCP31ThYxkWIu93G6Ei4=
88-
github.com/WangZzzhe/katalyst-api v0.0.0-20240626083651-4a90fe53af11/go.mod h1:Y2IeIorxQamF2a3oa0+URztl5QCSty6Jj3zD83R8J9k=
87+
github.com/WangZzzhe/katalyst-api v0.0.0-20240719035252-ac200da4db6c h1:/0fwVknrQEJoRKnT2H0f5xkzCdcDIH4qfNvpPn7QoH8=
88+
github.com/WangZzzhe/katalyst-api v0.0.0-20240719035252-ac200da4db6c/go.mod h1:HHUJnOrDN5xrzKhEspq70ZJL859b09j07pMAl9ACnwU=
8989
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
9090
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
9191
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=

pkg/controller/npd/indicator-plugin/loadaware/handler.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ func (p *Plugin) OnPodAdd(obj interface{}) {
7272
p.Lock()
7373
defer p.Unlock()
7474
if p.podUsageSelectorKey != "" {
75-
if value, exist := pod.Labels[p.podUsageSelectorKey]; exist && value == p.podUsageSelectorVal {
75+
if value, exist := pod.Labels[p.podUsageSelectorKey]; exist &&
76+
value == p.podUsageSelectorVal &&
77+
p.podUsageSelectorNamespace == pod.Namespace {
7678
klog.Info("start sync pod usage to nodeMonitor")
7779
p.enableSyncPodUsage = true
7880
}
@@ -143,7 +145,9 @@ func (p *Plugin) OnPodDelete(obj interface{}) {
143145
p.Lock()
144146
defer p.Unlock()
145147
if p.podUsageSelectorVal != "" {
146-
if value, exist := pod.Labels[p.podUsageSelectorKey]; exist && value == p.podUsageSelectorVal {
148+
if value, exist := pod.Labels[p.podUsageSelectorKey]; exist &&
149+
value == p.podUsageSelectorVal &&
150+
p.podUsageSelectorNamespace == pod.Namespace {
147151
klog.Info("stop sync pod usage to nodeMonitor")
148152
p.enableSyncPodUsage = false
149153
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package loadaware
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
v1 "k8s.io/api/core/v1"
8+
"k8s.io/apimachinery/pkg/api/resource"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/apimachinery/pkg/util/sets"
11+
)
12+
13+
func TestOnNodeAdd(t *testing.T) {
14+
t.Parallel()
15+
16+
p := &Plugin{
17+
workers: 3,
18+
nodePoolMap: map[int32]sets.String{},
19+
nodeStatDataMap: map[string]*NodeMetricData{},
20+
}
21+
22+
testNode1 := &v1.Node{
23+
ObjectMeta: metav1.ObjectMeta{
24+
Name: "testNode1",
25+
},
26+
Status: v1.NodeStatus{
27+
Allocatable: map[v1.ResourceName]resource.Quantity{
28+
v1.ResourceCPU: resource.MustParse("16"),
29+
v1.ResourceMemory: resource.MustParse("32Gi"),
30+
},
31+
},
32+
}
33+
34+
p.OnNodeAdd(testNode1)
35+
assert.NotNil(t, p.nodeStatDataMap["testNode1"])
36+
assert.Equal(t, 2, len(p.nodeStatDataMap["testNode1"].TotalRes))
37+
38+
p.OnNodeDelete(testNode1)
39+
assert.Nil(t, p.nodeStatDataMap["testNode1"])
40+
}
41+
42+
func TestOnNodeUpdate(t *testing.T) {
43+
t.Parallel()
44+
45+
p := &Plugin{
46+
nodeStatDataMap: map[string]*NodeMetricData{},
47+
}
48+
49+
testNode1 := &v1.Node{
50+
ObjectMeta: metav1.ObjectMeta{
51+
Name: "testNode1",
52+
},
53+
Status: v1.NodeStatus{
54+
Allocatable: map[v1.ResourceName]resource.Quantity{
55+
v1.ResourceCPU: resource.MustParse("16"),
56+
v1.ResourceMemory: resource.MustParse("32Gi"),
57+
},
58+
},
59+
}
60+
61+
p.OnNodeUpdate(nil, testNode1)
62+
assert.NotNil(t, p.nodeStatDataMap["testNode1"])
63+
assert.Equal(t, 2, len(p.nodeStatDataMap["testNode1"].TotalRes))
64+
}
65+
66+
func TestOnPodAdd(t *testing.T) {
67+
t.Parallel()
68+
69+
p := &Plugin{
70+
nodeToPodsMap: map[string]map[string]struct{}{},
71+
podUsageSelectorKey: "app",
72+
podUsageSelectorVal: "testPod",
73+
podUsageSelectorNamespace: "katalyst-system",
74+
}
75+
76+
testPod1 := &v1.Pod{
77+
ObjectMeta: metav1.ObjectMeta{
78+
Name: "testPod1",
79+
Namespace: "katalyst-system",
80+
Labels: map[string]string{
81+
"app": "testPod",
82+
},
83+
},
84+
Spec: v1.PodSpec{
85+
NodeName: "testNode1",
86+
},
87+
}
88+
89+
p.OnPodAdd(testPod1)
90+
assert.NotNil(t, p.nodeToPodsMap["testNode1"])
91+
assert.Equal(t, 1, len(p.nodeToPodsMap["testNode1"]))
92+
93+
p.OnPodDelete(testPod1)
94+
assert.Equal(t, 0, len(p.nodeToPodsMap["testNode1"]))
95+
96+
p.OnPodDelete("")
97+
}
98+
99+
func TestOnPodUpdate(t *testing.T) {
100+
t.Parallel()
101+
102+
p := &Plugin{
103+
nodeToPodsMap: map[string]map[string]struct{}{},
104+
podUsageSelectorKey: "app",
105+
podUsageSelectorVal: "testPod",
106+
podUsageSelectorNamespace: "katalyst-system",
107+
}
108+
109+
testPod1 := &v1.Pod{
110+
ObjectMeta: metav1.ObjectMeta{
111+
Name: "testPod1",
112+
Namespace: "katalyst-system",
113+
Labels: map[string]string{
114+
"app": "testPod",
115+
},
116+
},
117+
Spec: v1.PodSpec{
118+
NodeName: "testNode1",
119+
},
120+
}
121+
122+
p.OnPodUpdate(nil, testPod1)
123+
assert.NotNil(t, p.nodeToPodsMap["testNode1"])
124+
assert.Equal(t, 1, len(p.nodeToPodsMap["testNode1"]))
125+
126+
p.OnPodUpdate(nil, "")
127+
}

pkg/controller/npd/indicator-plugin/loadaware/helper.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package loadaware
22

33
import (
4-
"github.com/kubewharf/katalyst-core/pkg/controller/npd/indicator-plugin/loadaware/sorter"
4+
"time"
5+
56
corev1 "k8s.io/api/core/v1"
67
"k8s.io/apimachinery/pkg/api/resource"
78
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
89
quotav1 "k8s.io/apiserver/pkg/quota/v1"
910
"k8s.io/metrics/pkg/apis/metrics/v1beta1"
10-
"time"
11+
12+
"github.com/kubewharf/katalyst-core/pkg/controller/npd/indicator-plugin/loadaware/sorter"
1113
)
1214

1315
// getUsage transfer cpu Nano to Milli, memory Ki to Mega
@@ -92,7 +94,7 @@ func refreshNodeMetricData(metricData *NodeMetricData, metricInfo *v1beta1.NodeM
9294
max1Hour := calCPUAndMemoryMax(metricData.Latest1HourCache)
9395
metricData.Max1Hour = max1Hour.DeepCopy()
9496

95-
//calculate 1 day max data
97+
// calculate 1 day max data
9698
if metricData.ifCanInsertLatest1DayCache(now) {
9799
resWithTime := &ResourceListWithTime{
98100
ResourceList: max1Hour.DeepCopy(),
@@ -115,7 +117,7 @@ func refreshPodMetricData(metricData *PodMetricData, metricInfo *v1beta1.PodMetr
115117
podUsage = quotav1.Add(podUsage, containerMetrics.Usage)
116118
}
117119
metricData.LatestUsage = podUsage.DeepCopy()
118-
//calculate 5 min avg data
120+
// calculate 5 min avg data
119121
metricData.Latest5MinCache = append(metricData.Latest5MinCache, getUsage(podUsage))
120122
if len(metricData.Latest5MinCache) > Avg5MinPointNumber {
121123
metricData.Latest5MinCache = metricData.Latest5MinCache[len(metricData.Latest5MinCache)-Avg5MinPointNumber:]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package loadaware
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
corev1 "k8s.io/api/core/v1"
9+
"k8s.io/apimachinery/pkg/api/resource"
10+
)
11+
12+
func TestGetTopNPodUsages(t *testing.T) {
13+
podRealUsage := map[string]corev1.ResourceList{
14+
"default/test-1": {
15+
corev1.ResourceCPU: resource.MustParse("80"),
16+
corev1.ResourceMemory: resource.MustParse("10Gi"),
17+
},
18+
"default/test-2": {
19+
corev1.ResourceCPU: resource.MustParse("30"),
20+
corev1.ResourceMemory: resource.MustParse("10Gi"),
21+
},
22+
"default/test-3": {
23+
corev1.ResourceCPU: resource.MustParse("50"),
24+
corev1.ResourceMemory: resource.MustParse("10Gi"),
25+
},
26+
"default/test-4": {
27+
corev1.ResourceCPU: resource.MustParse("70"),
28+
corev1.ResourceMemory: resource.MustParse("10Gi"),
29+
},
30+
"default/test-5": {
31+
corev1.ResourceCPU: resource.MustParse("10"),
32+
corev1.ResourceMemory: resource.MustParse("10Gi"),
33+
},
34+
"default/test-6": {
35+
corev1.ResourceCPU: resource.MustParse("40"),
36+
corev1.ResourceMemory: resource.MustParse("10Gi"),
37+
},
38+
"default/test-7": {
39+
corev1.ResourceCPU: resource.MustParse("60"),
40+
corev1.ResourceMemory: resource.MustParse("10Gi"),
41+
},
42+
}
43+
resultMap := getTopNPodUsages(podRealUsage, 3)
44+
expected := []string{"default/test-1", "default/test-4", "default/test-7"}
45+
assert.Equal(t, len(resultMap), 3)
46+
for _, v := range expected {
47+
if _, ok := resultMap[v]; !ok {
48+
t.Error(fmt.Errorf("not exit"))
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)