Skip to content

Commit

Permalink
[Elastic Agent] Add verification check when updating communication to…
Browse files Browse the repository at this point in the history
… Kibana. (#24489) (#24519)

* Add verification check when updating communication to Kibana.

* Add changelog.

* Add const.

(cherry picked from commit ad3300d)
  • Loading branch information
blakerouse committed Mar 15, 2021
1 parent 71df987 commit 58bb5c5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
- Fix bad substitution of API key. {pull}24036[24036]
- Fix docker enrollment issue related to Fleet Server change. {pull}24155[24155]
- Improve log on failure of Endpoint Security installation. {pull}24429[24429]
- Verify communication to Kibana before updating Fleet client. {pull}24489[24489]

==== New features

Expand Down
14 changes: 10 additions & 4 deletions x-pack/elastic-agent/pkg/agent/application/fleet_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,20 @@ func (f *fleetGateway) worker() {
actions[idx] = a
}

var errMsg string
if err := f.dispatcher.Dispatch(f.acker, actions...); err != nil {
msg := fmt.Sprintf("failed to dispatch actions, error: %s", err)
f.log.Error(msg)
f.statusReporter.Update(state.Degraded, msg)
errMsg = fmt.Sprintf("failed to dispatch actions, error: %s", err)
f.log.Error(errMsg)
f.statusReporter.Update(state.Failed, errMsg)
}

f.log.Debugf("FleetGateway is sleeping, next update in %s", f.settings.Duration)
f.statusReporter.Update(state.Healthy, "")
if errMsg != "" {
f.statusReporter.Update(state.Failed, errMsg)
} else {
f.statusReporter.Update(state.Healthy, "")
}

case <-f.bgContext.Done():
f.stop()
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io"
"sort"
"time"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/info"

Expand All @@ -24,6 +25,10 @@ import (
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/kibana"
)

const (
apiStatusTimeout = 15 * time.Second
)

type clientSetter interface {
SetClient(clienter)
}
Expand All @@ -50,7 +55,7 @@ func (h *handlerPolicyChange) Handle(ctx context.Context, a action, acker fleetA
}

h.log.Debugf("handlerPolicyChange: emit configuration for action %+v", a)
err = h.handleKibanaHosts(c)
err = h.handleKibanaHosts(ctx, c)
if err != nil {
return err
}
Expand All @@ -61,7 +66,7 @@ func (h *handlerPolicyChange) Handle(ctx context.Context, a action, acker fleetA
return acker.Ack(ctx, action)
}

func (h *handlerPolicyChange) handleKibanaHosts(c *config.Config) (err error) {
func (h *handlerPolicyChange) handleKibanaHosts(ctx context.Context, c *config.Config) (err error) {
// do not update kibana host from policy; no setters provided with local Fleet Server
if len(h.setters) == 0 {
return nil
Expand Down Expand Up @@ -99,6 +104,14 @@ func (h *handlerPolicyChange) handleKibanaHosts(c *config.Config) (err error) {
err, "fail to create API client with updated hosts",
errors.TypeNetwork, errors.M("hosts", h.config.Fleet.Kibana.Hosts))
}
ctx, cancel := context.WithTimeout(ctx, apiStatusTimeout)
defer cancel()
_, err = client.Send(ctx, "GET", "/api/status", nil, nil, nil)
if err != nil {
return errors.New(
err, "fail to communicate with updated API client hosts",
errors.TypeNetwork, errors.M("hosts", h.config.Fleet.Kibana.Hosts))
}
reader, err := fleetToReader(h.agentInfo, h.config)
if err != nil {
return errors.New(
Expand Down

0 comments on commit 58bb5c5

Please sign in to comment.