Skip to content

Commit 3f8696e

Browse files
committed
feat: add flags to define the thresholds that control the colored output
1 parent 1e8e3e1 commit 3f8696e

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ It creates a single binary file for the host machine platform/architecture in th
4747

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

50-
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".
50+
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*.
5151

5252
### Demo
5353

@@ -89,4 +89,4 @@ $ kubectl vpa-recommendation --help
8989

9090
## License
9191

92-
This plugin is licensed under the **MIT** license. See the [LICENSE](LICENSE) file.
92+
This plugin is licensed under the **MIT** license. See the [LICENSE](LICENSE) file.

cli/command.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ func NewCmd(streams genericclioptions.IOStreams, name string) *cobra.Command {
7474
comps := get.CompGetResource(f, cmd, vpaPlural, tc)
7575
return comps, cobra.ShellCompDirectiveNoFileComp
7676
},
77-
PersistentPreRun: func(_ *cobra.Command, _ []string) {
78-
opts.Flags.Tidy()
77+
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
78+
return opts.Flags.Tidy()
7979
},
8080
}
8181
cmd.SetVersionTemplate("{{printf \"%s\" .Version}}\n")

cli/flags.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const (
2929
flagOutput = "output"
3030
flagOutputShorthand = "o"
3131
flagRecommendationType = "recommendation-type"
32+
flagWarningThreshold = "warning-threshold"
33+
flagCriticalThreshold = "critical-threshold"
3234
)
3335

3436
const (
@@ -58,6 +60,8 @@ type Flags struct {
5860
FieldSelector string
5961
Output string
6062
RecommendationType vpa.RecommendationType
63+
WarningThreshold float64
64+
CriticalThreshold float64
6165

6266
wide bool
6367
split bool
@@ -69,6 +73,8 @@ func DefaultFlags() *Flags {
6973
SortOrder: defaultSortOrder,
7074
SortColumns: defaultSortColumns,
7175
RecommendationType: defaultRecommendationType,
76+
WarningThreshold: 20,
77+
CriticalThreshold: 50,
7278
}
7379
return f
7480
}
@@ -113,10 +119,19 @@ func (f *Flags) AddFlags(flags *pflag.FlagSet) {
113119

114120
flags.BoolVar(&f.ShowStats, flagShowStats, f.ShowStats,
115121
"Show statistics about all VPA recommendations and requests")
122+
123+
flags.Float64Var(&f.WarningThreshold, flagWarningThreshold, f.WarningThreshold,
124+
"Warning threshold of percentage difference for colored output")
125+
126+
flags.Float64Var(&f.CriticalThreshold, flagCriticalThreshold, f.CriticalThreshold,
127+
"Critical threshold of percentage difference for colored output")
116128
}
117129

118130
// Tidy post-processes the flags.
119-
func (f *Flags) Tidy() {
131+
func (f *Flags) Tidy() error {
132+
if f.CriticalThreshold <= f.WarningThreshold {
133+
return fmt.Errorf("critical threshold must be strictly greater than warning")
134+
}
120135
switch f.Output {
121136
case wideOutput:
122137
f.wide = true
@@ -126,6 +141,7 @@ func (f *Flags) Tidy() {
126141
f.wide = true
127142
f.split = true
128143
}
144+
return nil
129145
}
130146

131147
func sortColumnsFlagValues() []string {

cli/table.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (tr tableRow) toTableData(flags *Flags, isChild bool) []string {
9393
formatQuantity(tr.Requests.CPU), formatQuantity(tr.Recommendations.CPU),
9494
)
9595
}
96-
rowData = append(rowData, formatPercentage(tr.CPUDifference, flags.NoColors))
96+
rowData = append(rowData, formatPercentage(tr.CPUDifference, flags))
9797
if flags.wide {
9898
var str string
9999
d := inf.Dec{}
@@ -112,7 +112,7 @@ func (tr tableRow) toTableData(flags *Flags, isChild bool) []string {
112112
str,
113113
)
114114
}
115-
rowData = append(rowData, formatPercentage(tr.MemoryDifference, flags.NoColors))
115+
rowData = append(rowData, formatPercentage(tr.MemoryDifference, flags))
116116

117117
return rowData
118118
}
@@ -392,22 +392,23 @@ var columnLessFunc = map[string]lessFunc{
392392
},
393393
}
394394

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

401-
if termenv.EnvNoColor() || noColors {
401+
if termenv.EnvNoColor() || flags.NoColors {
402402
return n
403403
}
404404
p := termenv.ColorProfile()
405405
s := termenv.String(n)
406406

407+
warn, crit := flags.WarningThreshold, flags.CriticalThreshold
407408
switch {
408-
case *f >= -10 && *f <= 20:
409+
case *f > -warn && *f < warn:
409410
s = s.Foreground(p.Color("#A8CC8C"))
410-
case (*f > 20 && *f < 50) || (*f < -10 && *f > -50):
411+
case (*f >= warn && *f < crit) || (*f <= -warn && *f > -crit):
411412
s = s.Foreground(p.Color("#DBAB79"))
412413
default:
413414
s = s.Foreground(p.Color("#E88388"))

0 commit comments

Comments
 (0)