Skip to content

Commit

Permalink
Added auto update on event "LAST SEEN" (#58)
Browse files Browse the repository at this point in the history
* Added updating "LAST SEEN" column

* Added coloring to event reason

* Updated CHANGELOG.md
  • Loading branch information
applejag committed Nov 2, 2023
1 parent 8a5468e commit fa6d985
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ This project tries to follow [SemVer 2.0.0](https://semver.org/).
- Added timer on pod's "STATUS" column when a pod is deleted
(e.g `Deleted (3m ago)`). (#56)

- Added auto updating on event's "LAST SEEN" column. (#58)

- Added coloring on event's "REASON" column. (#58)

- Fixed glitches when using flag `--watch-kubeconfig` / `-W`.
The watch was not properly restarting, but works great now. (#57)

Expand Down
22 changes: 11 additions & 11 deletions pkg/klock/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var (
StyleStatusWarning = lipgloss.NewStyle().Foreground(lipgloss.ANSIColor(3))
)

func ParseFractionStyle(str string) (lipgloss.Style, bool) {
func FractionStyle(str string) (lipgloss.Style, bool) {
var count int
var total int
if _, err := fmt.Sscanf(str, "%d/%d", &count, &total); err != nil {
Expand All @@ -51,7 +51,7 @@ func ParseFractionStyle(str string) (lipgloss.Style, bool) {
return StyleFractionWarning, true
}

func ParseStatusStyle(status string) lipgloss.Style {
func StatusStyle(status string) lipgloss.Style {
switch status {
case
// from https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/events/event.go
Expand Down Expand Up @@ -113,7 +113,6 @@ func ParseStatusStyle(status string) lipgloss.Style {
"Preempting",
// Pod event reason list
// Image event reason list
"Pulling",
// kubelet event reason list
"NodeNotReady",
"NodeSchedulable",
Expand All @@ -139,18 +138,19 @@ func ParseStatusStyle(status string) lipgloss.Style {
case
"Running",
"Completed",
"Pulled",
"Created",
"Rebooted",
"NodeReady",
"Started",
"Normal",
"VolumeResizeSuccessful",
"FileSystemResizeSuccessful",
"Ready":
return StyleStatusOK
}
// some ok status, not colored:
// "Pulled",
// "Created",
// "Rebooted",
// "SandboxChanged",
// "VolumeResizeSuccessful",
// "FileSystemResizeSuccessful",
// "NodeReady",
// "Started",
// "Normal",
// "Pulling",
return StyleStatusDefault
}
24 changes: 19 additions & 5 deletions pkg/klock/klock.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,13 @@ func (p *Printer) addObjectToTable(objTable *metav1.Table, eventType watch.Event
Fields: make([]any, 0, len(p.colDefs)),
SortField: name,
}
if p.apiVersion == "v1" && p.kind == "Event" {
tableRow.SortField = creationTimestamp
}
if p.printNamespace {
namespace := metadata["namespace"]
tableRow.Fields = append(tableRow.Fields, namespace)
tableRow.SortField = fmt.Sprintf("%s/%s", namespace, name)
tableRow.SortField = fmt.Sprintf("%s/%s", namespace, tableRow.SortField)
}
for i, cell := range row.Cells {
if i >= len(p.colDefs) {
Expand All @@ -400,7 +403,7 @@ func (p *Printer) addObjectToTable(objTable *metav1.Table, eventType watch.Event
if colDef.Priority != 0 && !p.WideOutput {
continue
}
tableRow.Fields = append(tableRow.Fields, p.parseCell(cell, eventType, colDef, creationTime))
tableRow.Fields = append(tableRow.Fields, p.parseCell(cell, eventType, unstrucObj.Object, colDef, creationTime))
}
for _, label := range p.LabelCols {
labelValue := unstrucObj.GetLabels()[label]
Expand All @@ -420,7 +423,7 @@ func (p *Printer) addObjectToTable(objTable *metav1.Table, eventType watch.Event
return cmd, nil
}

func (p *Printer) parseCell(cell any, eventType watch.EventType, colDef metav1.TableColumnDefinition, creationTime time.Time) any {
func (p *Printer) parseCell(cell any, eventType watch.EventType, object map[string]any, colDef metav1.TableColumnDefinition, creationTime time.Time) any {
cellStr := fmt.Sprint(cell)
columnNameLower := strings.ToLower(colDef.Name)
switch {
Expand All @@ -435,13 +438,24 @@ func (p *Printer) parseCell(cell any, eventType watch.EventType, colDef metav1.T
Time: time.Now(),
}
} else {
style := ParseStatusStyle(cellStr)
style := StatusStyle(cellStr)
cell = table.StyledColumn{
Value: cell,
Style: style,
}
}
return cell
case p.apiVersion == "v1" && p.kind == "Event" && columnNameLower == "last seen":
dur, ok := parseHumanDuration(cellStr)
if !ok {
return cell
}
return time.Now().Add(-dur)
case p.apiVersion == "v1" && p.kind == "Event" && columnNameLower == "reason":
return table.StyledColumn{
Value: cell,
Style: StatusStyle(cellStr),
}
case p.apiVersion == "v1" && p.kind == "Pod" && columnNameLower == "restarts":
// 0, the most common case
if cellStr == "0" {
Expand All @@ -465,7 +479,7 @@ func (p *Printer) parseCell(cell any, eventType watch.EventType, colDef metav1.T
// Only parse fraction (e.g "1/2") if the resources was not deleted,
// so we don't have colored fraction on a grayed-out row.
case eventType != watch.Deleted:
fractionStyle, ok := ParseFractionStyle(cellStr)
fractionStyle, ok := FractionStyle(cellStr)
if ok {
cell = table.StyledColumn{
Value: cell,
Expand Down

0 comments on commit fa6d985

Please sign in to comment.