Skip to content

Commit 7f0c05b

Browse files
authored
Merge pull request kubernetes#90544 from chendave/weight
configurable weight on the CPU and memory
2 parents 8de8d25 + 621c73b commit 7f0c05b

File tree

15 files changed

+581
-28
lines changed

15 files changed

+581
-28
lines changed

pkg/scheduler/apis/config/register.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func addKnownTypes(scheme *runtime.Scheme) error {
4646
&RequestedToCapacityRatioArgs{},
4747
&ServiceAffinityArgs{},
4848
&VolumeBindingArgs{},
49+
&NodeResourcesLeastAllocatedArgs{},
50+
&NodeResourcesMostAllocatedArgs{},
4951
)
5052
scheme.AddKnownTypes(schema.GroupVersion{Group: "", Version: runtime.APIVersionInternal}, &Policy{})
5153
return nil

pkg/scheduler/apis/config/scheme/scheme_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ profiles:
6666
- name: ServiceAffinity
6767
args:
6868
affinityLabels: ["bar"]
69+
- name: NodeResourcesLeastAllocated
70+
args:
71+
resources:
72+
- name: cpu
73+
weight: 2
74+
- name: unknown
75+
weight: 1
76+
- name: NodeResourcesMostAllocated
77+
args:
78+
resources:
79+
- name: memory
80+
weight: 1
6981
`),
7082
wantProfiles: []config.KubeSchedulerProfile{
7183
{
@@ -103,6 +115,18 @@ profiles:
103115
AffinityLabels: []string{"bar"},
104116
},
105117
},
118+
{
119+
Name: "NodeResourcesLeastAllocated",
120+
Args: &config.NodeResourcesLeastAllocatedArgs{
121+
Resources: []config.ResourceSpec{{Name: "cpu", Weight: 2}, {Name: "unknown", Weight: 1}},
122+
},
123+
},
124+
{
125+
Name: "NodeResourcesMostAllocated",
126+
Args: &config.NodeResourcesMostAllocatedArgs{
127+
Resources: []config.ResourceSpec{{Name: "memory", Weight: 1}},
128+
},
129+
},
106130
},
107131
},
108132
},
@@ -226,6 +250,10 @@ profiles:
226250
- name: NodeResourcesFit
227251
- name: OutOfTreePlugin
228252
args:
253+
- name: NodeResourcesLeastAllocated
254+
args:
255+
- name: NodeResourcesMostAllocated
256+
args:
229257
`),
230258
wantProfiles: []config.KubeSchedulerProfile{
231259
{
@@ -242,6 +270,18 @@ profiles:
242270
Args: &config.NodeResourcesFitArgs{},
243271
},
244272
{Name: "OutOfTreePlugin"},
273+
{
274+
Name: "NodeResourcesLeastAllocated",
275+
Args: &config.NodeResourcesLeastAllocatedArgs{
276+
Resources: []config.ResourceSpec{{Name: "cpu", Weight: 1}, {Name: "memory", Weight: 1}},
277+
},
278+
},
279+
{
280+
Name: "NodeResourcesMostAllocated",
281+
Args: &config.NodeResourcesMostAllocatedArgs{
282+
Resources: []config.ResourceSpec{{Name: "cpu", Weight: 1}, {Name: "memory", Weight: 1}},
283+
},
284+
},
245285
},
246286
},
247287
},
@@ -306,6 +346,16 @@ func TestCodecsEncodePluginConfig(t *testing.T) {
306346
},
307347
},
308348
},
349+
{
350+
Name: "NodeResourcesLeastAllocated",
351+
Args: runtime.RawExtension{
352+
Object: &v1alpha2.NodeResourcesLeastAllocatedArgs{
353+
Resources: []v1alpha2.ResourceSpec{
354+
{Name: "mem", Weight: 2},
355+
},
356+
},
357+
},
358+
},
309359
{
310360
Name: "OutOfTreePlugin",
311361
Args: runtime.RawExtension{
@@ -349,6 +399,13 @@ profiles:
349399
- Score: 2
350400
Utilization: 1
351401
name: RequestedToCapacityRatio
402+
- args:
403+
apiVersion: kubescheduler.config.k8s.io/v1alpha2
404+
kind: NodeResourcesLeastAllocatedArgs
405+
resources:
406+
- Name: mem
407+
Weight: 2
408+
name: NodeResourcesLeastAllocated
352409
- args:
353410
foo: bar
354411
name: OutOfTreePlugin
@@ -367,6 +424,12 @@ profiles:
367424
HardPodAffinityWeight: 5,
368425
},
369426
},
427+
{
428+
Name: "NodeResourcesMostAllocated",
429+
Args: &config.NodeResourcesMostAllocatedArgs{
430+
Resources: []config.ResourceSpec{{Name: "cpu", Weight: 1}},
431+
},
432+
},
370433
{
371434
Name: "OutOfTreePlugin",
372435
Args: &runtime.Unknown{
@@ -409,6 +472,13 @@ profiles:
409472
hardPodAffinityWeight: 5
410473
kind: InterPodAffinityArgs
411474
name: InterPodAffinity
475+
- args:
476+
apiVersion: kubescheduler.config.k8s.io/v1alpha2
477+
kind: NodeResourcesMostAllocatedArgs
478+
resources:
479+
- Name: cpu
480+
Weight: 1
481+
name: NodeResourcesMostAllocated
412482
- args:
413483
foo: bar
414484
name: OutOfTreePlugin

pkg/scheduler/apis/config/types_pluginargs.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,33 @@ type RequestedToCapacityRatioArgs struct {
8282

8383
// Points defining priority function shape
8484
Shape []UtilizationShapePoint
85-
// Resources to be managed
85+
// Resources to be considered when scoring.
86+
// The default resource set includes "cpu" and "memory" with an equal weight.
87+
// Allowed weights go from 1 to 100.
88+
Resources []ResourceSpec
89+
}
90+
91+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
92+
93+
// NodeResourcesLeastAllocatedArgs holds arguments used to configure NodeResourcesLeastAllocated plugin.
94+
type NodeResourcesLeastAllocatedArgs struct {
95+
metav1.TypeMeta
96+
97+
// Resources to be considered when scoring.
98+
// The default resource set includes "cpu" and "memory" with an equal weight.
99+
// Allowed weights go from 1 to 100.
100+
Resources []ResourceSpec
101+
}
102+
103+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
104+
105+
// NodeResourcesMostAllocatedArgs holds arguments used to configure NodeResourcesMostAllocated plugin.
106+
type NodeResourcesMostAllocatedArgs struct {
107+
metav1.TypeMeta
108+
109+
// Resources to be considered when scoring.
110+
// The default resource set includes "cpu" and "memory" with an equal weight.
111+
// Allowed weights go from 1 to 100.
86112
Resources []ResourceSpec
87113
}
88114

@@ -94,9 +120,9 @@ type UtilizationShapePoint struct {
94120
Score int32
95121
}
96122

97-
// ResourceSpec represents single resource for bin packing of priority RequestedToCapacityRatioArgs.
123+
// ResourceSpec represents single resource.
98124
type ResourceSpec struct {
99-
// Name of the resource to be managed by RequestedToCapacityRatio function.
125+
// Name of the resource.
100126
Name string
101127
// Weight of the resource.
102128
Weight int64

pkg/scheduler/apis/config/v1alpha2/defaults.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"net"
2121
"strconv"
2222

23+
v1 "k8s.io/api/core/v1"
2324
"k8s.io/apimachinery/pkg/runtime"
2425
componentbaseconfigv1alpha1 "k8s.io/component-base/config/v1alpha1"
2526
"k8s.io/kube-scheduler/config/v1alpha2"
@@ -30,6 +31,11 @@ import (
3031
api "k8s.io/kubernetes/pkg/apis/core"
3132
)
3233

34+
var defaultResourceSpec = []v1alpha2.ResourceSpec{
35+
{Name: string(v1.ResourceCPU), Weight: 1},
36+
{Name: string(v1.ResourceMemory), Weight: 1},
37+
}
38+
3339
func addDefaultingFuncs(scheme *runtime.Scheme) error {
3440
return RegisterDefaults(scheme)
3541
}
@@ -165,3 +171,17 @@ func SetDefaults_InterPodAffinityArgs(obj *v1alpha2.InterPodAffinityArgs) {
165171
obj.HardPodAffinityWeight = pointer.Int32Ptr(1)
166172
}
167173
}
174+
175+
func SetDefaults_NodeResourcesLeastAllocatedArgs(obj *v1alpha2.NodeResourcesLeastAllocatedArgs) {
176+
if len(obj.Resources) == 0 {
177+
// If no resources specified, used the default set.
178+
obj.Resources = append(obj.Resources, defaultResourceSpec...)
179+
}
180+
}
181+
182+
func SetDefaults_NodeResourcesMostAllocatedArgs(obj *v1alpha2.NodeResourcesMostAllocatedArgs) {
183+
if len(obj.Resources) == 0 {
184+
// If no resources specified, used the default set.
185+
obj.Resources = append(obj.Resources, defaultResourceSpec...)
186+
}
187+
}

pkg/scheduler/apis/config/v1alpha2/defaults_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,52 @@ func TestPluginArgsDefaults(t *testing.T) {
299299
HardPodAffinityWeight: pointer.Int32Ptr(5),
300300
},
301301
},
302+
{
303+
name: "NodeResourcesLeastAllocatedArgs resources empty",
304+
in: &v1alpha2.NodeResourcesLeastAllocatedArgs{},
305+
want: &v1alpha2.NodeResourcesLeastAllocatedArgs{
306+
Resources: []v1alpha2.ResourceSpec{
307+
{Name: "cpu", Weight: 1},
308+
{Name: "memory", Weight: 1},
309+
},
310+
},
311+
},
312+
{
313+
name: "NodeResourcesLeastAllocatedArgs resources with value",
314+
in: &v1alpha2.NodeResourcesLeastAllocatedArgs{
315+
Resources: []v1alpha2.ResourceSpec{
316+
{Name: "resource", Weight: 2},
317+
},
318+
},
319+
want: &v1alpha2.NodeResourcesLeastAllocatedArgs{
320+
Resources: []v1alpha2.ResourceSpec{
321+
{Name: "resource", Weight: 2},
322+
},
323+
},
324+
},
325+
{
326+
name: "NodeResourcesMostAllocatedArgs resources empty",
327+
in: &v1alpha2.NodeResourcesMostAllocatedArgs{},
328+
want: &v1alpha2.NodeResourcesMostAllocatedArgs{
329+
Resources: []v1alpha2.ResourceSpec{
330+
{Name: "cpu", Weight: 1},
331+
{Name: "memory", Weight: 1},
332+
},
333+
},
334+
},
335+
{
336+
name: "NodeResourcesMostAllocatedArgs resources with value",
337+
in: &v1alpha2.NodeResourcesMostAllocatedArgs{
338+
Resources: []v1alpha2.ResourceSpec{
339+
{Name: "resource", Weight: 2},
340+
},
341+
},
342+
want: &v1alpha2.NodeResourcesMostAllocatedArgs{
343+
Resources: []v1alpha2.ResourceSpec{
344+
{Name: "resource", Weight: 2},
345+
},
346+
},
347+
},
302348
}
303349
for _, tc := range tests {
304350
scheme := runtime.NewScheme()

pkg/scheduler/apis/config/v1alpha2/zz_generated.conversion.go

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/scheduler/apis/config/v1alpha2/zz_generated.defaults.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)