From d33e36bb14fd959235b264fb9d2c080bee907699 Mon Sep 17 00:00:00 2001 From: Srinivas Chippagiri Date: Fri, 6 Nov 2026 20:48:03 -0800 Subject: [PATCH 1/7] feat(fluentbit): let a ClusterFilter run after the namespaced Filters Signed-off-by: Srinivas Chippagiri --- .../fluentbit/v1alpha2/clusterfilter_types.go | 33 +++++++-- .../v1alpha2/clusterfluentbitconfig_types.go | 32 +++++---- .../clusterfluentbitconfig_types_test.go | 71 +++++++++++++++++++ .../fluentbit.fluent.io_clusterfilters.yaml | 6 ++ .../fluentbit.fluent.io_filters.yaml | 6 ++ .../fluentbit.fluent.io_clusterfilters.yaml | 6 ++ .../crds/fluentbit.fluent.io_filters.yaml | 6 ++ .../fluentbit.fluent.io_clusterfilters.yaml | 6 ++ .../bases/fluentbit.fluent.io_filters.yaml | 6 ++ docs/fluentbit.md | 1 + manifests/setup/setup.yaml | 12 ++++ 11 files changed, 169 insertions(+), 16 deletions(-) diff --git a/apis/fluentbit/v1alpha2/clusterfilter_types.go b/apis/fluentbit/v1alpha2/clusterfilter_types.go index 583bb24da..94495bb69 100644 --- a/apis/fluentbit/v1alpha2/clusterfilter_types.go +++ b/apis/fluentbit/v1alpha2/clusterfilter_types.go @@ -46,6 +46,10 @@ type FilterSpec struct { FilterItems []FilterItem `json:"filters,omitempty"` // An ordinal to influence filter ordering Ordinal int32 `json:"ordinal,omitempty"` + // AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, + // so it processes records after they have been through them. ClusterFilters run before the + // namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + AfterNamespaceFilters bool `json:"afterNamespaceFilters,omitempty"` } type FilterItem struct { @@ -175,14 +179,35 @@ func (list ClusterFilterList) Load(sl plugins.SecretLoader) (string, error) { return buf.String(), nil } -func (list ClusterFilterList) LoadAsYaml(sl plugins.SecretLoader, depth int) (string, error) { - var buf bytes.Buffer +// SplitByNamespaceFilters splits the list into the ClusterFilters that run before the namespaced +// Filters and those that run after them (see FilterSpec.AfterNamespaceFilters). +func (list ClusterFilterList) SplitByNamespaceFilters() (before, after ClusterFilterList) { + for _, item := range list.Items { + if item.Spec.AfterNamespaceFilters { + after.Items = append(after.Items, item) + } else { + before.Items = append(before.Items, item) + } + } + return before, after +} - sort.Sort(FilterByOrdinalAndName(list.Items)) +func (list ClusterFilterList) LoadAsYaml(sl plugins.SecretLoader, depth int) (string, error) { if len(list.Items) == 0 { return "", nil } - fmt.Fprintf(&buf, "%sfilters:\n", utils.YamlIndent(depth)) + items, err := list.loadYamlItems(sl, depth) + if err != nil { + return "", err + } + return fmt.Sprintf("%sfilters:\n", utils.YamlIndent(depth)) + items, nil +} + +// loadYamlItems renders the filters as YAML list items, without the "filters:" header. +func (list ClusterFilterList) loadYamlItems(sl plugins.SecretLoader, depth int) (string, error) { + var buf bytes.Buffer + + sort.Sort(FilterByOrdinalAndName(list.Items)) padding := utils.YamlIndent(depth + 2) for _, item := range list.Items { diff --git a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go index b4a3719be..794ba4cd2 100644 --- a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go +++ b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go @@ -305,7 +305,12 @@ func (cfg ClusterFluentBitConfig) RenderMainConfigInYaml( return "", fmt.Errorf("failed to load inputs: %w", err) } - filterSections, err := filters.LoadAsYaml(sl, 1) + beforeFilters, afterFilters := filters.SplitByNamespaceFilters() + filterSections, err := beforeFilters.loadYamlItems(sl, 1) + if err != nil { + return "", fmt.Errorf("failed to load filters: %w", err) + } + afterFilterSections, err := afterFilters.loadYamlItems(sl, 1) if err != nil { return "", fmt.Errorf("failed to load filters: %w", err) } @@ -362,24 +367,21 @@ func (cfg ClusterFluentBitConfig) RenderMainConfigInYaml( } buf.WriteString(inputSections) - // rewriteTagConfigs entries are headerless "filters:" list items (see - // generateRewriteTagConfig), so they must share a single "filters:" - // header with filterSections/nsFilterSections rather than writing their - // own, which would otherwise produce duplicate "filters:" keys. - if filterSections == "" && (nsFilterSections != nil || len(rewriteTagConfigs) > 0) { + // The cluster filters, rewriteTagConfigs entries (see generateRewriteTagConfig) + // and namespaced filters are all headerless "filters:" list items, so they + // share a single "filters:" header; one header per section would produce + // duplicate "filters:" keys. + if len(filters.Items) > 0 || nsFilterSections != nil || len(rewriteTagConfigs) > 0 { fmt.Fprintf(&buf, "%sfilters:\n", utils.YamlIndent(1)) - } else { - // 1. filterSections == "" && nsFilterSections == nil && rewriteTagConfigs empty - // 2. filterSections != "" && (nsFilterSections != nil || rewriteTagConfigs non-empty) - // 3. filterSections != "" && nsFilterSections == nil && rewriteTagConfigs empty - buf.WriteString(filterSections) } + buf.WriteString(filterSections) for _, rtc := range rewriteTagConfigs { buf.WriteString(rtc) } for _, filters := range nsFilterSections { buf.WriteString(filters) } + buf.WriteString(afterFilterSections) if outputSections == "" && nsOutputSections != nil { fmt.Fprintf(&buf, "%soutputs:\n", utils.YamlIndent(1)) } else { @@ -416,7 +418,12 @@ func (cfg ClusterFluentBitConfig) RenderMainConfig( return "", err } - filterSections, err := filters.Load(sl) + beforeFilters, afterFilters := filters.SplitByNamespaceFilters() + filterSections, err := beforeFilters.Load(sl) + if err != nil { + return "", err + } + afterFilterSections, err := afterFilters.Load(sl) if err != nil { return "", err } @@ -472,6 +479,7 @@ func (cfg ClusterFluentBitConfig) RenderMainConfig( for _, filters := range nsFilterSections { buf.WriteString(filters) } + buf.WriteString(afterFilterSections) for _, outputs := range nsOutputSections { buf.WriteString(outputs) } diff --git a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go index f470acdbf..f146ecbfd 100644 --- a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go +++ b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go @@ -1521,3 +1521,74 @@ func TestRenderMainConfigInYaml_RewriteTagConfigMergesIntoSingleFiltersSection(t g.Expect(ok).To(BeTrue()) g.Expect(filterEntries).To(HaveLen(2)) } + +func TestRenderMainConfig_ClusterFilterAfterNamespaceFilters(t *testing.T) { + g := NewGomegaWithT(t) + sl := plugins.NewSecretLoader(nil, "testnamespace") + cfg := ClusterFluentBitConfig{} + + modify := func(value string) []FilterItem { + return []FilterItem{{Modify: &filter.Modify{ + Rules: []filter.Rule{{Set: map[string]string{"stage": value}}}, + }}} + } + + inputs := ClusterInputList{ + Items: []ClusterInput{{ + ObjectMeta: metav1.ObjectMeta{Name: "input0"}, + Spec: InputSpec{Tail: &input.Tail{Tag: "kube.*", Path: "/var/log/containers/*.log"}}, + }}, + } + // The ordinal of the "after" filter is lower, it must still be placed after the namespaced filters. + filters := ClusterFilterList{ + Items: []ClusterFilter{ + { + ObjectMeta: metav1.ObjectMeta{Name: "after"}, + Spec: FilterSpec{Match: "kube.*", Ordinal: -1, AfterNamespaceFilters: true, FilterItems: modify("cluster-after")}, + }, + { + ObjectMeta: metav1.ObjectMeta{Name: "before"}, + Spec: FilterSpec{Match: "kube.*", FilterItems: modify("cluster-before")}, + }, + }, + } + nsFilters := []FilterList{{ + Items: []Filter{{ + ObjectMeta: metav1.ObjectMeta{Name: "ns", Namespace: "foo"}, + Spec: FilterSpec{Match: "kube.*", FilterItems: modify("namespaced")}, + }}, + }} + + expectOrder := func(config string) { + before := strings.Index(config, "cluster-before") + namespaced := strings.Index(config, "namespaced") + after := strings.Index(config, "cluster-after") + g.Expect(before).To(BeNumerically(">=", 0)) + g.Expect(namespaced).To(BeNumerically(">", before)) + g.Expect(after).To(BeNumerically(">", namespaced)) + } + + config, err := cfg.RenderMainConfig(sl, inputs, filters, ClusterOutputList{}, nsFilters, nil, nil) + g.Expect(err).NotTo(HaveOccurred()) + expectOrder(config) + + yamlConfig, err := cfg.RenderMainConfigInYaml(sl, inputs, filters, ClusterOutputList{}, nsFilters, nil, nil) + g.Expect(err).NotTo(HaveOccurred()) + expectOrder(yamlConfig) + g.Expect(strings.Count(yamlConfig, "filters:\n")).To(Equal(1)) + + var parsed map[string]any + g.Expect(yaml.Unmarshal([]byte(yamlConfig), &parsed)).To(Succeed()) + pipeline, ok := parsed["pipeline"].(map[string]any) + g.Expect(ok).To(BeTrue()) + g.Expect(pipeline["filters"]).To(HaveLen(3)) + + // With only "after" cluster filters and no namespaced filters, the YAML still has a filters header. + _, afterOnly := filters.SplitByNamespaceFilters() + yamlConfig, err = cfg.RenderMainConfigInYaml(sl, inputs, afterOnly, ClusterOutputList{}, nil, nil, nil) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(yaml.Unmarshal([]byte(yamlConfig), &parsed)).To(Succeed()) + pipeline, ok = parsed["pipeline"].(map[string]any) + g.Expect(ok).To(BeTrue()) + g.Expect(pipeline["filters"]).To(HaveLen(1)) +} diff --git a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml index 05c9fa911..29787e166 100644 --- a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml +++ b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml @@ -42,6 +42,12 @@ spec: spec: description: Specification of desired Filter configuration. properties: + afterNamespaceFilters: + description: |- + AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, + so it processes records after they have been through them. ClusterFilters run before the + namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + type: boolean filters: description: A set of filter plugins in order. items: diff --git a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml index 0c89e284e..f7f7c7f32 100644 --- a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml +++ b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml @@ -42,6 +42,12 @@ spec: spec: description: FilterSpec defines the desired state of ClusterFilter properties: + afterNamespaceFilters: + description: |- + AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, + so it processes records after they have been through them. ClusterFilters run before the + namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + type: boolean filters: description: A set of filter plugins in order. items: diff --git a/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml b/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml index 5d403736d..00a051f09 100644 --- a/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml +++ b/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml @@ -40,6 +40,12 @@ spec: spec: description: Specification of desired Filter configuration. properties: + afterNamespaceFilters: + description: |- + AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, + so it processes records after they have been through them. ClusterFilters run before the + namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + type: boolean filters: description: A set of filter plugins in order. items: diff --git a/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml b/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml index 0a12abc82..0095324d7 100644 --- a/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml +++ b/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml @@ -40,6 +40,12 @@ spec: spec: description: FilterSpec defines the desired state of ClusterFilter properties: + afterNamespaceFilters: + description: |- + AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, + so it processes records after they have been through them. ClusterFilters run before the + namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + type: boolean filters: description: A set of filter plugins in order. items: diff --git a/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml b/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml index 18cec014d..eea32ced2 100644 --- a/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml +++ b/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml @@ -41,6 +41,12 @@ spec: spec: description: Specification of desired Filter configuration. properties: + afterNamespaceFilters: + description: |- + AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, + so it processes records after they have been through them. ClusterFilters run before the + namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + type: boolean filters: description: A set of filter plugins in order. items: diff --git a/config/crd/bases/fluentbit.fluent.io_filters.yaml b/config/crd/bases/fluentbit.fluent.io_filters.yaml index 7450c5b0d..dcc5af568 100644 --- a/config/crd/bases/fluentbit.fluent.io_filters.yaml +++ b/config/crd/bases/fluentbit.fluent.io_filters.yaml @@ -41,6 +41,12 @@ spec: spec: description: FilterSpec defines the desired state of ClusterFilter properties: + afterNamespaceFilters: + description: |- + AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, + so it processes records after they have been through them. ClusterFilters run before the + namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + type: boolean filters: description: A set of filter plugins in order. items: diff --git a/docs/fluentbit.md b/docs/fluentbit.md index 4c0fb67e5..97b7c7b5c 100644 --- a/docs/fluentbit.md +++ b/docs/fluentbit.md @@ -314,6 +314,7 @@ FilterSpec defines the desired state of ClusterFilter | logLevel | | string | | filters | A set of filter plugins in order. | [][FilterItem](#filteritem) | | ordinal | An ordinal to influence filter ordering | int32 | +| afterNamespaceFilters | AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. | bool | [Back to TOC](#table-of-contents) # FluentBit diff --git a/manifests/setup/setup.yaml b/manifests/setup/setup.yaml index 6cb2e3153..f0361d8de 100644 --- a/manifests/setup/setup.yaml +++ b/manifests/setup/setup.yaml @@ -40,6 +40,12 @@ spec: spec: description: Specification of desired Filter configuration. properties: + afterNamespaceFilters: + description: |- + AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, + so it processes records after they have been through them. ClusterFilters run before the + namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + type: boolean filters: description: A set of filter plugins in order. items: @@ -16277,6 +16283,12 @@ spec: spec: description: FilterSpec defines the desired state of ClusterFilter properties: + afterNamespaceFilters: + description: |- + AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, + so it processes records after they have been through them. ClusterFilters run before the + namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + type: boolean filters: description: A set of filter plugins in order. items: From c56e7cf73dc47c669d19916350b189ca56f8c49c Mon Sep 17 00:00:00 2001 From: Srinivas Chippagiri Date: Tue, 10 Nov 2026 16:36:51 -0800 Subject: [PATCH 2/7] feat(fluentbit): reject afterNamespaceFilters on namespaced Filters and tidy the ordering docs and test Signed-off-by: Srinivas Chippagiri --- .../fluentbit/v1alpha2/clusterfilter_types.go | 8 +++---- .../v1alpha2/clusterfluentbitconfig_types.go | 4 ++-- .../clusterfluentbitconfig_types_test.go | 21 +++++++++++-------- apis/fluentbit/v1alpha2/filter_types.go | 1 + .../fluentbit.fluent.io_clusterfilters.yaml | 4 ++-- .../fluentbit.fluent.io_filters.yaml | 7 +++++-- .../fluentbit.fluent.io_clusterfilters.yaml | 4 ++-- .../crds/fluentbit.fluent.io_filters.yaml | 7 +++++-- .../fluentbit.fluent.io_clusterfilters.yaml | 4 ++-- .../bases/fluentbit.fluent.io_filters.yaml | 7 +++++-- docs/best-practice/misc.md | 20 ++++++++++++++++++ docs/fluentbit.md | 4 ++-- manifests/setup/setup.yaml | 11 ++++++---- 13 files changed, 69 insertions(+), 33 deletions(-) diff --git a/apis/fluentbit/v1alpha2/clusterfilter_types.go b/apis/fluentbit/v1alpha2/clusterfilter_types.go index 94495bb69..9125d9efe 100644 --- a/apis/fluentbit/v1alpha2/clusterfilter_types.go +++ b/apis/fluentbit/v1alpha2/clusterfilter_types.go @@ -44,11 +44,11 @@ type FilterSpec struct { LogLevel string `json:"logLevel,omitempty"` // A set of filter plugins in order. FilterItems []FilterItem `json:"filters,omitempty"` - // An ordinal to influence filter ordering + // An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them Ordinal int32 `json:"ordinal,omitempty"` // AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, // so it processes records after they have been through them. ClusterFilters run before the - // namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + // namespaced Filters by default. Only applies to ClusterFilters, a namespaced Filter that sets it is rejected. AfterNamespaceFilters bool `json:"afterNamespaceFilters,omitempty"` } @@ -179,9 +179,9 @@ func (list ClusterFilterList) Load(sl plugins.SecretLoader) (string, error) { return buf.String(), nil } -// SplitByNamespaceFilters splits the list into the ClusterFilters that run before the namespaced +// splitByNamespaceFilters splits the list into the ClusterFilters that run before the namespaced // Filters and those that run after them (see FilterSpec.AfterNamespaceFilters). -func (list ClusterFilterList) SplitByNamespaceFilters() (before, after ClusterFilterList) { +func (list ClusterFilterList) splitByNamespaceFilters() (before, after ClusterFilterList) { for _, item := range list.Items { if item.Spec.AfterNamespaceFilters { after.Items = append(after.Items, item) diff --git a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go index 794ba4cd2..56ccbb6c9 100644 --- a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go +++ b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go @@ -305,7 +305,7 @@ func (cfg ClusterFluentBitConfig) RenderMainConfigInYaml( return "", fmt.Errorf("failed to load inputs: %w", err) } - beforeFilters, afterFilters := filters.SplitByNamespaceFilters() + beforeFilters, afterFilters := filters.splitByNamespaceFilters() filterSections, err := beforeFilters.loadYamlItems(sl, 1) if err != nil { return "", fmt.Errorf("failed to load filters: %w", err) @@ -418,7 +418,7 @@ func (cfg ClusterFluentBitConfig) RenderMainConfig( return "", err } - beforeFilters, afterFilters := filters.SplitByNamespaceFilters() + beforeFilters, afterFilters := filters.splitByNamespaceFilters() filterSections, err := beforeFilters.Load(sl) if err != nil { return "", err diff --git a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go index f146ecbfd..c127b1030 100644 --- a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go +++ b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go @@ -1544,25 +1544,25 @@ func TestRenderMainConfig_ClusterFilterAfterNamespaceFilters(t *testing.T) { Items: []ClusterFilter{ { ObjectMeta: metav1.ObjectMeta{Name: "after"}, - Spec: FilterSpec{Match: "kube.*", Ordinal: -1, AfterNamespaceFilters: true, FilterItems: modify("cluster-after")}, + Spec: FilterSpec{Match: "kube.*", Ordinal: -1, AfterNamespaceFilters: true, FilterItems: modify("stage-cluster-after")}, }, { ObjectMeta: metav1.ObjectMeta{Name: "before"}, - Spec: FilterSpec{Match: "kube.*", FilterItems: modify("cluster-before")}, + Spec: FilterSpec{Match: "kube.*", FilterItems: modify("stage-cluster-before")}, }, }, } nsFilters := []FilterList{{ Items: []Filter{{ ObjectMeta: metav1.ObjectMeta{Name: "ns", Namespace: "foo"}, - Spec: FilterSpec{Match: "kube.*", FilterItems: modify("namespaced")}, + Spec: FilterSpec{Match: "kube.*", FilterItems: modify("stage-namespaced")}, }}, }} expectOrder := func(config string) { - before := strings.Index(config, "cluster-before") - namespaced := strings.Index(config, "namespaced") - after := strings.Index(config, "cluster-after") + before := strings.Index(config, "stage-cluster-before") + namespaced := strings.Index(config, "stage-namespaced") + after := strings.Index(config, "stage-cluster-after") g.Expect(before).To(BeNumerically(">=", 0)) g.Expect(namespaced).To(BeNumerically(">", before)) g.Expect(after).To(BeNumerically(">", namespaced)) @@ -1584,11 +1584,14 @@ func TestRenderMainConfig_ClusterFilterAfterNamespaceFilters(t *testing.T) { g.Expect(pipeline["filters"]).To(HaveLen(3)) // With only "after" cluster filters and no namespaced filters, the YAML still has a filters header. - _, afterOnly := filters.SplitByNamespaceFilters() + // afterOnly holds just the "after" ClusterFilter, so the before group and the namespaced Filters are + // both empty and nothing else would write the header. + _, afterOnly := filters.splitByNamespaceFilters() yamlConfig, err = cfg.RenderMainConfigInYaml(sl, inputs, afterOnly, ClusterOutputList{}, nil, nil, nil) g.Expect(err).NotTo(HaveOccurred()) - g.Expect(yaml.Unmarshal([]byte(yamlConfig), &parsed)).To(Succeed()) - pipeline, ok = parsed["pipeline"].(map[string]any) + var afterOnlyParsed map[string]any + g.Expect(yaml.Unmarshal([]byte(yamlConfig), &afterOnlyParsed)).To(Succeed()) + pipeline, ok = afterOnlyParsed["pipeline"].(map[string]any) g.Expect(ok).To(BeTrue()) g.Expect(pipeline["filters"]).To(HaveLen(1)) } diff --git a/apis/fluentbit/v1alpha2/filter_types.go b/apis/fluentbit/v1alpha2/filter_types.go index 4bfda624e..528618558 100644 --- a/apis/fluentbit/v1alpha2/filter_types.go +++ b/apis/fluentbit/v1alpha2/filter_types.go @@ -39,6 +39,7 @@ type Filter struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` + // +kubebuilder:validation:XValidation:rule="!has(self.afterNamespaceFilters) || !self.afterNamespaceFilters",message="afterNamespaceFilters only applies to ClusterFilters" Spec FilterSpec `json:"spec,omitempty"` } diff --git a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml index 29787e166..2685c0951 100644 --- a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml +++ b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml @@ -46,7 +46,7 @@ spec: description: |- AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the - namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + namespaced Filters by default. Only applies to ClusterFilters, a namespaced Filter that sets it is rejected. type: boolean filters: description: A set of filter plugins in order. @@ -1005,7 +1005,7 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering + description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them format: int32 type: integer type: object diff --git a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml index f7f7c7f32..61c1b29f6 100644 --- a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml +++ b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml @@ -46,7 +46,7 @@ spec: description: |- AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the - namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + namespaced Filters by default. Only applies to ClusterFilters, a namespaced Filter that sets it is rejected. type: boolean filters: description: A set of filter plugins in order. @@ -1005,10 +1005,13 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering + description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them format: int32 type: integer type: object + x-kubernetes-validations: + - message: afterNamespaceFilters only applies to ClusterFilters + rule: '!has(self.afterNamespaceFilters) || !self.afterNamespaceFilters' type: object served: true storage: true diff --git a/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml b/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml index 00a051f09..8777d8403 100644 --- a/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml +++ b/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml @@ -44,7 +44,7 @@ spec: description: |- AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the - namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + namespaced Filters by default. Only applies to ClusterFilters, a namespaced Filter that sets it is rejected. type: boolean filters: description: A set of filter plugins in order. @@ -1003,7 +1003,7 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering + description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them format: int32 type: integer type: object diff --git a/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml b/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml index 0095324d7..0a5140010 100644 --- a/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml +++ b/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml @@ -44,7 +44,7 @@ spec: description: |- AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the - namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + namespaced Filters by default. Only applies to ClusterFilters, a namespaced Filter that sets it is rejected. type: boolean filters: description: A set of filter plugins in order. @@ -1003,10 +1003,13 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering + description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them format: int32 type: integer type: object + x-kubernetes-validations: + - message: afterNamespaceFilters only applies to ClusterFilters + rule: '!has(self.afterNamespaceFilters) || !self.afterNamespaceFilters' type: object served: true storage: true diff --git a/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml b/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml index eea32ced2..7377ca754 100644 --- a/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml +++ b/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml @@ -45,7 +45,7 @@ spec: description: |- AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the - namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + namespaced Filters by default. Only applies to ClusterFilters, a namespaced Filter that sets it is rejected. type: boolean filters: description: A set of filter plugins in order. @@ -1004,7 +1004,7 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering + description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them format: int32 type: integer type: object diff --git a/config/crd/bases/fluentbit.fluent.io_filters.yaml b/config/crd/bases/fluentbit.fluent.io_filters.yaml index dcc5af568..3228710f5 100644 --- a/config/crd/bases/fluentbit.fluent.io_filters.yaml +++ b/config/crd/bases/fluentbit.fluent.io_filters.yaml @@ -45,7 +45,7 @@ spec: description: |- AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the - namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + namespaced Filters by default. Only applies to ClusterFilters, a namespaced Filter that sets it is rejected. type: boolean filters: description: A set of filter plugins in order. @@ -1004,10 +1004,13 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering + description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them format: int32 type: integer type: object + x-kubernetes-validations: + - message: afterNamespaceFilters only applies to ClusterFilters + rule: '!has(self.afterNamespaceFilters) || !self.afterNamespaceFilters' type: object served: true storage: true diff --git a/docs/best-practice/misc.md b/docs/best-practice/misc.md index bf70fbd95..3849ae253 100644 --- a/docs/best-practice/misc.md +++ b/docs/best-practice/misc.md @@ -13,3 +13,23 @@ Path to file in Fluent Bit config should be well regulated. Fluent Bit Operator + +# Filter Ordering + +ClusterFilters run before the namespaced Filters, sorted by `ordinal` and then by name. Set `afterNamespaceFilters: true` on a ClusterFilter to run it after the namespaced Filters instead, e.g. to drop a field that the namespaced Filters still need. `ordinal` orders ClusterFilters within their group, so a ClusterFilter that runs after the namespaced Filters always comes after them, whatever its ordinal. Namespaced Filters reject the field. + +```yaml +apiVersion: fluentbit.fluent.io/v1alpha2 +kind: ClusterFilter +metadata: + name: drop-internal-fields + labels: + fluentbit.fluent.io/enabled: "true" +spec: + match: kube.* + afterNamespaceFilters: true + filters: + - recordModifier: + removeKeys: + - internal_trace_id +``` diff --git a/docs/fluentbit.md b/docs/fluentbit.md index 97b7c7b5c..7c2751ed2 100644 --- a/docs/fluentbit.md +++ b/docs/fluentbit.md @@ -313,8 +313,8 @@ FilterSpec defines the desired state of ClusterFilter | matchRegex | A regular expression to match against the tags of incoming records. Use this option if you want to use the full regex syntax. | string | | logLevel | | string | | filters | A set of filter plugins in order. | [][FilterItem](#filteritem) | -| ordinal | An ordinal to influence filter ordering | int32 | -| afterNamespaceFilters | AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. | bool | +| ordinal | An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them | int32 | +| afterNamespaceFilters | AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the namespaced Filters by default. Only applies to ClusterFilters, a namespaced Filter that sets it is rejected. | bool | [Back to TOC](#table-of-contents) # FluentBit diff --git a/manifests/setup/setup.yaml b/manifests/setup/setup.yaml index f0361d8de..d90a55e56 100644 --- a/manifests/setup/setup.yaml +++ b/manifests/setup/setup.yaml @@ -44,7 +44,7 @@ spec: description: |- AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the - namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + namespaced Filters by default. Only applies to ClusterFilters, a namespaced Filter that sets it is rejected. type: boolean filters: description: A set of filter plugins in order. @@ -1003,7 +1003,7 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering + description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them format: int32 type: integer type: object @@ -16287,7 +16287,7 @@ spec: description: |- AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the - namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + namespaced Filters by default. Only applies to ClusterFilters, a namespaced Filter that sets it is rejected. type: boolean filters: description: A set of filter plugins in order. @@ -17246,10 +17246,13 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering + description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them format: int32 type: integer type: object + x-kubernetes-validations: + - message: afterNamespaceFilters only applies to ClusterFilters + rule: '!has(self.afterNamespaceFilters) || !self.afterNamespaceFilters' type: object served: true storage: true From 7ab01de42ecb073f579f94c743729332dcd064a7 Mon Sep 17 00:00:00 2001 From: Srinivas Chippagiri Date: Tue, 10 Nov 2026 16:52:00 -0800 Subject: [PATCH 3/7] chore: wrap the ordinal description like controller-gen does Signed-off-by: Srinivas Chippagiri --- .../templates/fluentbit.fluent.io_clusterfilters.yaml | 4 +++- .../templates/fluentbit.fluent.io_filters.yaml | 4 +++- .../crds/fluentbit.fluent.io_clusterfilters.yaml | 4 +++- .../fluent-operator/crds/fluentbit.fluent.io_filters.yaml | 4 +++- config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml | 4 +++- config/crd/bases/fluentbit.fluent.io_filters.yaml | 4 +++- manifests/setup/setup.yaml | 8 ++++++-- 7 files changed, 24 insertions(+), 8 deletions(-) diff --git a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml index 2685c0951..601480d24 100644 --- a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml +++ b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml @@ -1005,7 +1005,9 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them + description: An ordinal to influence filter ordering. ClusterFilters + are ordered within their group, the ones that run before the namespaced + Filters and the ones that run after them format: int32 type: integer type: object diff --git a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml index 61c1b29f6..7fa51ae6c 100644 --- a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml +++ b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml @@ -1005,7 +1005,9 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them + description: An ordinal to influence filter ordering. ClusterFilters + are ordered within their group, the ones that run before the namespaced + Filters and the ones that run after them format: int32 type: integer type: object diff --git a/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml b/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml index 8777d8403..f0560a419 100644 --- a/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml +++ b/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml @@ -1003,7 +1003,9 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them + description: An ordinal to influence filter ordering. ClusterFilters + are ordered within their group, the ones that run before the namespaced + Filters and the ones that run after them format: int32 type: integer type: object diff --git a/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml b/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml index 0a5140010..d831e89dc 100644 --- a/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml +++ b/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml @@ -1003,7 +1003,9 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them + description: An ordinal to influence filter ordering. ClusterFilters + are ordered within their group, the ones that run before the namespaced + Filters and the ones that run after them format: int32 type: integer type: object diff --git a/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml b/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml index 7377ca754..7afd6d739 100644 --- a/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml +++ b/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml @@ -1004,7 +1004,9 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them + description: An ordinal to influence filter ordering. ClusterFilters + are ordered within their group, the ones that run before the namespaced + Filters and the ones that run after them format: int32 type: integer type: object diff --git a/config/crd/bases/fluentbit.fluent.io_filters.yaml b/config/crd/bases/fluentbit.fluent.io_filters.yaml index 3228710f5..410858816 100644 --- a/config/crd/bases/fluentbit.fluent.io_filters.yaml +++ b/config/crd/bases/fluentbit.fluent.io_filters.yaml @@ -1004,7 +1004,9 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them + description: An ordinal to influence filter ordering. ClusterFilters + are ordered within their group, the ones that run before the namespaced + Filters and the ones that run after them format: int32 type: integer type: object diff --git a/manifests/setup/setup.yaml b/manifests/setup/setup.yaml index d90a55e56..41145bb34 100644 --- a/manifests/setup/setup.yaml +++ b/manifests/setup/setup.yaml @@ -1003,7 +1003,9 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them + description: An ordinal to influence filter ordering. ClusterFilters + are ordered within their group, the ones that run before the namespaced + Filters and the ones that run after them format: int32 type: integer type: object @@ -17246,7 +17248,9 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them + description: An ordinal to influence filter ordering. ClusterFilters + are ordered within their group, the ones that run before the namespaced + Filters and the ones that run after them format: int32 type: integer type: object From 050b80b587c37d724a6a61c14183fd2046acd12a Mon Sep 17 00:00:00 2001 From: Srinivas Chippagiri Date: Fri, 6 Nov 2026 20:48:03 -0800 Subject: [PATCH 4/7] feat(fluentbit): let a ClusterFilter run after the namespaced Filters Signed-off-by: Srinivas Chippagiri --- .../fluentbit/v1alpha2/clusterfilter_types.go | 33 +++++++-- .../v1alpha2/clusterfluentbitconfig_types.go | 32 +++++---- .../clusterfluentbitconfig_types_test.go | 71 +++++++++++++++++++ .../fluentbit.fluent.io_clusterfilters.yaml | 6 ++ .../fluentbit.fluent.io_filters.yaml | 6 ++ .../fluentbit.fluent.io_clusterfilters.yaml | 6 ++ .../crds/fluentbit.fluent.io_filters.yaml | 6 ++ .../fluentbit.fluent.io_clusterfilters.yaml | 6 ++ .../bases/fluentbit.fluent.io_filters.yaml | 6 ++ docs/fluentbit.md | 1 + manifests/setup/setup.yaml | 12 ++++ 11 files changed, 169 insertions(+), 16 deletions(-) diff --git a/apis/fluentbit/v1alpha2/clusterfilter_types.go b/apis/fluentbit/v1alpha2/clusterfilter_types.go index 583bb24da..94495bb69 100644 --- a/apis/fluentbit/v1alpha2/clusterfilter_types.go +++ b/apis/fluentbit/v1alpha2/clusterfilter_types.go @@ -46,6 +46,10 @@ type FilterSpec struct { FilterItems []FilterItem `json:"filters,omitempty"` // An ordinal to influence filter ordering Ordinal int32 `json:"ordinal,omitempty"` + // AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, + // so it processes records after they have been through them. ClusterFilters run before the + // namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + AfterNamespaceFilters bool `json:"afterNamespaceFilters,omitempty"` } type FilterItem struct { @@ -175,14 +179,35 @@ func (list ClusterFilterList) Load(sl plugins.SecretLoader) (string, error) { return buf.String(), nil } -func (list ClusterFilterList) LoadAsYaml(sl plugins.SecretLoader, depth int) (string, error) { - var buf bytes.Buffer +// SplitByNamespaceFilters splits the list into the ClusterFilters that run before the namespaced +// Filters and those that run after them (see FilterSpec.AfterNamespaceFilters). +func (list ClusterFilterList) SplitByNamespaceFilters() (before, after ClusterFilterList) { + for _, item := range list.Items { + if item.Spec.AfterNamespaceFilters { + after.Items = append(after.Items, item) + } else { + before.Items = append(before.Items, item) + } + } + return before, after +} - sort.Sort(FilterByOrdinalAndName(list.Items)) +func (list ClusterFilterList) LoadAsYaml(sl plugins.SecretLoader, depth int) (string, error) { if len(list.Items) == 0 { return "", nil } - fmt.Fprintf(&buf, "%sfilters:\n", utils.YamlIndent(depth)) + items, err := list.loadYamlItems(sl, depth) + if err != nil { + return "", err + } + return fmt.Sprintf("%sfilters:\n", utils.YamlIndent(depth)) + items, nil +} + +// loadYamlItems renders the filters as YAML list items, without the "filters:" header. +func (list ClusterFilterList) loadYamlItems(sl plugins.SecretLoader, depth int) (string, error) { + var buf bytes.Buffer + + sort.Sort(FilterByOrdinalAndName(list.Items)) padding := utils.YamlIndent(depth + 2) for _, item := range list.Items { diff --git a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go index b4a3719be..794ba4cd2 100644 --- a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go +++ b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go @@ -305,7 +305,12 @@ func (cfg ClusterFluentBitConfig) RenderMainConfigInYaml( return "", fmt.Errorf("failed to load inputs: %w", err) } - filterSections, err := filters.LoadAsYaml(sl, 1) + beforeFilters, afterFilters := filters.SplitByNamespaceFilters() + filterSections, err := beforeFilters.loadYamlItems(sl, 1) + if err != nil { + return "", fmt.Errorf("failed to load filters: %w", err) + } + afterFilterSections, err := afterFilters.loadYamlItems(sl, 1) if err != nil { return "", fmt.Errorf("failed to load filters: %w", err) } @@ -362,24 +367,21 @@ func (cfg ClusterFluentBitConfig) RenderMainConfigInYaml( } buf.WriteString(inputSections) - // rewriteTagConfigs entries are headerless "filters:" list items (see - // generateRewriteTagConfig), so they must share a single "filters:" - // header with filterSections/nsFilterSections rather than writing their - // own, which would otherwise produce duplicate "filters:" keys. - if filterSections == "" && (nsFilterSections != nil || len(rewriteTagConfigs) > 0) { + // The cluster filters, rewriteTagConfigs entries (see generateRewriteTagConfig) + // and namespaced filters are all headerless "filters:" list items, so they + // share a single "filters:" header; one header per section would produce + // duplicate "filters:" keys. + if len(filters.Items) > 0 || nsFilterSections != nil || len(rewriteTagConfigs) > 0 { fmt.Fprintf(&buf, "%sfilters:\n", utils.YamlIndent(1)) - } else { - // 1. filterSections == "" && nsFilterSections == nil && rewriteTagConfigs empty - // 2. filterSections != "" && (nsFilterSections != nil || rewriteTagConfigs non-empty) - // 3. filterSections != "" && nsFilterSections == nil && rewriteTagConfigs empty - buf.WriteString(filterSections) } + buf.WriteString(filterSections) for _, rtc := range rewriteTagConfigs { buf.WriteString(rtc) } for _, filters := range nsFilterSections { buf.WriteString(filters) } + buf.WriteString(afterFilterSections) if outputSections == "" && nsOutputSections != nil { fmt.Fprintf(&buf, "%soutputs:\n", utils.YamlIndent(1)) } else { @@ -416,7 +418,12 @@ func (cfg ClusterFluentBitConfig) RenderMainConfig( return "", err } - filterSections, err := filters.Load(sl) + beforeFilters, afterFilters := filters.SplitByNamespaceFilters() + filterSections, err := beforeFilters.Load(sl) + if err != nil { + return "", err + } + afterFilterSections, err := afterFilters.Load(sl) if err != nil { return "", err } @@ -472,6 +479,7 @@ func (cfg ClusterFluentBitConfig) RenderMainConfig( for _, filters := range nsFilterSections { buf.WriteString(filters) } + buf.WriteString(afterFilterSections) for _, outputs := range nsOutputSections { buf.WriteString(outputs) } diff --git a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go index f470acdbf..f146ecbfd 100644 --- a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go +++ b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go @@ -1521,3 +1521,74 @@ func TestRenderMainConfigInYaml_RewriteTagConfigMergesIntoSingleFiltersSection(t g.Expect(ok).To(BeTrue()) g.Expect(filterEntries).To(HaveLen(2)) } + +func TestRenderMainConfig_ClusterFilterAfterNamespaceFilters(t *testing.T) { + g := NewGomegaWithT(t) + sl := plugins.NewSecretLoader(nil, "testnamespace") + cfg := ClusterFluentBitConfig{} + + modify := func(value string) []FilterItem { + return []FilterItem{{Modify: &filter.Modify{ + Rules: []filter.Rule{{Set: map[string]string{"stage": value}}}, + }}} + } + + inputs := ClusterInputList{ + Items: []ClusterInput{{ + ObjectMeta: metav1.ObjectMeta{Name: "input0"}, + Spec: InputSpec{Tail: &input.Tail{Tag: "kube.*", Path: "/var/log/containers/*.log"}}, + }}, + } + // The ordinal of the "after" filter is lower, it must still be placed after the namespaced filters. + filters := ClusterFilterList{ + Items: []ClusterFilter{ + { + ObjectMeta: metav1.ObjectMeta{Name: "after"}, + Spec: FilterSpec{Match: "kube.*", Ordinal: -1, AfterNamespaceFilters: true, FilterItems: modify("cluster-after")}, + }, + { + ObjectMeta: metav1.ObjectMeta{Name: "before"}, + Spec: FilterSpec{Match: "kube.*", FilterItems: modify("cluster-before")}, + }, + }, + } + nsFilters := []FilterList{{ + Items: []Filter{{ + ObjectMeta: metav1.ObjectMeta{Name: "ns", Namespace: "foo"}, + Spec: FilterSpec{Match: "kube.*", FilterItems: modify("namespaced")}, + }}, + }} + + expectOrder := func(config string) { + before := strings.Index(config, "cluster-before") + namespaced := strings.Index(config, "namespaced") + after := strings.Index(config, "cluster-after") + g.Expect(before).To(BeNumerically(">=", 0)) + g.Expect(namespaced).To(BeNumerically(">", before)) + g.Expect(after).To(BeNumerically(">", namespaced)) + } + + config, err := cfg.RenderMainConfig(sl, inputs, filters, ClusterOutputList{}, nsFilters, nil, nil) + g.Expect(err).NotTo(HaveOccurred()) + expectOrder(config) + + yamlConfig, err := cfg.RenderMainConfigInYaml(sl, inputs, filters, ClusterOutputList{}, nsFilters, nil, nil) + g.Expect(err).NotTo(HaveOccurred()) + expectOrder(yamlConfig) + g.Expect(strings.Count(yamlConfig, "filters:\n")).To(Equal(1)) + + var parsed map[string]any + g.Expect(yaml.Unmarshal([]byte(yamlConfig), &parsed)).To(Succeed()) + pipeline, ok := parsed["pipeline"].(map[string]any) + g.Expect(ok).To(BeTrue()) + g.Expect(pipeline["filters"]).To(HaveLen(3)) + + // With only "after" cluster filters and no namespaced filters, the YAML still has a filters header. + _, afterOnly := filters.SplitByNamespaceFilters() + yamlConfig, err = cfg.RenderMainConfigInYaml(sl, inputs, afterOnly, ClusterOutputList{}, nil, nil, nil) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(yaml.Unmarshal([]byte(yamlConfig), &parsed)).To(Succeed()) + pipeline, ok = parsed["pipeline"].(map[string]any) + g.Expect(ok).To(BeTrue()) + g.Expect(pipeline["filters"]).To(HaveLen(1)) +} diff --git a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml index 05c9fa911..29787e166 100644 --- a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml +++ b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml @@ -42,6 +42,12 @@ spec: spec: description: Specification of desired Filter configuration. properties: + afterNamespaceFilters: + description: |- + AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, + so it processes records after they have been through them. ClusterFilters run before the + namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + type: boolean filters: description: A set of filter plugins in order. items: diff --git a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml index 0c89e284e..f7f7c7f32 100644 --- a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml +++ b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml @@ -42,6 +42,12 @@ spec: spec: description: FilterSpec defines the desired state of ClusterFilter properties: + afterNamespaceFilters: + description: |- + AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, + so it processes records after they have been through them. ClusterFilters run before the + namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + type: boolean filters: description: A set of filter plugins in order. items: diff --git a/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml b/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml index 5d403736d..00a051f09 100644 --- a/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml +++ b/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml @@ -40,6 +40,12 @@ spec: spec: description: Specification of desired Filter configuration. properties: + afterNamespaceFilters: + description: |- + AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, + so it processes records after they have been through them. ClusterFilters run before the + namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + type: boolean filters: description: A set of filter plugins in order. items: diff --git a/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml b/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml index 0a12abc82..0095324d7 100644 --- a/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml +++ b/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml @@ -40,6 +40,12 @@ spec: spec: description: FilterSpec defines the desired state of ClusterFilter properties: + afterNamespaceFilters: + description: |- + AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, + so it processes records after they have been through them. ClusterFilters run before the + namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + type: boolean filters: description: A set of filter plugins in order. items: diff --git a/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml b/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml index 18cec014d..eea32ced2 100644 --- a/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml +++ b/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml @@ -41,6 +41,12 @@ spec: spec: description: Specification of desired Filter configuration. properties: + afterNamespaceFilters: + description: |- + AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, + so it processes records after they have been through them. ClusterFilters run before the + namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + type: boolean filters: description: A set of filter plugins in order. items: diff --git a/config/crd/bases/fluentbit.fluent.io_filters.yaml b/config/crd/bases/fluentbit.fluent.io_filters.yaml index 7450c5b0d..dcc5af568 100644 --- a/config/crd/bases/fluentbit.fluent.io_filters.yaml +++ b/config/crd/bases/fluentbit.fluent.io_filters.yaml @@ -41,6 +41,12 @@ spec: spec: description: FilterSpec defines the desired state of ClusterFilter properties: + afterNamespaceFilters: + description: |- + AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, + so it processes records after they have been through them. ClusterFilters run before the + namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + type: boolean filters: description: A set of filter plugins in order. items: diff --git a/docs/fluentbit.md b/docs/fluentbit.md index 4c0fb67e5..97b7c7b5c 100644 --- a/docs/fluentbit.md +++ b/docs/fluentbit.md @@ -314,6 +314,7 @@ FilterSpec defines the desired state of ClusterFilter | logLevel | | string | | filters | A set of filter plugins in order. | [][FilterItem](#filteritem) | | ordinal | An ordinal to influence filter ordering | int32 | +| afterNamespaceFilters | AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. | bool | [Back to TOC](#table-of-contents) # FluentBit diff --git a/manifests/setup/setup.yaml b/manifests/setup/setup.yaml index dd867914e..4a896c344 100644 --- a/manifests/setup/setup.yaml +++ b/manifests/setup/setup.yaml @@ -40,6 +40,12 @@ spec: spec: description: Specification of desired Filter configuration. properties: + afterNamespaceFilters: + description: |- + AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, + so it processes records after they have been through them. ClusterFilters run before the + namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + type: boolean filters: description: A set of filter plugins in order. items: @@ -16281,6 +16287,12 @@ spec: spec: description: FilterSpec defines the desired state of ClusterFilter properties: + afterNamespaceFilters: + description: |- + AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, + so it processes records after they have been through them. ClusterFilters run before the + namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + type: boolean filters: description: A set of filter plugins in order. items: From 4d42cbb2c8c079d094aaf9b70d6f1b93ed6f9789 Mon Sep 17 00:00:00 2001 From: Srinivas Chippagiri Date: Tue, 10 Nov 2026 16:36:51 -0800 Subject: [PATCH 5/7] feat(fluentbit): reject afterNamespaceFilters on namespaced Filters and tidy the ordering docs and test Signed-off-by: Srinivas Chippagiri --- .../fluentbit/v1alpha2/clusterfilter_types.go | 8 +++---- .../v1alpha2/clusterfluentbitconfig_types.go | 4 ++-- .../clusterfluentbitconfig_types_test.go | 21 +++++++++++-------- apis/fluentbit/v1alpha2/filter_types.go | 1 + .../fluentbit.fluent.io_clusterfilters.yaml | 4 ++-- .../fluentbit.fluent.io_filters.yaml | 7 +++++-- .../fluentbit.fluent.io_clusterfilters.yaml | 4 ++-- .../crds/fluentbit.fluent.io_filters.yaml | 7 +++++-- .../fluentbit.fluent.io_clusterfilters.yaml | 4 ++-- .../bases/fluentbit.fluent.io_filters.yaml | 7 +++++-- docs/best-practice/misc.md | 20 ++++++++++++++++++ docs/fluentbit.md | 4 ++-- manifests/setup/setup.yaml | 11 ++++++---- 13 files changed, 69 insertions(+), 33 deletions(-) diff --git a/apis/fluentbit/v1alpha2/clusterfilter_types.go b/apis/fluentbit/v1alpha2/clusterfilter_types.go index 94495bb69..9125d9efe 100644 --- a/apis/fluentbit/v1alpha2/clusterfilter_types.go +++ b/apis/fluentbit/v1alpha2/clusterfilter_types.go @@ -44,11 +44,11 @@ type FilterSpec struct { LogLevel string `json:"logLevel,omitempty"` // A set of filter plugins in order. FilterItems []FilterItem `json:"filters,omitempty"` - // An ordinal to influence filter ordering + // An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them Ordinal int32 `json:"ordinal,omitempty"` // AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, // so it processes records after they have been through them. ClusterFilters run before the - // namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + // namespaced Filters by default. Only applies to ClusterFilters, a namespaced Filter that sets it is rejected. AfterNamespaceFilters bool `json:"afterNamespaceFilters,omitempty"` } @@ -179,9 +179,9 @@ func (list ClusterFilterList) Load(sl plugins.SecretLoader) (string, error) { return buf.String(), nil } -// SplitByNamespaceFilters splits the list into the ClusterFilters that run before the namespaced +// splitByNamespaceFilters splits the list into the ClusterFilters that run before the namespaced // Filters and those that run after them (see FilterSpec.AfterNamespaceFilters). -func (list ClusterFilterList) SplitByNamespaceFilters() (before, after ClusterFilterList) { +func (list ClusterFilterList) splitByNamespaceFilters() (before, after ClusterFilterList) { for _, item := range list.Items { if item.Spec.AfterNamespaceFilters { after.Items = append(after.Items, item) diff --git a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go index 794ba4cd2..56ccbb6c9 100644 --- a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go +++ b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go @@ -305,7 +305,7 @@ func (cfg ClusterFluentBitConfig) RenderMainConfigInYaml( return "", fmt.Errorf("failed to load inputs: %w", err) } - beforeFilters, afterFilters := filters.SplitByNamespaceFilters() + beforeFilters, afterFilters := filters.splitByNamespaceFilters() filterSections, err := beforeFilters.loadYamlItems(sl, 1) if err != nil { return "", fmt.Errorf("failed to load filters: %w", err) @@ -418,7 +418,7 @@ func (cfg ClusterFluentBitConfig) RenderMainConfig( return "", err } - beforeFilters, afterFilters := filters.SplitByNamespaceFilters() + beforeFilters, afterFilters := filters.splitByNamespaceFilters() filterSections, err := beforeFilters.Load(sl) if err != nil { return "", err diff --git a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go index f146ecbfd..c127b1030 100644 --- a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go +++ b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go @@ -1544,25 +1544,25 @@ func TestRenderMainConfig_ClusterFilterAfterNamespaceFilters(t *testing.T) { Items: []ClusterFilter{ { ObjectMeta: metav1.ObjectMeta{Name: "after"}, - Spec: FilterSpec{Match: "kube.*", Ordinal: -1, AfterNamespaceFilters: true, FilterItems: modify("cluster-after")}, + Spec: FilterSpec{Match: "kube.*", Ordinal: -1, AfterNamespaceFilters: true, FilterItems: modify("stage-cluster-after")}, }, { ObjectMeta: metav1.ObjectMeta{Name: "before"}, - Spec: FilterSpec{Match: "kube.*", FilterItems: modify("cluster-before")}, + Spec: FilterSpec{Match: "kube.*", FilterItems: modify("stage-cluster-before")}, }, }, } nsFilters := []FilterList{{ Items: []Filter{{ ObjectMeta: metav1.ObjectMeta{Name: "ns", Namespace: "foo"}, - Spec: FilterSpec{Match: "kube.*", FilterItems: modify("namespaced")}, + Spec: FilterSpec{Match: "kube.*", FilterItems: modify("stage-namespaced")}, }}, }} expectOrder := func(config string) { - before := strings.Index(config, "cluster-before") - namespaced := strings.Index(config, "namespaced") - after := strings.Index(config, "cluster-after") + before := strings.Index(config, "stage-cluster-before") + namespaced := strings.Index(config, "stage-namespaced") + after := strings.Index(config, "stage-cluster-after") g.Expect(before).To(BeNumerically(">=", 0)) g.Expect(namespaced).To(BeNumerically(">", before)) g.Expect(after).To(BeNumerically(">", namespaced)) @@ -1584,11 +1584,14 @@ func TestRenderMainConfig_ClusterFilterAfterNamespaceFilters(t *testing.T) { g.Expect(pipeline["filters"]).To(HaveLen(3)) // With only "after" cluster filters and no namespaced filters, the YAML still has a filters header. - _, afterOnly := filters.SplitByNamespaceFilters() + // afterOnly holds just the "after" ClusterFilter, so the before group and the namespaced Filters are + // both empty and nothing else would write the header. + _, afterOnly := filters.splitByNamespaceFilters() yamlConfig, err = cfg.RenderMainConfigInYaml(sl, inputs, afterOnly, ClusterOutputList{}, nil, nil, nil) g.Expect(err).NotTo(HaveOccurred()) - g.Expect(yaml.Unmarshal([]byte(yamlConfig), &parsed)).To(Succeed()) - pipeline, ok = parsed["pipeline"].(map[string]any) + var afterOnlyParsed map[string]any + g.Expect(yaml.Unmarshal([]byte(yamlConfig), &afterOnlyParsed)).To(Succeed()) + pipeline, ok = afterOnlyParsed["pipeline"].(map[string]any) g.Expect(ok).To(BeTrue()) g.Expect(pipeline["filters"]).To(HaveLen(1)) } diff --git a/apis/fluentbit/v1alpha2/filter_types.go b/apis/fluentbit/v1alpha2/filter_types.go index 4bfda624e..528618558 100644 --- a/apis/fluentbit/v1alpha2/filter_types.go +++ b/apis/fluentbit/v1alpha2/filter_types.go @@ -39,6 +39,7 @@ type Filter struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` + // +kubebuilder:validation:XValidation:rule="!has(self.afterNamespaceFilters) || !self.afterNamespaceFilters",message="afterNamespaceFilters only applies to ClusterFilters" Spec FilterSpec `json:"spec,omitempty"` } diff --git a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml index 29787e166..2685c0951 100644 --- a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml +++ b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml @@ -46,7 +46,7 @@ spec: description: |- AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the - namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + namespaced Filters by default. Only applies to ClusterFilters, a namespaced Filter that sets it is rejected. type: boolean filters: description: A set of filter plugins in order. @@ -1005,7 +1005,7 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering + description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them format: int32 type: integer type: object diff --git a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml index f7f7c7f32..61c1b29f6 100644 --- a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml +++ b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml @@ -46,7 +46,7 @@ spec: description: |- AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the - namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + namespaced Filters by default. Only applies to ClusterFilters, a namespaced Filter that sets it is rejected. type: boolean filters: description: A set of filter plugins in order. @@ -1005,10 +1005,13 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering + description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them format: int32 type: integer type: object + x-kubernetes-validations: + - message: afterNamespaceFilters only applies to ClusterFilters + rule: '!has(self.afterNamespaceFilters) || !self.afterNamespaceFilters' type: object served: true storage: true diff --git a/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml b/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml index 00a051f09..8777d8403 100644 --- a/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml +++ b/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml @@ -44,7 +44,7 @@ spec: description: |- AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the - namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + namespaced Filters by default. Only applies to ClusterFilters, a namespaced Filter that sets it is rejected. type: boolean filters: description: A set of filter plugins in order. @@ -1003,7 +1003,7 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering + description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them format: int32 type: integer type: object diff --git a/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml b/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml index 0095324d7..0a5140010 100644 --- a/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml +++ b/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml @@ -44,7 +44,7 @@ spec: description: |- AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the - namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + namespaced Filters by default. Only applies to ClusterFilters, a namespaced Filter that sets it is rejected. type: boolean filters: description: A set of filter plugins in order. @@ -1003,10 +1003,13 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering + description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them format: int32 type: integer type: object + x-kubernetes-validations: + - message: afterNamespaceFilters only applies to ClusterFilters + rule: '!has(self.afterNamespaceFilters) || !self.afterNamespaceFilters' type: object served: true storage: true diff --git a/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml b/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml index eea32ced2..7377ca754 100644 --- a/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml +++ b/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml @@ -45,7 +45,7 @@ spec: description: |- AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the - namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + namespaced Filters by default. Only applies to ClusterFilters, a namespaced Filter that sets it is rejected. type: boolean filters: description: A set of filter plugins in order. @@ -1004,7 +1004,7 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering + description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them format: int32 type: integer type: object diff --git a/config/crd/bases/fluentbit.fluent.io_filters.yaml b/config/crd/bases/fluentbit.fluent.io_filters.yaml index dcc5af568..3228710f5 100644 --- a/config/crd/bases/fluentbit.fluent.io_filters.yaml +++ b/config/crd/bases/fluentbit.fluent.io_filters.yaml @@ -45,7 +45,7 @@ spec: description: |- AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the - namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + namespaced Filters by default. Only applies to ClusterFilters, a namespaced Filter that sets it is rejected. type: boolean filters: description: A set of filter plugins in order. @@ -1004,10 +1004,13 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering + description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them format: int32 type: integer type: object + x-kubernetes-validations: + - message: afterNamespaceFilters only applies to ClusterFilters + rule: '!has(self.afterNamespaceFilters) || !self.afterNamespaceFilters' type: object served: true storage: true diff --git a/docs/best-practice/misc.md b/docs/best-practice/misc.md index bf70fbd95..3849ae253 100644 --- a/docs/best-practice/misc.md +++ b/docs/best-practice/misc.md @@ -13,3 +13,23 @@ Path to file in Fluent Bit config should be well regulated. Fluent Bit Operator + +# Filter Ordering + +ClusterFilters run before the namespaced Filters, sorted by `ordinal` and then by name. Set `afterNamespaceFilters: true` on a ClusterFilter to run it after the namespaced Filters instead, e.g. to drop a field that the namespaced Filters still need. `ordinal` orders ClusterFilters within their group, so a ClusterFilter that runs after the namespaced Filters always comes after them, whatever its ordinal. Namespaced Filters reject the field. + +```yaml +apiVersion: fluentbit.fluent.io/v1alpha2 +kind: ClusterFilter +metadata: + name: drop-internal-fields + labels: + fluentbit.fluent.io/enabled: "true" +spec: + match: kube.* + afterNamespaceFilters: true + filters: + - recordModifier: + removeKeys: + - internal_trace_id +``` diff --git a/docs/fluentbit.md b/docs/fluentbit.md index 97b7c7b5c..7c2751ed2 100644 --- a/docs/fluentbit.md +++ b/docs/fluentbit.md @@ -313,8 +313,8 @@ FilterSpec defines the desired state of ClusterFilter | matchRegex | A regular expression to match against the tags of incoming records. Use this option if you want to use the full regex syntax. | string | | logLevel | | string | | filters | A set of filter plugins in order. | [][FilterItem](#filteritem) | -| ordinal | An ordinal to influence filter ordering | int32 | -| afterNamespaceFilters | AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. | bool | +| ordinal | An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them | int32 | +| afterNamespaceFilters | AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the namespaced Filters by default. Only applies to ClusterFilters, a namespaced Filter that sets it is rejected. | bool | [Back to TOC](#table-of-contents) # FluentBit diff --git a/manifests/setup/setup.yaml b/manifests/setup/setup.yaml index 4a896c344..e71830781 100644 --- a/manifests/setup/setup.yaml +++ b/manifests/setup/setup.yaml @@ -44,7 +44,7 @@ spec: description: |- AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the - namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + namespaced Filters by default. Only applies to ClusterFilters, a namespaced Filter that sets it is rejected. type: boolean filters: description: A set of filter plugins in order. @@ -1003,7 +1003,7 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering + description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them format: int32 type: integer type: object @@ -16291,7 +16291,7 @@ spec: description: |- AfterNamespaceFilters places a ClusterFilter after the namespaced Filters in the pipeline, so it processes records after they have been through them. ClusterFilters run before the - namespaced Filters by default. Only applies to ClusterFilters, namespaced Filters ignore it. + namespaced Filters by default. Only applies to ClusterFilters, a namespaced Filter that sets it is rejected. type: boolean filters: description: A set of filter plugins in order. @@ -17250,10 +17250,13 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering + description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them format: int32 type: integer type: object + x-kubernetes-validations: + - message: afterNamespaceFilters only applies to ClusterFilters + rule: '!has(self.afterNamespaceFilters) || !self.afterNamespaceFilters' type: object served: true storage: true From b85bd82770899407cc601599545ccf35205459f3 Mon Sep 17 00:00:00 2001 From: Srinivas Chippagiri Date: Tue, 10 Nov 2026 16:52:00 -0800 Subject: [PATCH 6/7] chore: wrap the ordinal description like controller-gen does Signed-off-by: Srinivas Chippagiri --- .../templates/fluentbit.fluent.io_clusterfilters.yaml | 4 +++- .../templates/fluentbit.fluent.io_filters.yaml | 4 +++- .../crds/fluentbit.fluent.io_clusterfilters.yaml | 4 +++- .../fluent-operator/crds/fluentbit.fluent.io_filters.yaml | 4 +++- config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml | 4 +++- config/crd/bases/fluentbit.fluent.io_filters.yaml | 4 +++- manifests/setup/setup.yaml | 8 ++++++-- 7 files changed, 24 insertions(+), 8 deletions(-) diff --git a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml index 2685c0951..601480d24 100644 --- a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml +++ b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_clusterfilters.yaml @@ -1005,7 +1005,9 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them + description: An ordinal to influence filter ordering. ClusterFilters + are ordered within their group, the ones that run before the namespaced + Filters and the ones that run after them format: int32 type: integer type: object diff --git a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml index 61c1b29f6..7fa51ae6c 100644 --- a/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml +++ b/charts/fluent-operator-fluent-bit-crds/templates/fluentbit.fluent.io_filters.yaml @@ -1005,7 +1005,9 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them + description: An ordinal to influence filter ordering. ClusterFilters + are ordered within their group, the ones that run before the namespaced + Filters and the ones that run after them format: int32 type: integer type: object diff --git a/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml b/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml index 8777d8403..f0560a419 100644 --- a/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml +++ b/charts/fluent-operator/crds/fluentbit.fluent.io_clusterfilters.yaml @@ -1003,7 +1003,9 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them + description: An ordinal to influence filter ordering. ClusterFilters + are ordered within their group, the ones that run before the namespaced + Filters and the ones that run after them format: int32 type: integer type: object diff --git a/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml b/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml index 0a5140010..d831e89dc 100644 --- a/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml +++ b/charts/fluent-operator/crds/fluentbit.fluent.io_filters.yaml @@ -1003,7 +1003,9 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them + description: An ordinal to influence filter ordering. ClusterFilters + are ordered within their group, the ones that run before the namespaced + Filters and the ones that run after them format: int32 type: integer type: object diff --git a/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml b/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml index 7377ca754..7afd6d739 100644 --- a/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml +++ b/config/crd/bases/fluentbit.fluent.io_clusterfilters.yaml @@ -1004,7 +1004,9 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them + description: An ordinal to influence filter ordering. ClusterFilters + are ordered within their group, the ones that run before the namespaced + Filters and the ones that run after them format: int32 type: integer type: object diff --git a/config/crd/bases/fluentbit.fluent.io_filters.yaml b/config/crd/bases/fluentbit.fluent.io_filters.yaml index 3228710f5..410858816 100644 --- a/config/crd/bases/fluentbit.fluent.io_filters.yaml +++ b/config/crd/bases/fluentbit.fluent.io_filters.yaml @@ -1004,7 +1004,9 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them + description: An ordinal to influence filter ordering. ClusterFilters + are ordered within their group, the ones that run before the namespaced + Filters and the ones that run after them format: int32 type: integer type: object diff --git a/manifests/setup/setup.yaml b/manifests/setup/setup.yaml index e71830781..0e54d6016 100644 --- a/manifests/setup/setup.yaml +++ b/manifests/setup/setup.yaml @@ -1003,7 +1003,9 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them + description: An ordinal to influence filter ordering. ClusterFilters + are ordered within their group, the ones that run before the namespaced + Filters and the ones that run after them format: int32 type: integer type: object @@ -17250,7 +17252,9 @@ spec: Use this option if you want to use the full regex syntax. type: string ordinal: - description: An ordinal to influence filter ordering. ClusterFilters are ordered within their group, the ones that run before the namespaced Filters and the ones that run after them + description: An ordinal to influence filter ordering. ClusterFilters + are ordered within their group, the ones that run before the namespaced + Filters and the ones that run after them format: int32 type: integer type: object From ca5b90d1f2c91d27417035a3f7dc668b5671a9c4 Mon Sep 17 00:00:00 2001 From: Srinivas Chippagiri Date: Wed, 11 Nov 2026 21:17:11 -0800 Subject: [PATCH 7/7] fix(fluentbit): match re-tagged records in ClusterFilters that run after namespaced Filters Signed-off-by: Srinivas Chippagiri --- .../fluentbit/v1alpha2/clusterfilter_types.go | 33 +++++++++++++++- .../v1alpha2/clusterfilter_types_test.go | 26 +++++++++++++ .../clusterfluentbitconfig_types_test.go | 39 +++++++++++++++++-- docs/best-practice/misc.md | 2 + 4 files changed, 96 insertions(+), 4 deletions(-) diff --git a/apis/fluentbit/v1alpha2/clusterfilter_types.go b/apis/fluentbit/v1alpha2/clusterfilter_types.go index 9125d9efe..3e50b49d6 100644 --- a/apis/fluentbit/v1alpha2/clusterfilter_types.go +++ b/apis/fluentbit/v1alpha2/clusterfilter_types.go @@ -20,7 +20,9 @@ import ( "bytes" "fmt" "reflect" + "regexp" "sort" + "strings" "github.com/fluent/fluent-operator/v3/apis/fluentbit/v1alpha2/plugins" "github.com/fluent/fluent-operator/v3/apis/fluentbit/v1alpha2/plugins/custom" @@ -184,7 +186,7 @@ func (list ClusterFilterList) Load(sl plugins.SecretLoader) (string, error) { func (list ClusterFilterList) splitByNamespaceFilters() (before, after ClusterFilterList) { for _, item := range list.Items { if item.Spec.AfterNamespaceFilters { - after.Items = append(after.Items, item) + after.Items = append(after.Items, matchNamespacedTags(item)) } else { before.Items = append(before.Items, item) } @@ -192,6 +194,35 @@ func (list ClusterFilterList) splitByNamespaceFilters() (before, after ClusterFi return before, after } +// namespacedTagPrefix matches the "." prefix that the namespace routing rewrite_tag +// filter adds to the tags of records that go through namespaced Filters. +const namespacedTagPrefix = `(?:[0-9a-f]{32}\.)?` + +// matchNamespacedTags returns a copy of a ClusterFilter that runs after the namespaced Filters, with +// its match changed so that it also matches the re-tagged records. Without this a ClusterFilter +// matching kube.* would never see the records that went through namespaced Filters, because they are +// tagged .kube.* by then. +func matchNamespacedTags(item ClusterFilter) ClusterFilter { + out := *item.DeepCopy() + switch { + case out.Spec.MatchRegex != "": + // An unanchored regex already matches the prefixed tag, only one anchored at the start needs it. + if rest, ok := strings.CutPrefix(out.Spec.MatchRegex, "^"); ok { + out.Spec.MatchRegex = "^" + namespacedTagPrefix + rest + } + case out.Spec.Match != "" && out.Spec.Match != "*": + out.Spec.MatchRegex = "^" + namespacedTagPrefix + wildcardToRegex(out.Spec.Match) + "$" + out.Spec.Match = "" + } + return out +} + +// wildcardToRegex converts a Fluent Bit Match pattern, where "*" matches any sequence of characters, +// to the equivalent regular expression. +func wildcardToRegex(match string) string { + return strings.ReplaceAll(regexp.QuoteMeta(match), `\*`, ".*") +} + func (list ClusterFilterList) LoadAsYaml(sl plugins.SecretLoader, depth int) (string, error) { if len(list.Items) == 0 { return "", nil diff --git a/apis/fluentbit/v1alpha2/clusterfilter_types_test.go b/apis/fluentbit/v1alpha2/clusterfilter_types_test.go index bb22e89be..89abb2c5b 100644 --- a/apis/fluentbit/v1alpha2/clusterfilter_types_test.go +++ b/apis/fluentbit/v1alpha2/clusterfilter_types_test.go @@ -749,3 +749,29 @@ func TestClusterFilter_RecordModifier_Generated_Load_As_Yaml(t *testing.T) { g.Expect(err).NotTo(HaveOccurred()) g.Expect(clusterFilters).To(Equal(filtersExpected)) } + +func TestMatchNamespacedTags(t *testing.T) { + g := NewGomegaWithT(t) + prefix := "^" + namespacedTagPrefix + + for _, tc := range []struct { + name, match, matchRegex string + wantMatch, wantMatchRegex string + }{ + {name: "wildcard", match: "kube.*", wantMatchRegex: prefix + `kube\..*$`}, + {name: "literal tag", match: "app.log", wantMatchRegex: prefix + `app\.log$`}, + {name: "match all", match: "*", wantMatch: "*"}, + {name: "anchored regex", matchRegex: `^kube\..*`, wantMatchRegex: prefix + `kube\..*`}, + {name: "unanchored regex", matchRegex: `kube\..*`, wantMatchRegex: `kube\..*`}, + } { + t.Run(tc.name, func(t *testing.T) { + item := ClusterFilter{Spec: FilterSpec{Match: tc.match, MatchRegex: tc.matchRegex, AfterNamespaceFilters: true}} + got := matchNamespacedTags(item) + g.Expect(got.Spec.Match).To(Equal(tc.wantMatch)) + g.Expect(got.Spec.MatchRegex).To(Equal(tc.wantMatchRegex)) + // The original is left alone. + g.Expect(item.Spec.Match).To(Equal(tc.match)) + g.Expect(item.Spec.MatchRegex).To(Equal(tc.matchRegex)) + }) + } +} diff --git a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go index c127b1030..d04c7fe3d 100644 --- a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go +++ b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go @@ -2,6 +2,7 @@ package v1alpha2 import ( "fmt" + "regexp" "strings" "testing" @@ -1568,20 +1569,52 @@ func TestRenderMainConfig_ClusterFilterAfterNamespaceFilters(t *testing.T) { g.Expect(after).To(BeNumerically(">", namespaced)) } - config, err := cfg.RenderMainConfig(sl, inputs, filters, ClusterOutputList{}, nsFilters, nil, nil) + // The namespace routing filter re-tags the records of namespace foo to .$TAG, so the + // "after" filter has to match that form too, not only kube.*. + retagged := utils.GenerateNamespacedMatchExpr("foo", "kube.var.log.containers.app.log") + rule := fmt.Sprintf("$kubernetes['namespace_name'] ^(foo)$ %s.$TAG false", strings.Split(retagged, ".")[0]) + rewriteTagFilters := ClusterFilterList{Items: []ClusterFilter{{Spec: FilterSpec{ + Match: "kube.*", + FilterItems: []FilterItem{{RewriteTag: &filter.RewriteTag{Rules: []string{rule}}}}, + }}}} + rewriteTag, err := rewriteTagFilters.Load(sl) + g.Expect(err).NotTo(HaveOccurred()) + rendered, err := rewriteTagFilters.LoadAsYaml(sl, 1) + g.Expect(err).NotTo(HaveOccurred()) + rewriteTagYaml := strings.TrimPrefix(rendered, fmt.Sprintf("%sfilters:\n", utils.YamlIndent(1))) + + // expectAfterMatch checks the Match_Regex of the "after" filter (the last one rendered) against + // tags with and without the namespace prefix. + expectAfterMatch := func(config, key string) { + afterSection := config[strings.Index(config, "stage-namespaced"):] + start := strings.Index(afterSection, key) + g.Expect(start).To(BeNumerically(">=", 0)) + line := afterSection[start+len(key):] + line = strings.TrimSpace(line[:strings.Index(line, "\n")]) + re, err := regexp.Compile(line) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(re.MatchString("kube.var.log.containers.app.log")).To(BeTrue()) + g.Expect(re.MatchString(retagged)).To(BeTrue()) + g.Expect(re.MatchString("other.var.log")).To(BeFalse()) + g.Expect(re.MatchString("0123456789abcdef0123456789abcdef.other.var.log")).To(BeFalse()) + } + + config, err := cfg.RenderMainConfig(sl, inputs, filters, ClusterOutputList{}, nsFilters, nil, []string{rewriteTag}) g.Expect(err).NotTo(HaveOccurred()) expectOrder(config) + expectAfterMatch(config, "Match_Regex") - yamlConfig, err := cfg.RenderMainConfigInYaml(sl, inputs, filters, ClusterOutputList{}, nsFilters, nil, nil) + yamlConfig, err := cfg.RenderMainConfigInYaml(sl, inputs, filters, ClusterOutputList{}, nsFilters, nil, []string{rewriteTagYaml}) g.Expect(err).NotTo(HaveOccurred()) expectOrder(yamlConfig) + expectAfterMatch(yamlConfig, "match_regex:") g.Expect(strings.Count(yamlConfig, "filters:\n")).To(Equal(1)) var parsed map[string]any g.Expect(yaml.Unmarshal([]byte(yamlConfig), &parsed)).To(Succeed()) pipeline, ok := parsed["pipeline"].(map[string]any) g.Expect(ok).To(BeTrue()) - g.Expect(pipeline["filters"]).To(HaveLen(3)) + g.Expect(pipeline["filters"]).To(HaveLen(4)) // With only "after" cluster filters and no namespaced filters, the YAML still has a filters header. // afterOnly holds just the "after" ClusterFilter, so the before group and the namespaced Filters are diff --git a/docs/best-practice/misc.md b/docs/best-practice/misc.md index 3849ae253..a85221504 100644 --- a/docs/best-practice/misc.md +++ b/docs/best-practice/misc.md @@ -18,6 +18,8 @@ Path to file in Fluent Bit config should be well regulated. Fluent Bit Operator ClusterFilters run before the namespaced Filters, sorted by `ordinal` and then by name. Set `afterNamespaceFilters: true` on a ClusterFilter to run it after the namespaced Filters instead, e.g. to drop a field that the namespaced Filters still need. `ordinal` orders ClusterFilters within their group, so a ClusterFilter that runs after the namespaced Filters always comes after them, whatever its ordinal. Namespaced Filters reject the field. +By the time records leave the namespaced Filters, the operator has re-tagged them to `.`. So a `match` like `kube.*` on a ClusterFilter with `afterNamespaceFilters: true` is rendered as a `Match_Regex` that also allows that prefix, and a `matchRegex` anchored with `^` gets the same optional prefix. Records from namespaces without namespaced Filters keep their original tag and still match. + ```yaml apiVersion: fluentbit.fluent.io/v1alpha2 kind: ClusterFilter