Skip to content

Commit

Permalink
feat: add /machine/update-health handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jsiebens committed Feb 16, 2025
1 parent 4a3b539 commit ed3e1eb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
49 changes: 49 additions & 0 deletions internal/handlers/update_health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package handlers

import (
"github.com/jsiebens/ionscale/internal/domain"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
"net/http"
"tailscale.com/tailcfg"
"tailscale.com/types/key"
)

func NewUpdateHealthHandlers(machineKey key.MachinePublic, repository domain.Repository) *UpdateFeatureHandlers {
return &UpdateFeatureHandlers{machineKey: machineKey, repository: repository}
}

type UpdateFeatureHandlers struct {
machineKey key.MachinePublic
repository domain.Repository
}

func (h *UpdateFeatureHandlers) UpdateHealth(c echo.Context) error {
ctx := c.Request().Context()

req := new(tailcfg.HealthChangeRequest)
if err := c.Bind(req); err != nil {
return logError(err)
}

machineKey := h.machineKey.String()
nodeKey := req.NodeKey.String()

machine, err := h.repository.GetMachineByKeys(ctx, machineKey, nodeKey)
if err != nil {
return err
}

if machine == nil {
return echo.NewHTTPError(http.StatusBadRequest)
}

zap.L().Debug("Health checks updated",
zap.Uint64("tailnet", machine.TailnetID),
zap.Uint64("machine", machine.ID),
zap.String("subsystem", req.Subsys),
zap.String("err", req.Error),
)

return c.String(http.StatusOK, "OK")
}
2 changes: 2 additions & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func Start(ctx context.Context, c *config.Config) error {
idTokenHandlers := handlers.NewIDTokenHandlers(machinePublicKey, c, repository)
sshActionHandlers := handlers.NewSSHActionHandlers(machinePublicKey, c, repository)
queryFeatureHandlers := handlers.NewQueryFeatureHandlers(machinePublicKey, dnsProvider, repository)
updateHealthHandlers := handlers.NewUpdateHealthHandlers(machinePublicKey, repository)

e := echo.New()
e.Binder = handlers.JsonBinder{}
Expand All @@ -134,6 +135,7 @@ func Start(ctx context.Context, c *config.Config) error {
e.GET("/machine/ssh/action/:src_machine_id/to/:dst_machine_id/:check_period", sshActionHandlers.StartAuth)
e.GET("/machine/ssh/action/check/:key", sshActionHandlers.CheckAuth)
e.POST("/machine/feature/query", queryFeatureHandlers.QueryFeature)
e.POST("/machine/update-health", updateHealthHandlers.UpdateHealth)

return e
}
Expand Down

0 comments on commit ed3e1eb

Please sign in to comment.