[Fix] `k3d config migrate` missing nodefilter migration (#767)

configMigrate: add missing migrations for nodefilters and fix perm of outputfile
pull/768/head
Thorsten Klein 3 years ago committed by GitHub
parent 81a41bdab1
commit 53bdbec636
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      cmd/config/configMigrate.go
  2. 40
      pkg/config/v1alpha3/migrations.go
  3. 9
      pkg/util/util.go
  4. 2
      tests/assets/config_test_simple_migration_v1alpha2.yaml

@ -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)
}
}

@ -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)

@ -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
}

@ -25,7 +25,7 @@ env:
labels:
- label: foo=bar
nodeFilters:
- server:0
- server[0]
- loadbalancer
registries:
create: true

Loading…
Cancel
Save