Copyright 2022 The Volcano 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 cdp
import (
"time"
v1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
"volcano.sh/apis/pkg/apis/scheduling/v1beta1"
"volcano.sh/volcano/pkg/scheduler/api"
"volcano.sh/volcano/pkg/scheduler/framework"
"volcano.sh/volcano/pkg/scheduler/plugins/util"
)
const (
PluginName = "cdp"
)
type CooldownProtectionPlugin struct {
}
func New(arguments framework.Arguments) framework.Plugin {
return &CooldownProtectionPlugin{}
}
func (*CooldownProtectionPlugin) Name() string {
return PluginName
}
func (sp *CooldownProtectionPlugin) podCooldownTime(pod *v1.Pod) (value time.Duration, enabled bool) {
v, ok := pod.Labels[v1beta1.CooldownTime]
if !ok {
v, ok = pod.Annotations[v1beta1.CooldownTime]
if !ok {
return 0, false
}
}
vi, err := time.ParseDuration(v)
if err != nil {
klog.Warningf("invalid time duration %s=%s", v1beta1.CooldownTime, v)
return 0, false
}
return vi, true
}
func (sp *CooldownProtectionPlugin) OnSessionOpen(ssn *framework.Session) {
preemptableFn := func(preemptor *api.TaskInfo, preemptees []*api.TaskInfo) ([]*api.TaskInfo, int) {
var victims []*api.TaskInfo
for _, preemptee := range preemptees {
cooldownTime, enabled := sp.podCooldownTime(preemptee.Pod)
if !enabled {
victims = append(victims, preemptee)
continue
}
pod := preemptee.Pod
stableFiltered := false
if pod.Status.Phase == v1.PodRunning {
for _, c := range pod.Status.Conditions {
if c.Type == v1.PodScheduled && c.Status == v1.ConditionTrue {
if c.LastTransitionTime.Add(cooldownTime).After(time.Now()) {
stableFiltered = true
}
break
}
}
}
if !stableFiltered {
victims = append(victims, preemptee)
}
}
klog.V(4).Infof("Victims from cdp plugins are %+v", victims)
return victims, util.Permit
}
klog.V(4).Info("plugin cdp session open")
ssn.AddPreemptableFn(sp.Name(), preemptableFn)
}
func (*CooldownProtectionPlugin) OnSessionClose(ssn *framework.Session) {}