remove alert maximums (#28967)

This commit is contained in:
Forrest 2023-07-11 14:15:32 -07:00 committed by GitHub
parent fa785847ec
commit ae42a6e0d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 23 deletions

View file

@ -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)
@ -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...)
}

View file

@ -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{
@ -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{
@ -65,18 +65,18 @@ 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",
},
}
@ -84,7 +84,7 @@ func TestShowClusterAlerts(t *testing.T) {
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())
})

View file

@ -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.")
}

View file

@ -1927,21 +1927,13 @@ 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
}
// NOTE: we currently print all alerts that are marked as `on-login`, because we
// don't use the alert API very heavily. If we start to make more use of it, we
// could probably add a separate `tsh alerts ls` command, and truncate the list
// with a message like "run 'tsh alerts ls' to view N additional alerts".
if err := common.ShowClusterAlerts(cf.Context, proxyClient.CurrentCluster(), 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.")
}
@ -4088,7 +4080,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.")
}
}