-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #267 fixes #227
- Loading branch information
Showing
13 changed files
with
322 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,8 +12,8 @@ profiles: | |
compute: | ||
web: | ||
cpu: 2 | ||
memory: 3 | ||
disk: 5 | ||
memory: "128Mi" | ||
disk: "5Gi" | ||
|
||
placement: | ||
westcoast: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package sdl | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
var ( | ||
errNegativeValue = fmt.Errorf("invalid: negative value not allowed") | ||
) | ||
|
||
const ( | ||
unitk = 1000 | ||
unitKi = 1024 | ||
|
||
unitM = unitk * unitk | ||
unitMi = unitKi * unitKi | ||
|
||
unitG = unitM * unitk | ||
unitGi = unitMi * unitKi | ||
|
||
unitT = unitG * unitk | ||
unitTi = unitGi * unitKi | ||
|
||
unitP = unitT * unitk | ||
unitPi = unitTi * unitKi | ||
|
||
unitE = unitP * unitk | ||
unitEi = unitPi * unitKi | ||
) | ||
|
||
var suffixes = []struct { | ||
symbol string | ||
unit uint64 | ||
}{ | ||
{"k", unitk}, | ||
{"Ki", unitKi}, | ||
|
||
{"M", unitM}, | ||
{"Mi", unitMi}, | ||
|
||
{"G", unitG}, | ||
{"Gi", unitGi}, | ||
|
||
{"T", unitT}, | ||
{"Ti", unitTi}, | ||
|
||
{"P", unitP}, | ||
{"Pi", unitPi}, | ||
|
||
{"E", unitE}, | ||
{"Ei", unitEi}, | ||
} | ||
|
||
// CPU shares. One CPUQuantity = 1/1000 of a CPU | ||
type cpuQuantity uint32 | ||
|
||
func (u *cpuQuantity) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
|
||
var sval string | ||
if err := unmarshal(&sval); err != nil { | ||
return err | ||
} | ||
|
||
if strings.HasSuffix(sval, "m") { | ||
sval = strings.TrimSuffix(sval, "m") | ||
val, err := strconv.ParseUint(sval, 10, 32) | ||
if err != nil { | ||
return err | ||
} | ||
if val < 0 { | ||
return errNegativeValue | ||
} | ||
*u = cpuQuantity(val) | ||
return nil | ||
} | ||
|
||
val, err := strconv.ParseFloat(sval, 64) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
val *= 1000 | ||
|
||
if val < 0 { | ||
return errNegativeValue | ||
} | ||
|
||
*u = cpuQuantity(val) | ||
|
||
return nil | ||
} | ||
|
||
// Memory,Disk size in bytes. | ||
type byteQuantity uint64 | ||
|
||
func (u *byteQuantity) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
var sval string | ||
if err := unmarshal(&sval); err != nil { | ||
return err | ||
} | ||
val, err := parseWithSuffix(sval) | ||
if err != nil { | ||
return err | ||
} | ||
*u = byteQuantity(val) | ||
return nil | ||
} | ||
|
||
func parseWithSuffix(sval string) (uint64, error) { | ||
for _, suffix := range suffixes { | ||
if !strings.HasSuffix(sval, suffix.symbol) { | ||
continue | ||
} | ||
|
||
sval := strings.TrimSuffix(sval, suffix.symbol) | ||
|
||
val, err := strconv.ParseFloat(sval, 64) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
val *= float64(suffix.unit) | ||
|
||
if val < 0 { | ||
return 0, errNegativeValue | ||
} | ||
|
||
return uint64(val), nil | ||
} | ||
|
||
val, err := strconv.ParseFloat(sval, 64) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
if val < 0 { | ||
return 0, errNegativeValue | ||
} | ||
|
||
return uint64(val), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package sdl | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
yaml "gopkg.in/yaml.v2" | ||
) | ||
|
||
func TestCPUQuantity(t *testing.T) { | ||
|
||
type vtype struct { | ||
Val cpuQuantity `yaml:"val"` | ||
} | ||
|
||
tests := []struct { | ||
text string | ||
value uint32 | ||
err bool | ||
}{ | ||
{`val: 1`, 1000, false}, | ||
{`val: -1`, 1000, true}, | ||
|
||
{`val: 0.5`, 500, false}, | ||
{`val: -0.5`, 500, true}, | ||
|
||
{`val: "100m"`, 100, false}, | ||
{`val: "-100m"`, 100, true}, | ||
|
||
{`val: ""`, 0, true}, | ||
} | ||
|
||
for idx, test := range tests { | ||
buf := []byte(test.text) | ||
obj := &vtype{} | ||
|
||
err := yaml.UnmarshalStrict(buf, obj) | ||
|
||
if test.err { | ||
assert.Error(t, err, "idx:%v text:`%v`", idx, test.text) | ||
continue | ||
} | ||
|
||
if !assert.NoError(t, err, "idx:%v text:`%v`", idx, test.text) { | ||
continue | ||
} | ||
|
||
assert.Equal(t, cpuQuantity(test.value), obj.Val, "idx:%v text:`%v`", idx, test.text) | ||
} | ||
} | ||
|
||
func TestByteQuantity(t *testing.T) { | ||
type vtype struct { | ||
Val byteQuantity `yaml:"val"` | ||
} | ||
|
||
tests := []struct { | ||
text string | ||
value uint64 | ||
err bool | ||
}{ | ||
{`val: 1`, 1, false}, | ||
{`val: -1`, 1, true}, | ||
|
||
{`val: "1M"`, unitM, false}, | ||
{`val: "-1M"`, 0, true}, | ||
|
||
{`val: "0.5M"`, unitM / 2, false}, | ||
{`val: "-0.5M"`, 0, true}, | ||
|
||
{`val: "3M"`, 3 * unitM, false}, | ||
{`val: "3G"`, 3 * unitG, false}, | ||
{`val: "3T"`, 3 * unitT, false}, | ||
{`val: "3P"`, 3 * unitP, false}, | ||
{`val: "3E"`, 3 * unitE, false}, | ||
|
||
{`val: ""`, 0, true}, | ||
} | ||
|
||
for idx, test := range tests { | ||
buf := []byte(test.text) | ||
obj := &vtype{} | ||
|
||
err := yaml.UnmarshalStrict(buf, obj) | ||
|
||
if test.err { | ||
assert.Error(t, err, "idx:%v text:`%v`", idx, test.text) | ||
continue | ||
} | ||
|
||
if !assert.NoError(t, err, "idx:%v text:`%v`", idx, test.text) { | ||
continue | ||
} | ||
|
||
assert.Equal(t, byteQuantity(test.value), obj.Val, "idx:%v text:`%v`", idx, test.text) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.