Skip to content

Commit 4375250

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

File tree

27 files changed

+2373
-141
lines changed

27 files changed

+2373
-141
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: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2022 The Katalyst Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package loadaware
218

319
import (
@@ -72,7 +88,9 @@ func (p *Plugin) OnPodAdd(obj interface{}) {
7288
p.Lock()
7389
defer p.Unlock()
7490
if p.podUsageSelectorKey != "" {
75-
if value, exist := pod.Labels[p.podUsageSelectorKey]; exist && value == p.podUsageSelectorVal {
91+
if value, exist := pod.Labels[p.podUsageSelectorKey]; exist &&
92+
value == p.podUsageSelectorVal &&
93+
p.podUsageSelectorNamespace == pod.Namespace {
7694
klog.Info("start sync pod usage to nodeMonitor")
7795
p.enableSyncPodUsage = true
7896
}
@@ -143,7 +161,9 @@ func (p *Plugin) OnPodDelete(obj interface{}) {
143161
p.Lock()
144162
defer p.Unlock()
145163
if p.podUsageSelectorVal != "" {
146-
if value, exist := pod.Labels[p.podUsageSelectorKey]; exist && value == p.podUsageSelectorVal {
164+
if value, exist := pod.Labels[p.podUsageSelectorKey]; exist &&
165+
value == p.podUsageSelectorVal &&
166+
p.podUsageSelectorNamespace == pod.Namespace {
147167
klog.Info("stop sync pod usage to nodeMonitor")
148168
p.enableSyncPodUsage = false
149169
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
Copyright 2022 The Katalyst Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package loadaware
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
v1 "k8s.io/api/core/v1"
24+
"k8s.io/apimachinery/pkg/api/resource"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
"k8s.io/apimachinery/pkg/util/sets"
27+
)
28+
29+
func TestOnNodeAdd(t *testing.T) {
30+
t.Parallel()
31+
32+
p := &Plugin{
33+
workers: 3,
34+
nodePoolMap: map[int32]sets.String{},
35+
nodeStatDataMap: map[string]*NodeMetricData{},
36+
}
37+
38+
testNode1 := &v1.Node{
39+
ObjectMeta: metav1.ObjectMeta{
40+
Name: "testNode1",
41+
},
42+
Status: v1.NodeStatus{
43+
Allocatable: map[v1.ResourceName]resource.Quantity{
44+
v1.ResourceCPU: resource.MustParse("16"),
45+
v1.ResourceMemory: resource.MustParse("32Gi"),
46+
},
47+
},
48+
}
49+
50+
p.OnNodeAdd(testNode1)
51+
assert.NotNil(t, p.nodeStatDataMap["testNode1"])
52+
assert.Equal(t, 2, len(p.nodeStatDataMap["testNode1"].TotalRes))
53+
54+
p.OnNodeDelete(testNode1)
55+
assert.Nil(t, p.nodeStatDataMap["testNode1"])
56+
}
57+
58+
func TestOnNodeUpdate(t *testing.T) {
59+
t.Parallel()
60+
61+
p := &Plugin{
62+
nodeStatDataMap: map[string]*NodeMetricData{},
63+
}
64+
65+
testNode1 := &v1.Node{
66+
ObjectMeta: metav1.ObjectMeta{
67+
Name: "testNode1",
68+
},
69+
Status: v1.NodeStatus{
70+
Allocatable: map[v1.ResourceName]resource.Quantity{
71+
v1.ResourceCPU: resource.MustParse("16"),
72+
v1.ResourceMemory: resource.MustParse("32Gi"),
73+
},
74+
},
75+
}
76+
77+
p.OnNodeUpdate(nil, testNode1)
78+
assert.NotNil(t, p.nodeStatDataMap["testNode1"])
79+
assert.Equal(t, 2, len(p.nodeStatDataMap["testNode1"].TotalRes))
80+
}
81+
82+
func TestOnPodAdd(t *testing.T) {
83+
t.Parallel()
84+
85+
p := &Plugin{
86+
nodeToPodsMap: map[string]map[string]struct{}{},
87+
podUsageSelectorKey: "app",
88+
podUsageSelectorVal: "testPod",
89+
podUsageSelectorNamespace: "katalyst-system",
90+
}
91+
92+
testPod1 := &v1.Pod{
93+
ObjectMeta: metav1.ObjectMeta{
94+
Name: "testPod1",
95+
Namespace: "katalyst-system",
96+
Labels: map[string]string{
97+
"app": "testPod",
98+
},
99+
},
100+
Spec: v1.PodSpec{
101+
NodeName: "testNode1",
102+
},
103+
}
104+
105+
p.OnPodAdd(testPod1)
106+
assert.NotNil(t, p.nodeToPodsMap["testNode1"])
107+
assert.Equal(t, 1, len(p.nodeToPodsMap["testNode1"]))
108+
109+
p.OnPodDelete(testPod1)
110+
assert.Equal(t, 0, len(p.nodeToPodsMap["testNode1"]))
111+
112+
p.OnPodDelete("")
113+
}
114+
115+
func TestOnPodUpdate(t *testing.T) {
116+
t.Parallel()
117+
118+
p := &Plugin{
119+
nodeToPodsMap: map[string]map[string]struct{}{},
120+
podUsageSelectorKey: "app",
121+
podUsageSelectorVal: "testPod",
122+
podUsageSelectorNamespace: "katalyst-system",
123+
}
124+
125+
testPod1 := &v1.Pod{
126+
ObjectMeta: metav1.ObjectMeta{
127+
Name: "testPod1",
128+
Namespace: "katalyst-system",
129+
Labels: map[string]string{
130+
"app": "testPod",
131+
},
132+
},
133+
Spec: v1.PodSpec{
134+
NodeName: "testNode1",
135+
},
136+
}
137+
138+
p.OnPodUpdate(nil, testPod1)
139+
assert.NotNil(t, p.nodeToPodsMap["testNode1"])
140+
assert.Equal(t, 1, len(p.nodeToPodsMap["testNode1"]))
141+
142+
p.OnPodUpdate(nil, "")
143+
}

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

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
1+
/*
2+
Copyright 2022 The Katalyst Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package loadaware
218

319
import (
4-
"github.com/kubewharf/katalyst-core/pkg/controller/npd/indicator-plugin/loadaware/sorter"
20+
"time"
21+
522
corev1 "k8s.io/api/core/v1"
623
"k8s.io/apimachinery/pkg/api/resource"
724
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
825
quotav1 "k8s.io/apiserver/pkg/quota/v1"
926
"k8s.io/metrics/pkg/apis/metrics/v1beta1"
10-
"time"
27+
28+
"github.com/kubewharf/katalyst-core/pkg/controller/npd/indicator-plugin/loadaware/sorter"
1129
)
1230

1331
// getUsage transfer cpu Nano to Milli, memory Ki to Mega
@@ -92,7 +110,7 @@ func refreshNodeMetricData(metricData *NodeMetricData, metricInfo *v1beta1.NodeM
92110
max1Hour := calCPUAndMemoryMax(metricData.Latest1HourCache)
93111
metricData.Max1Hour = max1Hour.DeepCopy()
94112

95-
//calculate 1 day max data
113+
// calculate 1 day max data
96114
if metricData.ifCanInsertLatest1DayCache(now) {
97115
resWithTime := &ResourceListWithTime{
98116
ResourceList: max1Hour.DeepCopy(),
@@ -115,7 +133,7 @@ func refreshPodMetricData(metricData *PodMetricData, metricInfo *v1beta1.PodMetr
115133
podUsage = quotav1.Add(podUsage, containerMetrics.Usage)
116134
}
117135
metricData.LatestUsage = podUsage.DeepCopy()
118-
//calculate 5 min avg data
136+
// calculate 5 min avg data
119137
metricData.Latest5MinCache = append(metricData.Latest5MinCache, getUsage(podUsage))
120138
if len(metricData.Latest5MinCache) > Avg5MinPointNumber {
121139
metricData.Latest5MinCache = metricData.Latest5MinCache[len(metricData.Latest5MinCache)-Avg5MinPointNumber:]
@@ -153,25 +171,3 @@ func getTopNPodUsages(podUsages map[string]corev1.ResourceList, maxPodUsageCount
153171
}
154172
return topNPodUsages
155173
}
156-
157-
func calNodeLoad(resourceName corev1.ResourceName, usage, totalRes corev1.ResourceList) int64 {
158-
if usage == nil || totalRes == nil {
159-
return 0
160-
}
161-
used := int64(0)
162-
total := int64(0)
163-
if resourceName == corev1.ResourceCPU {
164-
used = usage.Cpu().MilliValue()
165-
total = totalRes.Cpu().MilliValue()
166-
} else {
167-
used = usage.Memory().Value()
168-
total = totalRes.Memory().Value()
169-
}
170-
if total == 0 {
171-
return 0
172-
}
173-
if used >= total {
174-
return 99
175-
}
176-
return used * 100 / total
177-
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Copyright 2022 The Katalyst Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package loadaware
18+
19+
import (
20+
"fmt"
21+
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
corev1 "k8s.io/api/core/v1"
25+
"k8s.io/apimachinery/pkg/api/resource"
26+
)
27+
28+
func TestGetTopNPodUsages(t *testing.T) {
29+
t.Parallel()
30+
podRealUsage := map[string]corev1.ResourceList{
31+
"default/test-1": {
32+
corev1.ResourceCPU: resource.MustParse("80"),
33+
corev1.ResourceMemory: resource.MustParse("10Gi"),
34+
},
35+
"default/test-2": {
36+
corev1.ResourceCPU: resource.MustParse("30"),
37+
corev1.ResourceMemory: resource.MustParse("10Gi"),
38+
},
39+
"default/test-3": {
40+
corev1.ResourceCPU: resource.MustParse("50"),
41+
corev1.ResourceMemory: resource.MustParse("10Gi"),
42+
},
43+
"default/test-4": {
44+
corev1.ResourceCPU: resource.MustParse("70"),
45+
corev1.ResourceMemory: resource.MustParse("10Gi"),
46+
},
47+
"default/test-5": {
48+
corev1.ResourceCPU: resource.MustParse("10"),
49+
corev1.ResourceMemory: resource.MustParse("10Gi"),
50+
},
51+
"default/test-6": {
52+
corev1.ResourceCPU: resource.MustParse("40"),
53+
corev1.ResourceMemory: resource.MustParse("10Gi"),
54+
},
55+
"default/test-7": {
56+
corev1.ResourceCPU: resource.MustParse("60"),
57+
corev1.ResourceMemory: resource.MustParse("10Gi"),
58+
},
59+
}
60+
resultMap := getTopNPodUsages(podRealUsage, 3)
61+
expected := []string{"default/test-1", "default/test-4", "default/test-7"}
62+
assert.Equal(t, len(resultMap), 3)
63+
for _, v := range expected {
64+
if _, ok := resultMap[v]; !ok {
65+
t.Error(fmt.Errorf("not exit"))
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)