Skip to content

Commit

Permalink
feat: add flags to define the thresholds that control the colored output
Browse files Browse the repository at this point in the history
  • Loading branch information
wI2L committed Mar 21, 2022
1 parent 1e8e3e1 commit 3f8696e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ It creates a single binary file for the host machine platform/architecture in th

The columns `% CPU DIFF` and `% MEMORY DIFF` represents the percentage of increase/decrease for the request in terms of the recommendation.

For example, if a request value is set to 4 CPU (4000m), and the recommendation is only 1 CPU (1000m), the difference printed is `+300%`. As a rule of thumb, you can think of positive values as "overcommitment" and negative values as "under commitment".
For example, if a request value is set to 4 CPU (`4000m`), and the recommendation is only 1 CPU (`1000m`), the difference printed is `+300%`. On the contrary, if the request (`125m`) is lower than the recommendation (`250m`), the difference is then `-50%`. As a rule of thumb, you can think of positive values as *over commitment* and negative values as *under commitment*.

### Demo

Expand Down Expand Up @@ -89,4 +89,4 @@ $ kubectl vpa-recommendation --help

## License

This plugin is licensed under the **MIT** license. See the [LICENSE](LICENSE) file.
This plugin is licensed under the **MIT** license. See the [LICENSE](LICENSE) file.
4 changes: 2 additions & 2 deletions cli/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func NewCmd(streams genericclioptions.IOStreams, name string) *cobra.Command {
comps := get.CompGetResource(f, cmd, vpaPlural, tc)
return comps, cobra.ShellCompDirectiveNoFileComp
},
PersistentPreRun: func(_ *cobra.Command, _ []string) {
opts.Flags.Tidy()
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
return opts.Flags.Tidy()
},
}
cmd.SetVersionTemplate("{{printf \"%s\" .Version}}\n")
Expand Down
18 changes: 17 additions & 1 deletion cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const (
flagOutput = "output"
flagOutputShorthand = "o"
flagRecommendationType = "recommendation-type"
flagWarningThreshold = "warning-threshold"
flagCriticalThreshold = "critical-threshold"
)

const (
Expand Down Expand Up @@ -58,6 +60,8 @@ type Flags struct {
FieldSelector string
Output string
RecommendationType vpa.RecommendationType
WarningThreshold float64
CriticalThreshold float64

wide bool
split bool
Expand All @@ -69,6 +73,8 @@ func DefaultFlags() *Flags {
SortOrder: defaultSortOrder,
SortColumns: defaultSortColumns,
RecommendationType: defaultRecommendationType,
WarningThreshold: 20,
CriticalThreshold: 50,
}
return f
}
Expand Down Expand Up @@ -113,10 +119,19 @@ func (f *Flags) AddFlags(flags *pflag.FlagSet) {

flags.BoolVar(&f.ShowStats, flagShowStats, f.ShowStats,
"Show statistics about all VPA recommendations and requests")

flags.Float64Var(&f.WarningThreshold, flagWarningThreshold, f.WarningThreshold,
"Warning threshold of percentage difference for colored output")

flags.Float64Var(&f.CriticalThreshold, flagCriticalThreshold, f.CriticalThreshold,
"Critical threshold of percentage difference for colored output")
}

// Tidy post-processes the flags.
func (f *Flags) Tidy() {
func (f *Flags) Tidy() error {
if f.CriticalThreshold <= f.WarningThreshold {
return fmt.Errorf("critical threshold must be strictly greater than warning")
}
switch f.Output {
case wideOutput:
f.wide = true
Expand All @@ -126,6 +141,7 @@ func (f *Flags) Tidy() {
f.wide = true
f.split = true
}
return nil
}

func sortColumnsFlagValues() []string {
Expand Down
13 changes: 7 additions & 6 deletions cli/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (tr tableRow) toTableData(flags *Flags, isChild bool) []string {
formatQuantity(tr.Requests.CPU), formatQuantity(tr.Recommendations.CPU),
)
}
rowData = append(rowData, formatPercentage(tr.CPUDifference, flags.NoColors))
rowData = append(rowData, formatPercentage(tr.CPUDifference, flags))
if flags.wide {
var str string
d := inf.Dec{}
Expand All @@ -112,7 +112,7 @@ func (tr tableRow) toTableData(flags *Flags, isChild bool) []string {
str,
)
}
rowData = append(rowData, formatPercentage(tr.MemoryDifference, flags.NoColors))
rowData = append(rowData, formatPercentage(tr.MemoryDifference, flags))

return rowData
}
Expand Down Expand Up @@ -392,22 +392,23 @@ var columnLessFunc = map[string]lessFunc{
},
}

func formatPercentage(f *float64, noColors bool) string {
func formatPercentage(f *float64, flags *Flags) string {
if f == nil {
return tableUnsetCell
}
n := fmt.Sprintf("%+.2f", *f)

if termenv.EnvNoColor() || noColors {
if termenv.EnvNoColor() || flags.NoColors {
return n
}
p := termenv.ColorProfile()
s := termenv.String(n)

warn, crit := flags.WarningThreshold, flags.CriticalThreshold
switch {
case *f >= -10 && *f <= 20:
case *f > -warn && *f < warn:
s = s.Foreground(p.Color("#A8CC8C"))
case (*f > 20 && *f < 50) || (*f < -10 && *f > -50):
case (*f >= warn && *f < crit) || (*f <= -warn && *f > -crit):
s = s.Foreground(p.Color("#DBAB79"))
default:
s = s.Foreground(p.Color("#E88388"))
Expand Down

0 comments on commit 3f8696e

Please sign in to comment.