Skip to content

Commit

Permalink
check if config is in correct format if we cannot unmarshal it into l…
Browse files Browse the repository at this point in the history
…egacy config in vcluster config convert

Signed-off-by: Paweł Bojanowski <pawelbojanowski@protonmail.com>
  • Loading branch information
hidalgopl committed Sep 5, 2024
1 parent c27c95e commit 8caf63e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
14 changes: 14 additions & 0 deletions config/legacyconfig/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func migrateK8sAndEKS(oldValues string, newConfig *config.Config) error {
oldConfig := &LegacyK8s{}
err := oldConfig.UnmarshalYAMLStrict([]byte(oldValues))
if err != nil {
if err := errIfConfigIsAlreadyConverted(oldValues); err != nil {
return err
}
return fmt.Errorf("unmarshal legacy config: %w", err)
}

Expand Down Expand Up @@ -91,6 +94,9 @@ func migrateK3sAndK0s(distro, oldValues string, newConfig *config.Config) error
oldConfig := &LegacyK0sAndK3s{}
err := oldConfig.UnmarshalYAMLStrict([]byte(oldValues))
if err != nil {
if err := errIfConfigIsAlreadyConverted(oldValues); err != nil {
return err
}
return fmt.Errorf("unmarshal legacy config: %w", err)
}

Expand Down Expand Up @@ -136,6 +142,14 @@ func migrateK3sAndK0s(distro, oldValues string, newConfig *config.Config) error
return convertBaseValues(oldConfig.BaseHelm, newConfig)
}

func errIfConfigIsAlreadyConverted(oldValues string) error {
currentConfig := &config.Config{}
if err := currentConfig.UnmarshalYAMLStrict([]byte(oldValues)); err == nil {
return fmt.Errorf("config is already in correct format")
}
return nil
}

func convertEtcd(oldConfig EtcdValues, newConfig *config.Config) error {
if oldConfig.Disabled {
newConfig.ControlPlane.BackingStore.Etcd.Deploy.StatefulSet.Enabled = false
Expand Down
44 changes: 44 additions & 0 deletions config/legacyconfig/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,50 @@ policies:
podManagementPolicy: OrderedReady`,
ExpectedErr: "",
},
{
Name: "k3s already migrated to correct format",
Distro: "k3s",
In: `sync:
fromHost:
nodes:
enabled: false
toHost:
serviceAccounts:
enabled: false
controlPlane:
distro:
k3s:
enabled: true
image:
tag: v1.30.2-k3s2
statefulSet:
scheduling:
podManagementPolicy: OrderedReady
`,
ExpectedErr: "migrate legacy k3s values: config is already in correct format",
},
{
Name: "k8s already migrated to correct format",
Distro: "k8s",
In: `sync:
fromHost:
nodes:
enabled: false
toHost:
serviceAccounts:
enabled: false
controlPlane:
distro:
k8s:
enabled: true
statefulSet:
scheduling:
podManagementPolicy: OrderedReady
`,
ExpectedErr: "migrate legacy k8s values: config is already in correct format",
},
}

for _, testCase := range testCases {
Expand Down

0 comments on commit 8caf63e

Please sign in to comment.