Skip to content

Commit 08eafb4

Browse files
parse memory from a potentially humanized string to mb
1 parent 00a73b9 commit 08eafb4

11 files changed

+193
-47
lines changed

docs/resources/cluster.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ resource "kubernetes_deployment" "deployment" {
158158
- `kvm_numa_count` (Number) Simulate numa node count in minikube, supported numa node count range is 1-8 (kvm2 driver only)
159159
- `kvm_qemu_uri` (String) The KVM QEMU connection URI. (kvm2 driver only)
160160
- `listen_address` (String) IP Address to use to expose ports (docker and podman driver only)
161-
- `memory` (String) Amount of RAM to allocate to Kubernetes (format: <number>[<unit>], where unit = b, k, m or g)
161+
- `memory` (String) Amount of RAM to allocate to Kubernetes (format: <number>[<unit>(case-insensitive)], where unit = b, k, kb, m, mb, g or gb)
162162
- `mount` (Boolean) This will start the mount daemon and automatically mount files into minikube.
163163
- `mount_9p_version` (String) Specify the 9p version that the mount should use
164164
- `mount_gid` (String) Default group id used for the mount

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.20
55
require (
66
github.com/docker/machine v0.16.2
77
github.com/golang/mock v1.6.0
8+
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
89
github.com/hashicorp/terraform-plugin-docs v0.16.0
910
github.com/hashicorp/terraform-plugin-log v0.9.0
1011
github.com/hashicorp/terraform-plugin-sdk/v2 v2.31.0
@@ -99,7 +100,6 @@ require (
99100
github.com/hashicorp/errwrap v1.1.0 // indirect
100101
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
101102
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
102-
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
103103
github.com/hashicorp/go-getter v1.7.3 // indirect
104104
github.com/hashicorp/go-hclog v1.5.0 // indirect
105105
github.com/hashicorp/go-multierror v1.1.1 // indirect

minikube/generator/schema_builder.go

+62-33
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ var computedFields []string = []string{
4242
}
4343

4444
type SchemaOverride struct {
45-
Description string
46-
Default string
47-
Type SchemaType
48-
DefaultFunc string
45+
Description string
46+
Default string
47+
Type SchemaType
48+
DefaultFunc string
49+
StateFunc string
50+
ValidateDiagFunc string
4951
}
5052

5153
var updateFields = []string{
@@ -54,9 +56,11 @@ var updateFields = []string{
5456

5557
var schemaOverrides map[string]SchemaOverride = map[string]SchemaOverride{
5658
"memory": {
57-
Default: "4000mb",
58-
Description: "Amount of RAM to allocate to Kubernetes (format: <number>[<unit>], where unit = b, k, m or g)",
59-
Type: String,
59+
Default: "4g",
60+
Description: "Amount of RAM to allocate to Kubernetes (format: <number>[<unit>(case-insensitive)], where unit = b, k, kb, m, mb, g or gb)",
61+
Type: String,
62+
StateFunc: "state_utils.MemoryConverter()",
63+
ValidateDiagFunc: "state_utils.MemoryValidator()",
6064
},
6165
"cpus": {
6266
Default: "2",
@@ -99,12 +103,14 @@ func run(ctx context.Context, args ...string) (string, error) {
99103
}
100104

101105
type SchemaEntry struct {
102-
Parameter string
103-
Default string
104-
DefaultFunc string
105-
Description string
106-
Type SchemaType
107-
ArrayType SchemaType
106+
Parameter string
107+
Default string
108+
DefaultFunc string
109+
StateFunc string
110+
ValidateDiagFunc string
111+
Description string
112+
Type SchemaType
113+
ArrayType SchemaType
108114
}
109115

110116
type SchemaBuilder struct {
@@ -194,6 +200,8 @@ func loadParameter(line string) SchemaEntry {
194200
schemaEntry.Default = val.Default
195201
schemaEntry.DefaultFunc = val.DefaultFunc
196202
schemaEntry.Type = val.Type
203+
schemaEntry.StateFunc = val.StateFunc
204+
schemaEntry.ValidateDiagFunc = val.ValidateDiagFunc
197205
}
198206

199207
if schemaEntry.Type == String {
@@ -207,19 +215,23 @@ func addEntry(entries []SchemaEntry, currentEntry SchemaEntry) ([]SchemaEntry, e
207215
switch currentEntry.Type {
208216
case String:
209217
entries = append(entries, SchemaEntry{
210-
Parameter: currentEntry.Parameter,
211-
Default: fmt.Sprintf("\"%s\"", currentEntry.Default),
212-
Type: currentEntry.Type,
213-
Description: currentEntry.Description,
214-
DefaultFunc: currentEntry.DefaultFunc,
218+
Parameter: currentEntry.Parameter,
219+
Default: fmt.Sprintf("\"%s\"", currentEntry.Default),
220+
Type: currentEntry.Type,
221+
Description: currentEntry.Description,
222+
DefaultFunc: currentEntry.DefaultFunc,
223+
StateFunc: currentEntry.StateFunc,
224+
ValidateDiagFunc: currentEntry.ValidateDiagFunc,
215225
})
216226
case Bool:
217227
entries = append(entries, SchemaEntry{
218-
Parameter: currentEntry.Parameter,
219-
Default: currentEntry.Default,
220-
Type: currentEntry.Type,
221-
Description: currentEntry.Description,
222-
DefaultFunc: currentEntry.DefaultFunc,
228+
Parameter: currentEntry.Parameter,
229+
Default: currentEntry.Default,
230+
Type: currentEntry.Type,
231+
Description: currentEntry.Description,
232+
DefaultFunc: currentEntry.DefaultFunc,
233+
StateFunc: currentEntry.StateFunc,
234+
ValidateDiagFunc: currentEntry.ValidateDiagFunc,
223235
})
224236
case Int:
225237
val, err := strconv.Atoi(currentEntry.Default)
@@ -233,19 +245,23 @@ func addEntry(entries []SchemaEntry, currentEntry SchemaEntry) ([]SchemaEntry, e
233245
currentEntry.Description = fmt.Sprintf("%s (Configured in minutes)", currentEntry.Description)
234246
}
235247
entries = append(entries, SchemaEntry{
236-
Parameter: currentEntry.Parameter,
237-
Default: strconv.Itoa(val),
238-
Type: currentEntry.Type,
239-
Description: currentEntry.Description,
240-
DefaultFunc: currentEntry.DefaultFunc,
248+
Parameter: currentEntry.Parameter,
249+
Default: strconv.Itoa(val),
250+
Type: currentEntry.Type,
251+
Description: currentEntry.Description,
252+
DefaultFunc: currentEntry.DefaultFunc,
253+
StateFunc: currentEntry.StateFunc,
254+
ValidateDiagFunc: currentEntry.ValidateDiagFunc,
241255
})
242256
case Array:
243257
entries = append(entries, SchemaEntry{
244-
Parameter: currentEntry.Parameter,
245-
Type: Array,
246-
ArrayType: String,
247-
Description: currentEntry.Description,
248-
DefaultFunc: currentEntry.DefaultFunc,
258+
Parameter: currentEntry.Parameter,
259+
Type: Array,
260+
ArrayType: String,
261+
Description: currentEntry.Description,
262+
DefaultFunc: currentEntry.DefaultFunc,
263+
StateFunc: currentEntry.StateFunc,
264+
ValidateDiagFunc: currentEntry.ValidateDiagFunc,
249265
})
250266
}
251267

@@ -265,6 +281,9 @@ package minikube
265281
import (
266282
"runtime"
267283
"os"
284+
285+
"github.com/scott-the-programmer/terraform-provider-minikube/minikube/state_utils"
286+
268287
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
269288
)
270289
@@ -348,6 +367,16 @@ var (
348367
Default: %s,`, entry.Default)
349368
}
350369

370+
if entry.StateFunc != "" {
371+
extraParams += fmt.Sprintf(`
372+
StateFunc: %s,`, entry.StateFunc)
373+
}
374+
375+
if entry.ValidateDiagFunc != "" {
376+
extraParams += fmt.Sprintf(`
377+
ValidateDiagFunc: %s,`, entry.ValidateDiagFunc)
378+
}
379+
351380
body = body + fmt.Sprintf(`
352381
"%s": {
353382
Type: %s,

minikube/generator/schema_builder_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ package minikube
1515
import (
1616
"runtime"
1717
"os"
18+
19+
"github.com/scott-the-programmer/terraform-provider-minikube/minikube/state_utils"
20+
1821
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1922
)
2023
@@ -333,12 +336,14 @@ func TestOverride(t *testing.T) {
333336
assert.Equal(t, header+`
334337
"memory": {
335338
Type: schema.TypeString,
336-
Description: "Amount of RAM to allocate to Kubernetes (format: <number>[<unit>], where unit = b, k, m or g)",
339+
Description: "Amount of RAM to allocate to Kubernetes (format: <number>[<unit>(case-insensitive)], where unit = b, k, kb, m, mb, g or gb)",
337340
338341
Optional: true,
339342
ForceNew: true,
340343
341-
Default: "4000mb",
344+
Default: "4g",
345+
StateFunc: state_utils.MemoryConverter(),
346+
ValidateDiagFunc: state_utils.MemoryValidator(),
342347
},
343348
344349
}

minikube/lib/mock_minikube_client.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

minikube/lib/mock_minikube_cluster.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

minikube/lib/mock_minikube_downloader.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

minikube/resource_cluster_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func getBaseMockClient(ctrl *gomock.Controller, clusterName string) *lib.MockClu
293293
MinikubeISO: defaultIso,
294294
KicBaseImage: clusterSchema["base_image"].Default.(string),
295295
Network: clusterSchema["network"].Default.(string),
296-
Memory: 4000,
296+
Memory: 4096,
297297
CPUs: 2,
298298
DiskSize: 20000,
299299
Driver: "some_driver",
@@ -429,7 +429,7 @@ func testAcceptanceClusterConfig(driver string, clusterName string) string {
429429
driver = "%s"
430430
cluster_name = "%s"
431431
cpus = 2
432-
memory = "6000mb"
432+
memory = "6GiB"
433433
434434
addons = [
435435
"dashboard",
@@ -446,7 +446,7 @@ func testAcceptanceClusterConfig_Update(driver string, clusterName string) strin
446446
driver = "%s"
447447
cluster_name = "%s"
448448
cpus = 2
449-
memory = "6000mb"
449+
memory = "6GiB"
450450
451451
addons = [
452452
"dashboard",
@@ -464,7 +464,7 @@ func testAcceptanceClusterConfig_StorageProvisioner(driver string, clusterName s
464464
driver = "%s"
465465
cluster_name = "%s"
466466
cpus = 2
467-
memory = "6000mb"
467+
memory = "6000GiB"
468468
469469
addons = [
470470
"dashboard",
@@ -482,7 +482,7 @@ func testAcceptanceClusterConfig_OutOfOrderAddons(driver string, clusterName str
482482
driver = "%s"
483483
cluster_name = "%s"
484484
cpus = 2
485-
memory = "6000mb"
485+
memory = "6000GiB"
486486
487487
addons = [
488488
"storage-provisioner",

minikube/schema_cluster.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ package minikube
55
import (
66
"runtime"
77
"os"
8+
9+
"github.com/scott-the-programmer/terraform-provider-minikube/minikube/state_utils"
10+
811
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
912
)
1013

@@ -648,12 +651,14 @@ var (
648651

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

653656
Optional: true,
654657
ForceNew: true,
655658

656-
Default: "4000mb",
659+
Default: "4g",
660+
StateFunc: state_utils.MemoryConverter(),
661+
ValidateDiagFunc: state_utils.MemoryValidator(),
657662
},
658663

659664
"mount": {

minikube/state_utils/memory.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package state_utils
2+
3+
import (
4+
"errors"
5+
"strconv"
6+
7+
"github.com/hashicorp/go-cty/cty"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
11+
pkgutil "k8s.io/minikube/pkg/util"
12+
)
13+
14+
func MemoryConverter() schema.SchemaStateFunc {
15+
return func(val interface{}) string {
16+
memory, ok := val.(string)
17+
if !ok {
18+
panic(errors.New("memory flag is not a string"))
19+
}
20+
memoryMb, err := pkgutil.CalculateSizeInMB(memory)
21+
if err != nil {
22+
panic(errors.New("invalid memory value"))
23+
}
24+
25+
return strconv.Itoa(memoryMb) + "mb"
26+
27+
}
28+
29+
}
30+
31+
func MemoryValidator() schema.SchemaValidateDiagFunc {
32+
return schema.SchemaValidateDiagFunc(func(val interface{}, path cty.Path) diag.Diagnostics {
33+
memory, ok := val.(string)
34+
if !ok {
35+
diag := diag.FromErr(errors.New("memory flag is not a string"))
36+
return diag
37+
}
38+
_, err := pkgutil.CalculateSizeInMB(memory)
39+
if err != nil {
40+
diag := diag.FromErr(errors.New("invalid memory value"))
41+
return diag
42+
}
43+
return nil
44+
})
45+
}

0 commit comments

Comments
 (0)