-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show plugin activation status (#1256)
* Show plugin activation status
- Loading branch information
Josef Karasek
authored
Sep 18, 2023
1 parent
1a9c02b
commit 03a50c9
Showing
13 changed files
with
149 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package plugin | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
const ( | ||
pluginRunning = "Running" | ||
pluginDeactivated = "Deactivated" | ||
) | ||
|
||
// HealthStats holds information about plugin health and restarts. | ||
type HealthStats struct { | ||
sync.RWMutex | ||
pluginStats map[string]pluginStats | ||
globalRestartThreshold int | ||
} | ||
|
||
type pluginStats struct { | ||
restartCount int | ||
restartThreshold int | ||
lastTransitionTime string | ||
} | ||
|
||
// NewHealthStats returns a new HealthStats instance. | ||
func NewHealthStats(threshold int) *HealthStats { | ||
return &HealthStats{ | ||
pluginStats: map[string]pluginStats{}, | ||
globalRestartThreshold: threshold, | ||
} | ||
} | ||
|
||
// Increment increments restart count for a plugin. | ||
func (h *HealthStats) Increment(plugin string) { | ||
h.Lock() | ||
defer h.Unlock() | ||
if _, ok := h.pluginStats[plugin]; !ok { | ||
h.pluginStats[plugin] = pluginStats{} | ||
} | ||
h.pluginStats[plugin] = pluginStats{ | ||
restartCount: h.pluginStats[plugin].restartCount + 1, | ||
lastTransitionTime: time.Now().Format(time.RFC3339), | ||
restartThreshold: h.globalRestartThreshold, | ||
} | ||
} | ||
|
||
// GetRestartCount returns restart count for a plugin. | ||
func (h *HealthStats) GetRestartCount(plugin string) int { | ||
h.RLock() | ||
defer h.RUnlock() | ||
if _, ok := h.pluginStats[plugin]; !ok { | ||
return 0 | ||
} | ||
return h.pluginStats[plugin].restartCount | ||
} | ||
|
||
// GetStats returns plugin status, restart count, restart threshold and last transition time. | ||
func (h *HealthStats) GetStats(plugin string) (status string, restarts int, threshold int, timestamp string) { | ||
h.RLock() | ||
defer h.RUnlock() | ||
status = pluginRunning | ||
if _, ok := h.pluginStats[plugin]; !ok { | ||
threshold = h.globalRestartThreshold | ||
return | ||
} | ||
|
||
if h.pluginStats[plugin].restartCount > h.pluginStats[plugin].restartThreshold { | ||
status = pluginDeactivated | ||
} | ||
restarts = h.pluginStats[plugin].restartCount | ||
threshold = h.pluginStats[plugin].restartThreshold | ||
timestamp = h.pluginStats[plugin].lastTransitionTime | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
"github.com/stretchr/testify/require" | ||
|
||
"github.com/kubeshop/botkube/internal/loggerx" | ||
"github.com/kubeshop/botkube/internal/plugin" | ||
"github.com/kubeshop/botkube/pkg/config" | ||
) | ||
|
||
|
@@ -50,9 +51,9 @@ func TestExecutorBindingsExecutor(t *testing.T) { | |
}, | ||
bindings: []string{"kubectl-team-a", "kubectl-team-b"}, | ||
expOutput: heredoc.Doc(` | ||
EXECUTOR ENABLED ALIASES | ||
botkube/echo true | ||
botkube/kubectl true k, kc`), | ||
EXECUTOR ENABLED ALIASES RESTARTS STATUS LAST_RESTART | ||
botkube/echo true 0/1 Running | ||
botkube/kubectl true k, kc 0/1 Running`), | ||
}, | ||
{ | ||
name: "executors and plugins", | ||
|
@@ -91,17 +92,18 @@ func TestExecutorBindingsExecutor(t *testing.T) { | |
}, | ||
bindings: []string{"kubectl", "botkube/helm", "botkube/[email protected]"}, | ||
expOutput: heredoc.Doc(` | ||
EXECUTOR ENABLED ALIASES | ||
botkube/[email protected] true e | ||
botkube/helm true h | ||
botkube/kubectl true`), | ||
EXECUTOR ENABLED ALIASES RESTARTS STATUS LAST_RESTART | ||
botkube/[email protected] true e 0/1 Running | ||
botkube/helm true h 0/1 Running | ||
botkube/kubectl true 0/1 Running`), | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
cmdCtx := CommandContext{ | ||
ExecutorFilter: newExecutorTextFilter(""), | ||
Conversation: Conversation{ExecutorBindings: tc.bindings}, | ||
ExecutorFilter: newExecutorTextFilter(""), | ||
Conversation: Conversation{ExecutorBindings: tc.bindings}, | ||
PluginHealthStats: plugin.NewHealthStats(1), | ||
} | ||
e := NewExecExecutor(loggerx.NewNoop(), tc.cfg) | ||
msg, err := e.List(context.Background(), cmdCtx) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.