fix: accept all docker memory units in resource values
Memory values with uppercase or other docker-supported units such as `1G`, `512M` or `1GiB` are now passed to docker unchanged instead of having an extra `m` appended, and the k3s scheduler interprets memory values with the same binary units so a value means the same thing on both schedulers. Invalid `--memory` and `--memory-swap` values are now rejected by `resource:limit` and `resource:reserve`. Closes #9153.
This commit is contained in:
13 files changed
+382
-30
No files matched your search
@@ -0,0 +1,61 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// memoryValueRegex matches a docker-compatible memory value: a number with an
|
||||
// optional unit (b, k, m, g, t, p in either case) optionally followed by i and/or b
|
||||
var memoryValueRegex = regexp.MustCompile(`^(\d+(?:\.\d+)?)(?:([kKmMgGtTpP])[iI]?)?([bB])?$`)
|
||||
|
||||
// IsValidMemoryValue returns true if the value is a docker-compatible memory value
|
||||
func IsValidMemoryValue(value string) bool {
|
||||
return memoryValueRegex.MatchString(value)
|
||||
}
|
||||
|
||||
// IsUnitlessMemoryValue returns true if the value is a valid memory value without any unit suffix
|
||||
func IsUnitlessMemoryValue(value string) bool {
|
||||
matches := memoryValueRegex.FindStringSubmatch(value)
|
||||
if matches == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return matches[2] == "" && matches[3] == ""
|
||||
}
|
||||
|
||||
// ParseMemoryBytes converts a docker-compatible memory value into bytes.
|
||||
// Units are binary, and a value without a unit is treated as megabytes
|
||||
func ParseMemoryBytes(value string) (int64, error) {
|
||||
matches := memoryValueRegex.FindStringSubmatch(value)
|
||||
if matches == nil {
|
||||
return 0, fmt.Errorf("Invalid memory value '%s'", value)
|
||||
}
|
||||
|
||||
size, err := strconv.ParseFloat(matches[1], 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("Invalid memory value '%s': %w", value, err)
|
||||
}
|
||||
|
||||
multipliers := map[string]float64{
|
||||
"k": 1 << 10,
|
||||
"m": 1 << 20,
|
||||
"g": 1 << 30,
|
||||
"t": 1 << 40,
|
||||
"p": 1 << 50,
|
||||
}
|
||||
|
||||
unit := strings.ToLower(matches[2])
|
||||
if unit == "" && matches[3] == "" {
|
||||
unit = "m"
|
||||
}
|
||||
|
||||
multiplier := float64(1)
|
||||
if unit != "" {
|
||||
multiplier = multipliers[unit]
|
||||
}
|
||||
|
||||
return int64(size * multiplier), nil
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestCommonIsValidMemoryValue(t *testing.T) {
|
||||
RegisterTestingT(t)
|
||||
|
||||
valid := []string{"0", "100", "1.5", "100b", "100B", "512k", "512K", "512kb", "512m", "512M", "512MB", "512Mi", "512MiB", "1g", "1G", "1GB", "1Gi", "1gib", "1t", "1T", "1p", "1P"}
|
||||
for _, value := range valid {
|
||||
Expect(IsValidMemoryValue(value)).To(BeTrue(), value)
|
||||
}
|
||||
|
||||
invalid := []string{"", "-1", "abc", "1x", "1Gm", "1i", "1ib", "1 G", "1.2.3", ".5g", "100G --foo", "1E", "1e9"}
|
||||
for _, value := range invalid {
|
||||
Expect(IsValidMemoryValue(value)).To(BeFalse(), value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommonIsUnitlessMemoryValue(t *testing.T) {
|
||||
RegisterTestingT(t)
|
||||
|
||||
Expect(IsUnitlessMemoryValue("100")).To(BeTrue())
|
||||
Expect(IsUnitlessMemoryValue("1.5")).To(BeTrue())
|
||||
Expect(IsUnitlessMemoryValue("100b")).To(BeFalse())
|
||||
Expect(IsUnitlessMemoryValue("1G")).To(BeFalse())
|
||||
Expect(IsUnitlessMemoryValue("1Gi")).To(BeFalse())
|
||||
Expect(IsUnitlessMemoryValue("abc")).To(BeFalse())
|
||||
}
|
||||
|
||||
func TestCommonParseMemoryBytes(t *testing.T) {
|
||||
RegisterTestingT(t)
|
||||
|
||||
expected := map[string]int64{
|
||||
"0": 0,
|
||||
"512": 536870912,
|
||||
"512m": 536870912,
|
||||
"512M": 536870912,
|
||||
"512MB": 536870912,
|
||||
"512Mi": 536870912,
|
||||
"512MiB": 536870912,
|
||||
"1g": 1073741824,
|
||||
"1G": 1073741824,
|
||||
"1GB": 1073741824,
|
||||
"1Gi": 1073741824,
|
||||
"1.5g": 1610612736,
|
||||
"1024K": 1048576,
|
||||
"100b": 100,
|
||||
"100B": 100,
|
||||
"1t": 1099511627776,
|
||||
"1p": 1125899906842624,
|
||||
}
|
||||
for value, bytes := range expected {
|
||||
parsed, err := ParseMemoryBytes(value)
|
||||
Expect(err).NotTo(HaveOccurred(), value)
|
||||
Expect(parsed).To(Equal(bytes), value)
|
||||
}
|
||||
|
||||
for _, value := range []string{"", "-1", "1x", "1Gm", "100G --foo"} {
|
||||
_, err := ParseMemoryBytes(value)
|
||||
Expect(err).To(HaveOccurred(), value)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user