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,15 +299,17 @@ var expectedConfig = `k9s:
active: ctx active: ctx
thresholds: thresholds:
cpu: cpu:
- 90 defcon:
- 80 - 90
- 75 - 80
- 70 - 75
- 70
memory: memory:
- 90 defcon:
- 80 - 90
- 75 - 80
- 70 - 75
- 70
` `
var resetConfig = `k9s: var resetConfig = `k9s:
@ -329,13 +331,15 @@ var resetConfig = `k9s:
active: po active: po
thresholds: thresholds:
cpu: cpu:
- 90 defcon:
- 80 - 90
- 75 - 80
- 70 - 75
- 70
memory: memory:
- 90 defcon:
- 80 - 90
- 75 - 80
- 70 - 75
- 70
` `

View File

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

View File

@ -7,6 +7,37 @@ import (
"github.com/stretchr/testify/assert" "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) { func TestDefConFor(t *testing.T) {
uu := map[string]struct { uu := map[string]struct {
k string k string
@ -16,7 +47,6 @@ func TestDefConFor(t *testing.T) {
"normal": { "normal": {
k: "cpu", k: "cpu",
v: 0, v: 0,
e: config.DefCon5, e: config.DefCon5,
}, },
"4": { "4": {