Skip to content
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

feat: add new endpoint for config-db push agent #574

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 0 additions & 43 deletions canary/controllers.go

This file was deleted.

4 changes: 2 additions & 2 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/flanksource/incident-commander/api"
v1 "github.com/flanksource/incident-commander/api/v1"
"github.com/flanksource/incident-commander/auth"
"github.com/flanksource/incident-commander/canary"
"github.com/flanksource/incident-commander/db"
"github.com/flanksource/incident-commander/events"
"github.com/flanksource/incident-commander/jobs"
Expand Down Expand Up @@ -161,7 +160,8 @@ func createHTTPServer(ctx api.Context) *echo.Echo {
upstreamGroup := e.Group("/upstream", rbac.Authorization(rbac.ObjectAgentPush, rbac.ActionWrite))
upstreamGroup.POST("/push", upstream.PushUpstream)
upstreamGroup.GET("/pull/:agent_name", upstream.Pull)
upstreamGroup.GET("/canary/pull/:agent_name", canary.Pull)
upstreamGroup.GET("/canary/pull/:agent_name", upstream.PullCanaries)
Copy link
Member

Choose a reason for hiding this comment

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

Can we add rbac for these ?

Copy link
Member Author

Choose a reason for hiding this comment

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

We already have it on the /upstream endpoint group.

upstreamGroup := e.Group("/upstream", rbac.Authorization(rbac.ObjectAgentPush, rbac.ActionWrite))

upstreamGroup.GET("/scrapeconfig/pull/:agent_name", upstream.PullScrapeConfigs)
upstreamGroup.GET("/status/:agent_name", upstream.Status)

playbook.RegisterRoutes(e, "playbook")
Expand Down
12 changes: 12 additions & 0 deletions db/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package db

import (
"github.com/flanksource/duty/models"
"github.com/flanksource/incident-commander/api"
"github.com/google/uuid"
)

func LookupRelatedConfigIDs(configID string, maxDepth int) ([]string, error) {
var configIDs []string

Expand All @@ -24,3 +30,9 @@ func LookupRelatedConfigIDs(configID string, maxDepth int) ([]string, error) {

return configIDs, nil
}

func GetScrapeConfigsOfAgent(ctx api.Context, agentID, since uuid.UUID) ([]models.ConfigScraper, error) {
var response []models.ConfigScraper
err := ctx.DB().Where("agent_id = ?", agentID).Where("id > ?", since).Order("id").Find(&response).Error
return response, err
}
64 changes: 64 additions & 0 deletions upstream/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,67 @@ func Status(c echo.Context) error {

return c.JSON(http.StatusOK, response)
}

// PullCanaries returns all canaries for the agent
func PullCanaries(c echo.Context) error {
ctx := c.(api.Context)

agentName := c.Param("agent_name")

agent, err := db.FindAgent(ctx, agentName)
if err != nil {
return c.JSON(http.StatusInternalServerError, api.HTTPError{Error: err.Error(), Message: "failed to get agent"})
} else if agent == nil {
return c.JSON(http.StatusNotFound, api.HTTPError{Message: fmt.Sprintf("agent(name=%s) not found", agentName)})
}

var since time.Time
if sinceRaw := c.QueryParam("since"); sinceRaw != "" {
since, err = time.Parse(time.RFC3339, sinceRaw)
if err != nil {
return c.JSON(http.StatusBadRequest, api.HTTPError{Error: err.Error(), Message: "'since' param needs to be a valid RFC3339 timestamp"})
}
}

canaries, err := db.GetCanariesOfAgent(ctx, agent.ID, since)
if err != nil {
return c.JSON(http.StatusInternalServerError, api.HTTPError{
Error: err.Error(),
Message: fmt.Sprintf("Error fetching canaries for agent(name=%s)", agentName),
})
}

return c.JSON(http.StatusOK, canaries)
}

// PullScrapeConfigs returns all scrape configs for the agent.
func PullScrapeConfigs(c echo.Context) error {
ctx := c.(api.Context)

agentName := c.Param("agent_name")

agent, err := db.FindAgent(ctx, agentName)
if err != nil {
return c.JSON(http.StatusInternalServerError, api.HTTPError{Error: err.Error(), Message: "failed to get agent"})
} else if agent == nil {
return c.JSON(http.StatusNotFound, api.HTTPError{Message: fmt.Sprintf("agent(name=%s) not found", agentName)})
}

var since uuid.UUID
if sinceRaw := c.QueryParam("since"); sinceRaw != "" {
since, err = uuid.Parse(sinceRaw)
if err != nil {
return c.JSON(http.StatusBadRequest, api.HTTPError{Error: err.Error(), Message: "'since' param needs to be a valid UUID"})
}
}

scrapeConfigs, err := db.GetScrapeConfigsOfAgent(ctx, agent.ID, since)
if err != nil {
return c.JSON(http.StatusInternalServerError, api.HTTPError{
Error: err.Error(),
Message: fmt.Sprintf("error fetching scrape configs for agent(name=%s)", agentName),
})
}

return c.JSON(http.StatusOK, scrapeConfigs)
}