-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(update-collection-v3): add migration for log format
- Loading branch information
Mikołaj Świątek
committed
Jan 19, 2023
1 parent
6a44e1a
commit c1305ee
Showing
6 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/go/cmd/update-collection-v3/migrations/logformat/migrate.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package logformat | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func Migrate(inputYaml string) (outputYaml string, err error) { | ||
inputValues, err := parseValues(inputYaml) | ||
if err != nil { | ||
return "", fmt.Errorf("error parsing input yaml: %v", err) | ||
} | ||
|
||
outputValues := migrate(&inputValues) | ||
if err != nil { | ||
return "", fmt.Errorf("error migrating: %v", err) | ||
} | ||
|
||
buffer := bytes.Buffer{} | ||
encoder := yaml.NewEncoder(&buffer) | ||
encoder.SetIndent(2) | ||
err = encoder.Encode(outputValues) | ||
return buffer.String(), err | ||
} | ||
|
||
func parseValues(inputYaml string) (InputValues, error) { | ||
var inputValues InputValues | ||
err := yaml.Unmarshal([]byte(inputYaml), &inputValues) | ||
return inputValues, err | ||
} | ||
|
||
func migrate(inputValues *InputValues) OutputValues { | ||
outputValues := *inputValues | ||
|
||
if outputValues.Fluentd == nil || | ||
outputValues.Fluentd.Logs == nil || | ||
outputValues.Fluentd.Logs.Output == nil || | ||
outputValues.Fluentd.Logs.Output.LogFormat == nil { | ||
// not set for Fluentd, leave things as is | ||
return outputValues | ||
} | ||
|
||
logFormat := *outputValues.Fluentd.Logs.Output.LogFormat | ||
|
||
if outputValues.Sumologic == nil { | ||
outputValues.Sumologic = &Sumologic{} | ||
} | ||
|
||
if outputValues.Sumologic.Logs == nil { | ||
outputValues.Sumologic.Logs = &SumologicLogs{} | ||
} | ||
|
||
if outputValues.Sumologic.Logs.Container == nil { | ||
outputValues.Sumologic.Logs.Container = &SumologicLogsContainer{} | ||
} | ||
|
||
if outputValues.Sumologic.Logs.Container.Format == nil { | ||
outputValues.Sumologic.Logs.Container.Format = new(string) | ||
*outputValues.Sumologic.Logs.Container.Format = logFormat | ||
} | ||
|
||
// remove log format for Fluentd and clean up empty structs | ||
outputValues.Fluentd.Logs.Output.LogFormat = nil | ||
if len(outputValues.Fluentd.Logs.Output.Rest) == 0 { | ||
outputValues.Fluentd.Logs.Output = nil | ||
} | ||
if len(outputValues.Fluentd.Logs.Rest) == 0 { | ||
outputValues.Fluentd.Logs = nil | ||
} | ||
if len(outputValues.Fluentd.Rest) == 0 { | ||
outputValues.Fluentd = nil | ||
} | ||
|
||
return outputValues | ||
} |
113 changes: 113 additions & 0 deletions
113
src/go/cmd/update-collection-v3/migrations/logformat/migrate_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package logformat | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type TestCase struct { | ||
inputYaml string | ||
outputYaml string | ||
err error | ||
description string | ||
} | ||
|
||
func runYamlTest(t *testing.T, testCase TestCase) { | ||
actualOutput, err := Migrate(testCase.inputYaml) | ||
if testCase.err == nil { | ||
require.NoError(t, err, testCase.description) | ||
} else { | ||
require.Equal(t, err, testCase.err, testCase.description) | ||
} | ||
require.Equal(t, strings.Trim(testCase.outputYaml, "\n "), strings.Trim(actualOutput, "\n "), testCase.description) | ||
} | ||
|
||
func Test_EventsEnabledProvider(t *testing.T) { | ||
testCases := []TestCase{ | ||
{ | ||
inputYaml: `{}`, | ||
outputYaml: `{}`, | ||
description: "No config, no change", | ||
}, | ||
{ | ||
inputYaml: ` | ||
fluentd: | ||
key: value | ||
`, | ||
outputYaml: ` | ||
fluentd: | ||
key: value | ||
`, | ||
description: "No config, no change", | ||
}, | ||
{ | ||
inputYaml: ` | ||
fluentd: | ||
logs: | ||
key: value | ||
`, | ||
outputYaml: ` | ||
fluentd: | ||
logs: | ||
key: value | ||
`, | ||
description: "No config, no change", | ||
}, | ||
{ | ||
inputYaml: ` | ||
fluentd: | ||
logs: | ||
output: | ||
key: value | ||
`, | ||
outputYaml: ` | ||
fluentd: | ||
logs: | ||
output: | ||
key: value | ||
`, | ||
description: "No config, no change", | ||
}, | ||
{ | ||
inputYaml: ` | ||
fluentd: | ||
logs: | ||
output: | ||
logFormat: text | ||
`, | ||
outputYaml: ` | ||
sumologic: | ||
logs: | ||
container: | ||
format: text | ||
`, | ||
description: "Moved", | ||
}, | ||
{ | ||
inputYaml: ` | ||
fluentd: | ||
logs: | ||
output: | ||
logFormat: text | ||
sumologic: | ||
logs: | ||
container: | ||
format: json_merge | ||
`, | ||
outputYaml: ` | ||
sumologic: | ||
logs: | ||
container: | ||
format: json_merge | ||
`, | ||
description: "Don't overwrite sumologic format if already set", | ||
}, | ||
} | ||
for _, testCase := range testCases { | ||
t.Run(testCase.description, func(t *testing.T) { | ||
runYamlTest(t, testCase) | ||
}) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/go/cmd/update-collection-v3/migrations/logformat/values.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package logformat | ||
|
||
type Values struct { | ||
Sumologic *Sumologic `yaml:"sumologic,omitempty"` | ||
Fluentd *Fluentd `yaml:"fluentd,omitempty"` | ||
Rest map[string]interface{} `yaml:",inline"` | ||
} | ||
|
||
type Sumologic struct { | ||
Logs *SumologicLogs `yaml:"logs,omitempty"` | ||
Rest map[string]interface{} `yaml:",inline"` | ||
} | ||
|
||
type SumologicLogs struct { | ||
Container *SumologicLogsContainer `yaml:"container,omitempty"` | ||
Rest map[string]interface{} `yaml:",inline"` | ||
} | ||
|
||
type SumologicLogsContainer struct { | ||
Format *string `yaml:"format,omitempty"` | ||
Rest map[string]interface{} `yaml:",inline"` | ||
} | ||
|
||
type Fluentd struct { | ||
Logs *FluentdLogs `yaml:"logs,omitempty"` | ||
Rest map[string]interface{} `yaml:",inline"` | ||
} | ||
|
||
type FluentdLogs struct { | ||
Output *FluentdLogsOutput `yaml:"output,omitempty"` | ||
Rest map[string]interface{} `yaml:",inline"` | ||
} | ||
|
||
type FluentdLogsOutput struct { | ||
LogFormat *string `yaml:"logFormat,omitempty"` | ||
Rest map[string]interface{} `yaml:",inline"` | ||
} | ||
|
||
type InputValues = Values | ||
type OutputValues = Values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ fluentd: | |
logs: | ||
autoscaling: | ||
enabled: true | ||
output: | ||
logFormat: text | ||
metrics: | ||
autoscaling: | ||
enabled: true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters