Skip to content

Commit aadfeeb

Browse files
authored
Merge pull request #813 from googs1025/feature/contextual_logging
feature: use contextal logging
2 parents 44370e5 + 59a8b1c commit aadfeeb

22 files changed

+334
-246
lines changed

pkg/capacityscheduling/capacity_scheduling.go

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func New(ctx context.Context, obj runtime.Object, handle framework.Handle) (fram
125125
podLister: handle.SharedInformerFactory().Core().V1().Pods().Lister(),
126126
pdbLister: getPDBLister(handle.SharedInformerFactory()),
127127
}
128+
logger := klog.FromContext(ctx)
128129

129130
client, err := client.New(handle.KubeConfig(), client.Options{Scheme: scheme})
130131
if err != nil {
@@ -187,7 +188,7 @@ func New(ctx context.Context, obj runtime.Object, handle framework.Handle) (fram
187188
},
188189
},
189190
)
190-
klog.InfoS("CapacityScheduling start")
191+
logger.Info("CapacityScheduling start")
191192
return c, nil
192193
}
193194

@@ -288,17 +289,19 @@ func (c *CapacityScheduling) PreFilterExtensions() framework.PreFilterExtensions
288289

289290
// AddPod from pre-computed data in cycleState.
290291
func (c *CapacityScheduling) AddPod(ctx context.Context, cycleState *framework.CycleState, podToSchedule *v1.Pod, podToAdd *framework.PodInfo, nodeInfo *framework.NodeInfo) *framework.Status {
292+
logger := klog.FromContext(ctx)
293+
291294
elasticQuotaSnapshotState, err := getElasticQuotaSnapshotState(cycleState)
292295
if err != nil {
293-
klog.ErrorS(err, "Failed to read elasticQuotaSnapshot from cycleState", "elasticQuotaSnapshotKey", ElasticQuotaSnapshotKey)
296+
logger.Error(err, "Failed to read elasticQuotaSnapshot from cycleState", "elasticQuotaSnapshotKey", ElasticQuotaSnapshotKey)
294297
return framework.NewStatus(framework.Error, err.Error())
295298
}
296299

297300
elasticQuotaInfo := elasticQuotaSnapshotState.elasticQuotaInfos[podToAdd.Pod.Namespace]
298301
if elasticQuotaInfo != nil {
299302
err := elasticQuotaInfo.addPodIfNotPresent(podToAdd.Pod)
300303
if err != nil {
301-
klog.ErrorS(err, "Failed to add Pod to its associated elasticQuota", "pod", klog.KObj(podToAdd.Pod))
304+
logger.Error(err, "Failed to add Pod to its associated elasticQuota", "pod", klog.KObj(podToAdd.Pod))
302305
}
303306
}
304307

@@ -307,17 +310,19 @@ func (c *CapacityScheduling) AddPod(ctx context.Context, cycleState *framework.C
307310

308311
// RemovePod from pre-computed data in cycleState.
309312
func (c *CapacityScheduling) RemovePod(ctx context.Context, cycleState *framework.CycleState, podToSchedule *v1.Pod, podToRemove *framework.PodInfo, nodeInfo *framework.NodeInfo) *framework.Status {
313+
logger := klog.FromContext(ctx)
314+
310315
elasticQuotaSnapshotState, err := getElasticQuotaSnapshotState(cycleState)
311316
if err != nil {
312-
klog.ErrorS(err, "Failed to read elasticQuotaSnapshot from cycleState", "elasticQuotaSnapshotKey", ElasticQuotaSnapshotKey)
317+
logger.Error(err, "Failed to read elasticQuotaSnapshot from cycleState", "elasticQuotaSnapshotKey", ElasticQuotaSnapshotKey)
313318
return framework.NewStatus(framework.Error, err.Error())
314319
}
315320

316321
elasticQuotaInfo := elasticQuotaSnapshotState.elasticQuotaInfos[podToRemove.Pod.Namespace]
317322
if elasticQuotaInfo != nil {
318323
err = elasticQuotaInfo.deletePodIfPresent(podToRemove.Pod)
319324
if err != nil {
320-
klog.ErrorS(err, "Failed to delete Pod from its associated elasticQuota", "pod", klog.KObj(podToRemove.Pod))
325+
logger.Error(err, "Failed to delete Pod from its associated elasticQuota", "pod", klog.KObj(podToRemove.Pod))
321326
}
322327
}
323328

@@ -348,11 +353,13 @@ func (c *CapacityScheduling) Reserve(ctx context.Context, state *framework.Cycle
348353
c.Lock()
349354
defer c.Unlock()
350355

356+
logger := klog.FromContext(ctx)
357+
351358
elasticQuotaInfo := c.elasticQuotaInfos[pod.Namespace]
352359
if elasticQuotaInfo != nil {
353360
err := elasticQuotaInfo.addPodIfNotPresent(pod)
354361
if err != nil {
355-
klog.ErrorS(err, "Failed to add Pod to its associated elasticQuota", "pod", klog.KObj(pod))
362+
logger.Error(err, "Failed to add Pod to its associated elasticQuota", "pod", klog.KObj(pod))
356363
return framework.NewStatus(framework.Error, err.Error())
357364
}
358365
}
@@ -363,11 +370,13 @@ func (c *CapacityScheduling) Unreserve(ctx context.Context, state *framework.Cyc
363370
c.Lock()
364371
defer c.Unlock()
365372

373+
logger := klog.FromContext(ctx)
374+
366375
elasticQuotaInfo := c.elasticQuotaInfos[pod.Namespace]
367376
if elasticQuotaInfo != nil {
368377
err := elasticQuotaInfo.deletePodIfPresent(pod)
369378
if err != nil {
370-
klog.ErrorS(err, "Failed to delete Pod from its associated elasticQuota", "pod", klog.KObj(pod))
379+
logger.Error(err, "Failed to delete Pod from its associated elasticQuota", "pod", klog.KObj(pod))
371380
}
372381
}
373382
}
@@ -400,14 +409,16 @@ func (p *preemptor) CandidatesToVictimsMap(candidates []preemption.Candidate) ma
400409
// We look at the node that is nominated for this pod and as long as there are
401410
// terminating pods on the node, we don't consider this for preempting more pods.
402411
func (p *preemptor) PodEligibleToPreemptOthers(pod *v1.Pod, nominatedNodeStatus *framework.Status) (bool, string) {
412+
logger := klog.FromContext(context.TODO())
413+
403414
if pod.Spec.PreemptionPolicy != nil && *pod.Spec.PreemptionPolicy == v1.PreemptNever {
404-
klog.V(5).InfoS("Pod is not eligible for preemption because of its preemptionPolicy", "pod", klog.KObj(pod), "preemptionPolicy", v1.PreemptNever)
415+
logger.V(5).Info("Pod is not eligible for preemption because of its preemptionPolicy", "pod", klog.KObj(pod), "preemptionPolicy", v1.PreemptNever)
405416
return false, "not eligible due to preemptionPolicy=Never."
406417
}
407418

408419
preFilterState, err := getPreFilterState(p.state)
409420
if err != nil {
410-
klog.V(5).InfoS("Failed to read preFilterState from cycleState, err: %s", err, "preFilterStateKey", preFilterStateKey)
421+
logger.V(5).Info("Failed to read preFilterState from cycleState, err: %s", err, "preFilterStateKey", preFilterStateKey)
411422
return false, "not eligible due to failed to read from cycleState"
412423
}
413424

@@ -422,7 +433,7 @@ func (p *preemptor) PodEligibleToPreemptOthers(pod *v1.Pod, nominatedNodeStatus
422433

423434
elasticQuotaSnapshotState, err := getElasticQuotaSnapshotState(p.state)
424435
if err != nil {
425-
klog.ErrorS(err, "Failed to read elasticQuotaSnapshot from cycleState", "elasticQuotaSnapshotKey", ElasticQuotaSnapshotKey)
436+
logger.Error(err, "Failed to read elasticQuotaSnapshot from cycleState", "elasticQuotaSnapshotKey", ElasticQuotaSnapshotKey)
426437
return true, ""
427438
}
428439

@@ -480,25 +491,27 @@ func (p *preemptor) SelectVictimsOnNode(
480491
pod *v1.Pod,
481492
nodeInfo *framework.NodeInfo,
482493
pdbs []*policy.PodDisruptionBudget) ([]*v1.Pod, int, *framework.Status) {
494+
495+
logger := klog.FromContext(ctx)
496+
483497
elasticQuotaSnapshotState, err := getElasticQuotaSnapshotState(state)
484498
if err != nil {
485499
msg := "Failed to read elasticQuotaSnapshot from cycleState"
486-
klog.ErrorS(err, msg, "elasticQuotaSnapshotKey", ElasticQuotaSnapshotKey)
500+
logger.Error(err, msg, "elasticQuotaSnapshotKey", ElasticQuotaSnapshotKey)
487501
return nil, 0, framework.NewStatus(framework.Unschedulable, msg)
488502
}
489503

490504
preFilterState, err := getPreFilterState(state)
491505
if err != nil {
492506
msg := "Failed to read preFilterState from cycleState"
493-
klog.ErrorS(err, msg, "preFilterStateKey", preFilterStateKey)
507+
logger.Error(err, msg, "preFilterStateKey", preFilterStateKey)
494508
return nil, 0, framework.NewStatus(framework.Unschedulable, msg)
495509
}
496510

497511
var nominatedPodsReqInEQWithPodReq framework.Resource
498512
var nominatedPodsReqWithPodReq framework.Resource
499513
podReq := preFilterState.podReq
500514

501-
logger := klog.FromContext(ctx)
502515
removePod := func(rpi *framework.PodInfo) error {
503516
if err := nodeInfo.RemovePod(logger, rpi.Pod); err != nil {
504517
return err
@@ -625,22 +638,22 @@ func (p *preemptor) SelectVictimsOnNode(
625638
return false, err
626639
}
627640
victims = append(victims, pi.Pod)
628-
klog.V(5).InfoS("Found a potential preemption victim on node", "pod", klog.KObj(pi.Pod), "node", klog.KObj(nodeInfo.Node()))
641+
logger.V(5).Info("Found a potential preemption victim on node", "pod", klog.KObj(pi.Pod), "node", klog.KObj(nodeInfo.Node()))
629642
}
630643

631644
if preemptorWithElasticQuota && (preemptorElasticQuotaInfo.usedOverMaxWith(&nominatedPodsReqInEQWithPodReq) || elasticQuotaInfos.aggregatedUsedOverMinWith(nominatedPodsReqWithPodReq)) {
632645
if err := removePod(pi); err != nil {
633646
return false, err
634647
}
635648
victims = append(victims, pi.Pod)
636-
klog.V(5).InfoS("Found a potential preemption victim on node", "pod", klog.KObj(pi.Pod), " node", klog.KObj(nodeInfo.Node()))
649+
logger.V(5).Info("Found a potential preemption victim on node", "pod", klog.KObj(pi.Pod), " node", klog.KObj(nodeInfo.Node()))
637650
}
638651

639652
return fits, nil
640653
}
641654
for _, pi := range violatingVictims {
642655
if fits, err := reprievePod(pi); err != nil {
643-
klog.ErrorS(err, "Failed to reprieve pod", "pod", klog.KObj(pi.Pod))
656+
logger.Error(err, "Failed to reprieve pod", "pod", klog.KObj(pi.Pod))
644657
return nil, 0, framework.AsStatus(err)
645658
} else if !fits {
646659
numViolatingVictim++
@@ -649,7 +662,7 @@ func (p *preemptor) SelectVictimsOnNode(
649662
// Now we try to reprieve non-violating victims.
650663
for _, pi := range nonViolatingVictims {
651664
if _, err := reprievePod(pi); err != nil {
652-
klog.ErrorS(err, "Failed to reprieve pod", "pod", klog.KObj(pi.Pod))
665+
logger.Error(err, "Failed to reprieve pod", "pod", klog.KObj(pi.Pod))
653666
return nil, 0, framework.AsStatus(err)
654667
}
655668
}
@@ -694,6 +707,9 @@ func (c *CapacityScheduling) deleteElasticQuota(obj interface{}) {
694707
}
695708

696709
func (c *CapacityScheduling) addPod(obj interface{}) {
710+
ctx := context.TODO()
711+
logger := klog.FromContext(ctx)
712+
697713
pod := obj.(*v1.Pod)
698714

699715
c.Lock()
@@ -703,8 +719,8 @@ func (c *CapacityScheduling) addPod(obj interface{}) {
703719
// If elasticQuotaInfo is nil, try to list ElasticQuotas through elasticQuotaLister
704720
if elasticQuotaInfo == nil {
705721
var eqList v1alpha1.ElasticQuotaList
706-
if err := c.client.List(context.Background(), &eqList, client.InNamespace(pod.Namespace)); err != nil {
707-
klog.ErrorS(err, "Failed to get elasticQuota", "elasticQuota", pod.Namespace)
722+
if err := c.client.List(ctx, &eqList, client.InNamespace(pod.Namespace)); err != nil {
723+
logger.Error(err, "Failed to get elasticQuota", "elasticQuota", pod.Namespace)
708724
return
709725
}
710726

@@ -724,11 +740,13 @@ func (c *CapacityScheduling) addPod(obj interface{}) {
724740

725741
err := elasticQuotaInfo.addPodIfNotPresent(pod)
726742
if err != nil {
727-
klog.ErrorS(err, "Failed to add Pod to its associated elasticQuota", "pod", klog.KObj(pod))
743+
logger.Error(err, "Failed to add Pod to its associated elasticQuota", "pod", klog.KObj(pod))
728744
}
729745
}
730746

731747
func (c *CapacityScheduling) updatePod(oldObj, newObj interface{}) {
748+
logger := klog.FromContext(context.TODO())
749+
732750
oldPod := oldObj.(*v1.Pod)
733751
newPod := newObj.(*v1.Pod)
734752

@@ -744,13 +762,15 @@ func (c *CapacityScheduling) updatePod(oldObj, newObj interface{}) {
744762
if elasticQuotaInfo != nil {
745763
err := elasticQuotaInfo.deletePodIfPresent(newPod)
746764
if err != nil {
747-
klog.ErrorS(err, "Failed to delete Pod from its associated elasticQuota", "pod", klog.KObj(newPod))
765+
logger.Error(err, "Failed to delete Pod from its associated elasticQuota", "pod", klog.KObj(newPod))
748766
}
749767
}
750768
}
751769
}
752770

753771
func (c *CapacityScheduling) deletePod(obj interface{}) {
772+
logger := klog.FromContext(context.TODO())
773+
754774
pod := obj.(*v1.Pod)
755775
c.Lock()
756776
defer c.Unlock()
@@ -759,7 +779,7 @@ func (c *CapacityScheduling) deletePod(obj interface{}) {
759779
if elasticQuotaInfo != nil {
760780
err := elasticQuotaInfo.deletePodIfPresent(pod)
761781
if err != nil {
762-
klog.ErrorS(err, "Failed to delete Pod from its associated elasticQuota", "pod", klog.KObj(pod))
782+
logger.Error(err, "Failed to delete Pod from its associated elasticQuota", "pod", klog.KObj(pod))
763783
}
764784
}
765785
}

pkg/capacityscheduling/capacity_scheduling_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ func TestReserve(t *testing.T) {
469469

470470
state := framework.NewCycleState()
471471
for i, pod := range tt.pods {
472-
got := cs.Reserve(nil, state, pod, "node-a")
472+
got := cs.Reserve(context.TODO(), state, pod, "node-a")
473473
if got.Code() != tt.expectedCodes[i] {
474474
t.Errorf("expected %v, got %v : %v", tt.expected[i], got.Code(), got.Message())
475475
}
@@ -591,7 +591,7 @@ func TestUnreserve(t *testing.T) {
591591

592592
state := framework.NewCycleState()
593593
for i, pod := range tt.pods {
594-
cs.Unreserve(nil, state, pod, "node-a")
594+
cs.Unreserve(context.TODO(), state, pod, "node-a")
595595
if !reflect.DeepEqual(cs.elasticQuotaInfos["ns1"], tt.expected[i]["ns1"]) {
596596
t.Errorf("expected %#v, got %#v", tt.expected[i]["ns1"].Used, cs.elasticQuotaInfos["ns1"].Used)
597597
}

pkg/coscheduling/core/core.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ type Manager interface {
6565
PreFilter(context.Context, *corev1.Pod) error
6666
Permit(context.Context, *framework.CycleState, *corev1.Pod) Status
6767
GetPodGroup(context.Context, *corev1.Pod) (string, *v1alpha1.PodGroup)
68-
GetCreationTimestamp(*corev1.Pod, time.Time) time.Time
69-
DeletePermittedPodGroup(string)
70-
CalculateAssignedPods(string, string) int
71-
ActivateSiblings(pod *corev1.Pod, state *framework.CycleState)
68+
GetCreationTimestamp(context.Context, *corev1.Pod, time.Time) time.Time
69+
DeletePermittedPodGroup(context.Context, string)
70+
CalculateAssignedPods(context.Context, string, string) int
71+
ActivateSiblings(ctx context.Context, pod *corev1.Pod, state *framework.CycleState)
7272
BackoffPodGroup(string, time.Duration)
7373
}
7474

@@ -112,7 +112,8 @@ func (pgMgr *PodGroupManager) BackoffPodGroup(pgName string, backoff time.Durati
112112

113113
// ActivateSiblings stashes the pods belonging to the same PodGroup of the given pod
114114
// in the given state, with a reserved key "kubernetes.io/pods-to-activate".
115-
func (pgMgr *PodGroupManager) ActivateSiblings(pod *corev1.Pod, state *framework.CycleState) {
115+
func (pgMgr *PodGroupManager) ActivateSiblings(ctx context.Context, pod *corev1.Pod, state *framework.CycleState) {
116+
lh := klog.FromContext(ctx)
116117
pgName := util.GetPodGroupLabel(pod)
117118
if pgName == "" {
118119
return
@@ -129,7 +130,7 @@ func (pgMgr *PodGroupManager) ActivateSiblings(pod *corev1.Pod, state *framework
129130
labels.SelectorFromSet(labels.Set{v1alpha1.PodGroupLabel: pgName}),
130131
)
131132
if err != nil {
132-
klog.ErrorS(err, "Failed to obtain pods belong to a PodGroup", "podGroup", pgName)
133+
lh.Error(err, "Failed to obtain pods belong to a PodGroup", "podGroup", pgName)
133134
return
134135
}
135136

@@ -159,7 +160,8 @@ func (pgMgr *PodGroupManager) ActivateSiblings(pod *corev1.Pod, state *framework
159160
// 2. the total number of pods in the podgroup is less than the minimum number of pods
160161
// that is required to be scheduled.
161162
func (pgMgr *PodGroupManager) PreFilter(ctx context.Context, pod *corev1.Pod) error {
162-
klog.V(5).InfoS("Pre-filter", "pod", klog.KObj(pod))
163+
lh := klog.FromContext(ctx)
164+
lh.V(5).Info("Pre-filter", "pod", klog.KObj(pod))
163165
pgFullName, pg := pgMgr.GetPodGroup(ctx, pod)
164166
if pg == nil {
165167
return nil
@@ -202,7 +204,7 @@ func (pgMgr *PodGroupManager) PreFilter(ctx context.Context, pod *corev1.Pod) er
202204
minResources[corev1.ResourcePods] = *podQuantity
203205
err = CheckClusterResource(ctx, nodes, minResources, pgFullName)
204206
if err != nil {
205-
klog.ErrorS(err, "Failed to PreFilter", "podGroup", klog.KObj(pg))
207+
lh.Error(err, "Failed to PreFilter", "podGroup", klog.KObj(pg))
206208
return err
207209
}
208210
pgMgr.permittedPG.Add(pgFullName, pgFullName, *pgMgr.scheduleTimeout)
@@ -220,7 +222,7 @@ func (pgMgr *PodGroupManager) Permit(ctx context.Context, state *framework.Cycle
220222
return PodGroupNotFound
221223
}
222224

223-
assigned := pgMgr.CalculateAssignedPods(pg.Name, pg.Namespace)
225+
assigned := pgMgr.CalculateAssignedPods(ctx, pg.Name, pg.Namespace)
224226
// The number of pods that have been assigned nodes is calculated from the snapshot.
225227
// The current pod in not included in the snapshot during the current scheduling cycle.
226228
if int32(assigned)+1 >= pg.Spec.MinMember {
@@ -243,20 +245,20 @@ func (pgMgr *PodGroupManager) Permit(ctx context.Context, state *framework.Cycle
243245
}
244246

245247
// GetCreationTimestamp returns the creation time of a podGroup or a pod.
246-
func (pgMgr *PodGroupManager) GetCreationTimestamp(pod *corev1.Pod, ts time.Time) time.Time {
248+
func (pgMgr *PodGroupManager) GetCreationTimestamp(ctx context.Context, pod *corev1.Pod, ts time.Time) time.Time {
247249
pgName := util.GetPodGroupLabel(pod)
248250
if len(pgName) == 0 {
249251
return ts
250252
}
251253
var pg v1alpha1.PodGroup
252-
if err := pgMgr.client.Get(context.TODO(), types.NamespacedName{Namespace: pod.Namespace, Name: pgName}, &pg); err != nil {
254+
if err := pgMgr.client.Get(ctx, types.NamespacedName{Namespace: pod.Namespace, Name: pgName}, &pg); err != nil {
253255
return ts
254256
}
255257
return pg.CreationTimestamp.Time
256258
}
257259

258260
// DeletePermittedPodGroup deletes a podGroup that passes Pre-Filter but reaches PostFilter.
259-
func (pgMgr *PodGroupManager) DeletePermittedPodGroup(pgFullName string) {
261+
func (pgMgr *PodGroupManager) DeletePermittedPodGroup(_ context.Context, pgFullName string) {
260262
pgMgr.permittedPG.Delete(pgFullName)
261263
}
262264

@@ -274,10 +276,11 @@ func (pgMgr *PodGroupManager) GetPodGroup(ctx context.Context, pod *corev1.Pod)
274276
}
275277

276278
// CalculateAssignedPods returns the number of pods that has been assigned nodes: assumed or bound.
277-
func (pgMgr *PodGroupManager) CalculateAssignedPods(podGroupName, namespace string) int {
279+
func (pgMgr *PodGroupManager) CalculateAssignedPods(ctx context.Context, podGroupName, namespace string) int {
280+
lh := klog.FromContext(ctx)
278281
nodeInfos, err := pgMgr.snapshotSharedLister.NodeInfos().List()
279282
if err != nil {
280-
klog.ErrorS(err, "Cannot get nodeInfos from frameworkHandle")
283+
lh.Error(err, "Cannot get nodeInfos from frameworkHandle")
281284
return 0
282285
}
283286
var count int
@@ -354,6 +357,6 @@ func getNodeResource(ctx context.Context, info *framework.NodeInfo, desiredPodGr
354357
leftResource.ScalarResources[k] = allocatableEx - requestEx
355358
}
356359
}
357-
klog.V(4).InfoS("Node left resource", "node", klog.KObj(info.Node()), "resource", leftResource)
360+
logger.V(4).Info("Node left resource", "node", klog.KObj(info.Node()), "resource", leftResource)
358361
return &leftResource
359362
}

0 commit comments

Comments
 (0)