forked from kubewharf/katalyst-core
115 lines
3.5 KiB
Go
115 lines
3.5 KiB
Go
/*
|
|
Copyright 2022 The Katalyst 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 eventhandlers
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
v1 "k8s.io/api/core/v1"
|
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
|
"k8s.io/client-go/informers"
|
|
"k8s.io/client-go/tools/cache"
|
|
"k8s.io/klog/v2"
|
|
|
|
"github.com/kubewharf/katalyst-api/pkg/client/informers/externalversions"
|
|
schedulercache "github.com/kubewharf/katalyst-core/pkg/scheduler/cache"
|
|
"github.com/kubewharf/katalyst-core/pkg/scheduler/util"
|
|
"github.com/kubewharf/katalyst-core/pkg/util/native"
|
|
)
|
|
|
|
// AddPodEventHandler adds Pod event handlers for the scheduler.
|
|
func AddPodEventHandler(informerFactory informers.SharedInformerFactory, _ externalversions.SharedInformerFactory) {
|
|
podInformer := informerFactory.Core().V1().Pods()
|
|
podInformer.Informer().AddEventHandler(
|
|
cache.FilteringResourceEventHandler{
|
|
FilterFunc: func(obj interface{}) bool {
|
|
switch t := obj.(type) {
|
|
case *v1.Pod:
|
|
return util.IsReclaimedPod(t) && native.IsAssignedPod(t)
|
|
case cache.DeletedFinalStateUnknown:
|
|
if pod, ok := t.Obj.(*v1.Pod); ok {
|
|
// The carried object may be stale, so we don't use it to check if
|
|
// it's assigned or not. Attempting to cleanup anyways.
|
|
return util.IsReclaimedPod(pod)
|
|
}
|
|
utilruntime.HandleError(fmt.Errorf("unable to convert object %T to *v1.Pod", obj))
|
|
return false
|
|
default:
|
|
utilruntime.HandleError(fmt.Errorf("unable to handle object: %T", obj))
|
|
return false
|
|
}
|
|
},
|
|
Handler: cache.ResourceEventHandlerFuncs{
|
|
AddFunc: addPodToCache,
|
|
UpdateFunc: updatePodInCache,
|
|
DeleteFunc: deletePodFromCache,
|
|
},
|
|
},
|
|
)
|
|
}
|
|
|
|
func addPodToCache(obj interface{}) {
|
|
pod, ok := obj.(*v1.Pod)
|
|
if !ok {
|
|
klog.ErrorS(nil, "Cannot convert to *v1.Pod", "obj", obj)
|
|
return
|
|
}
|
|
klog.V(3).InfoS("Add event for scheduled pod", "pod", klog.KObj(pod))
|
|
|
|
if err := schedulercache.GetCache().AddPod(pod); err != nil {
|
|
klog.ErrorS(err, "Scheduler cache AddPod failed", "pod", klog.KObj(pod))
|
|
}
|
|
}
|
|
|
|
// since we may have the functionality to change pod resources such as VPA,
|
|
// we should also handle pod update events.
|
|
func updatePodInCache(_, newObj interface{}) {
|
|
newPod, ok := newObj.(*v1.Pod)
|
|
if !ok {
|
|
klog.ErrorS(nil, "Cannot convert to *v1.Pod", "obj", newObj)
|
|
return
|
|
}
|
|
klog.V(3).InfoS("Add event for scheduled pod", "pod", klog.KObj(newPod))
|
|
|
|
if err := schedulercache.GetCache().AddPod(newPod); err != nil {
|
|
klog.ErrorS(err, "Scheduler cache AddPod failed", "pod", klog.KObj(newPod))
|
|
}
|
|
}
|
|
|
|
func deletePodFromCache(obj interface{}) {
|
|
var pod *v1.Pod
|
|
switch t := obj.(type) {
|
|
case *v1.Pod:
|
|
pod = t
|
|
case cache.DeletedFinalStateUnknown:
|
|
var ok bool
|
|
pod, ok = t.Obj.(*v1.Pod)
|
|
if !ok {
|
|
klog.ErrorS(nil, "Cannot convert to *v1.Pod", "obj", t.Obj)
|
|
return
|
|
}
|
|
default:
|
|
klog.ErrorS(nil, "Cannot convert to *v1.Pod", "obj", t)
|
|
return
|
|
}
|
|
klog.V(3).InfoS("Delete event for scheduled pod", "pod", klog.KObj(pod))
|
|
|
|
if err := schedulercache.GetCache().RemovePod(pod); err != nil {
|
|
klog.ErrorS(err, "Scheduler cache RemovePod failed", "pod", klog.KObj(pod))
|
|
}
|
|
}
|