Skip to content

Commit

Permalink
Support optional empty strings and fix the separator field in the con…
Browse files Browse the repository at this point in the history
…cat secret (#1235)
  • Loading branch information
pepov authored and aslafy-z committed May 23, 2023
1 parent e9ea849 commit be5f889
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pkg/sdk/logging/api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/sdk/logging/model/filter/concat.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ type Concat struct {
// Specify field name in the record to parse. If you leave empty the Container Runtime default will be used.
Key string `json:"key,omitempty"`
//The separator of lines. (default: "\n")
Separator string `json:"separator,omitempty"`
// +kubebuilder:validation:Optional
Separator *string `json:"separator,omitempty" plugin:"default:\"\\n\""`
//The number of lines. This is exclusive with multiline_start_regex.
NLines int `json:"n_lines,omitempty"`
//The regexp to match beginning of multiline. This is exclusive with n_lines.
Expand Down
46 changes: 36 additions & 10 deletions pkg/sdk/logging/model/filter/concat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,49 @@ import (
)

func TestConcat(t *testing.T) {
CONFIG := []byte(`
testData := []struct {
Config []byte
Expected string
}{
{
Config: []byte(`
partial_key: "partial_message"
separator: ""
n_lines: 10
`)
expected := `
`),
Expected: `
<filter **>
@type concat
@id test
key message
n_lines 10
partial_key partial_message
separator "\n"
</filter>
`
parser := &filter.Concat{}
require.NoError(t, yaml.Unmarshal(CONFIG, parser))
test := render.NewOutputPluginTest(t, parser)
test.DiffResult(expected)
`,
},
{
Config: []byte(`
partial_key: "partial_message"
n_lines: 10
separator: ""
`),
Expected: `
<filter **>
@type concat
@id test
key message
n_lines 10
partial_key partial_message
separator
</filter>
`,
},
}

for _, d := range testData {
parser := &filter.Concat{}
require.NoError(t, yaml.Unmarshal(d.Config, parser))
test := render.NewOutputPluginTest(t, parser)
test.DiffResult(d.Expected)
}
}
5 changes: 5 additions & 0 deletions pkg/sdk/logging/model/filter/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion pkg/sdk/logging/model/types/stringmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,19 @@ func (s *StructToStringMapper) processField(field reflect.StructField, value ref
}
}

var isPointer, isNil bool
if value.Kind() == reflect.Ptr {
isPointer = true
if value.IsNil() {
isNil = true
}
value = value.Elem()
}

switch value.Kind() { // nolint:exhaustive
case reflect.String, reflect.Int, reflect.Bool:
val := fmt.Sprintf("%v", value.Interface())
if val == "" {
if (isPointer && isNil) || (!isPointer && val == "") {
// check if default has been set and use it
if ok, def := pluginTagOpts.ValueForPrefix("default:"); ok {
val = def
Expand Down
22 changes: 16 additions & 6 deletions pkg/sdk/logging/model/types/stringmaps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"emperror.dev/errors"
"github.com/banzaicloud/logging-operator/pkg/sdk/logging/model/types"
"github.com/banzaicloud/operator-tools/pkg/secret"
"github.com/banzaicloud/operator-tools/pkg/utils"
corev1 "k8s.io/api/core/v1"
)

Expand Down Expand Up @@ -59,20 +60,29 @@ func TestRequiredMeansItCannotEvenBeEmpty(t *testing.T) {

func TestJsonTagsWithDefaultsAndOmitempty(t *testing.T) {
type Asd struct {
Field1 string `json:"field1"`
Field2 string `json:"field2,omitempty" plugin:"default:http://asdf and some space"`
Field3 string `json:"field3,omitempty"`
}
actual, err := types.NewStructToStringMapper(secret.NewSecretLoader(nil, "", "", nil)).StringsMap(Asd{Field1: "value"})
Field1 string `json:"field1"`
Field2 string `json:"field2,omitempty" plugin:"default:http://asdf and some space"`
Field3 string `json:"field3,omitempty"`
Field4 *string `json:"field4,omitempty" plugin:"default:nonempty"`
Field5 *string `json:"field5,omitempty" plugin:"default:nonempty"`
}
actual, err := types.NewStructToStringMapper(secret.NewSecretLoader(nil, "", "", nil)).StringsMap(
Asd{
Field1: "value",
Field4: utils.StringPointer(""), // looks like empty, but it's not, we expect this to be set
Field5: nil, // empty for real
})
if err != nil {
t.Fatalf("%+v", err)
}
expected := map[string]string{
"field1": "value",
"field2": "http://asdf and some space",
"field4": "",
"field5": "nonempty",
}
if !reflect.DeepEqual(expected, actual) {
t.Fatalf("failed to match expected %+v with %+v", expected, actual)
t.Fatalf("failed to match\nexpected:\n%+v\n\nactual:\n%+v", expected, actual)
}
}

Expand Down

0 comments on commit be5f889

Please sign in to comment.