Skip to content

Commit

Permalink
WIP - signal Fleet warning on elastic-agent status
Browse files Browse the repository at this point in the history
  • Loading branch information
pchila committed Sep 8, 2023
1 parent ac0212d commit 2a5a605
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
5 changes: 5 additions & 0 deletions internal/pkg/agent/application/coordinator/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/elastic/elastic-agent-client/v7/pkg/client"
"github.com/elastic/elastic-agent-libs/logp"
fleetgateway "github.com/elastic/elastic-agent/internal/pkg/agent/application/gateway/fleet"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/info"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/reexec"
"github.com/elastic/elastic-agent/internal/pkg/agent/configuration"
Expand Down Expand Up @@ -855,8 +856,12 @@ func (c *Coordinator) runLoopIteration(ctx context.Context) {

case configErr := <-c.managerChans.configManagerError:
if c.isManaged {
var wErr *fleetgateway.WarningError
if configErr == nil {
c.setFleetState(agentclient.Healthy, "Connected")
} else if errors.As(configErr, &wErr) {
// we received a warning from Fleet, set state to degraded and the warning as state string
c.setFleetState(agentclient.Degraded, wErr.Warning)
} else {
c.setFleetState(agentclient.Failed, configErr.Error())
}
Expand Down
16 changes: 15 additions & 1 deletion internal/pkg/agent/application/gateway/fleet/fleet_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ const fleetStateOnline = "online"
const fleetStateError = "error"
const fleetStateStarting = "starting"

// Error to be returned when we receive a warning in the Fleet response
type WarningError struct {
Warning string
}

func (w WarningError) Error() string {
return w.Warning
}

// Default Configuration for the Fleet Gateway.
var defaultGatewaySettings = &fleetGatewaySettings{
Duration: 1 * time.Second, // time between successful calls
Expand Down Expand Up @@ -222,7 +231,12 @@ func (f *fleetGateway) doExecute(ctx context.Context, bo backoff.Backoff) (*flee
}

f.checkinFailCounter = 0
f.errCh <- nil
if resp.FleetWarning != "" {
f.errCh <- &WarningError{Warning: resp.FleetWarning}
} else {
f.errCh <- nil
}

// Request was successful, return the collected actions.
return resp, nil
}
Expand Down
6 changes: 4 additions & 2 deletions internal/pkg/fleetapi/checkin_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ func (e *CheckinRequest) Validate() error {
// CheckinResponse is the response send back from the server which contains all the action that
// need to be executed or proxy to running processes.
type CheckinResponse struct {
AckToken string `json:"ack_token"`
Actions Actions `json:"actions"`
AckToken string `json:"ack_token"`
Actions Actions `json:"actions"`
FleetWarning string `json:"-"`
}

// Validate validates the response send from the server.
Expand Down Expand Up @@ -140,6 +141,7 @@ func (e *CheckinCmd) Execute(ctx context.Context, r *CheckinRequest) (*CheckinRe
}

checkinResponse := &CheckinResponse{}
checkinResponse.FleetWarning = resp.Header.Get("Warning")
decoder := json.NewDecoder(bytes.NewReader(rs))
if err := decoder.Decode(checkinResponse); err != nil {
return nil, sendDuration, errors.New(err,
Expand Down

0 comments on commit 2a5a605

Please sign in to comment.