Skip to content

Commit

Permalink
fix(agentctl): prevent panic when validating integration-included cfg…
Browse files Browse the repository at this point in the history
… file (#5070)

* fix(agentctl): prevent panic when validating integration-included cfg file
Also add unit tests to prevent regression

* doc(changelog): add new BUGFIXES entry
  • Loading branch information
hainenber committed Sep 5, 2023
1 parent 1244a8d commit acc06f6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ Main (unreleased)
each time it pulls. (@erikbaranowski)

- Allow overriding default `User-Agent` for `http.remote` component (@hainenber)
- Fix panic when running `grafana-agentctl config-check` against config files
having `integrations` block (both V1 and V2). (@hainenber)

- Fix a deadlock candidate in the `loki.process` component. (@tpaschalis)

Expand Down
23 changes: 23 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,26 @@ func TestConfig_EmptyServerConfigFails(t *testing.T) {
_, err := Load(fs, []string{"--config.file", "./testdata/server_empty.yml"}, logger)
require.Error(t, err)
}

func TestConfig_ValidateConfigWithIntegrationV1(t *testing.T) {
input := util.Untab(`
integrations:
agent:
enabled: true
`)
var cfg Config
require.NoError(t, LoadBytes([]byte(input), false, &cfg))
require.NoError(t, cfg.Validate(nil))
}

func TestConfig_ValidateConfigWithIntegrationV2(t *testing.T) {
input := util.Untab(`
integrations:
agent:
autoscrape:
enabled: true
`)
var cfg Config
require.NoError(t, LoadBytes([]byte(input), false, &cfg))
require.NoError(t, cfg.Validate(nil))
}
9 changes: 7 additions & 2 deletions pkg/config/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,15 @@ func (c VersionedIntegrations) IsZero() bool {

// ApplyDefaults applies defaults to the subsystem based on globals.
func (c *VersionedIntegrations) ApplyDefaults(sflags *server.Flags, mcfg *metrics.Config) error {
if c.version != integrationsVersion2 {
switch {
case c.version != integrationsVersion2 && c.ConfigV1 != nil:
return c.ConfigV1.ApplyDefaults(sflags, mcfg)
case c.configV2 != nil:
return c.configV2.ApplyDefaults(mcfg)
default:
// No-op
return nil
}
return c.configV2.ApplyDefaults(mcfg)
}

// setVersion completes the deferred unmarshal and unmarshals the raw YAML into
Expand Down

0 comments on commit acc06f6

Please sign in to comment.