Unverified Commit 01408014 authored by Lemon-miaow's avatar Lemon-miaow
Browse files

fix(api): quantityToMB 按 MiB 向上取整,配额不再少算零头

parent 2c5a85a0
Loading
Loading
Loading
Loading
+5 −10
Changes for internal/api/handlers_user.go: 5 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -1145,15 +1145,10 @@ func quantityToMilli(q resource.Quantity) int {
	return int(q.MilliValue())
}

// quantityToMB converts a K8s resource.Quantity to whole megabytes, rounding up
// (e.g. "4Gi"→4096, "1G"→1000). A zero/unset quantity returns 0.
// quantityToMB converts a K8s resource.Quantity to whole MiB, the MB of the quota
// columns, rounding up so a size between two counts as the larger: "4Gi"→4096,
// "1G"→954. A zero/unset quantity returns 0.
func quantityToMB(q resource.Quantity) int {
	if q.IsZero() {
		return 0
	}
	mb := q.Value() / (1024 * 1024)
	if mb < 1 {
		return 1
	}
	return int(mb)
	const mib = 1 << 20
	return int((q.Value() + mib - 1) / mib)
}
+31 −0
Changes for internal/api/quantity_test.go: 31 added lines, 0 removed lines.
Original line number Diff line number Diff line
package api

import (
	"testing"

	"k8s.io/apimachinery/pkg/api/resource"
)

// Sizes reach the quota sums in whole MiB, rounded up: a part-MiB used to be
// dropped, so a server sized in decimal units counted for less than it holds.
func TestQuantityToMB(t *testing.T) {
	for _, c := range []struct {
		in   string
		want int
	}{
		{"4Gi", 4096},
		{"1Mi", 1},
		{"1G", 954},     // 953.67 MiB
		{"1500M", 1431}, // 1430.51 MiB
		{"1048577", 2},  // one byte past 1 MiB
		{"1", 1},
		{"0", 0},
	} {
		if got := quantityToMB(resource.MustParse(c.in)); got != c.want {
			t.Errorf("quantityToMB(%s) = %d, want %d", c.in, got, c.want)
		}
	}
	if got := quantityToMB(resource.Quantity{}); got != 0 {
		t.Errorf("quantityToMB(unset) = %d, want 0", got)
	}
}