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

[v15] Display remaining alert ttl #43435

Merged
merged 5 commits into from
Jun 25, 2024
Merged
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: 9 additions & 0 deletions docs/pages/reference/cli/tctl.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,15 @@ Lists cluster alerts. This command can also be invoked as `tctl alerts ls`.
| `--verbose` (`-v`) | false |boolean | If set, display detailed alert info (including acknowledged alerts) |
| `--labels` | none | Comma-separated strings | A list of labels to filter by |

### Examples

```code
$ tctl alerts list
ID Severity Expires In Message
------------------------------------ -------- ---------- ----------------------------------------------------------------
da36b401-5688-426f-95b8-d0dd1ef27785 LOW 57m0s "The system is under maintenance, functionality may be limited."
```

## tctl auth export

Exports public cluster CA certificates. This is useful for configuring
Expand Down
25 changes: 22 additions & 3 deletions tool/tctl/common/alert_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (c *AlertCommand) List(ctx context.Context, client *authclient.Client) erro

func displayAlertsText(alerts []types.ClusterAlert, verbose bool) {
if verbose {
table := asciitable.MakeTable([]string{"ID", "Severity", "Message", "Created", "Labels"})
table := asciitable.MakeTable([]string{"ID", "Severity", "Expires In", "Message", "Created", "Labels"})
for _, alert := range alerts {
var labelPairs []string
for key, val := range alert.Metadata.Labels {
Expand All @@ -216,21 +216,40 @@ func displayAlertsText(alerts []types.ClusterAlert, verbose bool) {
table.AddRow([]string{
alert.GetName(),
alert.Spec.Severity.String(),
calculateTTL(alert.GetMetadata().Expires).String(),
fmt.Sprintf("%q", alert.Spec.Message),
alert.Spec.Created.Format(time.RFC822),
strings.Join(labelPairs, ", "),
})
}
fmt.Println(table.AsBuffer().String())
} else {
table := asciitable.MakeTable([]string{"ID", "Severity", "Message"})
table := asciitable.MakeTable([]string{"ID", "Severity", "Expires In", "Message"})
for _, alert := range alerts {
table.AddRow([]string{alert.GetName(), alert.Spec.Severity.String(), fmt.Sprintf("%q", alert.Spec.Message)})
table.AddRow([]string{
alert.GetName(),
alert.Spec.Severity.String(),
calculateTTL(alert.GetMetadata().Expires).String(),
fmt.Sprintf("%q", alert.Spec.Message),
})
}
fmt.Println(table.AsBuffer().String())
}
}

// calculateTTL returns the remaining TTL of the alert.
func calculateTTL(expiration *time.Time) time.Duration {
if expiration == nil {
return time.Duration(0)
}
remainingDuration := time.Until(*expiration)
if remainingDuration < 0 {
return time.Duration(0)
}

return remainingDuration.Round(time.Minute)
}

func displayAlertsJSON(alerts []types.ClusterAlert) error {
out, err := json.MarshalIndent(alerts, "", " ")
if err != nil {
Expand Down
Loading