Skip to content

WIP: debugging 3914 #3921

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

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
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func (h *PolicyChangeHandler) Handle(ctx context.Context, a fleetapi.Action, ack
// h.signatureValidationKey = signatureValidationKey

c, err := config.NewConfigFrom(action.Policy)
h.log.Infow("handlerPolicyChange: received policy", "policy", action.Policy)
if err != nil {
return errors.New(err, "could not parse the configuration from the policy", errors.TypeConfig)
}
Expand Down
29 changes: 23 additions & 6 deletions internal/pkg/agent/application/upgrade/artifact/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,16 @@ type Config struct {

// RetrySleepInitDuration: the duration to sleep for before the first retry attempt. This duration
// will increase for subsequent retry attempts in a randomized exponential backoff manner.
RetrySleepInitDuration time.Duration `yaml:"retry_sleep_init_duration" config:"retry_sleep_init_duration"`
// This key is, for some reason, problematic
RetrySleepInitDuration time.Duration `yaml:"retry_sleep_init_duration" config:"retry_sleep_init_duration"`
AnotherRetrySleepInitDuration time.Duration `yaml:"anotherretry_sleep_init_duration" config:"anotherretry_sleep_init_duration"`
YetOdd time.Duration `yaml:"one_two_three_underscores" config:"one_two_three_underscores"`
TwoUnderscores time.Duration `yaml:"with_two_underscores" config:"with_two_underscores"`

StrRetrySleepInitDuration string `yaml:"STRretry_sleep_init_duration" config:"retry_sleep_init_duration"`
StrAnotherRetrySleepInitDuration string `yaml:"STRanotherretry_sleep_init_duration" config:"anotherretry_sleep_init_duration"`
StrYetOdd string `yaml:"STRone_two_three_underscores" config:"one_two_three_underscores"`
StrTwoUnderscores string `yaml:"STRwith_two_underscores" config:"with_two_underscores"`

httpcommon.HTTPTransportSettings `config:",inline" yaml:",inline"` // Note: use anonymous struct for json inline
}
Expand Down Expand Up @@ -161,11 +170,19 @@ func DefaultConfig() *Config {
transport.Timeout = 120 * time.Minute

return &Config{
SourceURI: DefaultSourceURI,
TargetDirectory: paths.Downloads(),
InstallPath: paths.Install(),
RetrySleepInitDuration: 30 * time.Second,
HTTPTransportSettings: transport,
SourceURI: DefaultSourceURI,
TargetDirectory: paths.Downloads(),
InstallPath: paths.Install(),
RetrySleepInitDuration: 30 * time.Second,
AnotherRetrySleepInitDuration: 30 * time.Second,
YetOdd: 30 * time.Second,
TwoUnderscores: 30 * time.Second,

StrRetrySleepInitDuration: "30 * time.Second",
StrAnotherRetrySleepInitDuration: "30 * time.Second",
StrYetOdd: "30 * time.Second",
StrTwoUnderscores: "30 * time.Second",
HTTPTransportSettings: transport,
}
}

Expand Down
2 changes: 2 additions & 0 deletions internal/pkg/agent/application/upgrade/step_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ func (u *Upgrader) downloadWithRetries(

expBo := backoff.NewExponentialBackOff()
expBo.InitialInterval = settings.RetrySleepInitDuration
u.log.Infof("*Upgrader.downloadWithRetries: expBo.InitialInterval: %s",
expBo.InitialInterval)
boCtx := backoff.WithContext(expBo, cancelCtx)

var path string
Expand Down
13 changes: 13 additions & 0 deletions internal/pkg/agent/application/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,20 @@ func (u *Upgrader) Reload(rawConfig *config.Config) error {
cfg.Settings.DownloadConfig.SourceURI = artifact.DefaultSourceURI
}

u.log.Infof("*Upgrader.Reload: received RetrySleepInitDuration: %s",
cfg.Settings.DownloadConfig.RetrySleepInitDuration)
u.log.Infof("*Upgrader.Reload: received Timeout: %s",
cfg.Settings.DownloadConfig.Timeout)

u.log.Infof("*Upgrader.Reload: OLD: u.settings.RetrySleepInitDuration: %s",
u.settings.RetrySleepInitDuration)
u.log.Infof("*Upgrader.Reload: OLD: u.settings.Timeout: %s",
u.settings.Timeout)
u.settings = cfg.Settings.DownloadConfig
u.log.Infof("*Upgrader.Reload: NEW: u.settings.RetrySleepInitDuration: %s",
u.settings.RetrySleepInitDuration)
u.log.Infof("*Upgrader.Reload: NEW: u.settings.Timeout: %s",
u.settings.Timeout)
return nil
}

Expand Down
113 changes: 112 additions & 1 deletion internal/pkg/agent/application/upgrade/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import (
"runtime"
"strings"
"testing"
"time"

"github.com/gofrs/flock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/artifact"
"github.com/elastic/elastic-agent/internal/pkg/agent/configuration"
"github.com/elastic/elastic-agent/internal/pkg/agent/errors"
"github.com/elastic/elastic-agent/internal/pkg/config"
"github.com/elastic/elastic-agent/internal/pkg/release"
Expand Down Expand Up @@ -248,12 +250,21 @@ agent.download:
cfg: `
agent.download:
source_uri: "https://this.sourceURI.co/downloads/beats/"
`},
{
name: "retry_sleep_init_duration",
sourceURL: "https://this.gets.applied",
cfg: `
agent.download:
sourceURI: "https://this.gets.applied"
retry_sleep_init_duration: 42
process.stop_timeout: "1.618s"
`},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
log, _ := logger.NewTesting("")
log, o := logger.NewTesting("")

u := Upgrader{
log: log,
Expand All @@ -272,6 +283,106 @@ agent.download:
"ProxyURI should not be nil, want %s", tc.proxyURL)
assert.Equal(t, tc.proxyURL, u.settings.Proxy.URL.String())
}

