github.com/kubewharf/katalyst-core@v0.5.3/pkg/controller/overcommit/node/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 node 18 19 import ( 20 "fmt" 21 "reflect" 22 23 v1 "k8s.io/api/core/v1" 24 utilruntime "k8s.io/apimachinery/pkg/util/runtime" 25 "k8s.io/client-go/tools/cache" 26 "k8s.io/klog/v2" 27 28 nodev1alpha1 "github.com/kubewharf/katalyst-api/pkg/apis/node/v1alpha1" 29 "github.com/kubewharf/katalyst-api/pkg/apis/overcommit/v1alpha1" 30 "github.com/kubewharf/katalyst-api/pkg/consts" 31 "github.com/kubewharf/katalyst-core/pkg/util/native" 32 ) 33 34 type nodeOvercommitEvent struct { 35 nodeKey string 36 configKey string 37 eventType 38 } 39 40 type eventType string 41 42 const ( 43 nodeEvent eventType = "node" 44 configEvent eventType = "config" 45 ) 46 47 func (nc *NodeOvercommitController) addCNR(obj interface{}) { 48 cnr, ok := obj.(*nodev1alpha1.CustomNodeResource) 49 if !ok { 50 klog.Errorf("cannot convert obj to *CustomNodeResource: %v", obj) 51 return 52 } 53 54 klog.V(4).Infof("[noc] notice addition of CNR %s", cnr.Name) 55 nc.enqueueCNR(cnr) 56 } 57 58 func (nc *NodeOvercommitController) updateCNR(old, new interface{}) { 59 oldCNR, ok := old.(*nodev1alpha1.CustomNodeResource) 60 if !ok { 61 klog.Errorf("cannot convert obj to CustomNodeResource: %v", old) 62 return 63 } 64 65 newCNR, ok := new.(*nodev1alpha1.CustomNodeResource) 66 if !ok { 67 klog.Errorf("cannot convert obj to CustomNodeResource: %v", new) 68 return 69 } 70 71 if reflect.DeepEqual(newCNR.Annotations, oldCNR.Annotations) { 72 return 73 } 74 75 nc.enqueueCNR(newCNR) 76 } 77 78 func (nc *NodeOvercommitController) addNodeOvercommitConfig(obj interface{}) { 79 noc, ok := obj.(*v1alpha1.NodeOvercommitConfig) 80 if !ok { 81 klog.Errorf("cannot convert obj to *v1alpha1.NodeOvercommitConfig: %v", obj) 82 return 83 } 84 85 klog.V(4).Infof("[noc] notice addition of NodeOvercommitConfig %s", native.GenerateUniqObjectNameKey(noc)) 86 nc.enqueueNodeOvercommitConfig(noc, configEvent) 87 } 88 89 func (nc *NodeOvercommitController) updateNodeOvercommitConfig(old, new interface{}) { 90 noc, ok := new.(*v1alpha1.NodeOvercommitConfig) 91 if !ok { 92 klog.Errorf("cannot convert obj to *v1alpha1.NodeOvercommitConfig: %v", noc) 93 return 94 } 95 96 klog.V(4).Infof("[noc] notice update of NodeOvercommitConfig %s", native.GenerateUniqObjectNameKey(noc)) 97 nc.enqueueNodeOvercommitConfig(noc, configEvent) 98 } 99 100 func (nc *NodeOvercommitController) deleteNodeOvercommitConfig(obj interface{}) { 101 noc, ok := obj.(*v1alpha1.NodeOvercommitConfig) 102 if !ok { 103 klog.Errorf("cannot convert obj to *v1alpha1.NodeOvercommitConfig: %v", obj) 104 return 105 } 106 klog.V(4).Infof("[noc] notice deletion of NodeOvercommitConfig %s", native.GenerateUniqObjectNameKey(noc)) 107 108 nc.enqueueNodeOvercommitConfig(noc, configEvent) 109 } 110 111 func (nc *NodeOvercommitController) addNode(obj interface{}) { 112 node, ok := obj.(*v1.Node) 113 if !ok { 114 klog.Errorf("cannot convert obj to *v1.Node: %v", obj) 115 return 116 } 117 118 klog.V(4).Infof("[noc] notice addition of Node %s", node.Name) 119 nc.enqueueNode(node) 120 } 121 122 func (nc *NodeOvercommitController) updateNode(old, new interface{}) { 123 oldNode, ok := old.(*v1.Node) 124 if !ok { 125 klog.Errorf("cannot convert obj to *v1.Node: %v", old) 126 return 127 } 128 newNode, ok := new.(*v1.Node) 129 if !ok { 130 klog.Errorf("cannot convert obj to *v1.Node: %v", new) 131 return 132 } 133 var oldLabel, newLabel string 134 if len(oldNode.Labels) != 0 { 135 oldLabel = oldNode.Labels[consts.NodeOvercommitSelectorKey] 136 } 137 if len(newNode.Labels) != 0 { 138 newLabel = newNode.Labels[consts.NodeOvercommitSelectorKey] 139 } 140 if oldLabel == newLabel { 141 return 142 } 143 144 klog.V(4).Infof("[noc] notice update of Node %s", newNode.Name) 145 nc.enqueueNode(newNode) 146 } 147 148 func (nc *NodeOvercommitController) enqueueNodeOvercommitConfig(noc *v1alpha1.NodeOvercommitConfig, eventType eventType) { 149 if noc == nil { 150 klog.Warning("[noc] trying to enqueue a nil config") 151 return 152 } 153 154 key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(noc) 155 if err != nil { 156 utilruntime.HandleError(fmt.Errorf("couldn't get key for object %#v: %v", noc, err)) 157 return 158 } 159 160 nc.nocSyncQueue.Add(nodeOvercommitEvent{ 161 configKey: key, 162 eventType: eventType, 163 }) 164 } 165 166 func (nc *NodeOvercommitController) enqueueNode(node *v1.Node) { 167 if node == nil { 168 klog.Warning("[noc] trying to enqueue a nil node") 169 return 170 } 171 172 key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(node) 173 if err != nil { 174 utilruntime.HandleError(fmt.Errorf("couldn't get key for object %#v: %v", node, err)) 175 return 176 } 177 178 nc.nocSyncQueue.Add(nodeOvercommitEvent{ 179 nodeKey: key, 180 eventType: nodeEvent, 181 }) 182 } 183 184 func (nc *NodeOvercommitController) enqueueCNR(cnr *nodev1alpha1.CustomNodeResource) { 185 if cnr == nil { 186 klog.Warning("[noc] trying to enqueue a nil cnr") 187 return 188 } 189 190 key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(cnr) 191 if err != nil { 192 utilruntime.HandleError(fmt.Errorf("couldn't get key for object %#v: %v", cnr, err)) 193 return 194 } 195 196 nc.cnrSyncQueue.Add(key) 197 }