Skip to content

Commit

Permalink
Merge pull request #1224 from kube-logging/syslogng-flow-empty-match
Browse files Browse the repository at this point in the history
handle empty flow match same way as if it was undefined
  • Loading branch information
ahma authored Mar 3, 2023
2 parents f162b2c + ded5e9c commit ac9eb43
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
5 changes: 5 additions & 0 deletions pkg/sdk/logging/api/v1beta1/syslogng_flow_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ type SyslogNGFlowSpec struct {

type SyslogNGMatch filter.MatchExpr

// IsEmpty returns true if the match is not specified, i.e. empty.
func (m *SyslogNGMatch) IsEmpty() bool {
return (*filter.MatchExpr)(m).IsEmpty()
}

// Filter definition for SyslogNGFlowSpec
type SyslogNGFilter struct {
ID string `json:"id,omitempty" syslog-ng:"ignore"`
Expand Down
6 changes: 3 additions & 3 deletions pkg/sdk/logging/model/syslogng/config/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
"strconv"
"strings"

"github.com/cisco-open/operator-tools/pkg/secret"
"github.com/kube-logging/logging-operator/pkg/sdk/logging/api/v1beta1"
"github.com/kube-logging/logging-operator/pkg/sdk/logging/model/syslogng/config/model"
"github.com/kube-logging/logging-operator/pkg/sdk/logging/model/syslogng/config/render"
filter "github.com/kube-logging/logging-operator/pkg/sdk/logging/model/syslogng/filter"
"github.com/cisco-open/operator-tools/pkg/secret"
"github.com/siliconbrain/go-seqs/seqs"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand All @@ -42,7 +42,7 @@ func renderClusterFlow(sourceName string, f v1beta1.SyslogNGClusterFlow, secretL
[]string{sourceName},
seqs.ToSlice(seqs.Concat(
seqs.FromValues(
render.If(f.Spec.Match != nil, filterRefStmt(matchName)),
render.If(!f.Spec.Match.IsEmpty(), filterRefStmt(matchName)),
),
seqs.MapWithIndex(seqs.FromSlice(f.Spec.Filters), func(idx int, flt v1beta1.SyslogNGFilter) render.Renderer {
return parenDefStmt(filterKind(flt), render.Literal(filterID(flt, idx, baseName)))
Expand Down Expand Up @@ -88,7 +88,7 @@ func renderFlow(controlNS string, sourceName string, keyDelim string, f v1beta1.
}

func renderFlowMatch(name string, m *v1beta1.SyslogNGMatch) render.Renderer {
if m == nil {
if m.IsEmpty() {
return nil
}
return filterDefStmt(name, renderMatchExpr(filter.MatchExpr(*m)))
Expand Down
74 changes: 74 additions & 0 deletions pkg/sdk/logging/model/syslogng/config/flow_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright © 2023 Kube logging authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package config

import (
"strings"
"testing"

"github.com/kube-logging/logging-operator/pkg/sdk/logging/api/v1beta1"
"github.com/kube-logging/logging-operator/pkg/sdk/logging/model/syslogng/config/render"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestRenderClusterFlow(t *testing.T) {
testCases := map[string]struct {
clusterFlow v1beta1.SyslogNGClusterFlow
expected string
}{
"nil match": {
clusterFlow: v1beta1.SyslogNGClusterFlow{
ObjectMeta: v1.ObjectMeta{
Name: "test_clusterflow",
Namespace: "test_ns",
},
Spec: v1beta1.SyslogNGClusterFlowSpec{
Match: nil,
},
},
expected: Untab(`log {
source("test_input");
};
`),
},
"empty match": {
clusterFlow: v1beta1.SyslogNGClusterFlow{
ObjectMeta: v1.ObjectMeta{
Name: "test_clusterflow",
Namespace: "test_ns",
},
Spec: v1beta1.SyslogNGClusterFlowSpec{
Match: &v1beta1.SyslogNGMatch{},
},
},
expected: Untab(`log {
source("test_input");
};
`),
},
}
for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
out := strings.Builder{}
require.NoError(t, renderClusterFlow("test_input", testCase.clusterFlow, nil)(render.RenderContext{
Out: &out,
}))
assert.Equal(t, testCase.expected, out.String())
})
}
}
10 changes: 10 additions & 0 deletions pkg/sdk/logging/model/syslogng/filter/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ type _metaMatch interface{} //nolint:deadcode,unused
// +kubebuilder:object:generate=true
type MatchConfig MatchExpr

// IsEmpty returns true if the config is not specified, i.e. empty.
func (c *MatchConfig) IsEmpty() bool {
return (*MatchExpr)(c).IsEmpty()
}

// +kubebuilder:object:generate=true
type MatchExpr struct {
// +kubebuilder:pruning:PreserveUnknownFields
Expand All @@ -64,6 +69,11 @@ type MatchExpr struct {
Or []MatchExpr `json:"or,omitempty"`
}

// IsEmpty returns true if the expression is not specified, i.e. empty.
func (expr *MatchExpr) IsEmpty() bool {
return expr == nil || (len(expr.And) == 0 && expr.Not == nil && len(expr.Or) == 0 && expr.Regexp == nil)
}

// +kubebuilder:object:generate=true
// +docName:"Regexp Directive"
// Specify filtering rule. For details, see the [syslog-ng documentation](https://www.syslog-ng.com/technical-documents/doc/syslog-ng-open-source-edition/3.37/administration-guide/68#TOPIC-1829171).
Expand Down

0 comments on commit ac9eb43

Please sign in to comment.