Skip to content

Commit

Permalink
QoS Sort plugin sample src code
Browse files Browse the repository at this point in the history
  • Loading branch information
Huang-Wei committed Apr 3, 2020
1 parent 3abf6ba commit d3c3bab
Show file tree
Hide file tree
Showing 9 changed files with 554 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# build and test outputs
/bin/
/_output/
/_artifacts/

# used for the code generators only
/vendor/

# macOS
.DS_Store

# files generated by editors
.idea/
*.iml
.vscode/
*.swp
*.sublime-project
*.sublime-workspace
*~
39 changes: 39 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"math/rand"
"os"
"time"

"k8s.io/kubernetes/cmd/kube-scheduler/app"
"sigs.k8s.io/scheduler-plugins/pkg/qos"
)

func main() {
rand.Seed(time.Now().UnixNano())
// Register custom plugins to the scheduler framework.
// Later they can consist of scheduler profile(s) and hence
// used by various kinds of workloads.
command := app.NewSchedulerCommand(
app.WithPlugin(qos.Name, qos.New),
)
if err := command.Execute(); err != nil {
os.Exit(1)
}
}
23 changes: 23 additions & 0 deletions pkg/qos/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Overview

This folder holds some sample plugin implementations based on [QoS
(Quality of Service) class](https://kubernetes.io/docs/tasks/configure-pod-container/quality-service-pod/)
of Pods.

## Maturity Level

<!-- Check one of the values: Sample, Alpha, Beta, GA -->

- [x] 💡 Sample (for demonstrating and inspiring purpose)
- [ ] 👶 Alpha (used in companies for pilot projects)
- [ ] 👦 Beta (used in companies and developed actively)
- [ ] 👨 Stable (used in companies for production workloads)

## QOS QueueSort Plugin

Sorts pods by .spec.priority and breaks ties by the [quality of service class](https://kubernetes.io/docs/tasks/configure-pod-container/quality-service-pod/#qos-classes).
Specifically, this plugin enqueue the Pods with the following order:

- Guaranteed (requests == limits)
- Burstable (requests < limits)
- BestEffort (requests and limits not set)
63 changes: 63 additions & 0 deletions pkg/qos/queuesort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package qos

import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/kubernetes/pkg/api/v1/pod"
v1qos "k8s.io/kubernetes/pkg/apis/core/v1/helper/qos"
framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
)

// Name is the name of the plugin used in the plugin registry and configurations.
const Name = "QoSSort"

// QoSSort is a plugin that implements QoS class based sorting.
type Sort struct{}

var _ framework.QueueSortPlugin = &Sort{}

// Name returns name of the plugin.
func (pl *Sort) Name() string {
return Name
}

// Less is the function used by the activeQ heap algorithm to sort pods.
// It sorts pods based on their priority. When priorities are equal, it uses
// PodInfo.timestamp.
func (*Sort) Less(pInfo1, pInfo2 *framework.PodInfo) bool {
p1 := pod.GetPodPriority(pInfo1.Pod)
p2 := pod.GetPodPriority(pInfo2.Pod)
return (p1 > p2) || (p1 == p2 && compQOS(pInfo1.Pod, pInfo2.Pod))
}

func compQOS(p1, p2 *v1.Pod) bool {
p1QOS, p2QOS := v1qos.GetPodQOS(p1), v1qos.GetPodQOS(p2)
if p1QOS == v1.PodQOSGuaranteed {
return true
} else if p1QOS == v1.PodQOSBurstable {
return p2QOS != v1.PodQOSGuaranteed
} else {
return p2QOS == v1.PodQOSBestEffort
}
}

// New initializes a new plugin and returns it.
func New(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
return &Sort{}, nil
}
155 changes: 155 additions & 0 deletions pkg/qos/queuesort_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package qos

import (
"testing"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
)

func TestSortLess(t *testing.T) {
tests := []struct {
name string
pInfo1 *framework.PodInfo
pInfo2 *framework.PodInfo
want bool
}{
{
name: "p1's priority greater than p2",
pInfo1: &framework.PodInfo{
Pod: makePod("p1", 100, nil, nil),
},
pInfo2: &framework.PodInfo{
Pod: makePod("p2", 50, nil, nil),
},
want: true,
},
{
name: "p1's priority less than p2",
pInfo1: &framework.PodInfo{
Pod: makePod("p1", 50, nil, nil),
},
pInfo2: &framework.PodInfo{
Pod: makePod("p2", 80, nil, nil),
},
want: false,
},
{
name: "p1 and p2 are both BestEfforts",
pInfo1: &framework.PodInfo{
Pod: makePod("p1", 0, nil, nil),
},
pInfo2: &framework.PodInfo{
Pod: makePod("p2", 0, nil, nil),
},
want: true,
},
{
name: "p1 is BestEfforts, p2 is Guaranteed",
pInfo1: &framework.PodInfo{
Pod: makePod("p1", 0, nil, nil),
},
pInfo2: &framework.PodInfo{
Pod: makePod("p2", 0, getResList("100m", "100Mi"), getResList("100m", "100Mi")),
},
want: false,
},
{
name: "p1 is Burstable, p2 is Guaranteed",
pInfo1: &framework.PodInfo{
Pod: makePod("p1", 0, getResList("100m", "100Mi"), getResList("200m", "200Mi")),
},
pInfo2: &framework.PodInfo{
Pod: makePod("p2", 0, getResList("100m", "100Mi"), getResList("100m", "100Mi")),
},
want: false,
},
{
name: "both p1 and p2 are Burstable",
pInfo1: &framework.PodInfo{
Pod: makePod("p1", 0, getResList("100m", "100Mi"), getResList("200m", "200Mi")),
},
pInfo2: &framework.PodInfo{
Pod: makePod("p2", 0, getResList("100m", "100Mi"), getResList("200m", "200Mi")),
},
want: true,
},
{
name: "p1 is Guaranteed, p2 is Burstable",
pInfo1: &framework.PodInfo{
Pod: makePod("p1", 0, getResList("100m", "100Mi"), getResList("100m", "100Mi")),
},
pInfo2: &framework.PodInfo{
Pod: makePod("p2", 0, getResList("100m", "100Mi"), getResList("200m", "200Mi")),
},
want: true,
},
{
name: "both p1 and p2 are Guaranteed",
pInfo1: &framework.PodInfo{
Pod: makePod("p1", 0, getResList("100m", "100Mi"), getResList("100m", "100Mi")),
},
pInfo2: &framework.PodInfo{
Pod: makePod("p2", 0, getResList("100m", "100Mi"), getResList("100m", "100Mi")),
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &Sort{}
if got := s.Less(tt.pInfo1, tt.pInfo2); got != tt.want {
t.Errorf("Less() = %v, want %v", got, tt.want)
}
})
}
}

func makePod(name string, priority int32, requests, limits v1.ResourceList) *v1.Pod {
return &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v1.PodSpec{
Priority: &priority,
Containers: []v1.Container{
{
Name: name,
Resources: v1.ResourceRequirements{
Requests: requests,
Limits: limits,
},
},
},
},
}
}

func getResList(cpu, memory string) v1.ResourceList {
res := v1.ResourceList{}
if cpu != "" {
res[v1.ResourceCPU] = resource.MustParse(cpu)
}
if memory != "" {
res[v1.ResourceMemory] = resource.MustParse(memory)
}
return res
}
27 changes: 27 additions & 0 deletions test/integration/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package integration

import (
"testing"

"k8s.io/kubernetes/test/integration/framework"
)

func TestMain(m *testing.M) {
framework.EtcdMain(m.Run)
}
Loading

0 comments on commit d3c3bab

Please sign in to comment.