-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Mi units #667
Support Mi units #667
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,11 +27,12 @@ import ( | |
) | ||
|
||
var ( | ||
serverPort int | ||
showAllVPAs bool | ||
basePath string | ||
insightsHost string | ||
enableCost bool | ||
serverPort int | ||
showAllVPAs bool | ||
basePath string | ||
insightsHost string | ||
enableCost bool | ||
useMemoryBinarySI bool | ||
) | ||
|
||
func init() { | ||
|
@@ -43,6 +44,7 @@ func init() { | |
dashboardCmd.PersistentFlags().StringVar(&basePath, "base-path", "/", "Path on which the dashboard is served.") | ||
dashboardCmd.PersistentFlags().BoolVar(&enableCost, "enable-cost", true, "If set to false, the cost integration will be disabled on the dashboard.") | ||
dashboardCmd.PersistentFlags().StringVar(&insightsHost, "insights-host", "https://insights.fairwinds.com", "Insights host for retrieving optional cost data.") | ||
dashboardCmd.PersistentFlags().BoolVar(&useMemoryBinarySI, "binary-si", false, "Use binary SI units for memory (base 2) instead of decimal SI units (base 10).") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion please, naming is hard 😓 |
||
} | ||
|
||
var dashboardCmd = &cobra.Command{ | ||
|
@@ -59,6 +61,7 @@ var dashboardCmd = &cobra.Command{ | |
dashboard.ShowAllVPAs(showAllVPAs), | ||
dashboard.InsightsHost(insightsHost), | ||
dashboard.EnableCost(enableCost), | ||
dashboard.UseMemoryBinarySI(useMemoryBinarySI), | ||
) | ||
http.Handle("/", router) | ||
klog.Infof("Starting goldilocks dashboard server on port %d and basePath %v", serverPort, validBasePath) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,21 +77,56 @@ func Difference(a, b []string) (diff []string) { | |
return | ||
} | ||
|
||
const ( | ||
Kibibyte = 1024 | ||
Mebibyte = Kibibyte * 1024 | ||
Gibibyte = Mebibyte * 1024 | ||
Tebibyte = Gibibyte * 1024 | ||
) | ||
|
||
func FormatToBinarySI(actual resource.Quantity, scale resource.Scale) resource.Quantity { | ||
var scaleValue int64 | ||
switch scale { | ||
case resource.Kilo: | ||
scaleValue = Kibibyte | ||
case resource.Mega: | ||
scaleValue = Mebibyte | ||
case resource.Giga: | ||
scaleValue = Gibibyte | ||
case resource.Tera: | ||
scaleValue = Tebibyte | ||
Comment on lines
+90
to
+97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Followed the scaled used below
|
||
default: | ||
scaleValue = Mebibyte | ||
} | ||
value := actual.Value() / scaleValue | ||
if actual.Value()%scaleValue != 0 { | ||
value++ | ||
} | ||
return *resource.NewQuantity(value*scaleValue, resource.BinarySI) | ||
} | ||
|
||
// FormatResourceList scales the units of a ResourceList so that they are | ||
// human readable | ||
func FormatResourceList(rl v1.ResourceList) v1.ResourceList { | ||
func FormatResourceList(rl v1.ResourceList, useBinarySI bool) v1.ResourceList { | ||
memoryScales := []resource.Scale{ | ||
resource.Kilo, | ||
resource.Mega, | ||
resource.Giga, | ||
resource.Tera, | ||
} | ||
if mem, exists := rl[v1.ResourceMemory]; exists { | ||
i := 0 | ||
maxAllowableStringLen := 5 | ||
for len(mem.String()) > maxAllowableStringLen && i < len(memoryScales)-1 { | ||
maxLength := 5 | ||
if len(mem.String()) <= maxLength { | ||
return rl | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since RoundUp is the issue in this case I decided to simply return early here 🤔 |
||
} | ||
for i := 0; i < len(memoryScales); i++ { | ||
mem.RoundUp(memoryScales[i]) | ||
i++ | ||
if len(mem.String()) <= maxLength { | ||
if useBinarySI { | ||
mem = FormatToBinarySI(mem, memoryScales[i]) | ||
} | ||
break | ||
} | ||
} | ||
rl[v1.ResourceMemory] = mem | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about naming here 😅