Skip to content

Commit

Permalink
plugins/plugins: WARN state and optional message
Browse files Browse the repository at this point in the history
The WARN state can be used to signal admins that a plugin is in a
potentially dangerous or degraded state. The optional message may be
used to provide context about the warning.

Fixes open-policy-agent#2932

Signed-off-by: Grant Shively <gshively@godaddy.com>
  • Loading branch information
gshively11 authored and tsandall committed Dec 10, 2020
1 parent 970f2a3 commit 7a28f26
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
11 changes: 9 additions & 2 deletions plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,17 @@ const (
// StateErr indicates that the Plugin is in an error state and should not
// be considered as functional.
StateErr State = "ERROR"

// StateWarn indicates the Plugin is operating, but in a potentially dangerous or
// degraded state. It may be used to indicate manual remediation is needed, or to
// alert admins of some other noteworthy state.
StateWarn State = "WARN"
)

// Status has a Plugin's current status plus an optional Message.
type Status struct {
State State `json:"state"`
State State `json:"state"`
Message string `json:"message,omitempty"`
}

// StatusListener defines a handler to register for status updates.
Expand Down Expand Up @@ -569,7 +575,8 @@ func (m *Manager) copyPluginStatus() map[string]*Status {
var cpy *Status
if v != nil {
cpy = &Status{
State: v.State,
State: v.State,
Message: v.Message,
}
}
statusCpy[k] = cpy
Expand Down
10 changes: 5 additions & 5 deletions plugins/plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ func TestManagerPluginStatusListener(t *testing.T) {
}

// Push an update to a plugin, ensure current status is reflected and listeners were called
m.UpdatePluginStatus("p1", &Status{State: StateOK})
m.UpdatePluginStatus("p1", &Status{State: StateOK, Message: "foo"})
currentStatus = m.PluginStatus()
if len(currentStatus) != 1 || currentStatus["p1"].State != StateOK {
t.Fatalf("Expected 1 statuses in current plugin status map with state OK, got: %+v", currentStatus)
if len(currentStatus) != 1 || currentStatus["p1"].State != StateOK || currentStatus["p1"].Message != "foo" {
t.Fatalf("Expected 1 statuses in current plugin status map with state OK and message 'foo', got: %+v", currentStatus)
}
if !reflect.DeepEqual(currentStatus, l1Status) || !reflect.DeepEqual(l1Status, l2Status) {
t.Fatalf("Unexpected status in updates:\n\n\texpecting: %+v\n\n\tgot: l1: %+v l2: %+v\n", currentStatus, l1Status, l2Status)
Expand All @@ -66,7 +66,7 @@ func TestManagerPluginStatusListener(t *testing.T) {
// Send another update, ensure the status is ok and the remaining listener is still called
m.UpdatePluginStatus("p2", &Status{State: StateErr})
currentStatus = m.PluginStatus()
if len(currentStatus) != 2 || currentStatus["p1"].State != StateOK || currentStatus["p2"].State != StateErr {
if len(currentStatus) != 2 || currentStatus["p1"].State != StateOK || currentStatus["p1"].Message != "foo" || currentStatus["p2"].State != StateErr {
t.Fatalf("Unexpected current plugin status, got: %+v", currentStatus)
}
if !reflect.DeepEqual(currentStatus, l2Status) {
Expand All @@ -82,7 +82,7 @@ func TestManagerPluginStatusListener(t *testing.T) {
// Ensure updates can still be sent with no listeners
m.UpdatePluginStatus("p2", &Status{State: StateOK})
currentStatus = m.PluginStatus()
if len(currentStatus) != 2 || currentStatus["p1"].State != StateOK || currentStatus["p2"].State != StateOK {
if len(currentStatus) != 2 || currentStatus["p1"].State != StateOK || currentStatus["p1"].Message != "foo" || currentStatus["p2"].State != StateOK {
t.Fatalf("Unexpected current plugin status, got: %+v", currentStatus)
}
}
Expand Down

0 comments on commit 7a28f26

Please sign in to comment.