Utku Ozdemir 5ffc3f14bd
feat: show siderolink status on dashboard
Add a new resource, `SiderolinkStatus`, which combines the following info:
- The Siderolink API endpoint without the query parameters or fragments (potentially sensitive info due to the join token)
- The status of the Siderolink connection

This resource is not set as sensitive, so it can be retrieved by the users with `os:operator` role (e.g., using `talosctl dashboard` through Omni).

Make use of this resource in the dashboard to display the status of the Siderolink connection.

Additionally, rework the status columns in the dashboard to:
- Display a Linux terminal compatible "tick" or a "cross" prefix for statuses in addition to the red/green color coding.
- Move and combine some statuses to save rows and make them more even.

Closes siderolabs/talos#8643.

Signed-off-by: Utku Ozdemir <utku.ozdemir@siderolabs.com>
2024-06-18 12:31:54 +02:00

106 lines
2.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Package components implements specific widgets for the dashboard.
package components
import (
"fmt"
"strings"
"github.com/siderolabs/gen/xslices"
)
const (
noData = "..."
notAvailable = "n/a"
none = "<none>"
maxLogLines = 1000
)
// field represents a field in a widget consist of a name and a value, rendered next to each other.
type field struct {
Name string
Value string
}
func (f *field) render(nameWidth int) string {
return fmt.Sprintf("[::b]%s[::-] %s", padRight(f.Name, nameWidth), f.Value)
}
type fieldGroup struct {
fields []field
}
// String implements the Stringer interface.
func (fg *fieldGroup) String() string {
width := fg.maxFieldNameLength()
return strings.Join(
xslices.Map(fg.fields, func(t field) string {
return t.render(width)
}),
"\n",
)
}
func (fg *fieldGroup) maxFieldNameLength() int {
max := 0
for _, f := range fg.fields {
if len(f.Name) > max {
max = len(f.Name)
}
}
return max
}
// padRight pads a string to the specified width by appending spaces to the end.
func padRight(s string, width int) string {
return fmt.Sprintf("%-*s", width, s)
}
func toHealthStatus(healthy bool) string {
if healthy {
return formatStatus("Healthy")
}
return formatStatus("Unhealthy")
}
func formatStatus(status any) string {
statusStr := capitalizeFirst(fmt.Sprintf("%v", status))
switch strings.ToLower(statusStr) {
case "running", "healthy", "true":
return formatText(statusStr, true)
case "stopped", "unhealthy", "false":
return formatText(statusStr, false)
default:
return statusStr
}
}
func formatText(text string, ok bool) string {
if text == "" {
return ""
}
if ok {
return fmt.Sprintf("[green]√ %s[-]", text)
}
return fmt.Sprintf("[red]× %s[-]", text)
}
// capitalizeFirst capitalizes the first character of string.
func capitalizeFirst(s string) string {
if s == "" {
return s
}
return strings.ToUpper(string(s[0])) + strings.ToLower(s[1:])
}