for _, l := range o.TakeAll() {
t.Logf(l.Message)
}
})
}
}

func TestUpgraderReload_odd_Unpack(t *testing.T) {
defaultcfg := configuration.DefaultConfiguration()
fleetCfg := `
agent:
download:
sourceURI: "https://this.gets.applied"
retry_sleep_init_duration: "42s"
timeout: "11s"
# dropPath: "/tmp" # not set, therefore not overridden
`
log, o := logger.NewTesting("")

u := Upgrader{
log: log,
settings: artifact.DefaultConfig(),
}

cfg, err := config.NewConfigFrom(fleetCfg)
require.NoError(t, err, "failed to create new config")

err = u.Reload(cfg)
require.NoError(t, err, "error reloading config")

// this does not work for some reason
assert.Equal(t, u.settings.RetrySleepInitDuration, 42*time.Second)

// those works as expected
assert.Equal(t, u.settings.SourceURI, "https://this.gets.applied")
assert.Equal(t, u.settings.Timeout, 11*time.Second)
assert.NotEqual(t,
u.settings.DropPath, defaultcfg.Settings.DownloadConfig.DropPath)
for _, l := range o.TakeAll() {
t.Logf(l.Message)
}
}
func TestOdd_Unpack(t *testing.T) {
defaultcfg := configuration.DefaultConfiguration()
fleetCfg := `
#retry_sleep_init_duration: "42s"
fleet:
access_api_key: "changed-api-key"
agent:
download:
sourceURI: "https://this.gets.applied"
retry_sleep_init_duration: "42s"
anotherretry_sleep_init_duration: "43s"
one_two_three_underscores: "44s"
with_two_underscores: "45s"
STRretry_sleep_init_duration: "42s"
STRanotherretry_sleep_init_duration: "43s"
STRone_two_three_underscores: "44s"
STRwith_two_underscores: "45s"
timeout: "11s"
process:
sourceURI: "https://this.gets.applied"
retry_sleep_init_duration: "42s"
anotherretry_sleep_init_duration: "43s"
one_two_three_underscores: "44s"
with_two_underscores: "45s"
timeout: "11s"
# dropPath: "/tmp" # not set, therefore not overridden
`

rawcfg, err := config.NewConfigFrom(fleetCfg)
require.NoError(t, err, "failed to create new config")

cfg, err := configuration.NewFromConfig(rawcfg)
require.NoError(t, err, "error reloading config")

// those do not work for some reason
assert.Equalf(t, 42*time.Second, cfg.Settings.DownloadConfig.RetrySleepInitDuration, "download config is wrong")
assert.Equalf(t, 43*time.Second, cfg.Settings.DownloadConfig.AnotherRetrySleepInitDuration, "download config is wrong")
assert.Equalf(t, 44*time.Second, cfg.Settings.DownloadConfig.YetOdd, "download config is wrong")
assert.Equalf(t, 45*time.Second, cfg.Settings.DownloadConfig.TwoUnderscores, "download config is wrong")

assert.Equalf(t, "42*time.Second", cfg.Settings.DownloadConfig.StrRetrySleepInitDuration, "STR download config is wrong")
assert.Equalf(t, "43*time.Second", cfg.Settings.DownloadConfig.StrAnotherRetrySleepInitDuration, "STR download config is wrong")
assert.Equalf(t, "44*time.Second", cfg.Settings.DownloadConfig.StrYetOdd, "STR download config is wrong")
assert.Equalf(t, "45*time.Second", cfg.Settings.DownloadConfig.StrTwoUnderscores, "STR download config is wrong")

// those, on the other hand, work
assert.Equalf(t, 42*time.Second, cfg.Settings.ProcessConfig.RetrySleepInitDuration, "process config is wrong")
assert.Equalf(t, 43*time.Second, cfg.Settings.ProcessConfig.AnotherRetrySleepInitDuration, "process config is wrong")
assert.Equalf(t, 44*time.Second, cfg.Settings.ProcessConfig.YetOdd, "process config is wrong")
assert.Equalf(t, 45*time.Second, cfg.Settings.ProcessConfig.TwoUnderscores, "process config is wrong")

assert.Equalf(t, defaultcfg.RetrySleepInitDuration, cfg.RetrySleepInitDuration, "top level config is wrong")

// those works as expected
assert.Equal(t, cfg.Fleet.AccessAPIKey, "changed-api-key")
assert.Equal(t, "https://this.gets.applied", cfg.Settings.DownloadConfig.SourceURI)
assert.Equal(t, 11*time.Second, cfg.Settings.DownloadConfig.Timeout)
assert.Equal(t,
defaultcfg.Settings.DownloadConfig.DropPath, cfg.Settings.DownloadConfig.DropPath)
}
15 changes: 11 additions & 4 deletions internal/pkg/agent/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,34 @@
package configuration

