diff --git a/CHANGELOG.md b/CHANGELOG.md index 71b81cf..b0123a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/pkg/klock/color.go b/pkg/klock/color.go index 957467c..dd74805 100644 --- a/pkg/klock/color.go +++ b/pkg/klock/color.go @@ -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 { @@ -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 @@ -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", @@ -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 } diff --git a/pkg/klock/klock.go b/pkg/klock/klock.go index 3926d61..4853e57 100644 --- a/pkg/klock/klock.go +++ b/pkg/klock/klock.go @@ -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) { @@ -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] @@ -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 { @@ -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" { @@ -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,