Skip to content

Commit

Permalink
remove alert maximums (#28967) (#28983)
Browse files Browse the repository at this point in the history
  • Loading branch information
fspmarshall committed Jul 12, 2023
1 parent c9a18f3 commit a2a7c50
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 23 deletions.
10 changes: 4 additions & 6 deletions tool/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ type ClusterAlertGetter interface {
GetClusterAlerts(ctx context.Context, query types.GetClusterAlertsRequest) ([]types.ClusterAlert, error)
}

// ShowClusterAlerts shows cluster alerts with the given labels and severity.
func ShowClusterAlerts(ctx context.Context, client ClusterAlertGetter, w io.Writer, labels map[string]string, minSeverity, maxSeverity types.AlertSeverity) error {
// ShowClusterAlerts shows cluster alerts matching the given labels and minimum severity.
func ShowClusterAlerts(ctx context.Context, client ClusterAlertGetter, w io.Writer, labels map[string]string, severity types.AlertSeverity) error {
// get any "on login" alerts
alerts, err := client.GetClusterAlerts(ctx, types.GetClusterAlertsRequest{
Labels: labels,
Severity: minSeverity,
Severity: severity,
})
if err != nil && !trace.IsNotImplemented(err) {
return trace.Wrap(err)
Expand All @@ -133,9 +133,7 @@ func ShowClusterAlerts(ctx context.Context, client ClusterAlertGetter, w io.Writ
errs = append(errs, trace.Errorf("invalid alert %q: %w", alert.Metadata.Name, err))
continue
}
if alert.Spec.Severity <= maxSeverity {
fmt.Fprintf(w, "%s\n\n", utils.FormatAlert(alert))
}
fmt.Fprintf(w, "%s\n\n", utils.FormatAlert(alert))
}
return trace.NewAggregate(errs...)
}
12 changes: 6 additions & 6 deletions tool/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestShowClusterAlerts(t *testing.T) {
alerts []types.ClusterAlert
wantOut string
}{
"No filtered severities": {
"Single message": {
alerts: []types.ClusterAlert{
{
Spec: types.ClusterAlertSpec{
Expand All @@ -54,7 +54,7 @@ func TestShowClusterAlerts(t *testing.T) {
},
wantOut: "\x1b[33msomeMessage\x1b[0m\n\n",
},
"Filtered severities": {
"Sorted messages": {
alerts: []types.ClusterAlert{
{
ResourceHeader: types.ResourceHeader{
Expand All @@ -65,26 +65,26 @@ func TestShowClusterAlerts(t *testing.T) {
},
},
Spec: types.ClusterAlertSpec{
Severity: types.AlertSeverity_HIGH,
Severity: types.AlertSeverity_MEDIUM,
Message: "someOtherMessage",
},
}, {
Spec: types.ClusterAlertSpec{
Severity: types.AlertSeverity_MEDIUM,
Severity: types.AlertSeverity_HIGH,
Message: "someMessage",
},
},
},

wantOut: "\x1b[33msomeMessage\x1b[0m\n\n",
wantOut: "\x1b[31msomeMessage\x1b[0m\n\n\x1b[33msomeOtherMessage\x1b[0m\n\n",
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
alertGetter := mockAlertGetter(test.alerts)
var got bytes.Buffer
err := ShowClusterAlerts(context.Background(), alertGetter, &got, nil, types.AlertSeverity_LOW, types.AlertSeverity_MEDIUM)
err := ShowClusterAlerts(context.Background(), alertGetter, &got, nil, types.AlertSeverity_LOW)
require.NoError(t, err)
require.Equal(t, test.wantOut, got.String())
})
Expand Down
2 changes: 1 addition & 1 deletion tool/tctl/common/tctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func TryRun(commands []CLICommand, args []string) error {
ctx, cancel := context.WithTimeout(ctx, constants.TimeoutGetClusterAlerts)
defer cancel()
if err := common.ShowClusterAlerts(ctx, client, os.Stderr, nil,
types.AlertSeverity_HIGH, types.AlertSeverity_HIGH); err != nil {
types.AlertSeverity_HIGH); err != nil {
log.WithError(err).Warn("Failed to display cluster alerts.")
}

Expand Down
12 changes: 2 additions & 10 deletions tool/tsh/tsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -1897,17 +1897,9 @@ func onLogin(cf *CLIConf) error {
return trace.Wrap(err)
}

// Show on-login alerts, all high severity alerts are shown by onStatus
// so can be excluded here, except when Hardware Key Touch is required
// which skips on-status alerts.
alertSeverityMax := types.AlertSeverity_MEDIUM
if tc.PrivateKeyPolicy == keys.PrivateKeyPolicyHardwareKeyTouch {
alertSeverityMax = types.AlertSeverity_HIGH
}

if err := common.ShowClusterAlerts(cf.Context, tc, os.Stderr, map[string]string{
types.AlertOnLogin: "yes",
}, types.AlertSeverity_LOW, alertSeverityMax); err != nil {
}, types.AlertSeverity_LOW); err != nil {
log.WithError(err).Warn("Failed to display cluster alerts.")
}

Expand Down Expand Up @@ -4060,7 +4052,7 @@ func onStatus(cf *CLIConf) error {
log.Debug("Skipping cluster alerts due to Hardware Key Touch requirement.")
} else {
if err := common.ShowClusterAlerts(cf.Context, tc, os.Stderr, nil,
types.AlertSeverity_HIGH, types.AlertSeverity_HIGH); err != nil {
types.AlertSeverity_HIGH); err != nil {
log.WithError(err).Warn("Failed to display cluster alerts.")
}
}
Expand Down

0 comments on commit a2a7c50

Please sign in to comment.