import (
"time"

"github.com/elastic/elastic-agent/internal/pkg/agent/errors"
"github.com/elastic/elastic-agent/internal/pkg/config"
)

// Configuration is a overall agent configuration
type Configuration struct {
Fleet *FleetAgentConfig `config:"fleet" yaml:"fleet" json:"fleet"`
Settings *SettingsConfig `config:"agent" yaml:"agent" json:"agent"`
Fleet *FleetAgentConfig `config:"fleet" yaml:"fleet" json:"fleet"`
Settings *SettingsConfig `config:"agent" yaml:"agent" json:"agent"`
RetrySleepInitDuration time.Duration `yaml:"retry_sleep_init_duration" config:"retry_sleep_init_duration"`
}

// DefaultConfiguration creates a configuration prepopulated with default values.
func DefaultConfiguration() *Configuration {
return &Configuration{
Fleet: DefaultFleetAgentConfig(),
Settings: DefaultSettingsConfig(),
RetrySleepInitDuration: 30 * time.Second,
Fleet: DefaultFleetAgentConfig(),
Settings: DefaultSettingsConfig(),
}
}

// NewFromConfig creates a configuration based on common Config.
func NewFromConfig(cfg *config.Config) (*Configuration, error) {
c := DefaultConfiguration()
// c is right, RetrySleepInitDuration is set to the default 30s
// however is retry_sleep_init_duration isn't in cfg, RetrySleepInitDuration
// is set to 0
if err := cfg.Unpack(c); err != nil {
return nil, errors.New(err, errors.TypeConfig)
}
Expand Down
7 changes: 4 additions & 3 deletions internal/pkg/agent/configuration/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ func (e *FleetAgentConfig) Valid() error {
// DefaultFleetAgentConfig creates a default configuration for fleet.
func DefaultFleetAgentConfig() *FleetAgentConfig {
return &FleetAgentConfig{
Enabled: false,
Client: remote.DefaultClientConfig(),
Info: &AgentInfo{},
AccessAPIKey: "default",
Enabled: false,
Client: remote.DefaultClientConfig(),
Info: &AgentInfo{},
}
}
10 changes: 10 additions & 0 deletions pkg/core/process/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ type Config struct {
StopTimeout time.Duration `yaml:"stop_timeout" config:"stop_timeout"`
FailureTimeout time.Duration `yaml:"failure_timeout" config:"failure_timeout"`

RetrySleepInitDuration time.Duration `yaml:"retry_sleep_init_duration" config:"retry_sleep_init_duration"`
AnotherRetrySleepInitDuration time.Duration `yaml:"anotherretry_sleep_init_duration" config:"anotherretry_sleep_init_duration"`
YetOdd time.Duration `yaml:"one_two_three_underscores" config:"one_two_three_underscores"`
TwoUnderscores time.Duration `yaml:"with_two_underscores" config:"with_two_underscores"`

// TODO: cgroups and namespaces
}

Expand All @@ -21,5 +26,10 @@ func DefaultConfig() *Config {
SpawnTimeout: 30 * time.Second,
StopTimeout: 30 * time.Second,
FailureTimeout: 10 * time.Second,

RetrySleepInitDuration: 30 * time.Second,
AnotherRetrySleepInitDuration: 30 * time.Second,
YetOdd: 30 * time.Second,
TwoUnderscores: 30 * time.Second,
}
}