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

[Elastic Agent] Add verification check when updating communication to Kibana. #24489

Merged
merged 3 commits into from
Mar 12, 2021
Merged
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
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