Add color to hpa line when replicas >= maxpods (#2333)
* feat: paint hpa line when replicas >= maxpods * chore: add license headers * refactor: decrease Assignment Branch Condition * validate IndexOf returns a valid indexmine
parent
df7f0c9825
commit
61ea437574
|
|
@ -202,4 +202,14 @@ var Registry = map[string]ResourceMeta{
|
|||
"rbac.authorization.k8s.io/v1/rolebindings": {
|
||||
Renderer: &render.RoleBinding{},
|
||||
},
|
||||
|
||||
// Autoscaling...
|
||||
"autoscaling/v1/horizontalpodautoscalers": {
|
||||
Renderer: &render.HorizontalPodAutoscaler{},
|
||||
DAO: &dao.Table{},
|
||||
},
|
||||
"autoscaling/v2/horizontalpodautoscalers": {
|
||||
Renderer: &render.HorizontalPodAutoscaler{},
|
||||
DAO: &dao.Table{},
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
// SPDX-License-Identifier: Apache-2.0
|
||||
// Copyright Authors of K9s
|
||||
|
||||
package render
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/derailed/tcell/v2"
|
||||
)
|
||||
|
||||
// HorizontalPodAutoscaler renders a K8s HorizontalPodAutoscaler to screen.
|
||||
type HorizontalPodAutoscaler struct {
|
||||
Generic
|
||||
}
|
||||
|
||||
// ColorerFunc colors a resource row.
|
||||
func (hpa HorizontalPodAutoscaler) ColorerFunc() ColorerFunc {
|
||||
return func(ns string, h Header, re RowEvent) tcell.Color {
|
||||
c := DefaultColorer(ns, h, re)
|
||||
|
||||
maxPodsIndex := h.IndexOf("MAXPODS", true)
|
||||
replicasIndex := h.IndexOf("REPLICAS", true)
|
||||
if (maxPodsIndex < 0 || maxPodsIndex >= len(re.Row.Fields)) || (replicasIndex < 0 || replicasIndex >= len(re.Row.Fields)) {
|
||||
return c
|
||||
}
|
||||
|
||||
maxPodsS := strings.TrimSpace(re.Row.Fields[maxPodsIndex])
|
||||
currentReplicasS := strings.TrimSpace(re.Row.Fields[replicasIndex])
|
||||
|
||||
maxPods, err := strconv.Atoi(maxPodsS)
|
||||
if err != nil {
|
||||
return c
|
||||
}
|
||||
currentReplicas, err := strconv.Atoi(currentReplicasS)
|
||||
if err != nil {
|
||||
return c
|
||||
}
|
||||
if currentReplicas >= maxPods {
|
||||
c = ErrColor
|
||||
}
|
||||
return c
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
// SPDX-License-Identifier: Apache-2.0
|
||||
// Copyright Authors of K9s
|
||||
|
||||
package render
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/derailed/tcell/v2"
|
||||
"github.com/derailed/tview"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestHorizontalPodAutoscalerColorer(t *testing.T) {
|
||||
hpaHeader := Header{
|
||||
HeaderColumn{Name: "NAMESPACE"},
|
||||
HeaderColumn{Name: "NAME"},
|
||||
HeaderColumn{Name: "REFERENCE"},
|
||||
HeaderColumn{Name: "TARGETS%"},
|
||||
HeaderColumn{Name: "MINPODS", Align: tview.AlignRight},
|
||||
HeaderColumn{Name: "MAXPODS", Align: tview.AlignRight},
|
||||
HeaderColumn{Name: "REPLICAS", Align: tview.AlignRight},
|
||||
HeaderColumn{Name: "AGE", Time: true},
|
||||
}
|
||||
|
||||
uu := map[string]struct {
|
||||
h Header
|
||||
re RowEvent
|
||||
e tcell.Color
|
||||
}{
|
||||
"when replicas = maxpods": {
|
||||
h: hpaHeader,
|
||||
re: RowEvent{
|
||||
Kind: EventUnchanged,
|
||||
Row: Row{
|
||||
Fields: Fields{"blee", "fred", "fred", "100%", "1", "5", "5", "1d"},
|
||||
},
|
||||
},
|
||||
e: ErrColor,
|
||||
},
|
||||
"when replicas > maxpods, for some reason": {
|
||||
h: hpaHeader,
|
||||
re: RowEvent{
|
||||
Kind: EventUnchanged,
|
||||
Row: Row{
|
||||
Fields: Fields{"blee", "fred", "fred", "100%", "1", "5", "6", "1d"},
|
||||
},
|
||||
},
|
||||
e: ErrColor,
|
||||
},
|
||||
"when replicas < maxpods": {
|
||||
h: hpaHeader,
|
||||
re: RowEvent{
|
||||
Kind: EventUnchanged,
|
||||
Row: Row{
|
||||
Fields: Fields{"blee", "fred", "fred", "100%", "1", "5", "1", "1d"},
|
||||
},
|
||||
},
|
||||
e: StdColor,
|
||||
},
|
||||
}
|
||||
|
||||
var r HorizontalPodAutoscaler
|
||||
for k := range uu {
|
||||
u := uu[k]
|
||||
t.Run(k, func(t *testing.T) {
|
||||
assert.Equal(t, u.e, r.ColorerFunc()("", u.h, u.re))
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue