Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: Allow (Pod) label values as table columns #3076

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion internal/config/json/schemas/views.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@
"sortColumn": { "type": "string" },
"columns": {
"type": "array",
"items": { "type": "string" }
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"name": { "type": "string"},
"key": { "type": "string" }
}
}
}
},
"required": ["columns"]
Expand Down
20 changes: 18 additions & 2 deletions internal/config/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ type ViewConfigListener interface {

// ViewSetting represents a view configuration.
type ViewSetting struct {
Columns []string `yaml:"columns"`
Columns []Column `yaml:"columns"`
SortColumn string `yaml:"sortColumn"`
}

type Column struct {
Name string `yaml:"name"`
Key string `yaml:"key"`
}

func (v *ViewSetting) HasCols() bool {
return len(v.Columns) > 0
}
Expand All @@ -54,7 +59,18 @@ func (v *ViewSetting) Equals(vs *ViewSetting) bool {
if v == nil || vs == nil {
return v == nil && vs == nil
}
if c := slices.Compare(v.Columns, vs.Columns); c != 0 {

var vkeys []string
var vskeys []string

for idx := range v.Columns {
vkeys = append(vkeys, v.Columns[idx].Name)
}
for idx := range vs.Columns {
vskeys = append(vskeys, vs.Columns[idx].Name)
}

if c := slices.Compare(vkeys, vskeys); c != 0 {
return false
}
return cmp.Compare(v.SortColumn, vs.SortColumn) == 0
Expand Down
14 changes: 8 additions & 6 deletions internal/model1/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package model1
import (
"reflect"

"github.com/derailed/k9s/internal/config"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -64,11 +65,11 @@ func (h Header) Labelize(cols []int, labelCol int, rr *RowEvents) Header {
}

// MapIndices returns a collection of mapped column indices based of the requested columns.
func (h Header) MapIndices(cols []string, wide bool) []int {
func (h Header) MapIndices(cols []config.Column, wide bool) []int {
ii := make([]int, 0, len(cols))
cc := make(map[int]struct{}, len(cols))
for _, col := range cols {
idx, ok := h.IndexOf(col, true)
idx, ok := h.IndexOf(col.Name, true)
if !ok {
log.Warn().Msgf("Column %q not found on resource", col)
}
Expand All @@ -88,22 +89,23 @@ func (h Header) MapIndices(cols []string, wide bool) []int {
}

// Customize builds a header from custom col definitions.
func (h Header) Customize(cols []string, wide bool) Header {
func (h Header) Customize(cols []config.Column, wide bool) Header {
if len(cols) == 0 {
return h
}
cc := make(Header, 0, len(h))
xx := make(map[int]struct{}, len(h))
for _, c := range cols {
idx, ok := h.IndexOf(c, true)
idx, ok := h.IndexOf(c.Name, true)
if !ok {
log.Warn().Msgf("Column %s is not available on this resource", c)
cc = append(cc, HeaderColumn{Name: c})
log.Warn().Msgf("Column %s is not available on this resource", c.Name)
cc = append(cc, HeaderColumn{Name: c.Name})
continue
}
xx[idx] = struct{}{}
col := h[idx].Clone()
col.Wide = false

cc = append(cc, col)
}

Expand Down
7 changes: 7 additions & 0 deletions internal/render/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func (p Pod) Header(ns string) model1.Header {
model1.HeaderColumn{Name: "LABELS", Wide: true},
model1.HeaderColumn{Name: "VALID", Wide: true},
model1.HeaderColumn{Name: "AGE", Time: true},
model1.HeaderColumn{Name: "LABEL"},
}
}

Expand Down Expand Up @@ -166,11 +167,17 @@ func (p Pod) Render(o interface{}, ns string, row *model1.Row) error {
mapToStr(po.Labels),
AsStatus(p.diagnose(phase, cr, len(cs))),
ToAge(po.GetCreationTimestamp()),
ExtractLabel(po),
}

return nil
}

func ExtractLabel(pod v1.Pod) string {
// Need to get the "key" from the views.yaml config here
return pod.Labels["k8s-app"]
}

func (p Pod) diagnose(phase string, cr, ct int) error {
if phase == Completed {
return nil
Expand Down
1 change: 1 addition & 0 deletions internal/ui/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ func (t *Table) buildRow(r int, re, ore model1.RowEvent, h model1.Header, pads M
if h[c].Decorator != nil {
field = h[c].Decorator(field)
}

if h[c].Align == tview.AlignLeft {
field = formatCell(field, pads[c])
}
Expand Down