Configurable weight on the CPU and memory

This change also make it possible to score the resources beyond the "cpu"
and "memory" which is currently listed in "defaultRequestedRatioResources".

Signed-off-by: Dave Chen <dave.chen@arm.com>
This commit is contained in:
Dave Chen 2020-04-28 15:47:03 +08:00
parent c096a37226
commit 621c73b984
15 changed files with 581 additions and 28 deletions

View File

@ -46,6 +46,8 @@ func addKnownTypes(scheme *runtime.Scheme) error {
&RequestedToCapacityRatioArgs{},
&ServiceAffinityArgs{},
&VolumeBindingArgs{},
&NodeResourcesLeastAllocatedArgs{},
&NodeResourcesMostAllocatedArgs{},
)
scheme.AddKnownTypes(schema.GroupVersion{Group: "", Version: runtime.APIVersionInternal}, &Policy{})
return nil

View File

@ -66,6 +66,18 @@ profiles:
- name: ServiceAffinity
args:
affinityLabels: ["bar"]
- name: NodeResourcesLeastAllocated
args:
resources:
- name: cpu
weight: 2
- name: unknown
weight: 1
- name: NodeResourcesMostAllocated
args:
resources:
- name: memory
weight: 1
`),
wantProfiles: []config.KubeSchedulerProfile{
{
@ -103,6 +115,18 @@ profiles:
AffinityLabels: []string{"bar"},
},
},
{
Name: "NodeResourcesLeastAllocated",
Args: &config.NodeResourcesLeastAllocatedArgs{
Resources: []config.ResourceSpec{{Name: "cpu", Weight: 2}, {Name: "unknown", Weight: 1}},
},
},
{
Name: "NodeResourcesMostAllocated",
Args: &config.NodeResourcesMostAllocatedArgs{
Resources: []config.ResourceSpec{{Name: "memory", Weight: 1}},
},
},
},
},
},
@ -226,6 +250,10 @@ profiles:
- name: NodeResourcesFit
- name: OutOfTreePlugin
args:
- name: NodeResourcesLeastAllocated
args:
- name: NodeResourcesMostAllocated
args:
`),
wantProfiles: []config.KubeSchedulerProfile{
{
@ -242,6 +270,18 @@ profiles:
Args: &config.NodeResourcesFitArgs{},
},
{Name: "OutOfTreePlugin"},
{
Name: "NodeResourcesLeastAllocated",
Args: &config.NodeResourcesLeastAllocatedArgs{
Resources: []config.ResourceSpec{{Name: "cpu", Weight: 1}, {Name: "memory", Weight: 1}},
},
},
{
Name: "NodeResourcesMostAllocated",
Args: &config.NodeResourcesMostAllocatedArgs{
Resources: []config.ResourceSpec{{Name: "cpu", Weight: 1}, {Name: "memory", Weight: 1}},
},
},
},
},
},
@ -306,6 +346,16 @@ func TestCodecsEncodePluginConfig(t *testing.T) {
},
},
},
{
Name: "NodeResourcesLeastAllocated",
Args: runtime.RawExtension{
Object: &v1alpha2.NodeResourcesLeastAllocatedArgs{
Resources: []v1alpha2.ResourceSpec{
{Name: "mem", Weight: 2},
},
},
},
},
{
Name: "OutOfTreePlugin",
Args: runtime.RawExtension{
@ -349,6 +399,13 @@ profiles:
- Score: 2
Utilization: 1
name: RequestedToCapacityRatio
- args:
apiVersion: kubescheduler.config.k8s.io/v1alpha2
kind: NodeResourcesLeastAllocatedArgs
resources:
- Name: mem
Weight: 2
name: NodeResourcesLeastAllocated
- args:
foo: bar
name: OutOfTreePlugin
@ -367,6 +424,12 @@ profiles:
HardPodAffinityWeight: 5,
},
},
{
Name: "NodeResourcesMostAllocated",
Args: &config.NodeResourcesMostAllocatedArgs{
Resources: []config.ResourceSpec{{Name: "cpu", Weight: 1}},
},
},
{
Name: "OutOfTreePlugin",
Args: &runtime.Unknown{
@ -409,6 +472,13 @@ profiles:
hardPodAffinityWeight: 5
kind: InterPodAffinityArgs
name: InterPodAffinity
- args:
apiVersion: kubescheduler.config.k8s.io/v1alpha2
kind: NodeResourcesMostAllocatedArgs
resources:
- Name: cpu
Weight: 1
name: NodeResourcesMostAllocated
- args:
foo: bar
name: OutOfTreePlugin

View File

@ -82,7 +82,33 @@ type RequestedToCapacityRatioArgs struct {
// Points defining priority function shape
Shape []UtilizationShapePoint
// Resources to be managed
// Resources to be considered when scoring.
// The default resource set includes "cpu" and "memory" with an equal weight.
// Allowed weights go from 1 to 100.
Resources []ResourceSpec
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// NodeResourcesLeastAllocatedArgs holds arguments used to configure NodeResourcesLeastAllocated plugin.
type NodeResourcesLeastAllocatedArgs struct {
metav1.TypeMeta
// Resources to be considered when scoring.
// The default resource set includes "cpu" and "memory" with an equal weight.
// Allowed weights go from 1 to 100.
Resources []ResourceSpec
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// NodeResourcesMostAllocatedArgs holds arguments used to configure NodeResourcesMostAllocated plugin.
type NodeResourcesMostAllocatedArgs struct {
metav1.TypeMeta
// Resources to be considered when scoring.
// The default resource set includes "cpu" and "memory" with an equal weight.
// Allowed weights go from 1 to 100.
Resources []ResourceSpec
}
@ -94,9 +120,9 @@ type UtilizationShapePoint struct {
Score int32
}
// ResourceSpec represents single resource for bin packing of priority RequestedToCapacityRatioArgs.
// ResourceSpec represents single resource.
type ResourceSpec struct {
// Name of the resource to be managed by RequestedToCapacityRatio function.
// Name of the resource.
Name string
// Weight of the resource.
Weight int64

View File

@ -20,6 +20,7 @@ import (
"net"
"strconv"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
componentbaseconfigv1alpha1 "k8s.io/component-base/config/v1alpha1"
"k8s.io/kube-scheduler/config/v1alpha2"
@ -30,6 +31,11 @@ import (
api "k8s.io/kubernetes/pkg/apis/core"
)
var defaultResourceSpec = []v1alpha2.ResourceSpec{
{Name: string(v1.ResourceCPU), Weight: 1},
{Name: string(v1.ResourceMemory), Weight: 1},
}
func addDefaultingFuncs(scheme *runtime.Scheme) error {
return RegisterDefaults(scheme)
}
@ -165,3 +171,17 @@ func SetDefaults_InterPodAffinityArgs(obj *v1alpha2.InterPodAffinityArgs) {
obj.HardPodAffinityWeight = pointer.Int32Ptr(1)
}
}
func SetDefaults_NodeResourcesLeastAllocatedArgs(obj *v1alpha2.NodeResourcesLeastAllocatedArgs) {
if len(obj.Resources) == 0 {
// If no resources specified, used the default set.
obj.Resources = append(obj.Resources, defaultResourceSpec...)
}
}
func SetDefaults_NodeResourcesMostAllocatedArgs(obj *v1alpha2.NodeResourcesMostAllocatedArgs) {
if len(obj.Resources) == 0 {
// If no resources specified, used the default set.
obj.Resources = append(obj.Resources, defaultResourceSpec...)
}
}

View File

@ -299,6 +299,52 @@ func TestPluginArgsDefaults(t *testing.T) {
HardPodAffinityWeight: pointer.Int32Ptr(5),
},
},
{
name: "NodeResourcesLeastAllocatedArgs resources empty",
in: &v1alpha2.NodeResourcesLeastAllocatedArgs{},
want: &v1alpha2.NodeResourcesLeastAllocatedArgs{
Resources: []v1alpha2.ResourceSpec{
{Name: "cpu", Weight: 1},
{Name: "memory", Weight: 1},
},
},
},
{
name: "NodeResourcesLeastAllocatedArgs resources with value",
in: &v1alpha2.NodeResourcesLeastAllocatedArgs{
Resources: []v1alpha2.ResourceSpec{
{Name: "resource", Weight: 2},
},
},
want: &v1alpha2.NodeResourcesLeastAllocatedArgs{
Resources: []v1alpha2.ResourceSpec{
{Name: "resource", Weight: 2},
},
},
},
{
name: "NodeResourcesMostAllocatedArgs resources empty",
in: &v1alpha2.NodeResourcesMostAllocatedArgs{},
want: &v1alpha2.NodeResourcesMostAllocatedArgs{
Resources: []v1alpha2.ResourceSpec{
{Name: "cpu", Weight: 1},
{Name: "memory", Weight: 1},
},
},
},
{
name: "NodeResourcesMostAllocatedArgs resources with value",
in: &v1alpha2.NodeResourcesMostAllocatedArgs{
Resources: []v1alpha2.ResourceSpec{
{Name: "resource", Weight: 2},
},
},
want: &v1alpha2.NodeResourcesMostAllocatedArgs{
Resources: []v1alpha2.ResourceSpec{
{Name: "resource", Weight: 2},
},
},
},
}
for _, tc := range tests {
scheme := runtime.NewScheme()

View File

@ -80,6 +80,26 @@ func RegisterConversions(s *runtime.Scheme) error {
}); err != nil {
return err
}
if err := s.AddGeneratedConversionFunc((*v1alpha2.NodeResourcesLeastAllocatedArgs)(nil), (*config.NodeResourcesLeastAllocatedArgs)(nil), func(a, b interface{}, scope conversion.Scope) error {
return Convert_v1alpha2_NodeResourcesLeastAllocatedArgs_To_config_NodeResourcesLeastAllocatedArgs(a.(*v1alpha2.NodeResourcesLeastAllocatedArgs), b.(*config.NodeResourcesLeastAllocatedArgs), scope)
}); err != nil {
return err
}
if err := s.AddGeneratedConversionFunc((*config.NodeResourcesLeastAllocatedArgs)(nil), (*v1alpha2.NodeResourcesLeastAllocatedArgs)(nil), func(a, b interface{}, scope conversion.Scope) error {
return Convert_config_NodeResourcesLeastAllocatedArgs_To_v1alpha2_NodeResourcesLeastAllocatedArgs(a.(*config.NodeResourcesLeastAllocatedArgs), b.(*v1alpha2.NodeResourcesLeastAllocatedArgs), scope)
}); err != nil {
return err
}
if err := s.AddGeneratedConversionFunc((*v1alpha2.NodeResourcesMostAllocatedArgs)(nil), (*config.NodeResourcesMostAllocatedArgs)(nil), func(a, b interface{}, scope conversion.Scope) error {
return Convert_v1alpha2_NodeResourcesMostAllocatedArgs_To_config_NodeResourcesMostAllocatedArgs(a.(*v1alpha2.NodeResourcesMostAllocatedArgs), b.(*config.NodeResourcesMostAllocatedArgs), scope)
}); err != nil {
return err
}
if err := s.AddGeneratedConversionFunc((*config.NodeResourcesMostAllocatedArgs)(nil), (*v1alpha2.NodeResourcesMostAllocatedArgs)(nil), func(a, b interface{}, scope conversion.Scope) error {
return Convert_config_NodeResourcesMostAllocatedArgs_To_v1alpha2_NodeResourcesMostAllocatedArgs(a.(*config.NodeResourcesMostAllocatedArgs), b.(*v1alpha2.NodeResourcesMostAllocatedArgs), scope)
}); err != nil {
return err
}
if err := s.AddGeneratedConversionFunc((*v1alpha2.Plugin)(nil), (*config.Plugin)(nil), func(a, b interface{}, scope conversion.Scope) error {
return Convert_v1alpha2_Plugin_To_config_Plugin(a.(*v1alpha2.Plugin), b.(*config.Plugin), scope)
}); err != nil {
@ -410,6 +430,46 @@ func Convert_config_NodeResourcesFitArgs_To_v1alpha2_NodeResourcesFitArgs(in *co
return autoConvert_config_NodeResourcesFitArgs_To_v1alpha2_NodeResourcesFitArgs(in, out, s)
}
func autoConvert_v1alpha2_NodeResourcesLeastAllocatedArgs_To_config_NodeResourcesLeastAllocatedArgs(in *v1alpha2.NodeResourcesLeastAllocatedArgs, out *config.NodeResourcesLeastAllocatedArgs, s conversion.Scope) error {
out.Resources = *(*[]config.ResourceSpec)(unsafe.Pointer(&in.Resources))
return nil
}
// Convert_v1alpha2_NodeResourcesLeastAllocatedArgs_To_config_NodeResourcesLeastAllocatedArgs is an autogenerated conversion function.
func Convert_v1alpha2_NodeResourcesLeastAllocatedArgs_To_config_NodeResourcesLeastAllocatedArgs(in *v1alpha2.NodeResourcesLeastAllocatedArgs, out *config.NodeResourcesLeastAllocatedArgs, s conversion.Scope) error {
return autoConvert_v1alpha2_NodeResourcesLeastAllocatedArgs_To_config_NodeResourcesLeastAllocatedArgs(in, out, s)
}
func autoConvert_config_NodeResourcesLeastAllocatedArgs_To_v1alpha2_NodeResourcesLeastAllocatedArgs(in *config.NodeResourcesLeastAllocatedArgs, out *v1alpha2.NodeResourcesLeastAllocatedArgs, s conversion.Scope) error {
out.Resources = *(*[]v1alpha2.ResourceSpec)(unsafe.Pointer(&in.Resources))
return nil
}
// Convert_config_NodeResourcesLeastAllocatedArgs_To_v1alpha2_NodeResourcesLeastAllocatedArgs is an autogenerated conversion function.
func Convert_config_NodeResourcesLeastAllocatedArgs_To_v1alpha2_NodeResourcesLeastAllocatedArgs(in *config.NodeResourcesLeastAllocatedArgs, out *v1alpha2.NodeResourcesLeastAllocatedArgs, s conversion.Scope) error {
return autoConvert_config_NodeResourcesLeastAllocatedArgs_To_v1alpha2_NodeResourcesLeastAllocatedArgs(in, out, s)
}
func autoConvert_v1alpha2_NodeResourcesMostAllocatedArgs_To_config_NodeResourcesMostAllocatedArgs(in *v1alpha2.NodeResourcesMostAllocatedArgs, out *config.NodeResourcesMostAllocatedArgs, s conversion.Scope) error {
out.Resources = *(*[]config.ResourceSpec)(unsafe.Pointer(&in.Resources))
return nil
}
// Convert_v1alpha2_NodeResourcesMostAllocatedArgs_To_config_NodeResourcesMostAllocatedArgs is an autogenerated conversion function.
func Convert_v1alpha2_NodeResourcesMostAllocatedArgs_To_config_NodeResourcesMostAllocatedArgs(in *v1alpha2.NodeResourcesMostAllocatedArgs, out *config.NodeResourcesMostAllocatedArgs, s conversion.Scope) error {
return autoConvert_v1alpha2_NodeResourcesMostAllocatedArgs_To_config_NodeResourcesMostAllocatedArgs(in, out, s)
}
func autoConvert_config_NodeResourcesMostAllocatedArgs_To_v1alpha2_NodeResourcesMostAllocatedArgs(in *config.NodeResourcesMostAllocatedArgs, out *v1alpha2.NodeResourcesMostAllocatedArgs, s conversion.Scope) error {
out.Resources = *(*[]v1alpha2.ResourceSpec)(unsafe.Pointer(&in.Resources))
return nil
}
// Convert_config_NodeResourcesMostAllocatedArgs_To_v1alpha2_NodeResourcesMostAllocatedArgs is an autogenerated conversion function.
func Convert_config_NodeResourcesMostAllocatedArgs_To_v1alpha2_NodeResourcesMostAllocatedArgs(in *config.NodeResourcesMostAllocatedArgs, out *v1alpha2.NodeResourcesMostAllocatedArgs, s conversion.Scope) error {
return autoConvert_config_NodeResourcesMostAllocatedArgs_To_v1alpha2_NodeResourcesMostAllocatedArgs(in, out, s)
}
func autoConvert_v1alpha2_Plugin_To_config_Plugin(in *v1alpha2.Plugin, out *config.Plugin, s conversion.Scope) error {
out.Name = in.Name
if err := v1.Convert_Pointer_int32_To_int32(&in.Weight, &out.Weight, s); err != nil {

View File

@ -33,6 +33,12 @@ func RegisterDefaults(scheme *runtime.Scheme) error {
scheme.AddTypeDefaultingFunc(&v1alpha2.KubeSchedulerConfiguration{}, func(obj interface{}) {
SetObjectDefaults_KubeSchedulerConfiguration(obj.(*v1alpha2.KubeSchedulerConfiguration))
})
scheme.AddTypeDefaultingFunc(&v1alpha2.NodeResourcesLeastAllocatedArgs{}, func(obj interface{}) {
SetObjectDefaults_NodeResourcesLeastAllocatedArgs(obj.(*v1alpha2.NodeResourcesLeastAllocatedArgs))
})
scheme.AddTypeDefaultingFunc(&v1alpha2.NodeResourcesMostAllocatedArgs{}, func(obj interface{}) {
SetObjectDefaults_NodeResourcesMostAllocatedArgs(obj.(*v1alpha2.NodeResourcesMostAllocatedArgs))
})
return nil
}
@ -43,3 +49,11 @@ func SetObjectDefaults_InterPodAffinityArgs(in *v1alpha2.InterPodAffinityArgs) {
func SetObjectDefaults_KubeSchedulerConfiguration(in *v1alpha2.KubeSchedulerConfiguration) {
SetDefaults_KubeSchedulerConfiguration(in)
}
func SetObjectDefaults_NodeResourcesLeastAllocatedArgs(in *v1alpha2.NodeResourcesLeastAllocatedArgs) {
SetDefaults_NodeResourcesLeastAllocatedArgs(in)
}
func SetObjectDefaults_NodeResourcesMostAllocatedArgs(in *v1alpha2.NodeResourcesMostAllocatedArgs) {
SetDefaults_NodeResourcesMostAllocatedArgs(in)
}

View File

@ -306,6 +306,66 @@ func (in *NodeResourcesFitArgs) DeepCopyObject() runtime.Object {
return nil
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *NodeResourcesLeastAllocatedArgs) DeepCopyInto(out *NodeResourcesLeastAllocatedArgs) {
*out = *in
out.TypeMeta = in.TypeMeta
if in.Resources != nil {
in, out := &in.Resources, &out.Resources
*out = make([]ResourceSpec, len(*in))
copy(*out, *in)
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NodeResourcesLeastAllocatedArgs.
func (in *NodeResourcesLeastAllocatedArgs) DeepCopy() *NodeResourcesLeastAllocatedArgs {
if in == nil {
return nil
}
out := new(NodeResourcesLeastAllocatedArgs)
in.DeepCopyInto(out)
return out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func (in *NodeResourcesLeastAllocatedArgs) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *NodeResourcesMostAllocatedArgs) DeepCopyInto(out *NodeResourcesMostAllocatedArgs) {
*out = *in
out.TypeMeta = in.TypeMeta
if in.Resources != nil {
in, out := &in.Resources, &out.Resources
*out = make([]ResourceSpec, len(*in))
copy(*out, *in)
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NodeResourcesMostAllocatedArgs.
func (in *NodeResourcesMostAllocatedArgs) DeepCopy() *NodeResourcesMostAllocatedArgs {
if in == nil {
return nil
}
out := new(NodeResourcesMostAllocatedArgs)
in.DeepCopyInto(out)
return out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func (in *NodeResourcesMostAllocatedArgs) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Plugin) DeepCopyInto(out *Plugin) {
*out = *in

View File

@ -22,6 +22,7 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/kubernetes/pkg/scheduler/apis/config"
framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
)
@ -63,25 +64,43 @@ func (la *LeastAllocated) ScoreExtensions() framework.ScoreExtensions {
}
// NewLeastAllocated initializes a new plugin and returns it.
func NewLeastAllocated(_ runtime.Object, h framework.FrameworkHandle) (framework.Plugin, error) {
func NewLeastAllocated(laArgs runtime.Object, h framework.FrameworkHandle) (framework.Plugin, error) {
args, ok := laArgs.(*config.NodeResourcesLeastAllocatedArgs)
if !ok {
return nil, fmt.Errorf("want args to be of type NodeResourcesLeastAllocatedArgs, got %T", laArgs)
}
resToWeightMap := make(resourceToWeightMap)
for _, resource := range (*args).Resources {
if resource.Weight <= 0 {
return nil, fmt.Errorf("resource Weight of %v should be a positive value, got %v", resource.Name, resource.Weight)
}
if resource.Weight > framework.MaxNodeScore {
return nil, fmt.Errorf("resource Weight of %v should be less than 100, got %v", resource.Name, resource.Weight)
}
resToWeightMap[v1.ResourceName(resource.Name)] = resource.Weight
}
return &LeastAllocated{
handle: h,
resourceAllocationScorer: resourceAllocationScorer{
LeastAllocatedName,
leastResourceScorer,
defaultRequestedRatioResources,
Name: LeastAllocatedName,
scorer: leastResourceScorer(resToWeightMap),
resourceToWeightMap: resToWeightMap,
},
}, nil
}
func leastResourceScorer(requested, allocable resourceToValueMap, includeVolumes bool, requestedVolumes int, allocatableVolumes int) int64 {
var nodeScore, weightSum int64
for resource, weight := range defaultRequestedRatioResources {
resourceScore := leastRequestedScore(requested[resource], allocable[resource])
nodeScore += resourceScore * weight
weightSum += weight
func leastResourceScorer(resToWeightMap resourceToWeightMap) func(resourceToValueMap, resourceToValueMap, bool, int, int) int64 {
return func(requested, allocable resourceToValueMap, includeVolumes bool, requestedVolumes int, allocatableVolumes int) int64 {
var nodeScore, weightSum int64
for resource, weight := range resToWeightMap {
resourceScore := leastRequestedScore(requested[resource], allocable[resource])
nodeScore += resourceScore * weight
weightSum += weight
}
return nodeScore / weightSum
}
return nodeScore / weightSum
}
// The unused capacity is calculated on a scale of 0-MaxNodeScore

View File

@ -24,6 +24,7 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/scheduler/apis/config"
framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
"k8s.io/kubernetes/pkg/scheduler/internal/cache"
)
@ -90,10 +91,16 @@ func TestNodeResourcesLeastAllocated(t *testing.T) {
},
},
}
defaultResourceLeastAllocatedSet := []config.ResourceSpec{
{Name: string(v1.ResourceCPU), Weight: 1},
{Name: string(v1.ResourceMemory), Weight: 1},
}
tests := []struct {
pod *v1.Pod
pods []*v1.Pod
nodes []*v1.Node
args config.NodeResourcesLeastAllocatedArgs
wantErr string
expectedList framework.NodeScoreList
name string
}{
@ -108,6 +115,7 @@ func TestNodeResourcesLeastAllocated(t *testing.T) {
// Node2 Score: (100 + 100) / 2 = 100
pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)},
args: config.NodeResourcesLeastAllocatedArgs{Resources: defaultResourceLeastAllocatedSet},
expectedList: []framework.NodeScore{{Name: "machine1", Score: framework.MaxNodeScore}, {Name: "machine2", Score: framework.MaxNodeScore}},
name: "nothing scheduled, nothing requested",
},
@ -122,6 +130,7 @@ func TestNodeResourcesLeastAllocated(t *testing.T) {
// Node2 Score: (50 + 50) / 2 = 50
pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 6000, 10000)},
args: config.NodeResourcesLeastAllocatedArgs{Resources: defaultResourceLeastAllocatedSet},
expectedList: []framework.NodeScore{{Name: "machine1", Score: 37}, {Name: "machine2", Score: 50}},
name: "nothing scheduled, resources requested, differently sized machines",
},
@ -136,6 +145,7 @@ func TestNodeResourcesLeastAllocated(t *testing.T) {
// Node2 Score: (100 + 100) / 2 = 100
pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)},
args: config.NodeResourcesLeastAllocatedArgs{Resources: defaultResourceLeastAllocatedSet},
expectedList: []framework.NodeScore{{Name: "machine1", Score: framework.MaxNodeScore}, {Name: "machine2", Score: framework.MaxNodeScore}},
name: "no resources requested, pods scheduled",
pods: []*v1.Pod{
@ -156,6 +166,7 @@ func TestNodeResourcesLeastAllocated(t *testing.T) {
// Node2 Score: (40 + 75) / 2 = 57
pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 20000)},
args: config.NodeResourcesLeastAllocatedArgs{Resources: defaultResourceLeastAllocatedSet},
expectedList: []framework.NodeScore{{Name: "machine1", Score: 70}, {Name: "machine2", Score: 57}},
name: "no resources requested, pods scheduled with resources",
pods: []*v1.Pod{
@ -176,6 +187,7 @@ func TestNodeResourcesLeastAllocated(t *testing.T) {
// Node2 Score: (40 + 50) / 2 = 45
pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 20000)},
args: config.NodeResourcesLeastAllocatedArgs{Resources: defaultResourceLeastAllocatedSet},
expectedList: []framework.NodeScore{{Name: "machine1", Score: 57}, {Name: "machine2", Score: 45}},
name: "resources requested, pods scheduled with resources",
pods: []*v1.Pod{
@ -194,6 +206,7 @@ func TestNodeResourcesLeastAllocated(t *testing.T) {
// Node2 Score: (40 + 80) / 2 = 60
pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 50000)},
args: config.NodeResourcesLeastAllocatedArgs{Resources: defaultResourceLeastAllocatedSet},
expectedList: []framework.NodeScore{{Name: "machine1", Score: 57}, {Name: "machine2", Score: 60}},
name: "resources requested, pods scheduled with resources, differently sized machines",
pods: []*v1.Pod{
@ -212,6 +225,7 @@ func TestNodeResourcesLeastAllocated(t *testing.T) {
// Node2 Score: (0 + 50) / 2 = 25
pod: &v1.Pod{Spec: cpuOnly},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)},
args: config.NodeResourcesLeastAllocatedArgs{Resources: defaultResourceLeastAllocatedSet},
expectedList: []framework.NodeScore{{Name: "machine1", Score: 50}, {Name: "machine2", Score: 25}},
name: "requested resources exceed node capacity",
pods: []*v1.Pod{
@ -222,6 +236,7 @@ func TestNodeResourcesLeastAllocated(t *testing.T) {
{
pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 0, 0), makeNode("machine2", 0, 0)},
args: config.NodeResourcesLeastAllocatedArgs{Resources: defaultResourceLeastAllocatedSet},
expectedList: []framework.NodeScore{{Name: "machine1", Score: 0}, {Name: "machine2", Score: 0}},
name: "zero node resources, pods scheduled with resources",
pods: []*v1.Pod{
@ -229,13 +244,64 @@ func TestNodeResourcesLeastAllocated(t *testing.T) {
{Spec: cpuAndMemory},
},
},
{
// CPU Score: ((4000 - 3000) *100) / 4000 = 25
// Memory Score: ((10000 - 5000) *100) / 10000 = 50
// Node1 Score: (25 * 1 + 50 * 2) / (1 + 2) = 41
// CPU Score: ((6000 - 3000) *100) / 6000 = 50
// Memory Score: ((10000 - 5000) *100) / 10000 = 50
// Node2 Score: (50 * 1 + 50 * 2) / (1 + 2) = 50
pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 6000, 10000)},
args: config.NodeResourcesLeastAllocatedArgs{Resources: []config.ResourceSpec{{Name: "memory", Weight: 2}, {Name: "cpu", Weight: 1}}},
expectedList: []framework.NodeScore{{Name: "machine1", Score: 41}, {Name: "machine2", Score: 50}},
name: "nothing scheduled, resources requested with different weight on CPU and memory, differently sized machines",
},
{
// resource with negtive weight is not allowed
pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine", 4000, 10000)},
args: config.NodeResourcesLeastAllocatedArgs{Resources: []config.ResourceSpec{{Name: "memory", Weight: -1}, {Name: "cpu", Weight: 1}}},
wantErr: "resource Weight of memory should be a positive value, got -1",
name: "resource with negtive weight",
},
{
// resource with zero weight is not allowed
pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine", 4000, 10000)},
args: config.NodeResourcesLeastAllocatedArgs{Resources: []config.ResourceSpec{{Name: "memory", Weight: 1}, {Name: "cpu", Weight: 0}}},
wantErr: "resource Weight of cpu should be a positive value, got 0",
name: "resource with zero weight",
},
{
// resource weight should be less than MaxNodeScore
pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine", 4000, 10000)},
args: config.NodeResourcesLeastAllocatedArgs{Resources: []config.ResourceSpec{{Name: "memory", Weight: 120}}},
wantErr: "resource Weight of memory should be less than 100, got 120",
name: "resource weight larger than MaxNodeScore",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
snapshot := cache.NewSnapshot(test.pods, test.nodes)
fh, _ := framework.NewFramework(nil, nil, nil, framework.WithSnapshotSharedLister(snapshot))
p, _ := NewLeastAllocated(nil, fh)
p, err := NewLeastAllocated(&test.args, fh)
if len(test.wantErr) != 0 {
if err != nil && test.wantErr != err.Error() {
t.Fatalf("got err %w, want %w", err.Error(), test.wantErr)
} else if err == nil {
t.Fatal("no error produced, wanted %w", test.wantErr)
}
return
}
if err != nil && len(test.wantErr) == 0 {
t.Fatalf("failed to initialize plugin NodeResourcesLeastAllocated, got error: %v", err)
}
for i := range test.nodes {
hostResult, err := p.(framework.ScorePlugin).Score(context.Background(), nil, test.pod, test.nodes[i].Name)
if err != nil {

View File

@ -22,6 +22,7 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/kubernetes/pkg/scheduler/apis/config"
framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
)
@ -61,26 +62,44 @@ func (ma *MostAllocated) ScoreExtensions() framework.ScoreExtensions {
}
// NewMostAllocated initializes a new plugin and returns it.
func NewMostAllocated(_ runtime.Object, h framework.FrameworkHandle) (framework.Plugin, error) {
func NewMostAllocated(maArgs runtime.Object, h framework.FrameworkHandle) (framework.Plugin, error) {
args, ok := maArgs.(*config.NodeResourcesMostAllocatedArgs)
if !ok {
return nil, fmt.Errorf("want args to be of type NodeResourcesMostAllocatedArgs, got %T", args)
}
resToWeightMap := make(resourceToWeightMap)
for _, resource := range (*args).Resources {
if resource.Weight <= 0 {
return nil, fmt.Errorf("resource Weight of %v should be a positive value, got %v", resource.Name, resource.Weight)
}
if resource.Weight > framework.MaxNodeScore {
return nil, fmt.Errorf("resource Weight of %v should be less than 100, got %v", resource.Name, resource.Weight)
}
resToWeightMap[v1.ResourceName(resource.Name)] = resource.Weight
}
return &MostAllocated{
handle: h,
resourceAllocationScorer: resourceAllocationScorer{
MostAllocatedName,
mostResourceScorer,
defaultRequestedRatioResources,
Name: MostAllocatedName,
scorer: mostResourceScorer(resToWeightMap),
resourceToWeightMap: resToWeightMap,
},
}, nil
}
func mostResourceScorer(requested, allocable resourceToValueMap, includeVolumes bool, requestedVolumes int, allocatableVolumes int) int64 {
var nodeScore, weightSum int64
for resource, weight := range defaultRequestedRatioResources {
resourceScore := mostRequestedScore(requested[resource], allocable[resource])
nodeScore += resourceScore * weight
weightSum += weight
func mostResourceScorer(resToWeightMap resourceToWeightMap) func(requested, allocable resourceToValueMap, includeVolumes bool, requestedVolumes int, allocatableVolumes int) int64 {
return func(requested, allocable resourceToValueMap, includeVolumes bool, requestedVolumes int, allocatableVolumes int) int64 {
var nodeScore, weightSum int64
for resource, weight := range resToWeightMap {
resourceScore := mostRequestedScore(requested[resource], allocable[resource])
nodeScore += resourceScore * weight
weightSum += weight
}
return (nodeScore / weightSum)
}
return (nodeScore / weightSum)
}
// The used capacity is calculated on a scale of 0-10

View File

@ -24,6 +24,7 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/scheduler/apis/config"
framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
"k8s.io/kubernetes/pkg/scheduler/internal/cache"
)
@ -105,10 +106,16 @@ func TestNodeResourcesMostAllocated(t *testing.T) {
},
},
}
defaultResourceMostAllocatedSet := []config.ResourceSpec{
{Name: string(v1.ResourceCPU), Weight: 1},
{Name: string(v1.ResourceMemory), Weight: 1},
}
tests := []struct {
pod *v1.Pod
pods []*v1.Pod
nodes []*v1.Node
args config.NodeResourcesMostAllocatedArgs
wantErr string
expectedList framework.NodeScoreList
name string
}{
@ -123,6 +130,7 @@ func TestNodeResourcesMostAllocated(t *testing.T) {
// Node2 Score: (0 + 0) / 2 = 0
pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)},
args: config.NodeResourcesMostAllocatedArgs{Resources: defaultResourceMostAllocatedSet},
expectedList: []framework.NodeScore{{Name: "machine1", Score: 0}, {Name: "machine2", Score: 0}},
name: "nothing scheduled, nothing requested",
},
@ -137,6 +145,7 @@ func TestNodeResourcesMostAllocated(t *testing.T) {
// Node2 Score: (50 + 50) / 2 = 50
pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 6000, 10000)},
args: config.NodeResourcesMostAllocatedArgs{Resources: defaultResourceMostAllocatedSet},
expectedList: []framework.NodeScore{{Name: "machine1", Score: 62}, {Name: "machine2", Score: 50}},
name: "nothing scheduled, resources requested, differently sized machines",
},
@ -151,6 +160,7 @@ func TestNodeResourcesMostAllocated(t *testing.T) {
// Node2 Score: (60 + 25) / 2 = 42
pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 20000)},
args: config.NodeResourcesMostAllocatedArgs{Resources: defaultResourceMostAllocatedSet},
expectedList: []framework.NodeScore{{Name: "machine1", Score: 30}, {Name: "machine2", Score: 42}},
name: "no resources requested, pods scheduled with resources",
pods: []*v1.Pod{
@ -171,6 +181,7 @@ func TestNodeResourcesMostAllocated(t *testing.T) {
// Node2 Score: (60 + 50) / 2 = 55
pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 20000)},
args: config.NodeResourcesMostAllocatedArgs{Resources: defaultResourceMostAllocatedSet},
expectedList: []framework.NodeScore{{Name: "machine1", Score: 42}, {Name: "machine2", Score: 55}},
name: "resources requested, pods scheduled with resources",
pods: []*v1.Pod{
@ -189,16 +200,68 @@ func TestNodeResourcesMostAllocated(t *testing.T) {
// Node2 Score: (50 + 0) / 2 = 25
pod: &v1.Pod{Spec: bigCPUAndMemory},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 10000, 8000)},
args: config.NodeResourcesMostAllocatedArgs{Resources: defaultResourceMostAllocatedSet},
expectedList: []framework.NodeScore{{Name: "machine1", Score: 45}, {Name: "machine2", Score: 25}},
name: "resources requested with more than the node, pods scheduled with resources",
},
{
// CPU Score: (3000 *100) / 4000 = 75
// Memory Score: (5000 *100) / 10000 = 50
// Node1 Score: (75 * 1 + 50 * 2) / (1 + 2) = 58
// CPU Score: (3000 *100) / 6000 = 50
// Memory Score: (5000 *100) / 10000 = 50
// Node2 Score: (50 * 1 + 50 * 2) / (1 + 2) = 50
pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 6000, 10000)},
args: config.NodeResourcesMostAllocatedArgs{Resources: []config.ResourceSpec{{Name: "memory", Weight: 2}, {Name: "cpu", Weight: 1}}},
expectedList: []framework.NodeScore{{Name: "machine1", Score: 58}, {Name: "machine2", Score: 50}},
name: "nothing scheduled, resources requested, differently sized machines",
},
{
// resource with negtive weight is not allowed
pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine", 4000, 10000)},
args: config.NodeResourcesMostAllocatedArgs{Resources: []config.ResourceSpec{{Name: "memory", Weight: -1}, {Name: "cpu", Weight: 1}}},
wantErr: "resource Weight of memory should be a positive value, got -1",
name: "resource with negtive weight",
},
{
// resource with zero weight is not allowed
pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine", 4000, 10000)},
args: config.NodeResourcesMostAllocatedArgs{Resources: []config.ResourceSpec{{Name: "memory", Weight: 1}, {Name: "cpu", Weight: 0}}},
wantErr: "resource Weight of cpu should be a positive value, got 0",
name: "resource with zero weight",
},
{
// resource weight should be less than MaxNodeScore
pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine", 4000, 10000)},
args: config.NodeResourcesMostAllocatedArgs{Resources: []config.ResourceSpec{{Name: "memory", Weight: 120}}},
wantErr: "resource Weight of memory should be less than 100, got 120",
name: "resource weight larger than MaxNodeScore",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
snapshot := cache.NewSnapshot(test.pods, test.nodes)
fh, _ := framework.NewFramework(nil, nil, nil, framework.WithSnapshotSharedLister(snapshot))
p, _ := NewMostAllocated(nil, fh)
p, err := NewMostAllocated(&test.args, fh)
if len(test.wantErr) != 0 {
if err != nil && test.wantErr != err.Error() {
t.Fatalf("got err %w, want %w", err.Error(), test.wantErr)
} else if err == nil {
t.Fatal("no error produced, wanted %w", test.wantErr)
}
return
}
if err != nil && len(test.wantErr) == 0 {
t.Fatalf("failed to initialize plugin NodeResourcesMostAllocated, got error: %v", err)
}
for i := range test.nodes {
hostResult, err := p.(framework.ScorePlugin).Score(context.Background(), nil, test.pod, test.nodes[i].Name)
if err != nil {

View File

@ -44,6 +44,8 @@ func addKnownTypes(scheme *runtime.Scheme) error {
&PodTopologySpreadArgs{},
&RequestedToCapacityRatioArgs{},
&ServiceAffinityArgs{},
&NodeResourcesLeastAllocatedArgs{},
&NodeResourcesMostAllocatedArgs{},
)
return nil
}

View File

@ -97,6 +97,32 @@ type RequestedToCapacityRatioArgs struct {
Resources []ResourceSpec `json:"resources,omitempty"`
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// NodeResourcesLeastAllocatedArgs holds arguments used to configure NodeResourcesLeastAllocated plugin.
type NodeResourcesLeastAllocatedArgs struct {
metav1.TypeMeta `json:",inline"`
// Resources to be managed, if no resource is provided, default resource set with both
// the weight of "cpu" and "memory" set to "1" will be applied.
// Resource with "0" weight will not accountable for the final score.
// +listType=atomic
Resources []ResourceSpec `json:"resources,omitempty"`
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// NodeResourcesMostAllocatedArgs holds arguments used to configure NodeResourcesMostAllocated plugin.
type NodeResourcesMostAllocatedArgs struct {
metav1.TypeMeta `json:",inline"`
// Resources to be managed, if no resource is provided, default resource set with both
// the weight of "cpu" and "memory" set to "1" will be applied.
// Resource with "0" weight will not accountable for the final score.
// +listType=atomic
Resources []ResourceSpec `json:"resources,omitempty"`
}
// TODO add JSON tags and remove custom unmarshalling in v1beta1.
// UtilizationShapePoint and ResourceSpec fields are not annotated with JSON tags in v1alpha2
// to maintain backward compatibility with the args shipped with v1.18.

View File

@ -241,6 +241,66 @@ func (in *NodeResourcesFitArgs) DeepCopyObject() runtime.Object {
return nil
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *NodeResourcesLeastAllocatedArgs) DeepCopyInto(out *NodeResourcesLeastAllocatedArgs) {
*out = *in
out.TypeMeta = in.TypeMeta
if in.Resources != nil {
in, out := &in.Resources, &out.Resources
*out = make([]ResourceSpec, len(*in))
copy(*out, *in)
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NodeResourcesLeastAllocatedArgs.
func (in *NodeResourcesLeastAllocatedArgs) DeepCopy() *NodeResourcesLeastAllocatedArgs {
if in == nil {
return nil
}
out := new(NodeResourcesLeastAllocatedArgs)
in.DeepCopyInto(out)
return out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func (in *NodeResourcesLeastAllocatedArgs) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *NodeResourcesMostAllocatedArgs) DeepCopyInto(out *NodeResourcesMostAllocatedArgs) {
*out = *in
out.TypeMeta = in.TypeMeta
if in.Resources != nil {
in, out := &in.Resources, &out.Resources
*out = make([]ResourceSpec, len(*in))
copy(*out, *in)
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NodeResourcesMostAllocatedArgs.
func (in *NodeResourcesMostAllocatedArgs) DeepCopy() *NodeResourcesMostAllocatedArgs {
if in == nil {
return nil
}
out := new(NodeResourcesMostAllocatedArgs)
in.DeepCopyInto(out)
return out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func (in *NodeResourcesMostAllocatedArgs) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Plugin) DeepCopyInto(out *Plugin) {
*out = *in