Skip to content
Draft
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
6 changes: 6 additions & 0 deletions server/routes/clean_response_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ func (w customResponseWriter) WriteString(s string) (int, error) {
// cleanResponseMiddleware is a Gin middleware to clean up data in the response body.
func cleanResponseMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
// Skip buffering for streaming endpoints (e.g. pod logs with follow=true),
// otherwise the response would never be written to the client.
if strings.HasSuffix(c.FullPath(), "/pods/:pod/logs") {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we should implement a generic solution for future streaming endpoints eg:, new logs API

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

wilco

c.Next()
return
}
Comment thread
vaibhavtiwari33 marked this conversation as resolved.
// Replace the original response writer with our custom one
w := &customResponseWriter{body: bytes.NewBufferString(""), ResponseWriter: c.Writer}
c.Writer = w
Expand Down
25 changes: 25 additions & 0 deletions server/routes/clean_response_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,31 @@ func TestCustomResponseWriter_Write(t *testing.T) {
})
}

func TestCleanResponseMiddleware_SkipsPodLogsStreaming(t *testing.T) {
gin.SetMode(gin.TestMode)

t.Run("pod logs route bypasses response buffering", func(t *testing.T) {
router := gin.New()
router.Use(cleanResponseMiddleware())

w := httptest.NewRecorder()
var bytesReceivedDuringHandler int
router.GET("/api/v1/namespaces/:namespace/pods/:pod/logs", func(c *gin.Context) {
_, _ = c.Writer.WriteString("log line 1\n")
c.Writer.Flush()
bytesReceivedDuringHandler = w.Body.Len()
_, _ = c.Writer.WriteString("log line 2\n")
})

req, _ := http.NewRequest(http.MethodGet, "/api/v1/namespaces/ns/pods/my-pod/logs?container=numa&follow=true", nil)
router.ServeHTTP(w, req)

assert.Greater(t, bytesReceivedDuringHandler, 0, "logs should reach the client while the handler is still streaming")
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "log line 1\nlog line 2\n", w.Body.String())
})
}

func TestCleanResponseMiddleware_EdgeCases(t *testing.T) {
gin.SetMode(gin.TestMode)

Expand Down
Loading