File tree Expand file tree Collapse file tree 3 files changed +92
-3
lines changed Expand file tree Collapse file tree 3 files changed +92
-3
lines changed Original file line number Diff line number Diff line change @@ -4,13 +4,17 @@ import (
44 "k8s.io/minikube/pkg/minikube/machine"
55)
66
7-
87var NoLimit = "no-limit"
98var Max = "max"
109
1110// MemoryInfo holds system and container memory information
1211type MemoryInfo struct {
13- SystemMemory int
12+ SystemMemory int
13+ }
14+
15+ // CPUInfo holds system and container CPU information
16+ type CPUInfo struct {
17+ CPUs int
1418}
1519
1620// GetMemoryLimits returns the amount of memory allocated to the system and container
@@ -24,9 +28,18 @@ func GetMemoryLimit() (*MemoryInfo, error) {
2428
2529 // Subtract 1gb for overhead
2630 memInfo := & MemoryInfo {
27- SystemMemory : int (info .Memory ) - 1024 ,
31+ SystemMemory : int (info .Memory ) - 1024 ,
2832 }
2933
3034 return memInfo , nil
3135}
3236
37+ func GetCPULimit () (int , error ) {
38+ info , _ , _ , cpuErr := machine .LocalHostInfo ()
39+
40+ if cpuErr != nil {
41+ return 0 , cpuErr
42+ }
43+
44+ return int (info .CPUs ), nil
45+ }
Original file line number Diff line number Diff line change 11package state_utils
22
33import (
4+ "strconv"
5+
46 "github.com/scott-the-programmer/terraform-provider-minikube/minikube/lib"
57 pkgutil "k8s.io/minikube/pkg/util"
68)
@@ -36,3 +38,23 @@ func GetMemory(memoryStr string) (int, error) {
3638
3739 return memoryMb , err
3840}
41+
42+ func GetCPU (cpuStr string ) (int , error ) {
43+ var cpu int
44+ var err error
45+ if cpuStr == lib .Max {
46+ cpu , err = lib .GetCPULimit ()
47+ if err != nil {
48+ return 0 , err
49+ }
50+ } else if cpuStr == lib .NoLimit {
51+ cpu = 0
52+ } else {
53+ cpu , err = strconv .Atoi (cpuStr )
54+ if err != nil {
55+ return 0 , err
56+ }
57+ }
58+
59+ return cpu , nil
60+ }
Original file line number Diff line number Diff line change @@ -65,3 +65,57 @@ func TestGetMemory(t *testing.T) {
6565 })
6666 }
6767}
68+
69+ func TestGetCPU (t * testing.T ) {
70+ tests := []struct {
71+ name string
72+ input string
73+ expected int
74+ expectError bool
75+ }{
76+ {
77+ name : "valid CPU count" ,
78+ input : "2" ,
79+ expected : 2 ,
80+ expectError : false ,
81+ },
82+ {
83+ name : "no limit case" ,
84+ input : lib .NoLimit ,
85+ expected : 0 ,
86+ expectError : false ,
87+ },
88+ {
89+ name : "invalid CPU count" ,
90+ input : "invalid" ,
91+ expected : 0 ,
92+ expectError : true ,
93+ },
94+ {
95+ name : "negative CPU count" ,
96+ input : "-1" ,
97+ expected : 0 ,
98+ expectError : true ,
99+ },
100+ {
101+ name : "empty string" ,
102+ input : "" ,
103+ expected : 0 ,
104+ expectError : true ,
105+ },
106+ }
107+
108+ for _ , tt := range tests {
109+ t .Run (tt .name , func (t * testing.T ) {
110+
111+ result , err := GetCPU (tt .input )
112+
113+ if tt .expectError {
114+ assert .Error (t , err )
115+ } else {
116+ assert .NoError (t , err )
117+ assert .Equal (t , tt .expected , result )
118+ }
119+ })
120+ }
121+ }
You can’t perform that action at this time.
0 commit comments