Skip to content

Commit

Permalink
[Feature] Config v1alpha4 (#944)
Browse files Browse the repository at this point in the history
- add: hostAliases
- add: hostPidMode
  • Loading branch information
iwilltry42 authored Jan 28, 2022
1 parent c8eca27 commit 08bf145
Show file tree
Hide file tree
Showing 35 changed files with 689 additions and 61 deletions.
2 changes: 1 addition & 1 deletion cmd/cluster/clusterCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
cliconfig "github.com/rancher/k3d/v5/cmd/util/config"
k3dCluster "github.com/rancher/k3d/v5/pkg/client"
"github.com/rancher/k3d/v5/pkg/config"
conf "github.com/rancher/k3d/v5/pkg/config/v1alpha3"
conf "github.com/rancher/k3d/v5/pkg/config/v1alpha4"
l "github.com/rancher/k3d/v5/pkg/logger"
"github.com/rancher/k3d/v5/pkg/runtimes"
k3d "github.com/rancher/k3d/v5/pkg/types"
Expand Down
2 changes: 1 addition & 1 deletion cmd/cluster/clusterEdit.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/rancher/k3d/v5/cmd/util"
cliutil "github.com/rancher/k3d/v5/cmd/util"
"github.com/rancher/k3d/v5/pkg/client"
conf "github.com/rancher/k3d/v5/pkg/config/v1alpha3"
conf "github.com/rancher/k3d/v5/pkg/config/v1alpha4"
l "github.com/rancher/k3d/v5/pkg/logger"
"github.com/rancher/k3d/v5/pkg/runtimes"
k3d "github.com/rancher/k3d/v5/pkg/types"
Expand Down
2 changes: 1 addition & 1 deletion cmd/config/configInit.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"fmt"
"os"

config "github.com/rancher/k3d/v5/pkg/config/v1alpha3"
config "github.com/rancher/k3d/v5/pkg/config/v1alpha4"
l "github.com/rancher/k3d/v5/pkg/logger"
"github.com/spf13/cobra"
)
Expand Down
8 changes: 4 additions & 4 deletions docs/usage/configfile.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ Using a config file is as easy as putting it in a well-known place in your file

As of the time of writing this documentation, the config file only **requires** you to define two fields:

- `apiVersion` to match the version of the config file that you want to use (at this time it would be `apiVersion: k3d.io/v1alpha3`)
- `apiVersion` to match the version of the config file that you want to use (at this time it would be `apiVersion: k3d.io/v1alpha4`)
- `kind` to define the kind of config file that you want to use (currently we only have the `Simple` config)

So this would be the minimal config file, which configures absolutely nothing:

```yaml
apiVersion: k3d.io/v1alpha3
apiVersion: k3d.io/v1alpha4
kind: Simple
```
Expand All @@ -42,15 +42,15 @@ Currently, the config file is still in an Alpha-State, meaning, that it is subje
!!! info "Validation via JSON-Schema"
k3d uses a [JSON-Schema](https://json-schema.org/) to describe the expected format and fields of the configuration file.
This schema is also used to [validate](/~https://github.com/xeipuuv/gojsonschema#validation) a user-given config file.
This JSON-Schema can be found in the specific config version sub-directory in the repository (e.g. [here for `v1alpha3`](/~https://github.com/rancher/k3d/blob/main/pkg/config/v1alpha3/schema.json)) and could be used to lookup supported fields or by linters to validate the config file, e.g. in your code editor.
This JSON-Schema can be found in the specific config version sub-directory in the repository (e.g. [here for `v1alpha4`](/~https://github.com/rancher/k3d/blob/main/pkg/config/v1alpha4/schema.json)) and could be used to lookup supported fields or by linters to validate the config file, e.g. in your code editor.

### All Options: Example

Since the config options and the config file are changing quite a bit, it's hard to keep track of all the supported config file settings, so here's an example showing all of them as of the time of writing:

```yaml
# k3d configuration file, saved as e.g. /home/me/myk3dcluster.yaml
apiVersion: k3d.io/v1alpha3 # this will change in the future as we make everything more stable
apiVersion: k3d.io/v1alpha4 # this will change in the future as we make everything more stable
kind: Simple # internally, we also have a Cluster config, which is not yet available externally
name: mycluster # name that you want to give to your cluster (will still be prefixed with `k3d-`)
servers: 1 # same as `--servers 1`
Expand Down
2 changes: 1 addition & 1 deletion docs/usage/registries.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ This file can also be used for providing additional information necessary for ac
If you're using a `SimpleConfig` file to configure your k3d cluster, you may as well embed the registries.yaml in there directly:

```yaml
apiVersion: k3d.io/v1alpha3
apiVersion: k3d.io/v1alpha4
kind: Simple
name: test
servers: 1
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
"github.com/imdario/mergo"
copystruct "github.com/mitchellh/copystructure"
"github.com/rancher/k3d/v5/pkg/actions"
config "github.com/rancher/k3d/v5/pkg/config/v1alpha3"
config "github.com/rancher/k3d/v5/pkg/config/v1alpha4"
l "github.com/rancher/k3d/v5/pkg/logger"
"github.com/rancher/k3d/v5/pkg/runtimes"
k3drt "github.com/rancher/k3d/v5/pkg/runtimes"
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

"github.com/docker/go-connections/nat"
"github.com/rancher/k3d/v5/pkg/config/types"
config "github.com/rancher/k3d/v5/pkg/config/v1alpha3"
config "github.com/rancher/k3d/v5/pkg/config/v1alpha4"
l "github.com/rancher/k3d/v5/pkg/logger"
"github.com/rancher/k3d/v5/pkg/runtimes"
k3d "github.com/rancher/k3d/v5/pkg/types"
Expand Down
8 changes: 7 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import (

"github.com/rancher/k3d/v5/pkg/config/v1alpha2"
"github.com/rancher/k3d/v5/pkg/config/v1alpha3"
defaultConfig "github.com/rancher/k3d/v5/pkg/config/v1alpha3"
"github.com/rancher/k3d/v5/pkg/config/v1alpha4"
defaultConfig "github.com/rancher/k3d/v5/pkg/config/v1alpha4"

types "github.com/rancher/k3d/v5/pkg/config/types"
)
Expand All @@ -41,6 +42,7 @@ const DefaultConfigApiVersion = defaultConfig.ApiVersion
var Schemas = map[string]string{
v1alpha2.ApiVersion: v1alpha2.JSONSchema,
v1alpha3.ApiVersion: v1alpha3.JSONSchema,
v1alpha4.ApiVersion: v1alpha4.JSONSchema,
}

func GetSchemaByVersion(apiVersion string) ([]byte, error) {
Expand All @@ -66,6 +68,8 @@ func FromViper(config *viper.Viper) (types.Config, error) {
cfg, err = v1alpha2.GetConfigByKind(kind)
case "k3d.io/v1alpha3":
cfg, err = v1alpha3.GetConfigByKind(kind)
case "k3d.io/v1alpha4":
cfg, err = v1alpha4.GetConfigByKind(kind)
case "":
cfg, err = defaultConfig.GetConfigByKind(kind)
default:
Expand All @@ -87,6 +91,8 @@ func getMigrations(version string) map[string]func(types.Config) (types.Config,
switch version {
case v1alpha3.ApiVersion:
return v1alpha3.Migrations
case v1alpha4.ApiVersion:
return v1alpha4.Migrations
default:
return nil
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/config/config.versions.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
"type": "object",
"oneOf": [
{
"allOf": [
{
"properties": {
"version": {
"const": "v1alpha4"
}
}
},
{
"$ref": "https://raw.githubusercontent.com/rancher/k3d/main/pkg/config/v1alpha4/schema.json"
}
]
},{
"allOf": [
{
"properties": {
Expand Down
10 changes: 5 additions & 5 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

"github.com/go-test/deep"
configtypes "github.com/rancher/k3d/v5/pkg/config/types"
conf "github.com/rancher/k3d/v5/pkg/config/v1alpha3"
conf "github.com/rancher/k3d/v5/pkg/config/v1alpha4"
"github.com/spf13/viper"

k3d "github.com/rancher/k3d/v5/pkg/types"
Expand All @@ -41,7 +41,7 @@ func TestReadSimpleConfig(t *testing.T) {

expectedConfig := conf.SimpleConfig{
TypeMeta: configtypes.TypeMeta{
APIVersion: "k3d.io/v1alpha3",
APIVersion: "k3d.io/v1alpha4",
Kind: "Simple",
},
Name: "test",
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestReadClusterConfig(t *testing.T) {

expectedConfig := conf.ClusterConfig{
TypeMeta: configtypes.TypeMeta{
APIVersion: "k3d.io/v1alpha3",
APIVersion: "k3d.io/v1alpha4",
Kind: "Cluster",
},
Cluster: k3d.Cluster{
Expand Down Expand Up @@ -182,7 +182,7 @@ func TestReadClusterListConfig(t *testing.T) {

expectedConfig := conf.ClusterListConfig{
TypeMeta: configtypes.TypeMeta{
APIVersion: "k3d.io/v1alpha3",
APIVersion: "k3d.io/v1alpha4",
Kind: "ClusterList",
},
Clusters: []k3d.Cluster{
Expand Down Expand Up @@ -265,7 +265,7 @@ func TestReadSimpleConfigRegistries(t *testing.T) {

expectedConfig := conf.SimpleConfig{
TypeMeta: configtypes.TypeMeta{
APIVersion: "k3d.io/v1alpha3",
APIVersion: "k3d.io/v1alpha4",
Kind: "Simple",
},
Name: "test",
Expand Down
6 changes: 3 additions & 3 deletions pkg/config/jsonschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ package config
import (
"testing"

"github.com/rancher/k3d/v5/pkg/config/v1alpha3"
conf "github.com/rancher/k3d/v5/pkg/config/v1alpha4"
)

func TestValidateSchema(t *testing.T) {

cfgPath := "./test_assets/config_test_simple.yaml"

if err := ValidateSchemaFile(cfgPath, []byte(v1alpha3.JSONSchema)); err != nil {
if err := ValidateSchemaFile(cfgPath, []byte(conf.JSONSchema)); err != nil {
t.Errorf("Validation of config file %s against the default schema failed: %+v", cfgPath, err)
}

Expand All @@ -42,7 +42,7 @@ func TestValidateSchemaFail(t *testing.T) {
cfgPath := "./test_assets/config_test_simple_invalid_servers.yaml"

var err error
if err = ValidateSchemaFile(cfgPath, []byte(v1alpha3.JSONSchema)); err == nil {
if err = ValidateSchemaFile(cfgPath, []byte(conf.JSONSchema)); err == nil {
t.Errorf("Validation of config file %s against the default schema passed where we expected a failure", cfgPath)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/config/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"fmt"

"github.com/imdario/mergo"
conf "github.com/rancher/k3d/v5/pkg/config/v1alpha3"
conf "github.com/rancher/k3d/v5/pkg/config/v1alpha4"
l "github.com/rancher/k3d/v5/pkg/logger"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/config/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"testing"

configtypes "github.com/rancher/k3d/v5/pkg/config/types"
conf "github.com/rancher/k3d/v5/pkg/config/v1alpha3"
conf "github.com/rancher/k3d/v5/pkg/config/v1alpha4"
"github.com/spf13/viper"
"gotest.tools/assert"
)
Expand Down
8 changes: 8 additions & 0 deletions pkg/config/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,11 @@ func TestMigrateV1Alpha2ToV1Alpha3(t *testing.T) {
}

}

func TestMigrateV1Alpha2ToV1Alpha4(t *testing.T) {
t.Log("not implemented") // TODO: test migration v1alpha2 to v1alpha4
}

func TestMigrateV1Alpha3ToV1Alpha4(t *testing.T) {
t.Log("not implemented") // TODO: test migration v1alpha3 to v1alpha4
}
2 changes: 1 addition & 1 deletion pkg/config/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ package config
import (
"strings"

conf "github.com/rancher/k3d/v5/pkg/config/v1alpha3"
conf "github.com/rancher/k3d/v5/pkg/config/v1alpha4"
l "github.com/rancher/k3d/v5/pkg/logger"
runtimeutil "github.com/rancher/k3d/v5/pkg/runtimes/util"
k3d "github.com/rancher/k3d/v5/pkg/types"
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"strings"
"testing"

conf "github.com/rancher/k3d/v5/pkg/config/v1alpha3"
conf "github.com/rancher/k3d/v5/pkg/config/v1alpha4"
"github.com/rancher/k3d/v5/pkg/runtimes"
"github.com/rancher/k3d/v5/pkg/types/k3s"
"github.com/spf13/viper"
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/test_assets/config_test_cluster.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apiVersion: k3d.io/v1alpha3
apiVersion: k3d.io/v1alpha4
kind: Cluster
name: foo
nodes:
- name: foo-node-0
role: server
role: server
4 changes: 2 additions & 2 deletions pkg/config/test_assets/config_test_cluster_list.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
apiVersion: k3d.io/v1alpha3
apiVersion: k3d.io/v1alpha4
kind: ClusterList
clusters:
- name: foo
Expand All @@ -9,4 +9,4 @@ clusters:
- name: bar
nodes:
- name: bar-node-0
role: server
role: server
2 changes: 1 addition & 1 deletion pkg/config/test_assets/config_test_registries.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: k3d.io/v1alpha3
apiVersion: k3d.io/v1alpha4
kind: Simple
name: test
servers: 1
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/test_assets/config_test_simple.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: k3d.io/v1alpha3
apiVersion: k3d.io/v1alpha4
kind: Simple
name: test
servers: 1
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/test_assets/config_test_simple_2.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: k3d.io/v1alpha3
apiVersion: k3d.io/v1alpha4
kind: Simple
name: supertest
agents: 8
agents: 8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: k3d.io/v1alpha3
apiVersion: k3d.io/v1alpha4
kind: Simple
name: 1234
servers: 1
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/test_assets/config_test_unknown.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
apiVersion: k3d.io/v1alpha3
apiVersion: k3d.io/v1alpha4
kind: Unknown
foo: bar
foo: bar
2 changes: 1 addition & 1 deletion pkg/config/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/docker/go-connections/nat"
cliutil "github.com/rancher/k3d/v5/cmd/util" // TODO: move parseapiport to pkg
"github.com/rancher/k3d/v5/pkg/client"
conf "github.com/rancher/k3d/v5/pkg/config/v1alpha3"
conf "github.com/rancher/k3d/v5/pkg/config/v1alpha4"
"github.com/rancher/k3d/v5/pkg/runtimes"
k3d "github.com/rancher/k3d/v5/pkg/types"
"github.com/rancher/k3d/v5/pkg/types/k3s"
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"context"
"testing"

conf "github.com/rancher/k3d/v5/pkg/config/v1alpha3"
conf "github.com/rancher/k3d/v5/pkg/config/v1alpha4"
"github.com/rancher/k3d/v5/pkg/runtimes"
"github.com/spf13/viper"
)
Expand Down
18 changes: 0 additions & 18 deletions pkg/config/v1alpha3/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -304,24 +304,6 @@
},
"additionalProperties": false
}
},
"hostAliases": {
"type": "array",
"description": "Additional IP to multiple hostnames mappings",
"items": {
"type": "object",
"properties": {
"ip": {
"type": "string"
},
"hostnames": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
},
"additionalProperties": false,
Expand Down
3 changes: 0 additions & 3 deletions pkg/config/v1alpha3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"time"

config "github.com/rancher/k3d/v5/pkg/config/types"
"github.com/rancher/k3d/v5/pkg/types"
k3d "github.com/rancher/k3d/v5/pkg/types"
"github.com/rancher/k3d/v5/version"
)
Expand Down Expand Up @@ -105,7 +104,6 @@ type SimpleConfigOptionsRuntime struct {
GPURequest string `mapstructure:"gpuRequest" yaml:"gpuRequest,omitempty" json:"gpuRequest,omitempty"`
ServersMemory string `mapstructure:"serversMemory" yaml:"serversMemory,omitempty" json:"serversMemory,omitempty"`
AgentsMemory string `mapstructure:"agentsMemory" yaml:"agentsMemory,omitempty" json:"agentsMemory,omitempty"`
HostPidMode bool `mapstructure:"hostPidMode" yaml:"hostPidMode,omitempty" json:"hostPidMode,omitempty"`
Labels []LabelWithNodeFilters `mapstructure:"labels" yaml:"labels,omitempty" json:"labels,omitempty"`
}

Expand Down Expand Up @@ -156,7 +154,6 @@ type SimpleConfig struct {
Options SimpleConfigOptions `mapstructure:"options" yaml:"options,omitempty" json:"options,omitempty"`
Env []EnvVarWithNodeFilters `mapstructure:"env" yaml:"env,omitempty" json:"env,omitempty"`
Registries SimpleConfigRegistries `mapstructure:"registries" yaml:"registries,omitempty" json:"registries,omitempty"`
HostAliases []types.HostAlias `mapstructure:"hostAliases" yaml:"hostAliases,omitempty" json:"hostAliases,omitempty"`
}

type SimpleConfigIntermediateV1alpha2 struct {
Expand Down
Loading

0 comments on commit 08bf145

Please sign in to comment.