github.com/kubewharf/katalyst-core@v0.5.3/pkg/scheduler/eventhandlers/cnr_handler.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package eventhandlers
    18  
    19  import (
    20  	"k8s.io/client-go/informers"
    21  	"k8s.io/client-go/tools/cache"
    22  	"k8s.io/klog/v2"
    23  
    24  	apis "github.com/kubewharf/katalyst-api/pkg/apis/node/v1alpha1"
    25  	"github.com/kubewharf/katalyst-api/pkg/client/informers/externalversions"
    26  	schedulercache "github.com/kubewharf/katalyst-core/pkg/scheduler/cache"
    27  )
    28  
    29  const (
    30  	CommonCNRHandler = "CommonCNRHandler"
    31  )
    32  
    33  func RegisterCommonCNRHandler() {
    34  	RegisterEventHandler(CommonCNRHandler, AddCNREventHandler)
    35  }
    36  
    37  // AddCNREventHandler adds CNR event handlers for the scheduler.
    38  func AddCNREventHandler(_ informers.SharedInformerFactory, internalInformerFactory externalversions.SharedInformerFactory) {
    39  	cnrInformer := internalInformerFactory.Node().V1alpha1().CustomNodeResources()
    40  	cnrInformer.Informer().AddEventHandler(
    41  		cache.ResourceEventHandlerFuncs{
    42  			AddFunc:    addCNRToCache,
    43  			UpdateFunc: updateNodeInCache,
    44  			DeleteFunc: deleteCNRFromCache,
    45  		})
    46  }
    47  
    48  func addCNRToCache(obj interface{}) {
    49  	cnr, ok := obj.(*apis.CustomNodeResource)
    50  	if !ok {
    51  		klog.Errorf("cannot convert obj to *apis.CNR: %v", obj)
    52  		return
    53  	}
    54  	klog.V(3).InfoS("Add event for CNR", "CNR", klog.KObj(cnr))
    55  
    56  	schedulercache.GetCache().AddOrUpdateCNR(cnr)
    57  }
    58  
    59  func updateNodeInCache(_, newObj interface{}) {
    60  	newCNR, ok := newObj.(*apis.CustomNodeResource)
    61  	if !ok {
    62  		klog.ErrorS(nil, "Cannot convert newObj to *apis.CNR", "newObj", newObj)
    63  		return
    64  	}
    65  	klog.V(3).InfoS("Update event for CNR", "CNR", klog.KObj(newCNR))
    66  
    67  	schedulercache.GetCache().AddOrUpdateCNR(newCNR)
    68  }
    69  
    70  func deleteCNRFromCache(obj interface{}) {
    71  	var cnr *apis.CustomNodeResource
    72  	switch t := obj.(type) {
    73  	case *apis.CustomNodeResource:
    74  		cnr = t
    75  	case cache.DeletedFinalStateUnknown:
    76  		var ok bool
    77  		cnr, ok = t.Obj.(*apis.CustomNodeResource)
    78  		if !ok {
    79  			klog.ErrorS(nil, "Cannot convert to *apis.CNR", "obj", t.Obj)
    80  			return
    81  		}
    82  	default:
    83  		klog.ErrorS(nil, "Cannot convert to *apis.CNR", "obj", t)
    84  		return
    85  	}
    86  	klog.V(3).InfoS("Delete event for CNR", "CNR", klog.KObj(cnr))
    87  
    88  	schedulercache.GetCache().RemoveCNR(cnr)
    89  }