Skip to content

Commit

Permalink
[Fix] k3d config migrate missing nodefilter migration (#767)
Browse files Browse the repository at this point in the history
configMigrate: add missing migrations for nodefilters and fix perm of outputfile
  • Loading branch information
iwilltry42 authored Oct 5, 2021
1 parent 81a41bd commit 53bdbec
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/config/configMigrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
40 changes: 39 additions & 1 deletion pkg/config/v1alpha3/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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{}

Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion tests/assets/config_test_simple_migration_v1alpha2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ env:
labels:
- label: foo=bar
nodeFilters:
- server:0
- server[0]
- loadbalancer
registries:
create: true
Expand Down

0 comments on commit 53bdbec

Please sign in to comment.