cleaning up

mine
derailed 2020-03-04 17:44:57 -07:00
parent 7e06bd25a4
commit ecce7a140f
3 changed files with 93 additions and 38 deletions

View File

@ -299,11 +299,13 @@ var expectedConfig = `k9s:
active: ctx
thresholds:
cpu:
defcon:
- 90
- 80
- 75
- 70
memory:
defcon:
- 90
- 80
- 75
@ -329,11 +331,13 @@ var resetConfig = `k9s:
active: po
thresholds:
cpu:
defcon:
- 90
- 80
- 75
- 70
memory:
defcon:
- 90
- 80
- 75

View File

@ -8,41 +8,60 @@ import (
)
const (
// DefCon1 tracks high severity.
DefCon1 DefConLevel = iota + 1
// DefCon2 tracks warn level.
DefCon2
// DefCon3 tracks medium level.
DefCon3
// DefCon4 tracks low level.
DefCon4
// DefCon5 tracks all cool.
DefCon5
)
// DefConLevel tracks defcon severity.
type DefConLevel int
// DefCon tracks a resource alert level.
type DefCon [4]int
func newDefCon() DefCon {
return DefCon{90, 80, 75, 70}
type DefCon struct {
Levels []int `yaml:"defcon,omitempty"`
}
func (d DefCon) validate() {
dc := newDefCon()
for i := range d {
if !d.isValidRange(d[i]) {
d[i] = dc[i]
// NewDefCon returns a new instance.
func NewDefCon() *DefCon {
return &DefCon{Levels: []int{90, 80, 75, 70}}
}
// Validate checks all thresholds and make sure we're cool. If not use defaults.
func (d *DefCon) Validate() {
norm := NewDefCon()
if len(d.Levels) < 4 {
d.Levels = norm.Levels
return
}
for i, level := range d.Levels {
if !d.isValidRange(level) {
d.Levels[i] = norm.Levels[i]
}
}
}
func (d DefCon) String() string {
ss := make([]string, len(d))
for i := 0; i < len(d); i++ {
ss[i] = render.PrintPerc(d[i])
// String returns defcon settings a string.
func (d *DefCon) String() string {
ss := make([]string, len(d.Levels))
for i := 0; i < len(d.Levels); i++ {
ss[i] = render.PrintPerc(d.Levels[i])
}
return strings.Join(ss, "|")
}
func (d DefCon) isValidRange(v int) bool {
if v == 0 || v > 100 {
func (d *DefCon) isValidRange(v int) bool {
if v < 0 || v > 100 {
return false
}
@ -50,12 +69,13 @@ func (d DefCon) isValidRange(v int) bool {
}
// Threshold tracks threshold to alert user when excided.
type Threshold map[string]DefCon
type Threshold map[string]*DefCon
// NewThreshold returns a new threshold.
func NewThreshold() Threshold {
return Threshold{
"cpu": newDefCon(),
"memory": newDefCon(),
"cpu": NewDefCon(),
"memory": NewDefCon(),
}
}
@ -64,9 +84,9 @@ func (t Threshold) Validate(c client.Connection, ks KubeSettings) {
for _, k := range []string{"cpu", "memory"} {
v, ok := t[k]
if !ok {
t[k] = newDefCon()
t[k] = NewDefCon()
} else {
v.validate()
v.Validate()
}
}
}
@ -77,7 +97,7 @@ func (t Threshold) DefConFor(k string, v int) DefConLevel {
if !ok || v < 0 || v > 100 {
return DefCon5
}
for i, l := range dc {
for i, l := range dc.Levels {
if v >= l {
return dcLevelFor(i)
}
@ -86,6 +106,7 @@ func (t Threshold) DefConFor(k string, v int) DefConLevel {
return DefCon5
}
// DefConColorFor returns an defcon level associated level.
func (t *Threshold) DefConColorFor(k string, v int) string {
switch t.DefConFor(k, v) {
case DefCon1:

View File

@ -7,6 +7,37 @@ import (
"github.com/stretchr/testify/assert"
)
func TestDefConValidate(t *testing.T) {
uu := map[string]struct {
d, e *config.DefCon
}{
"default": {
d: config.NewDefCon(),
e: config.NewDefCon(),
},
"toast": {
d: &config.DefCon{Levels: []int{10}},
e: config.NewDefCon(),
},
"negative": {
d: &config.DefCon{Levels: []int{-1, 10, 10, 10}},
e: &config.DefCon{Levels: []int{90, 10, 10, 10}},
},
"out-of-range": {
d: &config.DefCon{Levels: []int{150, 200, 10, 300}},
e: &config.DefCon{Levels: []int{90, 80, 10, 70}},
},
}
for k := range uu {
u := uu[k]
t.Run(k, func(t *testing.T) {
u.d.Validate()
assert.Equal(t, u.e, u.d)
})
}
}
func TestDefConFor(t *testing.T) {
uu := map[string]struct {
k string
@ -16,7 +47,6 @@ func TestDefConFor(t *testing.T) {
"normal": {
k: "cpu",
v: 0,
e: config.DefCon5,
},
"4": {