Skip to content

Commit

Permalink
fix: podTransitionRule webhook (#132)
Browse files Browse the repository at this point in the history
podTransitionRule webhook fix
  • Loading branch information
Eikykun committed Dec 15, 2023
1 parent 6568dac commit 3afbc70
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions pkg/controllers/podtransitionrule/processor/rules/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (w *Webhook) Do(targets map[string]*corev1.Pod, subjects sets.String) *Filt

res, err := w.polling(taskId)

w.recordTime(taskId, res.Message)
w.recordTime(taskId, res.Message, false)

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

// First request
selfTraceId, res, err := w.query(effectiveSubjects)
taskId := getTaskId(res)
klog.Infof(
"do podtransitionrule webhook [%s], pods: %v, taskId: %s, traceId: %s, resp: %s",
w.key(),
effectiveSubjects.List(),
taskId,
selfTraceId,
utils.DumpJSON(res),
)
if err != nil {
for eft := range effectiveSubjects {
rejectedPods[eft] = fmt.Sprintf(
"fail to do webhook [%s], %v, traceId %s, taskId %s",
"fail to request webhook [%s], %v, traceId %s",
w.key(),
err,
selfTraceId,
taskId,
)
}
klog.Errorf(
"fail to request podtransitionrule webhook [%s], pods: %v, traceId: %s, resp: %s",
w.key(),
effectiveSubjects.List(),
selfTraceId,
utils.DumpJSON(res),
)
return &FilterResult{
Passed: checked,
Rejected: rejectedPods,
Err: err,
RuleState: &appsv1alpha1.RuleState{Name: w.RuleName, WebhookStatus: newWebhookState},
}
}

taskId := getTaskId(res)
klog.Infof(
"request podtransitionrule webhook [%s], pods: %v, taskId: %s, traceId: %s, resp: %s",
w.key(),
effectiveSubjects.List(),
taskId,
selfTraceId,
utils.DumpJSON(res),
)
if taskId != "" {
w.recordTime(taskId, res.Message)
w.recordTime(taskId, res.Message, true)
}

localFinished := sets.NewString(res.FinishedNames...)

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

func (w *Webhook) recordTime(taskId, msg string) {
func (w *Webhook) recordTime(taskId, msg string, isFirst bool) {
timeNow := time.Now()
newRecord := &appsv1alpha1.TaskInfo{
TaskId: taskId,
BeginTime: &metav1.Time{Time: timeNow},
LastTime: &metav1.Time{Time: timeNow},
Message: msg,
}
tm := w.getTaskInfo(taskId)
if tm != nil && tm.BeginTime != nil {
newRecord.BeginTime = tm.BeginTime.DeepCopy()
if !isFirst {
tm := w.getTaskInfo(taskId)
if tm != nil && tm.BeginTime != nil {
newRecord.BeginTime = tm.BeginTime.DeepCopy()
}
}
w.taskInfo[taskId] = newRecord
}
Expand Down Expand Up @@ -527,7 +533,7 @@ func shouldPoll(resp *appsv1alpha1.WebhookResponse) bool {
}

func getTaskId(resp *appsv1alpha1.WebhookResponse) string {
if resp.Async || resp.Poll {
if resp != nil && (resp.Async || resp.Poll) {
if resp.TaskId != "" {
return resp.TaskId
}
Expand Down

0 comments on commit 3afbc70

Please sign in to comment.