Skip to content

Commit

Permalink
fixed driver / mountstring os differences
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-the-programmer committed Jan 6, 2023
1 parent 41de52f commit 33807f5
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 9 deletions.
4 changes: 2 additions & 2 deletions docs/resources/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ resource "kubernetes_deployment" "deployment" {
- `docker_env` (List of String) Environment variables to pass to the Docker daemon. (format: key=value)
- `docker_opt` (List of String) Specify arbitrary flags to pass to the Docker daemon. (format: key=value)
- `download_only` (Boolean) If true, only download and cache files for later use - don't install or start anything.
- `driver` (String) Driver is one of: virtualbox, parallels, vmwarefusion, hyperkit, vmware, qemu2 (experimental), docker, podman (experimental), ssh (defaults to auto-detect)
- `driver` (String) Driver is one of the following - Windows: (hyperv, docker, virtualbox, vmware, qemu2, ssh) - OSX: (virtualbox, parallels, vmwarefusion, hyperkit, vmware, qemu2, docker, podman, ssh) - Linux: (docker, kvm2, virtualbox, qemu2, none, podman, ssh)
- `dry_run` (Boolean) dry-run mode. Validates configuration, but does not mutate system state
- `embed_certs` (Boolean) if true, will embed the certs in kubeconfig.
- `enable_default_cni` (Boolean) DEPRECATED: Replaced by --cni=bridge
Expand All @@ -141,7 +141,7 @@ resource "kubernetes_deployment" "deployment" {
- `install_addons` (Boolean) If set, install addons. Defaults to true.
- `interactive` (Boolean) Allow user prompts for more information
- `keep_context` (Boolean) This will keep the existing kubectl context and will create a minikube context.
- `kubernetes_version` (String) The Kubernetes version that the minikube VM will use (ex: v1.2.3, 'stable' for v1.25.2, 'latest' for v1.25.2). Defaults to 'stable'.
- `kubernetes_version` (String) The Kubernetes version that the minikube VM will use (ex: v1.2.3, 'stable' for v1.25.3, 'latest' for v1.25.3). Defaults to 'stable'.
- `kvm_gpu` (Boolean) Enable experimental NVIDIA GPU support in minikube
- `kvm_hidden` (Boolean) Hide the hypervisor signature from the guest in minikube (kvm2 driver only)
- `kvm_network` (String) The KVM default network name. (kvm2 driver only)
Expand Down
40 changes: 39 additions & 1 deletion minikube/generator/schema_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type SchemaOverride struct {
Description string
Default string
Type SchemaType
DefaultFunc string
}

var schemaOverrides map[string]SchemaOverride = map[string]SchemaOverride{
Expand All @@ -63,6 +64,29 @@ var schemaOverrides map[string]SchemaOverride = map[string]SchemaOverride{
Description: "Amount of CPUs to allocate to Kubernetes",
Type: Int,
},
// Customize the description to be the fullset of drivers
"driver": {
Default: "docker",
Description: "Driver is one of the following - Windows: (hyperv, docker, virtualbox, vmware, qemu2, ssh) - OSX: (virtualbox, parallels, vmwarefusion, hyperkit, vmware, qemu2, docker, podman, ssh) - Linux: (docker, kvm2, virtualbox, qemu2, none, podman, ssh)",
Type: String,
},
// Default schema to unix file paths first and let the provider translate them during runtime
"mount_string": {
Description: "The argument to pass the minikube mount command on start.",
Type: String,
DefaultFunc: `func() (any, error) {
if runtime.GOOS == "windows" {
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
return home + ":" + "/minikube-host", nil
} else if runtime.GOOS == "darwin" {
return "/Users:/minikube-host", nil
}
return "/home:/minikube-host", nil
}`,
},
}

func run(ctx context.Context, args ...string) (string, error) {
Expand All @@ -78,6 +102,7 @@ func run(ctx context.Context, args ...string) (string, error) {
type SchemaEntry struct {
Parameter string
Default string
DefaultFunc string
Description string
Type SchemaType
ArrayType SchemaType
Expand Down Expand Up @@ -161,12 +186,14 @@ func loadParameter(line string) SchemaEntry {
schemaEntry.Parameter = strings.TrimPrefix(seg[0], "--")
schemaEntry.Parameter = strings.Replace(schemaEntry.Parameter, "-", "_", -1)
schemaEntry.Default = strings.TrimSuffix(seg[1], ":")
schemaEntry.Default = strings.ReplaceAll(schemaEntry.Default, "\\", "\\\\")
schemaEntry.Type = getSchemaType(schemaEntry.Default)

// Apply explicit overrides
val, ok := schemaOverrides[schemaEntry.Parameter]
if ok {
schemaEntry.Default = val.Default
schemaEntry.DefaultFunc = val.DefaultFunc
schemaEntry.Type = val.Type
}

Expand All @@ -185,13 +212,15 @@ func addEntry(entries []SchemaEntry, currentEntry SchemaEntry) ([]SchemaEntry, e
Default: fmt.Sprintf("\"%s\"", currentEntry.Default),
Type: currentEntry.Type,
Description: currentEntry.Description,
DefaultFunc: currentEntry.DefaultFunc,
})
case Bool:
entries = append(entries, SchemaEntry{
Parameter: currentEntry.Parameter,
Default: currentEntry.Default,
Type: currentEntry.Type,
Description: currentEntry.Description,
DefaultFunc: currentEntry.DefaultFunc,
})
case Int:
val, err := strconv.Atoi(currentEntry.Default)
Expand All @@ -209,13 +238,15 @@ func addEntry(entries []SchemaEntry, currentEntry SchemaEntry) ([]SchemaEntry, e
Default: strconv.Itoa(val),
Type: currentEntry.Type,
Description: currentEntry.Description,
DefaultFunc: currentEntry.DefaultFunc,
})
case Array:
entries = append(entries, SchemaEntry{
Parameter: currentEntry.Parameter,
Type: Array,
ArrayType: String,
Description: currentEntry.Description,
DefaultFunc: currentEntry.DefaultFunc,
})
}

Expand All @@ -232,7 +263,11 @@ func constructSchema(entries []SchemaEntry) string {
// THIS FILE IS GENERATED DO NOT EDIT
package minikube
import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
import (
"runtime"
"os"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
var (
clusterSchema = map[string]*schema.Schema{
Expand Down Expand Up @@ -300,6 +335,9 @@ var (
Type: %s,
},
`, "schema.Type"+entry.ArrayType)
} else if entry.DefaultFunc != "" {
extraParams += fmt.Sprintf(`
DefaultFunc: %s,`, entry.DefaultFunc)
} else {
extraParams += fmt.Sprintf(`
Default: %s,`, entry.Default)
Expand Down
21 changes: 20 additions & 1 deletion minikube/generator/schema_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ const header = `//go:generate go run ../generate/main.go -target $GOFILE
// THIS FILE IS GENERATED DO NOT EDIT
package minikube
import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
import (
"runtime"
"os"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
var (
clusterSchema = map[string]*schema.Schema{
Expand Down Expand Up @@ -343,6 +347,21 @@ func GetClusterSchema() map[string]*schema.Schema {
`, schema)
}

func TestDefaultFuncOverride(t *testing.T) {
ctrl := gomock.NewController(t)
mockMinikube := NewMockMinikubeBinary(ctrl)
mockMinikube.EXPECT().GetVersion(gomock.Any()).Return("Version 999", nil)
mockMinikube.EXPECT().GetStartHelpText(gomock.Any()).Return(`
--mount-string='':
I am a great test description
`, nil)
builder := NewSchemaBuilder("fake.go", mockMinikube)
schema, err := builder.Build()
assert.NoError(t, err)
assert.Contains(t, schema, "DefaultFunc: func() (any, error) {")
}

func TestPropertyFailure(t *testing.T) {
ctrl := gomock.NewController(t)
mockMinikube := NewMockMinikubeBinary(ctrl)
Expand Down
3 changes: 2 additions & 1 deletion minikube/resource_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func mockSuccess(t *testing.T, clusterName string) schema.ConfigureContextFunc {
_ = os.WriteFile("test_output/key", d1, 0644)

clusterSchema := ResourceCluster().Schema
mountString, _ := clusterSchema["mount_string"].DefaultFunc()

k8sVersion := "v1.25.3"
kubernetesConfig := config.KubernetesConfig{
Expand Down Expand Up @@ -180,7 +181,7 @@ func mockSuccess(t *testing.T, clusterName string) schema.ConfigureContextFunc {
ExtraDisks: clusterSchema["extra_disks"].Default.(int),
CertExpiration: time.Duration(clusterSchema["cert_expiration"].Default.(int)) * time.Minute,
Mount: clusterSchema["hyperv_use_external_switch"].Default.(bool),
MountString: clusterSchema["mount_string"].Default.(string),
MountString: mountString.(string),
Mount9PVersion: "9p2000.L",
MountGID: "docker",
MountIP: "",
Expand Down
23 changes: 19 additions & 4 deletions minikube/schema_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
// THIS FILE IS GENERATED DO NOT EDIT
package minikube

import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
import (
"runtime"
"os"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var (
clusterSchema = map[string]*schema.Schema{
Expand Down Expand Up @@ -303,12 +307,12 @@ var (

"driver": {
Type: schema.TypeString,
Description: "Driver is one of: virtualbox, parallels, vmwarefusion, hyperkit, vmware, qemu2 (experimental), docker, podman (experimental), ssh (defaults to auto-detect)",
Description: "Driver is one of the following - Windows: (hyperv, docker, virtualbox, vmware, qemu2, ssh) - OSX: (virtualbox, parallels, vmwarefusion, hyperkit, vmware, qemu2, docker, podman, ssh) - Linux: (docker, kvm2, virtualbox, qemu2, none, podman, ssh)",

Optional: true,
ForceNew: true,

Default: "",
Default: "docker",
},

"dry_run": {
Expand Down Expand Up @@ -707,7 +711,18 @@ var (
Optional: true,
ForceNew: true,

Default: "/Users:/minikube-host",
DefaultFunc: func() (any, error) {
if runtime.GOOS == "windows" {
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
return home + ":" + "/minikube-host", nil
} else if runtime.GOOS == "darwin" {
return "/Users:/minikube-host", nil
}
return "/home:/minikube-host", nil
},
},

"mount_type": {
Expand Down

0 comments on commit 33807f5

Please sign in to comment.