Skip to content
This repository was archived by the owner on Aug 14, 2020. It is now read-only.

Commit 91ee069

Browse files
committed
schema/resource isolator: Allow either 'limit' or 'request' to be empty.
NewResourceMemoryIsolator/NewResourceCPUIsolator If 'limit' is empty, 'request' is set to 'limit', vice versa. If both are empty, then functions return error.
1 parent 328fd6d commit 91ee069

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

schema/types/isolator_resources.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ func (r ResourceCPU) AsIsolator() Isolator {
144144
}
145145

146146
func NewResourceCPUIsolator(request, limit string) (*ResourceCPU, error) {
147+
if request == "" {
148+
request = limit
149+
} else if limit == "" {
150+
limit = request
151+
}
152+
147153
req, err := resource.ParseQuantity(request)
148154
if err != nil {
149155
return nil, fmt.Errorf("error parsing request: %v", err)
@@ -198,6 +204,12 @@ func (r ResourceMemory) AsIsolator() Isolator {
198204
}
199205

200206
func NewResourceMemoryIsolator(request, limit string) (*ResourceMemory, error) {
207+
if request == "" {
208+
request = limit
209+
} else if limit == "" {
210+
limit = request
211+
}
212+
201213
req, err := resource.ParseQuantity(request)
202214
if err != nil {
203215
return nil, fmt.Errorf("error parsing request: %v", err)

schema/types/isolator_resources_test.go

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,44 @@ func TestResourceMemoryIsolator(t *testing.T) {
4848
},
4949
false,
5050
},
51+
// only empty request is valid.
52+
{
53+
"100M",
54+
"",
55+
56+
&ResourceMemory{
57+
ResourceBase{
58+
resourceValue{
59+
Request: mustQuantity("100M"),
60+
Limit: mustQuantity("100M"),
61+
},
62+
},
63+
},
64+
false,
65+
},
66+
// only empty limit is valid.
67+
{
68+
"",
69+
"200M",
70+
71+
&ResourceMemory{
72+
ResourceBase{
73+
resourceValue{
74+
Request: mustQuantity("200M"),
75+
Limit: mustQuantity("200M"),
76+
},
77+
},
78+
},
79+
false,
80+
},
81+
// both empty limit is invalid.
82+
{
83+
"",
84+
"",
85+
86+
nil,
87+
true,
88+
},
5189
}
5290
for i, tt := range tests {
5391
gres, err := NewResourceMemoryIsolator(tt.inreq, tt.inlim)
@@ -68,14 +106,44 @@ func TestResourceCPUIsolator(t *testing.T) {
68106
wres *ResourceCPU
69107
werr bool
70108
}{
71-
// empty is not valid
109+
// both empty is not valid
72110
{
73111
"",
74-
"2",
112+
"",
75113

76114
nil,
77115
true,
78116
},
117+
// only empty request is valid.
118+
{
119+
"1",
120+
"",
121+
122+
&ResourceCPU{
123+
ResourceBase{
124+
resourceValue{
125+
Request: mustQuantity("1"),
126+
Limit: mustQuantity("1"),
127+
},
128+
},
129+
},
130+
false,
131+
},
132+
// only empty limit is valid.
133+
{
134+
"",
135+
"2",
136+
137+
&ResourceCPU{
138+
ResourceBase{
139+
resourceValue{
140+
Request: mustQuantity("2"),
141+
Limit: mustQuantity("2"),
142+
},
143+
},
144+
},
145+
false,
146+
},
79147
// garbage value
80148
{
81149
"1",

spec/ace.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ If **request** is omitted, it defaults to the value of **limit**.
162162

163163
- **limit** is the maximum amount of a resource available to the app/pod.
164164
If the app/pod consumes a resource in excess of its limit, it must be terminated or throttled to no more than the limit.
165+
If **limit** is ommitted, it defaults to the value of **request**.
165166

166167
Limit and request quantities must always be represented internally (i.e. for encoding and any processing) as an integer value (i.e. NOT floating point) in a resource type's natural base units (e.g., bytes, not megabytes or gigabytes).
167168
For convenience, when specified by users quantities may either be unsuffixed, have metric suffices (E, P, T, G, M, K) or binary (power-of-two) suffices (Ei, Pi, Ti, Gi, Mi, Ki).

0 commit comments

Comments
 (0)