Skip to content

Commit

Permalink
fix(promtail): validate scrape_config job name, do not allow duplicat…
Browse files Browse the repository at this point in the history
…e job names (#13719)

Signed-off-by: François Gouteroux <francois.gouteroux@gmail.com>
  • Loading branch information
fgouteroux authored Sep 18, 2024
1 parent 45b8719 commit f2d3499
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
22 changes: 22 additions & 0 deletions clients/pkg/promtail/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@ type Config struct {
WAL wal.Config `yaml:"wal"`
}

// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = Config{}
// We want to set c to the defaults and then overwrite it with the input.
// To make unmarshal fill the plain data struct rather than calling UnmarshalYAML
// again, we have to hide it using a type indirection.
type plain Config
if err := unmarshal((*plain)(c)); err != nil {
return err
}

// Validate unique names.
jobNames := map[string]struct{}{}
for _, j := range c.ScrapeConfig {
if _, ok := jobNames[j.JobName]; ok {
return fmt.Errorf("found multiple scrape configs with job name %q", j.JobName)
}
jobNames[j.JobName] = struct{}{}
}
return nil
}

// RegisterFlags with prefix registers flags where every name is prefixed by
// prefix. If prefix is a non-empty string, prefix should end with a period.
func (c *Config) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
Expand Down
35 changes: 35 additions & 0 deletions clients/pkg/promtail/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,47 @@ clients:
name: value
`

const testDuplicateJobsName = `
clients:
- external_labels:
cluster: dev1
url: https://1:shh@example.com/loki/api/v1/push
- external_labels:
cluster: prod1
url: https://1:shh@example.com/loki/api/v1/push
scrape_configs:
- job_name: kubernetes-pods-name
kubernetes_sd_configs:
- role: pod
- job_name: system
static_configs:
- targets:
- localhost
labels:
job: varlogs
- job_name: system
static_configs:
- targets:
- localhost
labels:
job: varlogs2
limits_config:
readline_rate: 100
readline_burst: 200
`

func Test_Load(t *testing.T) {
var dst Config
err := yaml.Unmarshal([]byte(testFile), &dst)
require.Nil(t, err)
}

func Test_Load_DuplicateJobsName(t *testing.T) {
var dst Config
err := yaml.Unmarshal([]byte(testDuplicateJobsName), &dst)
require.ErrorContains(t, err, `found multiple scrape configs with job name "system"`)
}

func TestHeadersConfigLoad(t *testing.T) {
var dst Config
err := yaml.Unmarshal([]byte(headersTestFile), &dst)
Expand Down

0 comments on commit f2d3499

Please sign in to comment.