From cf3b539986bf039fbfaae66fb96c31b67bbb3182 Mon Sep 17 00:00:00 2001 From: juicer Date: Sun, 22 Oct 2023 11:33:33 -0700 Subject: [PATCH 1/2] fix autoTypeCast & renewRecord Signed-off-by: juicer --- apis/fluentd/v1alpha1/plugins/filter/types.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apis/fluentd/v1alpha1/plugins/filter/types.go b/apis/fluentd/v1alpha1/plugins/filter/types.go index 60547ff89..8ace91766 100644 --- a/apis/fluentd/v1alpha1/plugins/filter/types.go +++ b/apis/fluentd/v1alpha1/plugins/filter/types.go @@ -164,7 +164,10 @@ func (f *Filter) recordTransformerPlugin(parent *params.PluginStore, loader plug parent.InsertPairs("enable_ruby", fmt.Sprint(*f.RecordTransformer.EnableRuby)) } if f.RecordTransformer.AutoTypeCast != nil { - parent.InsertPairs("renew_record", fmt.Sprint(*f.RecordTransformer.AutoTypeCast)) + parent.InsertPairs("auto_typecast", fmt.Sprint(*f.RecordTransformer.AutoTypeCast)) + } + if f.RecordTransformer.RenewRecord != nil { + parent.InsertPairs("renew_record", fmt.Sprint(*f.RecordTransformer.RenewRecord)) } if f.RecordTransformer.RenewTimeKey != nil { parent.InsertPairs("renew_time_key", fmt.Sprint(*f.RecordTransformer.RenewTimeKey)) From 16b883d53bb0ff18e2e659f2a8286c9e9c01f5c3 Mon Sep 17 00:00:00 2001 From: juicer Date: Sun, 22 Oct 2023 12:01:58 -0700 Subject: [PATCH 2/2] add tests Signed-off-by: juicer --- ...d-cluster-cfg-filter-recordTransformer.cfg | 35 +++++++++++++++++++ apis/fluentd/v1alpha1/tests/helper_test.go | 27 ++++++++++++++ apis/fluentd/v1alpha1/tests/tools.go | 20 +++++++++++ 3 files changed, 82 insertions(+) create mode 100644 apis/fluentd/v1alpha1/tests/expected/fluentd-cluster-cfg-filter-recordTransformer.cfg diff --git a/apis/fluentd/v1alpha1/tests/expected/fluentd-cluster-cfg-filter-recordTransformer.cfg b/apis/fluentd/v1alpha1/tests/expected/fluentd-cluster-cfg-filter-recordTransformer.cfg new file mode 100644 index 000000000..1dc6756a0 --- /dev/null +++ b/apis/fluentd/v1alpha1/tests/expected/fluentd-cluster-cfg-filter-recordTransformer.cfg @@ -0,0 +1,35 @@ + + @type forward + bind 0.0.0.0 + port 24224 + + + @id main + @type label_router + + @label @a2170d34e9940ec56d328100e375c43e + + namespaces default,kube-system + + + + \ No newline at end of file diff --git a/apis/fluentd/v1alpha1/tests/helper_test.go b/apis/fluentd/v1alpha1/tests/helper_test.go index 22a8ad5a2..075e91440 100644 --- a/apis/fluentd/v1alpha1/tests/helper_test.go +++ b/apis/fluentd/v1alpha1/tests/helper_test.go @@ -607,3 +607,30 @@ func Test_DuplicateRemovalCRSpecs(t *testing.T) { i++ } } + +func Test_RecordTransformer(t *testing.T) { + g := NewGomegaWithT(t) + sl := plugins.NewSecretLoader(nil, Fluentd.Namespace, logr.Logger{}) + + psr := fluentdv1alpha1.NewGlobalPluginResources("main") + psr.CombineGlobalInputsPlugins(sl, Fluentd.Spec.GlobalInputs) + + clustercfgRouter, err := psr.BuildCfgRouter(&FluentdClusterFluentdConfig1) + g.Expect(err).NotTo(HaveOccurred()) + clusterFilters := []fluentdv1alpha1.ClusterFilter{FluentdClusterRecordTransformerFilter} + clusterOutputs := []fluentdv1alpha1.ClusterOutput{FluentdClusterOutputCluster} + clustercfgResources, _ := psr.PatchAndFilterClusterLevelResources(sl, FluentdClusterFluentdConfig1.GetCfgId(), clusterFilters, clusterOutputs) + err = psr.WithCfgResources(*clustercfgRouter.Label, clustercfgResources) + g.Expect(err).NotTo(HaveOccurred()) + + // we should not see any permutations in serialized config + i := 0 + for i < maxRuntimes { + config, errs := psr.RenderMainConfig(false) + // fmt.Println(config) + g.Expect(errs).NotTo(HaveOccurred()) + g.Expect(string(getExpectedCfg("./expected/fluentd-cluster-cfg-filter-recordTransformer.cfg"))).To(Equal(config)) + + i++ + } +} diff --git a/apis/fluentd/v1alpha1/tests/tools.go b/apis/fluentd/v1alpha1/tests/tools.go index b053c3e1e..22883582a 100644 --- a/apis/fluentd/v1alpha1/tests/tools.go +++ b/apis/fluentd/v1alpha1/tests/tools.go @@ -199,6 +199,25 @@ spec: value: ${record["kubernetes"]["namespace_name"]} ` + FluentdClusterRecordTransformerFilter fluentdv1alpha1.ClusterFilter + FluentdClusterRecordTransformerRaw = ` +apiVersion: fluentd.fluent.io/v1alpha1 +kind: ClusterFilter +metadata: + name: fluentd-filter + labels: + filter.fluentd.fluent.io/enabled: "true" +spec: + filters: + - recordTransformer: + enableRuby: true + autoTypeCast: true + renewRecord: true + records: + - key: kubernetes_ns + value: ${record["kubernetes"]["namespace_name"]} +` + FluentdClusterOutputBuffer fluentdv1alpha1.ClusterOutput FluentdClusterOutputBufferRaw = ` apiVersion: fluentd.fluent.io/v1alpha1 @@ -562,6 +581,7 @@ func init() { ParseIntoObject(FluentdConfigUser1Raw, &FluentdConfigUser1) ParseIntoObject(FluentdConfig1Raw, &FluentdConfig1) ParseIntoObject(FluentdClusterFilter1Raw, &FluentdClusterFilter1) + ParseIntoObject(FluentdClusterRecordTransformerRaw, &FluentdClusterRecordTransformerFilter) ParseIntoObject(FluentdClusterOutputClusterRaw, &FluentdClusterOutputCluster) ParseIntoObject(FluentdClusterOutputLogOperatorRaw, &FluentdClusterOutputLogOperator) ParseIntoObject(FluentdClusterOutputBufferRaw, &FluentdClusterOutputBuffer)