Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.10.0
github.com/unrolled/render v1.7.0
github.com/urfave/cli/v2 v2.27.1
golang.org/x/net v0.35.0
golang.org/x/term v0.29.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 h1:kdXcSzyDtseVEc4yCz2qF8ZrQvIDBJLl4S1c3GCXmoI=
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/unrolled/render v1.7.0 h1:1yke01/tZiZpiXfUG+zqB+6fq3G4I+KDmnh0EhPq7So=
github.com/unrolled/render v1.7.0/go.mod h1:LwQSeDhjml8NLjIO9GJO1/1qpFJxtfVIpzxXKjfVkoI=
github.com/urfave/cli v1.19.1/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
Expand Down
34 changes: 13 additions & 21 deletions pkg/scheduler/routes/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"

"github.com/julienschmidt/httprouter"
"github.com/unrolled/render"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
extenderv1 "k8s.io/kube-scheduler/extender/v1"
Expand Down Expand Up @@ -65,15 +65,12 @@
}
}

if resultBody, err := json.Marshal(extenderFilterResult); err != nil {
klog.ErrorS(err, "Failed to marshal extender filter result", "result", extenderFilterResult)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
} else {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
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

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.
if err != nil {
klog.ErrorS(err, "Failed to write JSON response")
Copy link

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.
return

Check warning on line 73 in pkg/scheduler/routes/route.go

View check run for this annotation

Codecov / codecov/patch

pkg/scheduler/routes/route.go#L68-L73

Added lines #L68 - L73 were not covered by tests
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()))

}
}
}
Expand Down Expand Up @@ -101,17 +98,12 @@
}
}

if response, err := json.Marshal(extenderBindingResult); err != nil {
klog.ErrorS(err, "Failed to marshal binding result", "result", extenderBindingResult)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
errMsg := fmt.Sprintf("{'error':'%s'}", err.Error())
w.Write([]byte(errMsg))
} else {
klog.V(5).InfoS("Returning bind response", "result", extenderBindingResult)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
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

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.
if err != nil {
klog.ErrorS(err, "Failed to write JSON response")
Copy link

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.
return

Check warning on line 106 in pkg/scheduler/routes/route.go

View check run for this annotation

Codecov / codecov/patch

pkg/scheduler/routes/route.go#L101-L106

Added lines #L101 - L106 were not covered by tests
}
}
}
Expand Down
Loading