Skip to content

Commit

Permalink
feat(update-collection-v3): add logs metadata migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikołaj Świątek committed Nov 24, 2022
1 parent 8a0a113 commit 34c3f98
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/go/cmd/update-collection-v3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
disablethanos "github.com/SumoLogic/sumologic-kubernetes-collection/tools/cmd/update-collection-v3/migrations/disable-thanos"
"github.com/SumoLogic/sumologic-kubernetes-collection/tools/cmd/update-collection-v3/migrations/events"
kubestatemetricscollectors "github.com/SumoLogic/sumologic-kubernetes-collection/tools/cmd/update-collection-v3/migrations/kube-state-metrics-collectors"
"github.com/SumoLogic/sumologic-kubernetes-collection/tools/cmd/update-collection-v3/migrations/logs"
tracingreplaces "github.com/SumoLogic/sumologic-kubernetes-collection/tools/cmd/update-collection-v3/migrations/tracing-replaces"
)

Expand Down Expand Up @@ -57,6 +58,7 @@ var migrationDirectoriesAndFunctions = map[string]migrateFunc{
"events": events.Migrate,
"disable-thanos": disablethanos.Migrate,
"tracing-replaces": tracingreplaces.Migrate,
"logs": logs.Migrate,
}

func migrateYaml(input string) (string, error) {
Expand Down
56 changes: 56 additions & 0 deletions src/go/cmd/update-collection-v3/migrations/logs/logs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package logs

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_LogsMetadataConfig(t *testing.T) {
testCases := []TestCase{
{
inputYaml: `{}`,
outputYaml: `{}`,
description: "No changes to config",
},
{
inputYaml: `
metadata:
logs:
config:
key: value
`,
outputYaml: `
metadata:
logs:
config:
merge:
key: value
`,
description: "Custom config moved to config.merge",
},
}
for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
runYamlTest(t, testCase)
})
}
}
40 changes: 40 additions & 0 deletions src/go/cmd/update-collection-v3/migrations/logs/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package logs

import (
"bytes"
"fmt"

"gopkg.in/yaml.v3"
)

func Migrate(yamlV2 string) (yamlV3 string, err error) {
valuesV2, err := parseValues(yamlV2)
if err != nil {
return "", fmt.Errorf("error parsing input yaml: %v", err)
}

valuesV3, err := migrate(&valuesV2)
if err != nil {
return "", fmt.Errorf("error migrating: %v", err)
}

buffer := bytes.Buffer{}
encoder := yaml.NewEncoder(&buffer)
encoder.SetIndent(2)
err = encoder.Encode(valuesV3)
return buffer.String(), err
}

func parseValues(yamlV2 string) (ValuesV2, error) {
var valuesV2 ValuesV2
err := yaml.Unmarshal([]byte(yamlV2), &valuesV2)
return valuesV2, err
}

func migrate(valuesV2 *ValuesV2) (ValuesV3, error) {
valuesV3 := ValuesV3{
Rest: valuesV2.Rest,
}
valuesV3.Metadata.Logs.Config.Merge = valuesV2.Metadata.Logs.Config
return valuesV3, nil
}
14 changes: 14 additions & 0 deletions src/go/cmd/update-collection-v3/migrations/logs/valuesv2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package logs

type ValuesV2 struct {
Metadata struct {
Logs LogsMetadataV2 `yaml:"logs,omitempty"`
Rest map[string]interface{} `yaml:",inline"`
} `yaml:"metadata,omitempty"`
Rest map[string]interface{} `yaml:",inline"`
}

type LogsMetadataV2 struct {
Config map[string]interface{} `yaml:"config,omitempty"`
Rest map[string]interface{} `yaml:",inline"`
}
17 changes: 17 additions & 0 deletions src/go/cmd/update-collection-v3/migrations/logs/valuesv3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package logs

type ValuesV3 struct {
Metadata struct {
Logs LogsMetadataV3 `yaml:"logs,omitempty"`
Rest map[string]interface{} `yaml:",inline"`
} `yaml:"metadata,omitempty"`
Rest map[string]interface{} `yaml:",inline"`
}

type LogsMetadataV3 struct {
Config struct {
Merge map[string]interface{} `yaml:"merge,omitempty"`
Override map[string]interface{} `yaml:"override,omitempty"`
} `yaml:"config,omitempty"`
Rest map[string]interface{} `yaml:",inline"`
}
4 changes: 4 additions & 0 deletions src/go/cmd/update-collection-v3/testdata/simple.input.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ kube-prometheus-stack:
resources:
limits:
cpu: 20m
metadata:
logs:
config:
key: value
sumologic:
accessId: xxx
accessKey: yyy
5 changes: 5 additions & 0 deletions src/go/cmd/update-collection-v3/testdata/simple.output.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
metadata:
logs:
config:
merge:
key: value
sumologic:
accessId: xxx
accessKey: yyy
Expand Down

0 comments on commit 34c3f98

Please sign in to comment.