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

fix(promtail): validate scrape_config job name #13719

Merged
merged 4 commits into from
Sep 18, 2024
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
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:[email protected]/loki/api/v1/push
- external_labels:
cluster: prod1
url: https://1:[email protected]/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
Loading