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
|
||||
}
|
||||
Reference in new issue
Block a user