|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes 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 queue |
| 18 | + |
| 19 | +import ( |
| 20 | + "slices" |
| 21 | + "sync" |
| 22 | + |
| 23 | + v1 "k8s.io/api/core/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/apimachinery/pkg/types" |
| 26 | + listersv1 "k8s.io/client-go/listers/core/v1" |
| 27 | + "k8s.io/klog/v2" |
| 28 | + "k8s.io/kubernetes/pkg/scheduler/framework" |
| 29 | +) |
| 30 | + |
| 31 | +// nominator is a structure that stores pods nominated to run on nodes. |
| 32 | +// It exists because nominatedNodeName of pod objects stored in the structure |
| 33 | +// may be different than what scheduler has here. We should be able to find pods |
| 34 | +// by their UID and update/delete them. |
| 35 | +type nominator struct { |
| 36 | + // nLock synchronizes all operations related to nominator. |
| 37 | + // It should not be used anywhere else. |
| 38 | + // Caution: DO NOT take ("SchedulingQueue.lock" or "activeQueue.lock") after taking "nLock". |
| 39 | + // You should always take "SchedulingQueue.lock" and "activeQueue.lock" first, |
| 40 | + // otherwise the nominator could end up in deadlock. |
| 41 | + // Correct locking order is: SchedulingQueue.lock > activeQueue.lock > nLock. |
| 42 | + nLock sync.RWMutex |
| 43 | + |
| 44 | + // podLister is used to verify if the given pod is alive. |
| 45 | + podLister listersv1.PodLister |
| 46 | + // nominatedPods is a map keyed by a node name and the value is a list of |
| 47 | + // pods which are nominated to run on the node. These are pods which can be in |
| 48 | + // the activeQ or unschedulablePods. |
| 49 | + nominatedPods map[string][]podRef |
| 50 | + // nominatedPodToNode is map keyed by a Pod UID to the node name where it is |
| 51 | + // nominated. |
| 52 | + nominatedPodToNode map[types.UID]string |
| 53 | +} |
| 54 | + |
| 55 | +func newPodNominator(podLister listersv1.PodLister) *nominator { |
| 56 | + return &nominator{ |
| 57 | + podLister: podLister, |
| 58 | + nominatedPods: make(map[string][]podRef), |
| 59 | + nominatedPodToNode: make(map[types.UID]string), |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// AddNominatedPod adds a pod to the nominated pods of the given node. |
| 64 | +// This is called during the preemption process after a node is nominated to run |
| 65 | +// the pod. We update the structure before sending a request to update the pod |
| 66 | +// object to avoid races with the following scheduling cycles. |
| 67 | +func (npm *nominator) AddNominatedPod(logger klog.Logger, pi *framework.PodInfo, nominatingInfo *framework.NominatingInfo) { |
| 68 | + npm.nLock.Lock() |
| 69 | + npm.addNominatedPodUnlocked(logger, pi, nominatingInfo) |
| 70 | + npm.nLock.Unlock() |
| 71 | +} |
| 72 | + |
| 73 | +func (npm *nominator) addNominatedPodUnlocked(logger klog.Logger, pi *framework.PodInfo, nominatingInfo *framework.NominatingInfo) { |
| 74 | + // Always delete the pod if it already exists, to ensure we never store more than |
| 75 | + // one instance of the pod. |
| 76 | + npm.deleteUnlocked(pi.Pod) |
| 77 | + |
| 78 | + var nodeName string |
| 79 | + if nominatingInfo.Mode() == framework.ModeOverride { |
| 80 | + nodeName = nominatingInfo.NominatedNodeName |
| 81 | + } else if nominatingInfo.Mode() == framework.ModeNoop { |
| 82 | + if pi.Pod.Status.NominatedNodeName == "" { |
| 83 | + return |
| 84 | + } |
| 85 | + nodeName = pi.Pod.Status.NominatedNodeName |
| 86 | + } |
| 87 | + |
| 88 | + if npm.podLister != nil { |
| 89 | + // If the pod was removed or if it was already scheduled, don't nominate it. |
| 90 | + updatedPod, err := npm.podLister.Pods(pi.Pod.Namespace).Get(pi.Pod.Name) |
| 91 | + if err != nil { |
| 92 | + logger.V(4).Info("Pod doesn't exist in podLister, aborted adding it to the nominator", "pod", klog.KObj(pi.Pod)) |
| 93 | + return |
| 94 | + } |
| 95 | + if updatedPod.Spec.NodeName != "" { |
| 96 | + logger.V(4).Info("Pod is already scheduled to a node, aborted adding it to the nominator", "pod", klog.KObj(pi.Pod), "node", updatedPod.Spec.NodeName) |
| 97 | + return |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + npm.nominatedPodToNode[pi.Pod.UID] = nodeName |
| 102 | + for _, np := range npm.nominatedPods[nodeName] { |
| 103 | + if np.uid == pi.Pod.UID { |
| 104 | + logger.V(4).Info("Pod already exists in the nominator", "pod", np.uid) |
| 105 | + return |
| 106 | + } |
| 107 | + } |
| 108 | + npm.nominatedPods[nodeName] = append(npm.nominatedPods[nodeName], podToRef(pi.Pod)) |
| 109 | +} |
| 110 | + |
| 111 | +// UpdateNominatedPod updates the <oldPod> with <newPod>. |
| 112 | +func (npm *nominator) UpdateNominatedPod(logger klog.Logger, oldPod *v1.Pod, newPodInfo *framework.PodInfo) { |
| 113 | + npm.nLock.Lock() |
| 114 | + defer npm.nLock.Unlock() |
| 115 | + // In some cases, an Update event with no "NominatedNode" present is received right |
| 116 | + // after a node("NominatedNode") is reserved for this pod in memory. |
| 117 | + // In this case, we need to keep reserving the NominatedNode when updating the pod pointer. |
| 118 | + var nominatingInfo *framework.NominatingInfo |
| 119 | + // We won't fall into below `if` block if the Update event represents: |
| 120 | + // (1) NominatedNode info is added |
| 121 | + // (2) NominatedNode info is updated |
| 122 | + // (3) NominatedNode info is removed |
| 123 | + if nominatedNodeName(oldPod) == "" && nominatedNodeName(newPodInfo.Pod) == "" { |
| 124 | + if nnn, ok := npm.nominatedPodToNode[oldPod.UID]; ok { |
| 125 | + // This is the only case we should continue reserving the NominatedNode |
| 126 | + nominatingInfo = &framework.NominatingInfo{ |
| 127 | + NominatingMode: framework.ModeOverride, |
| 128 | + NominatedNodeName: nnn, |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + // We update irrespective of the nominatedNodeName changed or not, to ensure |
| 133 | + // that pod pointer is updated. |
| 134 | + npm.deleteUnlocked(oldPod) |
| 135 | + npm.addNominatedPodUnlocked(logger, newPodInfo, nominatingInfo) |
| 136 | +} |
| 137 | + |
| 138 | +// DeleteNominatedPodIfExists deletes <pod> from nominatedPods. |
| 139 | +func (npm *nominator) DeleteNominatedPodIfExists(pod *v1.Pod) { |
| 140 | + npm.nLock.Lock() |
| 141 | + npm.deleteUnlocked(pod) |
| 142 | + npm.nLock.Unlock() |
| 143 | +} |
| 144 | + |
| 145 | +func (npm *nominator) deleteUnlocked(p *v1.Pod) { |
| 146 | + nnn, ok := npm.nominatedPodToNode[p.UID] |
| 147 | + if !ok { |
| 148 | + return |
| 149 | + } |
| 150 | + for i, np := range npm.nominatedPods[nnn] { |
| 151 | + if np.uid == p.UID { |
| 152 | + npm.nominatedPods[nnn] = append(npm.nominatedPods[nnn][:i], npm.nominatedPods[nnn][i+1:]...) |
| 153 | + if len(npm.nominatedPods[nnn]) == 0 { |
| 154 | + delete(npm.nominatedPods, nnn) |
| 155 | + } |
| 156 | + break |
| 157 | + } |
| 158 | + } |
| 159 | + delete(npm.nominatedPodToNode, p.UID) |
| 160 | +} |
| 161 | + |
| 162 | +func (npm *nominator) nominatedPodsForNode(nodeName string) []podRef { |
| 163 | + npm.nLock.RLock() |
| 164 | + defer npm.nLock.RUnlock() |
| 165 | + return slices.Clone(npm.nominatedPods[nodeName]) |
| 166 | +} |
| 167 | + |
| 168 | +// nominatedNodeName returns nominated node name of a Pod. |
| 169 | +func nominatedNodeName(pod *v1.Pod) string { |
| 170 | + return pod.Status.NominatedNodeName |
| 171 | +} |
| 172 | + |
| 173 | +type podRef struct { |
| 174 | + name string |
| 175 | + namespace string |
| 176 | + uid types.UID |
| 177 | +} |
| 178 | + |
| 179 | +func podToRef(pod *v1.Pod) podRef { |
| 180 | + return podRef{ |
| 181 | + name: pod.Name, |
| 182 | + namespace: pod.Namespace, |
| 183 | + uid: pod.UID, |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +func (np podRef) toPod() *v1.Pod { |
| 188 | + return &v1.Pod{ |
| 189 | + ObjectMeta: metav1.ObjectMeta{ |
| 190 | + Name: np.name, |
| 191 | + Namespace: np.namespace, |
| 192 | + UID: np.uid, |
| 193 | + }, |
| 194 | + } |
| 195 | +} |
0 commit comments