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
116 changes: 113 additions & 3 deletions horizon/internal/infrastructure/controller/controller.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package controller

import (
"errors"
"fmt"
"net/http"
"strconv"
"strings"

infrastructurehandler "github.com/Meesho/BharatMLStack/horizon/internal/infrastructure/handler"
workflowHandler "github.com/Meesho/BharatMLStack/horizon/internal/workflow/handler"
"github.com/Meesho/BharatMLStack/horizon/pkg/argocd"
"github.com/gin-gonic/gin"
)

Expand All @@ -18,17 +22,22 @@ func getWorkingEnv(ctx *gin.Context) string {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "workingEnv not found in context"})
return ""
}
return workingEnv.(string)
workingEnvStr, ok := workingEnv.(string)
if !ok {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "workingEnv has invalid type"})
return ""
}
return workingEnvStr
}

type InfrastructureController struct {
handler infrastructurehandler.InfrastructureHandler
handler infrastructurehandler.InfrastructureHandler
workflowHandler workflowHandler.Handler
}

func NewController() *InfrastructureController {
return &InfrastructureController{
handler: infrastructurehandler.InitInfrastructureHandler(),
handler: infrastructurehandler.InitInfrastructureHandler(),
workflowHandler: workflowHandler.GetWorkflowHandler(),
}
}
Expand Down Expand Up @@ -61,6 +70,17 @@ type UpdateAutoscalingTriggersRequest struct {
Triggers []interface{} `json:"triggers" binding:"required"` // Array of trigger objects (CPU, cron, prometheus, etc.)
}

// ApplicationLogsQuery holds query params for GET /applications/:appName/logs
type ApplicationLogsQuery struct {
PodName string `form:"podName"`
Container string `form:"container"`
Follow bool `form:"follow"`
Previous bool `form:"previous"`
SinceSeconds string `form:"sinceSeconds"` // string int64, default "0"
TailLines string `form:"tailLines"` // string int64, default "1000"
Filter string `form:"filter"`
}

func (c *InfrastructureController) GetHPAConfig(ctx *gin.Context) {
appName := ctx.Param("appName")
workingEnv := getWorkingEnv(ctx)
Expand Down Expand Up @@ -103,6 +123,96 @@ func (c *InfrastructureController) GetResourceDetail(ctx *gin.Context) {
ctx.JSON(http.StatusOK, resourceDetail)
}

func (c *InfrastructureController) GetApplicationLogs(ctx *gin.Context) {
appName := strings.TrimSpace(ctx.Param("appName"))
workingEnv := getWorkingEnv(ctx)
if workingEnv == "" {
return
}
if appName == "" {
ctx.JSON(http.StatusBadRequest, gin.H{"error": gin.H{
"message": "appName is required",
}})
return
}

var q ApplicationLogsQuery
if err := ctx.ShouldBindQuery(&q); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": gin.H{
"message": "invalid query parameters",
}})
return
}

container := strings.TrimSpace(q.Container)
if container == "" {
ctx.JSON(http.StatusBadRequest, gin.H{
"error": gin.H{
"message": "container is required",
},
})
return
}

if q.Follow {
ctx.JSON(http.StatusBadRequest, gin.H{
"error": gin.H{
"message": "streaming (follow=true) is not supported",
},
})
return
}

sinceSeconds := int64(0)
if q.SinceSeconds != "" {
var err error
sinceSeconds, err = strconv.ParseInt(q.SinceSeconds, 10, 64)
if err != nil || sinceSeconds < 0 {
ctx.JSON(http.StatusBadRequest, gin.H{"error": gin.H{
"message": "sinceSeconds must be a non-negative integer",
}})
return
}
}
tailLines := int64(1000)
if q.TailLines != "" {
var err error
tailLines, err = strconv.ParseInt(q.TailLines, 10, 64)
if err != nil || tailLines < 0 {
ctx.JSON(http.StatusBadRequest, gin.H{"error": gin.H{
"message": "tailLines must be a non-negative integer",
}})
return
}
}
Comment on lines +177 to +187
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟑 Minor

tailLines=0 is accepted here but not honored downstream.

Line 181 accepts 0, but the URL builder later coerces non-positive values to 1000, so caller intent is lost. Please make this contract consistent (either reject 0 here or preserve 0 end-to-end).

One consistent option (reject 0 at API layer)
 	if q.TailLines != "" {
 		var err error
 		tailLines, err = strconv.ParseInt(q.TailLines, 10, 64)
-		if err != nil || tailLines < 0 {
+		if err != nil || tailLines <= 0 {
 			ctx.JSON(http.StatusBadRequest, gin.H{"error": gin.H{
-				"message": "tailLines must be a non-negative integer",
+				"message": "tailLines must be a positive integer",
 			}})
 			return
 		}
 	}
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
tailLines := int64(1000)
if q.TailLines != "" {
var err error
tailLines, err = strconv.ParseInt(q.TailLines, 10, 64)
if err != nil || tailLines < 0 {
ctx.JSON(http.StatusBadRequest, gin.H{"error": gin.H{
"message": "tailLines must be a non-negative integer",
}})
return
}
}
tailLines := int64(1000)
if q.TailLines != "" {
var err error
tailLines, err = strconv.ParseInt(q.TailLines, 10, 64)
if err != nil || tailLines <= 0 {
ctx.JSON(http.StatusBadRequest, gin.H{"error": gin.H{
"message": "tailLines must be a positive integer",
}})
return
}
}
πŸ€– Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@horizon/internal/infrastructure/controller/controller.go` around lines 177 -
187, The handler currently allows tailLines == 0 but downstream URL builder
coerces non-positive values to 1000; make the contract consistent by rejecting 0
at the API layer: in the block that parses q.TailLines (the tailLines variable
and strconv.ParseInt usage around q.TailLines), change the validation to require
tailLines > 0 and return a 400 with a message like "tailLines must be a positive
integer" when it's 0 or negative; this preserves caller intent and aligns with
the URL builder behavior.


opts := &argocd.ApplicationLogsOptions{
PodName: q.PodName,
Container: container,
Previous: q.Previous,
SinceSeconds: sinceSeconds,
TailLines: tailLines,
Filter: q.Filter,
}

entries, err := c.handler.GetApplicationLogs(appName, workingEnv, opts)
if err != nil {
status := http.StatusInternalServerError
var apiErr *argocd.ArgoCDAPIError
if errors.As(err, &apiErr) {
status = apiErr.StatusCode
}
ctx.JSON(status, gin.H{
"error": gin.H{
"message": err.Error(),
},
})
return
}

ctx.JSON(http.StatusOK, entries)
}

func (c *InfrastructureController) RestartDeployment(ctx *gin.Context) {
appName := ctx.Param("appName")
workingEnv := getWorkingEnv(ctx)
Expand Down
Loading
Loading