From 835dfa7eb644af8347fde6d57f6a1c3c3d5893ea Mon Sep 17 00:00:00 2001 From: Liam Howe Date: Tue, 30 Apr 2024 09:58:47 +0100 Subject: [PATCH 1/3] Implement MarshalYAML for relabel.Config so that we do not generate a regex field if it was not provided in the first place Signed-off-by: Liam Howe --- model/relabel/relabel.go | 9 ++++++++ model/relabel/relabel_test.go | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/model/relabel/relabel.go b/model/relabel/relabel.go index d29c3d07aeb..692ed66c1e5 100644 --- a/model/relabel/relabel.go +++ b/model/relabel/relabel.go @@ -111,6 +111,15 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { return c.Validate() } +// MarshalYAML implements the yaml.Marshaler interface. +func (c Config) MarshalYAML() (interface{}, error) { + // Omit the regex if it is the default regex as it was not provided in the first place. + if c.Regex == DefaultRelabelConfig.Regex { + c.Regex.Regexp = nil + } + return c, nil +} + func (c *Config) Validate() error { if c.Action == "" { return fmt.Errorf("relabel action cannot be empty") diff --git a/model/relabel/relabel_test.go b/model/relabel/relabel_test.go index 6798fb02a57..4c5f4d8d12b 100644 --- a/model/relabel/relabel_test.go +++ b/model/relabel/relabel_test.go @@ -851,3 +851,42 @@ func BenchmarkRelabel(b *testing.B) { }) } } + +func TestConfig_UnmarshalThenMarshal(t *testing.T) { + tests := []struct { + name string + inputYaml string + }{ + { + name: "Values provided", + inputYaml: `source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_port] +separator: ; +regex: \\d+ +target_label: __meta_kubernetes_pod_container_port_number +replacement: $1 +action: replace +`, + }, + { + name: "No regex provided", + inputYaml: `source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_port] +separator: ; +target_label: __meta_kubernetes_pod_container_port_number +replacement: $1 +action: keepequal +`, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + unmarshalled := Config{} + err := yaml.Unmarshal([]byte(test.inputYaml), &unmarshalled) + require.NoError(t, err) + + marshalled, err := yaml.Marshal(&unmarshalled) + require.NoError(t, err) + + require.Equal(t, test.inputYaml, string(marshalled)) + }) + } +} From 5d97f17ff1fd8fdcdfed6e81588dfea21a959ca9 Mon Sep 17 00:00:00 2001 From: Liam Howe Date: Tue, 30 Apr 2024 15:45:56 +0100 Subject: [PATCH 2/3] Replace MarshalYAML() with IsZero() method for Regexp Signed-off-by: Liam Howe --- model/relabel/relabel.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/model/relabel/relabel.go b/model/relabel/relabel.go index 692ed66c1e5..3b42492342d 100644 --- a/model/relabel/relabel.go +++ b/model/relabel/relabel.go @@ -111,15 +111,6 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { return c.Validate() } -// MarshalYAML implements the yaml.Marshaler interface. -func (c Config) MarshalYAML() (interface{}, error) { - // Omit the regex if it is the default regex as it was not provided in the first place. - if c.Regex == DefaultRelabelConfig.Regex { - c.Regex.Regexp = nil - } - return c, nil -} - func (c *Config) Validate() error { if c.Action == "" { return fmt.Errorf("relabel action cannot be empty") @@ -214,6 +205,11 @@ func (re Regexp) MarshalYAML() (interface{}, error) { return nil, nil } +// IsZero implements the yaml.IsZeroer interface. +func (re Regexp) IsZero() bool { + return re.Regexp == DefaultRelabelConfig.Regex.Regexp +} + // String returns the original string used to compile the regular expression. func (re Regexp) String() string { str := re.Regexp.String() From 35d897ced4a1a4bb401a73ccf40629ca91453c18 Mon Sep 17 00:00:00 2001 From: Liam Howe Date: Mon, 6 May 2024 15:40:28 +0200 Subject: [PATCH 3/3] Add test case for default regex explicitly provided Signed-off-by: Liam Howe --- model/relabel/relabel_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/model/relabel/relabel_test.go b/model/relabel/relabel_test.go index 4c5f4d8d12b..d694982ec98 100644 --- a/model/relabel/relabel_test.go +++ b/model/relabel/relabel_test.go @@ -874,6 +874,16 @@ separator: ; target_label: __meta_kubernetes_pod_container_port_number replacement: $1 action: keepequal +`, + }, + { + name: "Default regex provided", + inputYaml: `source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_port] +separator: ; +regex: (.*) +target_label: __meta_kubernetes_pod_container_port_number +replacement: $1 +action: replace `, }, }