Skip to content

Commit

Permalink
parse memory from a potentially humanized string to mb
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-the-programmer committed Dec 23, 2023
1 parent 00a73b9 commit 08eafb4
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 47 deletions.
2 changes: 1 addition & 1 deletion docs/resources/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ resource "kubernetes_deployment" "deployment" {
- `kvm_numa_count` (Number) Simulate numa node count in minikube, supported numa node count range is 1-8 (kvm2 driver only)
- `kvm_qemu_uri` (String) The KVM QEMU connection URI. (kvm2 driver only)
- `listen_address` (String) IP Address to use to expose ports (docker and podman driver only)
- `memory` (String) Amount of RAM to allocate to Kubernetes (format: <number>[<unit>], where unit = b, k, m or g)
- `memory` (String) Amount of RAM to allocate to Kubernetes (format: <number>[<unit>(case-insensitive)], where unit = b, k, kb, m, mb, g or gb)
- `mount` (Boolean) This will start the mount daemon and automatically mount files into minikube.
- `mount_9p_version` (String) Specify the 9p version that the mount should use
- `mount_gid` (String) Default group id used for the mount
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/docker/machine v0.16.2
github.com/golang/mock v1.6.0
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-docs v0.16.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.31.0
Expand Down Expand Up @@ -99,7 +100,6 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
github.com/hashicorp/go-getter v1.7.3 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
Expand Down
95 changes: 62 additions & 33 deletions minikube/generator/schema_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ var computedFields []string = []string{
}

type SchemaOverride struct {
Description string
Default string
Type SchemaType
DefaultFunc string
Description string
Default string
Type SchemaType
DefaultFunc string
StateFunc string
ValidateDiagFunc string
}

var updateFields = []string{
Expand All @@ -54,9 +56,11 @@ var updateFields = []string{

var schemaOverrides map[string]SchemaOverride = map[string]SchemaOverride{
"memory": {
Default: "4000mb",
Description: "Amount of RAM to allocate to Kubernetes (format: <number>[<unit>], where unit = b, k, m or g)",
Type: String,
Default: "4g",
Description: "Amount of RAM to allocate to Kubernetes (format: <number>[<unit>(case-insensitive)], where unit = b, k, kb, m, mb, g or gb)",
Type: String,
StateFunc: "state_utils.MemoryConverter()",
ValidateDiagFunc: "state_utils.MemoryValidator()",
},
"cpus": {
Default: "2",
Expand Down Expand Up @@ -99,12 +103,14 @@ func run(ctx context.Context, args ...string) (string, error) {
}

type SchemaEntry struct {
Parameter string
Default string
DefaultFunc string
Description string
Type SchemaType
ArrayType SchemaType
Parameter string
Default string
DefaultFunc string
StateFunc string
ValidateDiagFunc string
Description string
Type SchemaType
ArrayType SchemaType
}

type SchemaBuilder struct {
Expand Down Expand Up @@ -194,6 +200,8 @@ func loadParameter(line string) SchemaEntry {
schemaEntry.Default = val.Default
schemaEntry.DefaultFunc = val.DefaultFunc
schemaEntry.Type = val.Type
schemaEntry.StateFunc = val.StateFunc
schemaEntry.ValidateDiagFunc = val.ValidateDiagFunc
}

if schemaEntry.Type == String {
Expand All @@ -207,19 +215,23 @@ func addEntry(entries []SchemaEntry, currentEntry SchemaEntry) ([]SchemaEntry, e
switch currentEntry.Type {
case String:
entries = append(entries, SchemaEntry{
Parameter: currentEntry.Parameter,
Default: fmt.Sprintf("\"%s\"", currentEntry.Default),
Type: currentEntry.Type,
Description: currentEntry.Description,
DefaultFunc: currentEntry.DefaultFunc,
Parameter: currentEntry.Parameter,
Default: fmt.Sprintf("\"%s\"", currentEntry.Default),
Type: currentEntry.Type,
Description: currentEntry.Description,
DefaultFunc: currentEntry.DefaultFunc,
StateFunc: currentEntry.StateFunc,
ValidateDiagFunc: currentEntry.ValidateDiagFunc,
})
case Bool:
entries = append(entries, SchemaEntry{
Parameter: currentEntry.Parameter,
Default: currentEntry.Default,
Type: currentEntry.Type,
Description: currentEntry.Description,
DefaultFunc: currentEntry.DefaultFunc,
Parameter: currentEntry.Parameter,
Default: currentEntry.Default,
Type: currentEntry.Type,
Description: currentEntry.Description,
DefaultFunc: currentEntry.DefaultFunc,
StateFunc: currentEntry.StateFunc,
ValidateDiagFunc: currentEntry.ValidateDiagFunc,
})
case Int:
val, err := strconv.Atoi(currentEntry.Default)
Expand All @@ -233,19 +245,23 @@ func addEntry(entries []SchemaEntry, currentEntry SchemaEntry) ([]SchemaEntry, e
currentEntry.Description = fmt.Sprintf("%s (Configured in minutes)", currentEntry.Description)
}
entries = append(entries, SchemaEntry{
Parameter: currentEntry.Parameter,
Default: strconv.Itoa(val),
Type: currentEntry.Type,
Description: currentEntry.Description,
DefaultFunc: currentEntry.DefaultFunc,
Parameter: currentEntry.Parameter,
Default: strconv.Itoa(val),
Type: currentEntry.Type,
Description: currentEntry.Description,
DefaultFunc: currentEntry.DefaultFunc,
StateFunc: currentEntry.StateFunc,
ValidateDiagFunc: currentEntry.ValidateDiagFunc,
})
case Array:
entries = append(entries, SchemaEntry{
Parameter: currentEntry.Parameter,
Type: Array,
ArrayType: String,
Description: currentEntry.Description,
DefaultFunc: currentEntry.DefaultFunc,
Parameter: currentEntry.Parameter,
Type: Array,
ArrayType: String,
Description: currentEntry.Description,
DefaultFunc: currentEntry.DefaultFunc,
StateFunc: currentEntry.StateFunc,
ValidateDiagFunc: currentEntry.ValidateDiagFunc,
})
}

Expand All @@ -265,6 +281,9 @@ package minikube
import (
"runtime"
"os"
"github.com/scott-the-programmer/terraform-provider-minikube/minikube/state_utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -348,6 +367,16 @@ var (
Default: %s,`, entry.Default)
}

if entry.StateFunc != "" {
extraParams += fmt.Sprintf(`
StateFunc: %s,`, entry.StateFunc)
}

if entry.ValidateDiagFunc != "" {
extraParams += fmt.Sprintf(`
ValidateDiagFunc: %s,`, entry.ValidateDiagFunc)
}

body = body + fmt.Sprintf(`
"%s": {
Type: %s,
Expand Down
9 changes: 7 additions & 2 deletions minikube/generator/schema_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ package minikube
import (
"runtime"
"os"
"github.com/scott-the-programmer/terraform-provider-minikube/minikube/state_utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -333,12 +336,14 @@ func TestOverride(t *testing.T) {
assert.Equal(t, header+`
"memory": {
Type: schema.TypeString,
Description: "Amount of RAM to allocate to Kubernetes (format: <number>[<unit>], where unit = b, k, m or g)",
Description: "Amount of RAM to allocate to Kubernetes (format: <number>[<unit>(case-insensitive)], where unit = b, k, kb, m, mb, g or gb)",
Optional: true,
ForceNew: true,
Default: "4000mb",
Default: "4g",
StateFunc: state_utils.MemoryConverter(),
ValidateDiagFunc: state_utils.MemoryValidator(),
},
}
Expand Down
2 changes: 1 addition & 1 deletion minikube/lib/mock_minikube_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion minikube/lib/mock_minikube_cluster.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion minikube/lib/mock_minikube_downloader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions minikube/resource_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func getBaseMockClient(ctrl *gomock.Controller, clusterName string) *lib.MockClu
MinikubeISO: defaultIso,
KicBaseImage: clusterSchema["base_image"].Default.(string),
Network: clusterSchema["network"].Default.(string),
Memory: 4000,
Memory: 4096,
CPUs: 2,
DiskSize: 20000,
Driver: "some_driver",
Expand Down Expand Up @@ -429,7 +429,7 @@ func testAcceptanceClusterConfig(driver string, clusterName string) string {
driver = "%s"
cluster_name = "%s"
cpus = 2
memory = "6000mb"
memory = "6GiB"
addons = [
"dashboard",
Expand All @@ -446,7 +446,7 @@ func testAcceptanceClusterConfig_Update(driver string, clusterName string) strin
driver = "%s"
cluster_name = "%s"
cpus = 2
memory = "6000mb"
memory = "6GiB"
addons = [
"dashboard",
Expand All @@ -464,7 +464,7 @@ func testAcceptanceClusterConfig_StorageProvisioner(driver string, clusterName s
driver = "%s"
cluster_name = "%s"
cpus = 2
memory = "6000mb"
memory = "6000GiB"
addons = [
"dashboard",
Expand All @@ -482,7 +482,7 @@ func testAcceptanceClusterConfig_OutOfOrderAddons(driver string, clusterName str
driver = "%s"
cluster_name = "%s"
cpus = 2
memory = "6000mb"
memory = "6000GiB"
addons = [
"storage-provisioner",
Expand Down
9 changes: 7 additions & 2 deletions minikube/schema_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ package minikube
import (
"runtime"
"os"

"github.com/scott-the-programmer/terraform-provider-minikube/minikube/state_utils"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -648,12 +651,14 @@ var (

"memory": {
Type: schema.TypeString,
Description: "Amount of RAM to allocate to Kubernetes (format: <number>[<unit>], where unit = b, k, m or g)",
Description: "Amount of RAM to allocate to Kubernetes (format: <number>[<unit>(case-insensitive)], where unit = b, k, kb, m, mb, g or gb)",

Optional: true,
ForceNew: true,

Default: "4000mb",
Default: "4g",
StateFunc: state_utils.MemoryConverter(),
ValidateDiagFunc: state_utils.MemoryValidator(),
},

"mount": {
Expand Down
45 changes: 45 additions & 0 deletions minikube/state_utils/memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package state_utils

import (
"errors"
"strconv"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

pkgutil "k8s.io/minikube/pkg/util"
)

func MemoryConverter() schema.SchemaStateFunc {
return func(val interface{}) string {
memory, ok := val.(string)
if !ok {
panic(errors.New("memory flag is not a string"))
}
memoryMb, err := pkgutil.CalculateSizeInMB(memory)
if err != nil {
panic(errors.New("invalid memory value"))
}

return strconv.Itoa(memoryMb) + "mb"

}

}

func MemoryValidator() schema.SchemaValidateDiagFunc {
return schema.SchemaValidateDiagFunc(func(val interface{}, path cty.Path) diag.Diagnostics {
memory, ok := val.(string)
if !ok {
diag := diag.FromErr(errors.New("memory flag is not a string"))
return diag
}
_, err := pkgutil.CalculateSizeInMB(memory)
if err != nil {
diag := diag.FromErr(errors.New("invalid memory value"))
return diag
}
return nil
})
}
Loading

0 comments on commit 08eafb4

Please sign in to comment.