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] Added source uri reloading #756

Closed
wants to merge 9 commits into from
Closed
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
2 changes: 2 additions & 0 deletions internal/pkg/agent/application/managed_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/elastic/elastic-agent/internal/pkg/agent/operation"
"github.com/elastic/elastic-agent/internal/pkg/agent/storage"
"github.com/elastic/elastic-agent/internal/pkg/agent/storage/store"
"github.com/elastic/elastic-agent/internal/pkg/artifact"
"github.com/elastic/elastic-agent/internal/pkg/capabilities"
"github.com/elastic/elastic-agent/internal/pkg/composable"
"github.com/elastic/elastic-agent/internal/pkg/config"
Expand Down Expand Up @@ -157,6 +158,7 @@ func newManaged(
},
caps,
monitor,
artifact.NewReloader(cfg.Settings.DownloadConfig, log),
)
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (h *PolicyChange) handleFleetServerHosts(ctx context.Context, c *config.Con
errors.TypeNetwork, errors.M("hosts", h.config.Fleet.Client.Hosts))
}
// discard body for proper cancellation and connection reuse
io.Copy(ioutil.Discard, resp.Body)
_, _ = io.Copy(ioutil.Discard, resp.Body)
Copy link
Member

Choose a reason for hiding this comment

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

👍

resp.Body.Close()

reader, err := fleetToReader(h.agentInfo, h.config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestPolicyChange(t *testing.T) {

conf := map[string]interface{}{"hello": "world"}
action := &fleetapi.ActionPolicyChange{
ActionID: "abc123",
ActionID: "TestPolicyChange-abc1",
ActionType: "POLICY_CHANGE",
Policy: conf,
}
Expand All @@ -69,7 +69,7 @@ func TestPolicyChange(t *testing.T) {

conf := map[string]interface{}{"hello": "world"}
action := &fleetapi.ActionPolicyChange{
ActionID: "abc123",
ActionID: "TestPolicyChange-abc2",
ActionType: "POLICY_CHANGE",
Policy: conf,
}
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestPolicyAcked(t *testing.T) {
emitter := &mockEmitter{err: mockErr}

config := map[string]interface{}{"hello": "world"}
actionID := "abc123"
actionID := "TestPolicyAcked-abc1"
action := &fleetapi.ActionPolicyChange{
ActionID: actionID,
ActionType: "POLICY_CHANGE",
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestPolicyAcked(t *testing.T) {
emitter := &mockEmitter{}

config := map[string]interface{}{"hello": "world"}
actionID := "abc123"
actionID := "TestPolicyAcked-abc2"
action := &fleetapi.ActionPolicyChange{
ActionID: actionID,
ActionType: "POLICY_CHANGE",
Expand Down
34 changes: 34 additions & 0 deletions internal/pkg/artifact/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

"github.com/elastic/elastic-agent-libs/transport/httpcommon"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
"github.com/elastic/elastic-agent/internal/pkg/config"
"github.com/elastic/elastic-agent/pkg/core/logger"
)

const (
Expand Down Expand Up @@ -46,6 +48,38 @@ type Config struct {
httpcommon.HTTPTransportSettings `config:",inline" yaml:",inline"` // Note: use anonymous struct for json inline
}

type Reloader struct {
log *logger.Logger
cfg *Config
}

func NewReloader(cfg *Config, log *logger.Logger) *Reloader {
return &Reloader{
log: log,
cfg: cfg,
}
}

func (r *Reloader) Reload(rawConfig *config.Config) error {
type c struct {
Config *Config `config:"agent.download" yaml:"agent.download" json:"agent.download"`
}

cfg := &c{
Config: DefaultConfig(),
}
if err := rawConfig.Unpack(&cfg); err != nil {
return err
}

r.log.Debugf("Source URI changed from %q to %q", r.cfg.SourceURI, cfg.Config.SourceURI)
if cfg.Config.SourceURI != "" {
r.cfg.SourceURI = cfg.Config.SourceURI
}

return nil
}

Comment on lines +51 to +82
Copy link
Member

Choose a reason for hiding this comment

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

[Blocker]
Sorry but I'm a bit puzzled here. How does it changes the source URI the agent uses? The changes made by the Reload method seem to be only local. It only affects r.cfg.SourceURI that isn't used anywhere else. Perhaps you forgot to change the SourceURI returned by DefaultConfig as elastic-agent/pull/686 did?

Also, it'd be good to add a test for that

Copy link
Contributor

Choose a reason for hiding this comment

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

r has a pointer to a config on a heap. this one is propagated down the stack to each component.
downloader has the pointer to the same thing so when it tries to resolve URI it will pick it up.
same logic applied for monitoring reloader.

// DefaultConfig creates a config with pre-set default values.
func DefaultConfig() *Config {
transport := httpcommon.DefaultHTTPTransportSettings()
Expand Down