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
@@ -2,25 +2,38 @@ package resource
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/dokku/dokku/plugins/common"
|
||||
)
|
||||
|
||||
func addMemorySuffixForDocker(key string, value string) string {
|
||||
if key == "memory" {
|
||||
hasSuffix := false
|
||||
suffixes := []string{"b", "k", "m", "g", "KB", "MB", "GB", "B"}
|
||||
for _, suffix := range suffixes {
|
||||
if strings.HasSuffix(value, suffix) {
|
||||
hasSuffix = true
|
||||
}
|
||||
// validateResourceValue returns an error if the value is not valid for the given resource key
|
||||
func validateResourceValue(key string, value string) error {
|
||||
switch key {
|
||||
case "memory":
|
||||
if !common.IsValidMemoryValue(value) {
|
||||
return fmt.Errorf("Invalid memory value '%s': must be a number with an optional unit suffix of b, k, m, g, t or p", value)
|
||||
}
|
||||
if !hasSuffix {
|
||||
value = value + "m"
|
||||
case "memory-swap":
|
||||
if value != "-1" && !common.IsValidMemoryValue(value) {
|
||||
return fmt.Errorf("Invalid memory-swap value '%s': must be -1 or a number with an optional unit suffix of b, k, m, g, t or p", value)
|
||||
}
|
||||
}
|
||||
return value
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// formatResourceValueForDocker validates a resource value and formats it for use as a docker flag value.
|
||||
// Memory values without a unit default to megabytes
|
||||
func formatResourceValueForDocker(key string, value string) (string, error) {
|
||||
if err := validateResourceValue(key, value); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if key == "memory" && common.IsUnitlessMemoryValue(value) {
|
||||
value = value + "m"
|
||||
}
|
||||
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func clearByResourceType(appName string, processType string, resourceType string) {
|
||||
@@ -88,6 +101,16 @@ func setResourceType(appName string, processType string, r Resource, resourceTyp
|
||||
return nil
|
||||
}
|
||||
|
||||
for key, value := range resources {
|
||||
if value == "" || value == "clear" {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := validateResourceValue(key, value); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
noun := "limits"
|
||||
if resourceType == "reserve" {
|
||||
noun = "reservation"
|
||||
|
||||
Reference in new issue
Block a user