Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 61 additions & 5 deletions apis/fluentbit/v1alpha2/clusterfilter_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -44,8 +46,12 @@ 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, a namespaced Filter that sets it is rejected.
AfterNamespaceFilters bool `json:"afterNamespaceFilters,omitempty"`
}

type FilterItem struct {
Expand Down Expand Up @@ -177,14 +183,64 @@ 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, matchNamespacedTags(item))
} else {
before.Items = append(before.Items, item)
}
}
return before, after
}

sort.Sort(FilterByOrdinalAndName(list.Items))
// namespacedTagPrefix matches the "<md5(namespace)>." 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 <md5(namespace)>.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
}
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 {
Expand Down
26 changes: 26 additions & 0 deletions apis/fluentbit/v1alpha2/clusterfilter_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,32 @@ func TestClusterFilter_RecordModifier_Generated_Load_As_Yaml(t *testing.T) {
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))
})
}
}

func TestClusterFilterList_Load_Sysinfo(t *testing.T) {
g := NewGomegaWithT(t)
sl := plugins.NewSecretLoader(nil, "testnamespace")
Expand Down
32 changes: 20 additions & 12 deletions apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
107 changes: 107 additions & 0 deletions apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1alpha2

import (
"fmt"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -1521,3 +1522,109 @@ 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("stage-cluster-after")},
},
{
ObjectMeta: metav1.ObjectMeta{Name: "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("stage-namespaced")},
}},
}}

expectOrder := func(config string) {
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))
}

// The namespace routing filter re-tags the records of namespace foo to <md5(foo)>.$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, []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(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
// 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())
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))
}
1 change: 1 addition & 0 deletions apis/fluentbit/v1alpha2/filter_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, a namespaced Filter that sets it is rejected.
type: boolean
filters:
description: A set of filter plugins in order.
items:
Expand Down Expand Up @@ -1036,7 +1042,9 @@ 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, a namespaced Filter that sets it is rejected.
type: boolean
filters:
description: A set of filter plugins in order.
items:
Expand Down Expand Up @@ -1036,10 +1042,15 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -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, a namespaced Filter that sets it is rejected.
type: boolean
filters:
description: A set of filter plugins in order.
items:
Expand Down Expand Up @@ -1034,7 +1040,9 @@ 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
Expand Down
Loading
Loading