/* 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 defaultpreemption import ( "context" "time" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" utilfeature "k8s.io/apiserver/pkg/util/feature" kubefeatures "k8s.io/kubernetes/pkg/features" "k8s.io/kubernetes/pkg/scheduler/core" framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1" "k8s.io/kubernetes/pkg/scheduler/metrics" ) const ( // Name of the plugin used in the plugin registry and configurations. Name = "DefaultPreemption" ) // DefaultPreemption is a PostFilter plugin implements the preemption logic. type DefaultPreemption struct { fh framework.FrameworkHandle } var _ framework.PostFilterPlugin = &DefaultPreemption{} // Name returns name of the plugin. It is used in logs, etc. func (pl *DefaultPreemption) Name() string { return Name } // New initializes a new plugin and returns it. func New(_ runtime.Object, fh framework.FrameworkHandle) (framework.Plugin, error) { pl := DefaultPreemption{fh} if utilfeature.DefaultFeatureGate.Enabled(kubefeatures.PodDisruptionBudget) { // A hack to initialize pdbLister in sharedInformerFactory. fh.SharedInformerFactory().Policy().V1beta1().PodDisruptionBudgets().Lister() } return &pl, nil } // PostFilter invoked at the postFilter extension point. func (pl *DefaultPreemption) PostFilter(ctx context.Context, state *framework.CycleState, pod *v1.Pod, m framework.NodeToStatusMap) (*framework.PostFilterResult, *framework.Status) { preemptionStartTime := time.Now() defer func() { metrics.PreemptionAttempts.Inc() metrics.SchedulingAlgorithmPreemptionEvaluationDuration.Observe(metrics.SinceInSeconds(preemptionStartTime)) metrics.DeprecatedSchedulingDuration.WithLabelValues(metrics.PreemptionEvaluation).Observe(metrics.SinceInSeconds(preemptionStartTime)) }() nnn, err := core.Preempt(ctx, pl.fh, state, pod, m) if err != nil { return nil, framework.NewStatus(framework.Error, err.Error()) } if nnn == "" { return nil, framework.NewStatus(framework.Unschedulable) } return &framework.PostFilterResult{NominatedNodeName: nnn}, framework.NewStatus(framework.Success) }