Skip to content

Optimize the handling of the return messages of the extender scheduler #908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

learner0810
Copy link
Contributor

What type of PR is this?

/kind flake

What this PR does / why we need it:

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Optimize the handling of the return messages of the extender scheduler

Does this PR introduce a user-facing change?:

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Overview

This PR optimizes the handling of return messages in the extender scheduler by refactoring manual JSON marshalling and response writing to use the unrolled render package. Key changes include:

  • Removal of redundant fmt import.
  • Updated PredicateRoute and Bind handlers to use render.New(...).JSON.
  • Consistent logging of JSON rendering errors.

Reviewed Changes

File Description
pkg/scheduler/routes/route.go Refactored JSON response construction in two routes using render for cleaner code

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

w.Write(resultBody)
klog.V(5).InfoS("Returning predicate response", "result", extenderFilterResult)

err := render.New(render.Options{IndentJSON: true}).JSON(w, http.StatusOK, extenderFilterResult)
Copy link
Preview

Copilot AI Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If render.New(...).JSON fails, the client receives no fallback error response, which may lead to unexpected behavior. Consider adding a mechanism to send an appropriate error message to the client.

Copilot uses AI. Check for mistakes.

w.Write(response)
klog.V(5).InfoS("Returning bind response", "result", extenderBindingResult)

err := render.New(render.Options{IndentJSON: true}).JSON(w, http.StatusOK, extenderBindingResult)
Copy link
Preview

Copilot AI Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the Bind handler, if JSON rendering fails, no error response is sent to the client. It is recommended to include fallback error handling, similar to how errors were handled in the previous implementation.

Copilot uses AI. Check for mistakes.

err := render.New(render.Options{IndentJSON: true}).JSON(w, http.StatusOK, extenderFilterResult)
if err != nil {
klog.ErrorS(err, "Failed to write JSON response")
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When render.New(...).JSON fails, should write error info to client. like before code:

                         w.Header().Set("Content-Type", "application/json")
			w.WriteHeader(http.StatusInternalServerError)
			w.Write([]byte(err.Error()))

@wawa0210 wawa0210 requested a review from Copilot April 19, 2025 06:56
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR optimizes the handling of return messages in the extender scheduler by replacing manual JSON marshalling with a unified JSON rendering approach.

  • Replaces custom JSON marshalling and manual response writing with unrolled/render for both predicate and binding endpoints.
  • Removes unused fmt import and streamlines error handling for JSON responses.
Files not reviewed (1)
  • go.mod: Language not supported


err := render.New(render.Options{IndentJSON: true}).JSON(w, http.StatusOK, extenderFilterResult)
if err != nil {
klog.ErrorS(err, "Failed to write JSON response")
Copy link
Preview

Copilot AI Apr 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If render.New().JSON fails to write the response, it logs the error but does not set an appropriate HTTP error status or write an error message to the response body. Consider adding error handling that also sends an HTTP error code and message to the client.

Suggested change
klog.ErrorS(err, "Failed to write JSON response")
klog.ErrorS(err, "Failed to write JSON response")
http.Error(w, "Failed to write JSON response", http.StatusInternalServerError)

Copilot uses AI. Check for mistakes.


err := render.New(render.Options{IndentJSON: true}).JSON(w, http.StatusOK, extenderBindingResult)
if err != nil {
klog.ErrorS(err, "Failed to write JSON response")
Copy link
Preview

Copilot AI Apr 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If render.New().JSON fails here, the error is only logged; the client is not informed via an HTTP error status or message. Consider handling the error by setting a proper HTTP error response.

Suggested change
klog.ErrorS(err, "Failed to write JSON response")
klog.ErrorS(err, "Failed to write JSON response")
http.Error(w, "Failed to process the response", http.StatusInternalServerError)

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants