diff --git a/pkgutils/configchecker.go b/pkgutils/configchecker.go index f4fa2121..23863063 100644 --- a/pkgutils/configchecker.go +++ b/pkgutils/configchecker.go @@ -11,6 +11,8 @@ import ( "github.com/dmachard/go-dnscollector/dnsutils" "github.com/dmachard/go-dnscollector/pkgconfig" "github.com/pkg/errors" + "github.com/prometheus/common/model" + "github.com/prometheus/prometheus/model/relabel" "gopkg.in/yaml.v3" ) @@ -85,6 +87,19 @@ func CheckConfig(userConfigPath string, dmRef dnsutils.DNSMessage) error { defaultConfig.IngoingTransformers.Relabeling.Remove = append(defaultConfig.IngoingTransformers.Relabeling.Remove, pkgconfig.RelabelingConfig{}) defaultConfig.IngoingTransformers.Relabeling.Rename = append(defaultConfig.IngoingTransformers.Relabeling.Rename, pkgconfig.RelabelingConfig{}) + // add fake value in relabel.Config because yaml tag is set to omitempty + // avoid this type of exception in future .... + defaultConfig.Loggers.LokiClient.RelabelConfigs = append(defaultConfig.Loggers.LokiClient.RelabelConfigs, + &relabel.Config{ + TargetLabel: "target", + SourceLabels: []model.LabelName{"source"}, + Separator: "separator", + Replacement: "replacement", + Action: relabel.Action("action"), + Regex: relabel.Regexp{}, + }, + ) + // Convert default config to map // And get unique YAML keys defaultConfigMap, err := convertConfigToMap(defaultConfig) diff --git a/pkgutils/configchecker_test.go b/pkgutils/configchecker_test.go index 33130427..8019e834 100644 --- a/pkgutils/configchecker_test.go +++ b/pkgutils/configchecker_test.go @@ -356,3 +356,49 @@ pipelines: t.Errorf("Expected error, but got nil") } } + +// test for issue https://github.com/dmachard/go-dnscollector/issues/643 +func TestConfig_CheckConfig_SpecificsItems(t *testing.T) { + userConfigFile, err := os.CreateTemp("", "user-config.yaml") + if err != nil { + t.Fatal("Error creating temporary file:", err) + } + defer os.Remove(userConfigFile.Name()) + defer userConfigFile.Close() + + userConfigContent := ` +multiplexer: + collectors: + - name: tap + dnstap: + listen-ip: 0.0.0.0 + listen-port: 6000 + loggers: + - name: loki + lokiclient: + server-url: "https://grafana-loki.example.com/loki/api/v1/push" + job-name: "dnscollector" + mode: "flat-json" + tls-insecure: true + tenant-id: fake + relabel-configs: + - source_labels: ["__dns_qtype"] + target_label: "qtype" + replacement: "test" + action: "update" + separator: "," + regex: "test" + routes: + - from: [ tap ] + to: [ loki ] +` + err = os.WriteFile(userConfigFile.Name(), []byte(userConfigContent), 0644) + if err != nil { + t.Fatal("Error writing to user configuration file:", err) + } + + dm := dnsutils.GetReferenceDNSMessage() + if err := CheckConfig(userConfigFile.Name(), dm); err != nil { + t.Errorf("failed: Unexpected error: %v", err) + } +}