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:
@@ -1946,7 +1946,7 @@ func getProcessResources(appName string, processType string) (ProcessResourcesMa
|
||||
if err != nil {
|
||||
return ProcessResourcesMap{}, fmt.Errorf("Error parsing memory limit: %w", err)
|
||||
}
|
||||
if quantity != "0Mi" {
|
||||
if quantity != "0" {
|
||||
processResources.Limits.Memory = quantity
|
||||
} else {
|
||||
processResources.Limits.Memory = ""
|
||||
@@ -1977,7 +1977,7 @@ func getProcessResources(appName string, processType string) (ProcessResourcesMa
|
||||
if err != nil {
|
||||
return ProcessResourcesMap{}, fmt.Errorf("Error parsing memory request: %w", err)
|
||||
}
|
||||
if quantity != "0Mi" {
|
||||
if quantity != "0" {
|
||||
processResources.Requests.Memory = quantity
|
||||
} else {
|
||||
processResources.Requests.Memory = ""
|
||||
@@ -2720,17 +2720,14 @@ func cronIDLabelValue(cronID string) string {
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
// parseMemoryQuantity parses a string into a valid memory quantity
|
||||
// parseMemoryQuantity parses a docker-compatible memory value into a binary memory quantity
|
||||
func parseMemoryQuantity(input string) (string, error) {
|
||||
if _, err := strconv.ParseInt(input, 10, 64); err == nil {
|
||||
input = fmt.Sprintf("%sMi", input)
|
||||
}
|
||||
quantity, err := resource.ParseQuantity(input)
|
||||
bytes, err := common.ParseMemoryBytes(input)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return quantity.String(), nil
|
||||
return resource.NewQuantity(bytes, resource.BinarySI).String(), nil
|
||||
}
|
||||
|
||||
func uninstallHelperCommands(ctx context.Context) error {
|
||||
|
||||
@@ -487,6 +487,47 @@ func TestDomainSlug(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMemoryQuantity(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
input string
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "unitless value defaults to megabytes", input: "512", want: "512Mi"},
|
||||
{name: "lowercase megabytes are not millibytes", input: "256m", want: "256Mi"},
|
||||
{name: "uppercase gigabytes are binary", input: "1G", want: "1Gi"},
|
||||
{name: "lowercase gigabytes", input: "1g", want: "1Gi"},
|
||||
{name: "kubernetes binary unit", input: "1Gi", want: "1Gi"},
|
||||
{name: "two letter unit", input: "512MB", want: "512Mi"},
|
||||
{name: "decimal value", input: "1.5g", want: "1536Mi"},
|
||||
{name: "bytes", input: "100b", want: "100"},
|
||||
{name: "zero", input: "0Mi", want: "0"},
|
||||
{name: "invalid unit", input: "1x", wantErr: true},
|
||||
{name: "kubernetes exponent", input: "1e9", wantErr: true},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
got, err := parseMemoryQuantity(tc.input)
|
||||
if tc.wantErr {
|
||||
if err == nil {
|
||||
t.Fatalf("parseMemoryQuantity(%q) = %q, want error", tc.input, got)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("parseMemoryQuantity(%q) unexpected error: %v", tc.input, err)
|
||||
}
|
||||
if got != tc.want {
|
||||
t.Errorf("parseMemoryQuantity(%q) = %q, want %q", tc.input, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDomainSlugDistinguishesWildcardFromApex(t *testing.T) {
|
||||
// A kubernetes wildcard host matches exactly one label, so *.example.com does not
|
||||
// cover example.com and both are commonly added to the same app. The generated
|
||||
|
||||
Reference in New Issue
Block a user