Skip to content

Commit

Permalink
feat(update-collection-v3): migrate otellogs.config.override
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikołaj Świątek committed Nov 28, 2022
1 parent eb88c15 commit 01ffa3a
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/go/cmd/update-collection-v3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
eventsconfigmerge "github.com/SumoLogic/sumologic-kubernetes-collection/tools/cmd/update-collection-v3/migrations/events-config-merge"
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/logsmetadataconfig"
otellogsconfigmerge "github.com/SumoLogic/sumologic-kubernetes-collection/tools/cmd/update-collection-v3/migrations/otellogs-config-merge"
tracingreplaces "github.com/SumoLogic/sumologic-kubernetes-collection/tools/cmd/update-collection-v3/migrations/tracing-replaces"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -86,6 +87,10 @@ var migrations = []Migration{
directory: "logs-metadata-config",
action: logsmetadataconfig.Migrate,
},
{
directory: "otellogs-config-merge",
action: otellogsconfigmerge.Migrate,
},
}

func migrateYaml(input string) (string, error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package otellogsconfigmerge

import (
"bytes"
"fmt"

"gopkg.in/yaml.v3"
)

type InputValues struct {
Otellogs struct {
Config struct {
Override map[string]interface{} `yaml:"override,omitempty"`
} `yaml:"config,omitempty"`
Rest map[string]interface{} `yaml:",inline"`
} `yaml:"otellogs,omitempty"`
Rest map[string]interface{} `yaml:",inline"`
}

type OutputValues struct {
Otellogs Otellogs `yaml:"otellogs,omitempty"`
Rest map[string]interface{} `yaml:",inline"`
}

type Otellogs struct {
Config Config `yaml:"config,omitempty"`
Rest map[string]interface{} `yaml:",inline"`
}

type Config struct {
Merge map[string]interface{} `yaml:"merge,omitempty"`
Override map[string]interface{} `yaml:"override,omitempty"`
}

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, err := 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, error) {
outputValues := OutputValues{
Rest: inputValues.Rest,
Otellogs: Otellogs{
Config: Config{
Merge: inputValues.Otellogs.Config.Override,
},
Rest: inputValues.Otellogs.Rest,
},
}
return outputValues, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
otellogs:
config:
override:
key: value
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
otellogs:
config:
merge:
key: value
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 @@ -12,6 +12,10 @@ otelevents:
config:
override:
key: value
otellogs:
config:
override:
key: value
metadata:
logs:
config:
Expand Down
4 changes: 4 additions & 0 deletions src/go/cmd/update-collection-v3/testdata/simple.output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ otelevents:
config:
merge:
key: value
otellogs:
config:
merge:
key: value
sumologic:
accessId: xxx
accessKey: yyy
Expand Down

0 comments on commit 01ffa3a

Please sign in to comment.