mirror of https://github.com/k3d-io/k3d
config: fix failing v1alpha2 -> v1alpha3 migration (#799)
- add validation as extra step after migration - add missing json struct tags to new v1alpha3 config fields - add unit tests - add missing `omitempty` tags to configs to avoid (un-)marshalling issues - drop `string` type for `time.Duration` type field in JSON Schema (doesn't work properly sometimes due to broken handling of time.Duration as per https://github.com/golang/go/issues/10275)pull/808/head
parent
12180ffdd5
commit
594e3ced3f
@ -0,0 +1,76 @@ |
||||
/* |
||||
Copyright © 2020-2021 The k3d Author(s) |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
of this software and associated documentation files (the "Software"), to deal |
||||
in the Software without restriction, including without limitation the rights |
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
copies of the Software, and to permit persons to whom the Software is |
||||
furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included in |
||||
all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
||||
THE SOFTWARE. |
||||
*/ |
||||
|
||||
package config |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/go-test/deep" |
||||
l "github.com/rancher/k3d/v5/pkg/logger" |
||||
"github.com/spf13/viper" |
||||
) |
||||
|
||||
func TestMigrateV1Alpha2ToV1Alpha3(t *testing.T) { |
||||
|
||||
actualPath := "test_assets/config_test_simple_migration_v1alpha2.yaml" |
||||
expectedPath := "test_assets/config_test_simple_migration_v1alpha3.yaml" |
||||
|
||||
actualViper := viper.New() |
||||
expectedViper := viper.New() |
||||
|
||||
actualViper.SetConfigType("yaml") |
||||
expectedViper.SetConfigType("yaml") |
||||
|
||||
actualViper.SetConfigFile(actualPath) |
||||
expectedViper.SetConfigFile(expectedPath) |
||||
|
||||
if err := actualViper.ReadInConfig(); err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
if err := expectedViper.ReadInConfig(); err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
actualCfg, err := FromViper(actualViper) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
if actualCfg.GetAPIVersion() != DefaultConfigApiVersion { |
||||
actualCfg, err = Migrate(actualCfg, DefaultConfigApiVersion) |
||||
if err != nil { |
||||
l.Log().Fatalln(err) |
||||
} |
||||
} |
||||
|
||||
expectedCfg, err := FromViper(expectedViper) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
if diff := deep.Equal(actualCfg, expectedCfg); diff != nil { |
||||
t.Fatalf("Actual\n%#v\ndoes not match expected\n%+v\nDiff:\n%#v", actualCfg, expectedCfg, diff) |
||||
} |
||||
|
||||
} |
@ -0,0 +1,51 @@ |
||||
apiVersion: k3d.io/v1alpha2 |
||||
kind: Simple |
||||
name: test |
||||
servers: 3 |
||||
agents: 2 |
||||
kubeAPI: |
||||
hostIP: "0.0.0.0" |
||||
hostPort: "6446" |
||||
#image: rancher/k3s:latest |
||||
volumes: |
||||
- volume: /my/path:/some/path |
||||
nodeFilters: |
||||
- all |
||||
ports: |
||||
- port: 80:80 |
||||
nodeFilters: |
||||
- loadbalancer |
||||
- port: 0.0.0.0:443:443 |
||||
nodeFilters: |
||||
- loadbalancer |
||||
env: |
||||
- envVar: bar=baz,bob |
||||
nodeFilters: |
||||
- all |
||||
labels: |
||||
- label: foo=bar |
||||
nodeFilters: |
||||
- server[0] |
||||
- loadbalancer |
||||
registries: |
||||
create: true |
||||
use: [] |
||||
config: | |
||||
mirrors: |
||||
"my.company.registry": |
||||
endpoint: |
||||
- http://my.company.registry:5000 |
||||
|
||||
options: |
||||
k3d: |
||||
wait: true |
||||
timeout: "360s" # should be pretty high for multi-server clusters to allow for a proper startup routine |
||||
disableLoadbalancer: false |
||||
disableImageVolume: false |
||||
k3s: |
||||
extraServerArgs: |
||||
- --tls-san=127.0.0.1 |
||||
extraAgentArgs: [] |
||||
kubeconfig: |
||||
updateDefaultKubeconfig: true |
||||
switchCurrentContext: true |
@ -0,0 +1,55 @@ |
||||
apiVersion: k3d.io/v1alpha3 |
||||
kind: Simple |
||||
name: test |
||||
servers: 3 |
||||
agents: 2 |
||||
kubeAPI: |
||||
hostIP: "0.0.0.0" |
||||
hostPort: "6446" |
||||
#image: rancher/k3s:latest |
||||
volumes: |
||||
- volume: /my/path:/some/path |
||||
nodeFilters: |
||||
- all |
||||
ports: |
||||
- port: 80:80 |
||||
nodeFilters: |
||||
- loadbalancer |
||||
- port: 0.0.0.0:443:443 |
||||
nodeFilters: |
||||
- loadbalancer |
||||
env: |
||||
- envVar: bar=baz,bob |
||||
nodeFilters: |
||||
- all |
||||
registries: |
||||
create: |
||||
name: k3d-test-registry |
||||
host: "0.0.0.0" |
||||
hostPort: random |
||||
config: | |
||||
mirrors: |
||||
"my.company.registry": |
||||
endpoint: |
||||
- http://my.company.registry:5000 |
||||
|
||||
options: |
||||
k3d: |
||||
wait: true |
||||
timeout: "360s" # should be pretty high for multi-server clusters to allow for a proper startup routine |
||||
disableLoadbalancer: false |
||||
disableImageVolume: false |
||||
k3s: |
||||
extraArgs: |
||||
- arg: --tls-san=127.0.0.1 |
||||
nodeFilters: |
||||
- server:* |
||||
kubeconfig: |
||||
updateDefaultKubeconfig: true |
||||
switchCurrentContext: true |
||||
runtime: |
||||
labels: |
||||
- label: foo=bar |
||||
nodeFilters: |
||||
- server:0 |
||||
- loadbalancer |
Loading…
Reference in new issue