Skip to content

Commit 3afbc70

Browse files
authored
fix: podTransitionRule webhook (#132)
podTransitionRule webhook fix
1 parent 6568dac commit 3afbc70

File tree

1 file changed

+26
-20
lines changed
  • pkg/controllers/podtransitionrule/processor/rules

1 file changed

+26
-20
lines changed

pkg/controllers/podtransitionrule/processor/rules/webhook.go

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func (w *Webhook) Do(targets map[string]*corev1.Pod, subjects sets.String) *Filt
218218

219219
res, err := w.polling(taskId)
220220

221-
w.recordTime(taskId, res.Message)
221+
w.recordTime(taskId, res.Message, false)
222222

223223
if err != nil {
224224
newWebhookState.ItemStatus = appendStatus(newWebhookState.ItemStatus, pods, func(po string) bool {
@@ -304,37 +304,41 @@ func (w *Webhook) Do(targets map[string]*corev1.Pod, subjects sets.String) *Filt
304304

305305
// First request
306306
selfTraceId, res, err := w.query(effectiveSubjects)
307-
taskId := getTaskId(res)
308-
klog.Infof(
309-
"do podtransitionrule webhook [%s], pods: %v, taskId: %s, traceId: %s, resp: %s",
310-
w.key(),
311-
effectiveSubjects.List(),
312-
taskId,
313-
selfTraceId,
314-
utils.DumpJSON(res),
315-
)
316307
if err != nil {
317308
for eft := range effectiveSubjects {
318309
rejectedPods[eft] = fmt.Sprintf(
319-
"fail to do webhook [%s], %v, traceId %s, taskId %s",
310+
"fail to request webhook [%s], %v, traceId %s",
320311
w.key(),
321312
err,
322313
selfTraceId,
323-
taskId,
324314
)
325315
}
316+
klog.Errorf(
317+
"fail to request podtransitionrule webhook [%s], pods: %v, traceId: %s, resp: %s",
318+
w.key(),
319+
effectiveSubjects.List(),
320+
selfTraceId,
321+
utils.DumpJSON(res),
322+
)
326323
return &FilterResult{
327324
Passed: checked,
328325
Rejected: rejectedPods,
329326
Err: err,
330327
RuleState: &appsv1alpha1.RuleState{Name: w.RuleName, WebhookStatus: newWebhookState},
331328
}
332329
}
333-
330+
taskId := getTaskId(res)
331+
klog.Infof(
332+
"request podtransitionrule webhook [%s], pods: %v, taskId: %s, traceId: %s, resp: %s",
333+
w.key(),
334+
effectiveSubjects.List(),
335+
taskId,
336+
selfTraceId,
337+
utils.DumpJSON(res),
338+
)
334339
if taskId != "" {
335-
w.recordTime(taskId, res.Message)
340+
w.recordTime(taskId, res.Message, true)
336341
}
337-
338342
localFinished := sets.NewString(res.FinishedNames...)
339343

340344
if !res.Success {
@@ -403,17 +407,19 @@ func (w *Webhook) convTaskInfo(infoMap map[string]*appsv1alpha1.TaskInfo) []apps
403407
return states
404408
}
405409

406-
func (w *Webhook) recordTime(taskId, msg string) {
410+
func (w *Webhook) recordTime(taskId, msg string, isFirst bool) {
407411
timeNow := time.Now()
408412
newRecord := &appsv1alpha1.TaskInfo{
409413
TaskId: taskId,
410414
BeginTime: &metav1.Time{Time: timeNow},
411415
LastTime: &metav1.Time{Time: timeNow},
412416
Message: msg,
413417
}
414-
tm := w.getTaskInfo(taskId)
415-
if tm != nil && tm.BeginTime != nil {
416-
newRecord.BeginTime = tm.BeginTime.DeepCopy()
418+
if !isFirst {
419+
tm := w.getTaskInfo(taskId)
420+
if tm != nil && tm.BeginTime != nil {
421+
newRecord.BeginTime = tm.BeginTime.DeepCopy()
422+
}
417423
}
418424
w.taskInfo[taskId] = newRecord
419425
}
@@ -527,7 +533,7 @@ func shouldPoll(resp *appsv1alpha1.WebhookResponse) bool {
527533
}
528534

529535
func getTaskId(resp *appsv1alpha1.WebhookResponse) string {
530-
if resp.Async || resp.Poll {
536+
if resp != nil && (resp.Async || resp.Poll) {
531537
if resp.TaskId != "" {
532538
return resp.TaskId
533539
}

0 commit comments

Comments
 (0)