diff --git a/cmd/config/configMigrate.go b/cmd/config/configMigrate.go index 4b79615f..d0e80d0c 100644 --- a/cmd/config/configMigrate.go +++ b/cmd/config/configMigrate.go @@ -100,7 +100,7 @@ func NewCmdConfigMigrate() *cobra.Command { l.Log().Fatalln(err) } } else { - if err := os.WriteFile(output, yamlout, os.ModeAppend); err != nil { + if err := os.WriteFile(output, yamlout, os.ModePerm); err != nil { l.Log().Fatalln(err) } } diff --git a/pkg/config/v1alpha3/migrations.go b/pkg/config/v1alpha3/migrations.go index 0faeff1b..5b20c9bb 100644 --- a/pkg/config/v1alpha3/migrations.go +++ b/pkg/config/v1alpha3/migrations.go @@ -25,11 +25,13 @@ package v1alpha3 import ( "encoding/json" "fmt" + "strings" configtypes "github.com/rancher/k3d/v5/pkg/config/types" "github.com/rancher/k3d/v5/pkg/config/v1alpha2" l "github.com/rancher/k3d/v5/pkg/logger" k3d "github.com/rancher/k3d/v5/pkg/types" + "github.com/rancher/k3d/v5/pkg/util" ) var Migrations = map[string]func(configtypes.Config) (configtypes.Config, error){ @@ -39,11 +41,23 @@ var Migrations = map[string]func(configtypes.Config) (configtypes.Config, error) func MigrateV1Alpha2(input configtypes.Config) (configtypes.Config, error) { l.Log().Debugln("Migrating v1alpha2 to v1alpha3") + // nodefilters changed from `@group[index]` to `@group:index` + nodeFilterReplacer := strings.NewReplacer( + "[", ":", // replace opening bracket + "]", "", // drop closing bracket + ) + + /* + * We're migrating matching fields between versions by marshalling to JSON and back + */ injson, err := json.Marshal(input) if err != nil { return nil, err } + /* + * Migrate config of `kind: Simple` + */ if input.GetKind() == "Simple" { cfgIntermediate := SimpleConfigIntermediateV1alpha2{} @@ -60,15 +74,20 @@ func MigrateV1Alpha2(input configtypes.Config) (configtypes.Config, error) { return nil, err } + // simple nodefilter changes cfg.Options.Runtime.Labels = []LabelWithNodeFilters{} for _, label := range input.(v1alpha2.SimpleConfig).Labels { cfg.Options.Runtime.Labels = append(cfg.Options.Runtime.Labels, LabelWithNodeFilters{ Label: label.Label, - NodeFilters: label.NodeFilters, + NodeFilters: util.ReplaceInAllElements(nodeFilterReplacer, label.NodeFilters), }) } + /* + * structural changes (e.g. added nodefilter support) + */ + cfg.Options.K3sOptions.ExtraArgs = []K3sArgWithNodeFilters{} for _, arg := range input.(v1alpha2.SimpleConfig).Options.K3sOptions.ExtraServerArgs { @@ -97,6 +116,25 @@ func MigrateV1Alpha2(input configtypes.Config) (configtypes.Config, error) { } } + /* + * Matching fields with only syntactical changes (e.g. nodefilter syntax changed) + */ + for _, env := range cfg.Env { + env.NodeFilters = util.ReplaceInAllElements(nodeFilterReplacer, env.NodeFilters) + } + + for _, vol := range cfg.Volumes { + vol.NodeFilters = util.ReplaceInAllElements(nodeFilterReplacer, vol.NodeFilters) + } + + for _, p := range cfg.Ports { + p.NodeFilters = util.ReplaceInAllElements(nodeFilterReplacer, p.NodeFilters) + } + + /* + * Finalizing + */ + cfg.APIVersion = ApiVersion l.Log().Debugf("Migrated config: %+v", cfg) diff --git a/pkg/util/util.go b/pkg/util/util.go index 0fa9cda2..9d760172 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -22,7 +22,16 @@ THE SOFTWARE. package util +import "strings" + func RemoveElementFromStringSlice(slice []string, index int) []string { slice[index] = slice[len(slice)-1] return slice[:len(slice)-1] } + +func ReplaceInAllElements(replacer *strings.Replacer, arr []string) []string { + for i, elem := range arr { + arr[i] = replacer.Replace(elem) + } + return arr +} diff --git a/tests/assets/config_test_simple_migration_v1alpha2.yaml b/tests/assets/config_test_simple_migration_v1alpha2.yaml index 49fb1bfe..e58d896c 100755 --- a/tests/assets/config_test_simple_migration_v1alpha2.yaml +++ b/tests/assets/config_test_simple_migration_v1alpha2.yaml @@ -25,7 +25,7 @@ env: labels: - label: foo=bar nodeFilters: - - server:0 + - server[0] - loadbalancer registries: create: true