From 054aba200a5ee358ce4f0c8de65a8b1d6d7f129a Mon Sep 17 00:00:00 2001 From: Dominik Rosiek Date: Mon, 28 Nov 2022 08:15:06 +0100 Subject: [PATCH] feat(upgrade_to_v3): add migration to remove sumologic.cluster.load_config_file Signed-off-by: Dominik Rosiek --- src/go/cmd/update-collection-v3/main.go | 14 +++--- .../remove-load-config-file/migrate.go | 50 +++++++++++++++++++ .../testdata/complex_config.input.yaml | 7 +++ .../testdata/complex_config.output.yaml | 6 +++ .../testdata/load_config_file_only.input.yaml | 3 ++ .../load_config_file_only.output.yaml | 1 + 6 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 src/go/cmd/update-collection-v3/migrations/remove-load-config-file/migrate.go create mode 100644 src/go/cmd/update-collection-v3/migrations/remove-load-config-file/testdata/complex_config.input.yaml create mode 100644 src/go/cmd/update-collection-v3/migrations/remove-load-config-file/testdata/complex_config.output.yaml create mode 100644 src/go/cmd/update-collection-v3/migrations/remove-load-config-file/testdata/load_config_file_only.input.yaml create mode 100644 src/go/cmd/update-collection-v3/migrations/remove-load-config-file/testdata/load_config_file_only.output.yaml diff --git a/src/go/cmd/update-collection-v3/main.go b/src/go/cmd/update-collection-v3/main.go index 57eb8e88..285cb5c2 100644 --- a/src/go/cmd/update-collection-v3/main.go +++ b/src/go/cmd/update-collection-v3/main.go @@ -12,6 +12,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" + removeloadconfigfile "github.com/SumoLogic/sumologic-kubernetes-collection/tools/cmd/update-collection-v3/migrations/remove-load-config-file" tracingreplaces "github.com/SumoLogic/sumologic-kubernetes-collection/tools/cmd/update-collection-v3/migrations/tracing-replaces" ) @@ -55,12 +56,13 @@ func migrateYamlFile(yamlV2FilePath string, yamlV3FilePath string) error { } var migrationDirectoriesAndFunctions = map[string]migrateFunc{ - "kube-prometheus-stack": kubestatemetricscollectors.Migrate, - "events": events.Migrate, - "disable-thanos": disablethanos.Migrate, - "tracing-replaces": tracingreplaces.Migrate, - "events-config-merge": eventsconfigmerge.Migrate, - "logs-metadata-config": logsmetadataconfig.Migrate, + "kube-prometheus-stack": kubestatemetricscollectors.Migrate, + "events": events.Migrate, + "disable-thanos": disablethanos.Migrate, + "tracing-replaces": tracingreplaces.Migrate, + "events-config-merge": eventsconfigmerge.Migrate, + "logs-metadata-config": logsmetadataconfig.Migrate, + "remove-load-config-file": removeloadconfigfile.Migrate, } func migrateYaml(input string) (string, error) { diff --git a/src/go/cmd/update-collection-v3/migrations/remove-load-config-file/migrate.go b/src/go/cmd/update-collection-v3/migrations/remove-load-config-file/migrate.go new file mode 100644 index 00000000..ec389419 --- /dev/null +++ b/src/go/cmd/update-collection-v3/migrations/remove-load-config-file/migrate.go @@ -0,0 +1,50 @@ +package removeloadconfigfile + +import ( + "bytes" + "fmt" + + "gopkg.in/yaml.v3" +) + +type Values struct { + Sumologic struct { + Cluster Cluster `yaml:"cluster,omitempty"` + Rest map[string]interface{} `yaml:",inline"` + } `yaml:"sumologic,omitempty"` + Rest map[string]interface{} `yaml:",inline"` +} + +type Cluster struct { + LoadConfigFile *bool `yaml:"load_config_file,omitempty"` + Rest map[string]interface{} `yaml:",inline"` +} + +func Migrate(inputYaml string) (outputYaml string, err error) { + values, err := parseValues(inputYaml) + if err != nil { + return "", fmt.Errorf("error parsing input yaml: %v", err) + } + + outputValues, err := migrate(&values) + 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) (Values, error) { + var inputValues Values + err := yaml.Unmarshal([]byte(inputYaml), &inputValues) + return inputValues, err +} + +func migrate(values *Values) (Values, error) { + values.Sumologic.Cluster.LoadConfigFile = nil + return *values, nil +} diff --git a/src/go/cmd/update-collection-v3/migrations/remove-load-config-file/testdata/complex_config.input.yaml b/src/go/cmd/update-collection-v3/migrations/remove-load-config-file/testdata/complex_config.input.yaml new file mode 100644 index 00000000..c83a9e98 --- /dev/null +++ b/src/go/cmd/update-collection-v3/migrations/remove-load-config-file/testdata/complex_config.input.yaml @@ -0,0 +1,7 @@ +sumologic: + a: b + cluster: + c: d + load_config_file: false + e: f +g: h diff --git a/src/go/cmd/update-collection-v3/migrations/remove-load-config-file/testdata/complex_config.output.yaml b/src/go/cmd/update-collection-v3/migrations/remove-load-config-file/testdata/complex_config.output.yaml new file mode 100644 index 00000000..95e46735 --- /dev/null +++ b/src/go/cmd/update-collection-v3/migrations/remove-load-config-file/testdata/complex_config.output.yaml @@ -0,0 +1,6 @@ +sumologic: + cluster: + c: d + e: f + a: b +g: h diff --git a/src/go/cmd/update-collection-v3/migrations/remove-load-config-file/testdata/load_config_file_only.input.yaml b/src/go/cmd/update-collection-v3/migrations/remove-load-config-file/testdata/load_config_file_only.input.yaml new file mode 100644 index 00000000..383b9bfa --- /dev/null +++ b/src/go/cmd/update-collection-v3/migrations/remove-load-config-file/testdata/load_config_file_only.input.yaml @@ -0,0 +1,3 @@ +sumologic: + cluster: + load_config_file: false diff --git a/src/go/cmd/update-collection-v3/migrations/remove-load-config-file/testdata/load_config_file_only.output.yaml b/src/go/cmd/update-collection-v3/migrations/remove-load-config-file/testdata/load_config_file_only.output.yaml new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/src/go/cmd/update-collection-v3/migrations/remove-load-config-file/testdata/load_config_file_only.output.yaml @@ -0,0 +1 @@ +{}