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

Unify commands #867

Merged
merged 3 commits into from
Dec 13, 2022
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
5 changes: 5 additions & 0 deletions .github/workflows/codeql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
Expand Down
57 changes: 55 additions & 2 deletions cmd/botkube/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
Expand Down Expand Up @@ -44,6 +45,7 @@ import (
"github.com/kubeshop/botkube/pkg/notifier"
"github.com/kubeshop/botkube/pkg/recommendation"
"github.com/kubeshop/botkube/pkg/sink"
"github.com/kubeshop/botkube/pkg/version"
)

const (
Expand Down Expand Up @@ -158,12 +160,19 @@ func run() error {
cmdGuard := kubectl.NewCommandGuard(logger.WithField(componentLogFieldKey, "Command Guard"), discoveryCli)
commander := kubectl.NewCommander(logger.WithField(componentLogFieldKey, "Commander"), kcMerger, cmdGuard)

runner := &execute.OSCommand{}
k8sVersion, err := findK8sVersion(runner)
if err != nil {
return reportFatalError("while fetching kubernetes version", err)
}
botkubeVersion := findBotkubeVersion(k8sVersion)

// Create executor factory
cfgManager := config.NewManager(logger.WithField(componentLogFieldKey, "Config manager"), conf.Settings.PersistentConfig, k8sCli)
executorFactory := execute.NewExecutorFactory(
executorFactory, err := execute.NewExecutorFactory(
execute.DefaultExecutorFactoryParams{
Log: logger.WithField(componentLogFieldKey, "Executor"),
CmdRunner: &execute.OSCommand{},
CmdRunner: runner,
Cfg: *conf,
FilterEngine: filterEngine,
KcChecker: kubectl.NewChecker(resourceNameNormalizerFunc),
Expand All @@ -173,9 +182,14 @@ func run() error {
NamespaceLister: k8sCli.CoreV1().Namespaces(),
CommandGuard: cmdGuard,
PluginManager: pluginManager,
BotKubeVersion: botkubeVersion,
},
)

if err != nil {
return reportFatalError("while creating executor factory", err)
}

router := source.NewRouter(mapper, dynamicCli, logger.WithField(componentLogFieldKey, "Router"))

var (
Expand Down Expand Up @@ -462,3 +476,42 @@ func sendHelp(ctx context.Context, s *storage.Help, clusterName string, notifier

return s.MarkHelpAsSent(ctx, sent)
}

func findK8sVersion(runner *execute.OSCommand) (string, error) {
mszostok marked this conversation as resolved.
Show resolved Hide resolved
type kubectlVersionOutput struct {
Server struct {
GitVersion string `json:"gitVersion"`
} `json:"serverVersion"`
}

args := []string{"-c", fmt.Sprintf("%s version --output=json", execute.KubectlBinary)}
stdout, stderr, err := runner.RunSeparateOutput("sh", args)
if err != nil {
return "", fmt.Errorf("unable to execute kubectl version: %w [%q]", err, stderr)
}

var out kubectlVersionOutput
err = json.Unmarshal([]byte(stdout), &out)
if err != nil {
return "", err
}
if out.Server.GitVersion == "" {
return "", fmt.Errorf("unable to unmarshal server git version from %q", stdout)
}

ver := out.Server.GitVersion
if stderr != "" {
ver += "\n" + stderr
}

return ver, nil
}

func findBotkubeVersion(k8sVersion string) (versions string) {
botkubeVersion := version.Short()
if len(botkubeVersion) == 0 {
botkubeVersion = "Unknown"
}

return fmt.Sprintf("K8s Server Version: %s\nBotkube version: %s", k8sVersion, botkubeVersion)
}
2 changes: 1 addition & 1 deletion global_config.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ sources:

# Filter settings for various sources.
# Currently, all filters are globally enabled or disabled.
# You can enable or disable filters with `@Botkube filters` commands.
# You can enable or disable filters with `@Botkube enable/disable filters` commands.
filters:
kubernetes:
# If true, enables support for `botkube.io/disable` and `botkube.io/channel` resource annotations.
Expand Down
4 changes: 2 additions & 2 deletions helm/botkube/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Controller for the Botkube Slack app which helps you monitor your Kubernetes clu
| [sources.k8s-create-events.kubernetes.event](./values.yaml#L336) | object | `{"types":["create"]}` | Describes event constraints for Kubernetes resources. These constraints are applied for every resource specified in the `resources` list, unless they are overridden by the resource's own `events` object. |
| [sources.k8s-create-events.kubernetes.event.types](./values.yaml#L338) | list | `["create"]` | Lists all event types to be watched. |
| [sources.k8s-create-events.kubernetes.resources](./values.yaml#L343) | list | See the `values.yaml` file for full object. | Describes the Kubernetes resources you want to watch. |
| [filters](./values.yaml#L359) | object | See the `values.yaml` file for full object. | Filter settings for various sources. Currently, all filters are globally enabled or disabled. You can enable or disable filters with `@Botkube filters` commands. |
| [filters](./values.yaml#L359) | object | See the `values.yaml` file for full object. | Filter settings for various sources. Currently, all filters are globally enabled or disabled. You can enable or disable filters with `@Botkube enable/disable filters` commands. |
| [filters.kubernetes.objectAnnotationChecker](./values.yaml#L362) | bool | `true` | If true, enables support for `botkube.io/disable` and `botkube.io/channel` resource annotations. |
| [filters.kubernetes.nodeEventsChecker](./values.yaml#L364) | bool | `true` | If true, filters out Node-related events that are not important. |
| [executors](./values.yaml#L372) | object | See the `values.yaml` file for full object. | Map of executors. Executor contains configuration for running `kubectl` commands. The property name under `executors` is an alias for a given configuration. You can define multiple executor configurations with different names. Key name is used as a binding reference. |
Expand Down Expand Up @@ -121,7 +121,7 @@ Controller for the Botkube Slack app which helps you monitor your Kubernetes clu
| [communications.default-group.teams.appID](./values.yaml#L515) | string | `"APPLICATION_ID"` | The Botkube application ID generated while registering Bot to MS Teams. |
| [communications.default-group.teams.appPassword](./values.yaml#L517) | string | `"APPLICATION_PASSWORD"` | The Botkube application password generated while registering Bot to MS Teams. |
| [communications.default-group.teams.bindings.executors](./values.yaml#L520) | list | `["kubectl-read-only"]` | Executor bindings apply to all MS Teams channels where Botkube has access to. |
| [communications.default-group.teams.bindings.sources](./values.yaml#L523) | list | `["k8s-err-events","k8s-recommendation-events"]` | Source bindings apply to all channels which have notification turned on with `@Botkube notifier start` command. |
| [communications.default-group.teams.bindings.sources](./values.yaml#L523) | list | `["k8s-err-events","k8s-recommendation-events"]` | Source bindings apply to all channels which have notification turned on with `@Botkube start notifications` command. |
| [communications.default-group.teams.messagePath](./values.yaml#L527) | string | `"/bots/teams"` | The path in endpoint URL provided while registering Botkube to MS Teams. |
| [communications.default-group.teams.port](./values.yaml#L529) | int | `3978` | The Service port for bot endpoint on Botkube container. |
| [communications.default-group.discord.enabled](./values.yaml#L534) | bool | `false` | If true, enables Discord bot. |
Expand Down
4 changes: 2 additions & 2 deletions helm/botkube/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ sources:

# -- Filter settings for various sources.
# Currently, all filters are globally enabled or disabled.
# You can enable or disable filters with `@Botkube filters` commands.
# You can enable or disable filters with `@Botkube enable/disable filters` commands.
josefkarasek marked this conversation as resolved.
Show resolved Hide resolved
# @default -- See the `values.yaml` file for full object.
filters:
kubernetes:
Expand Down Expand Up @@ -519,7 +519,7 @@ communications:
# -- Executor bindings apply to all MS Teams channels where Botkube has access to.
executors:
- kubectl-read-only
# -- Source bindings apply to all channels which have notification turned on with `@Botkube notifier start` command.
# -- Source bindings apply to all channels which have notification turned on with `@Botkube start notifications` command.
sources:
- k8s-err-events
- k8s-recommendation-events
Expand Down
4 changes: 2 additions & 2 deletions internal/analytics/segment_reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ func TestSegmentReporter_ReportCommand(t *testing.T) {
segmentReporter, segmentCli := fakeSegmentReporterWithIdentity(identity)

// when
err := segmentReporter.ReportCommand(config.DiscordCommPlatformIntegration, "notifier stop", command.TypedOrigin, false)
err := segmentReporter.ReportCommand(config.DiscordCommPlatformIntegration, "stop notifications", command.TypedOrigin, false)
require.NoError(t, err)

err = segmentReporter.ReportCommand(config.SlackCommPlatformIntegration, "get", command.ButtonClickOrigin, false)
require.NoError(t, err)

err = segmentReporter.ReportCommand(config.TeamsCommPlatformIntegration, "notifier start", command.SelectValueChangeOrigin, false)
err = segmentReporter.ReportCommand(config.TeamsCommPlatformIntegration, "start notifications", command.SelectValueChangeOrigin, false)
require.NoError(t, err)

// then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"event": "Command executed",
"timestamp": "2009-11-17T20:34:58.651387237Z",
"properties": {
"command": "notifier stop",
"command": "stop notifications",
"filtered": false,
"origin": "typed",
"platform": "discord"
Expand All @@ -32,7 +32,7 @@
"event": "Command executed",
"timestamp": "2009-11-17T20:34:58.651387237Z",
"properties": {
"command": "notifier start",
"command": "start notifications",
"filtered": false,
"origin": "selectValueChange",
"platform": "teams"
Expand Down
29 changes: 23 additions & 6 deletions pkg/bot/interactive/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (h *HelpMessage) Build() Message {
h.cluster,
h.notificationSections,
h.actionSections,
h.configSections,
h.kubectlSections,
h.filters,
h.feedback,
Expand Down Expand Up @@ -118,13 +119,13 @@ func (h *HelpMessage) notificationSections() []Section {
Base: Base{
Header: "Manage incoming notifications",
Body: Body{
CodeBlock: fmt.Sprintf("%s notifier [start|stop|status]\n", h.botName),
CodeBlock: fmt.Sprintf("%s [start|stop|status] notifications\n", h.botName),
},
},
Buttons: []Button{
h.btnBuilder.ForCommandWithoutDesc("Start notifications", "notifier start"),
h.btnBuilder.ForCommandWithoutDesc("Stop notifications", "notifier stop"),
h.btnBuilder.ForCommandWithoutDesc("Get status", "notifier status"),
h.btnBuilder.ForCommandWithoutDesc("Start notifications", "start notifications"),
h.btnBuilder.ForCommandWithoutDesc("Stop notifications", "stop notifications"),
h.btnBuilder.ForCommandWithoutDesc("Get status", "status notifications"),
},
},
{
Expand Down Expand Up @@ -155,6 +156,22 @@ func (h *HelpMessage) actionSections() []Section {
}
}

func (h *HelpMessage) configSections() []Section {
return []Section{
{
Base: Base{
Header: "View current Botkube configuration",
Body: Body{
CodeBlock: fmt.Sprintf("%s config\n", h.botName),
},
},
Buttons: []Button{
h.btnBuilder.ForCommandWithoutDesc("Display configuration", "config"),
},
},
}
}

func (h *HelpMessage) kubectlSections() []Section {
// TODO(https://github.com/kubeshop/botkube/issues/802): remove this warning in after releasing 0.17.
warn := ":warning: Botkube 0.17 and above will require a prefix (`k`, `kc`, `kubectl`) when running kubectl commands through the bot.\n\ne.g. `@Botkube k get pods` instead of `@Botkube get pods`\n"
Expand All @@ -180,7 +197,7 @@ func (h *HelpMessage) kubectlSections() []Section {
Description: "Alternatively use kubectl as usual with all supported commands",
},
Buttons: []Button{
h.btnBuilder.ForCommand("List commands", "commands list", "k | kc | kubectl [command] [options] [flags]"),
h.btnBuilder.ForCommand("List commands", "list commands", "k | kc | kubectl [command] [options] [flags]"),
},
},
}
Expand All @@ -204,7 +221,7 @@ func (h *HelpMessage) kubectlSections() []Section {
Description: "To list all supported kubectl commands",
},
Buttons: []Button{
h.btnBuilder.ForCommandWithDescCmd("List commands", "commands list"),
h.btnBuilder.ForCommandWithDescCmd("List commands", "list commands"),
},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ Check the status of connected Kubernetes cluster(s).

*Manage incoming notifications*
```
@Botkube notifier [start|stop|status]
@Botkube [start|stop|status] notifications
```
- `@Botkube notifier start`
- `@Botkube notifier stop`
- `@Botkube notifier status`
- `@Botkube start notifications`
- `@Botkube stop notifications`
- `@Botkube status notifications`

*Notification settings for this channel*
By default, Botkube will notify only about cluster errors and recommendations.
Expand All @@ -28,6 +28,12 @@ By default, Botkube will notify only about cluster errors and recommendations.
```
- `@Botkube list actions`

*View current Botkube configuration*
```
@Botkube config
```
- `@Botkube config`

*Run kubectl commands (if enabled)*
:warning: Botkube 0.17 and above will require a prefix (`k`, `kc`, `kubectl`) when running kubectl commands through the bot.

Expand All @@ -39,7 +45,7 @@ You can run kubectl commands directly from Platform!
- `@Botkube kubectl get deployments`

To list all supported kubectl commands
- `@Botkube commands list`
- `@Botkube list commands`

*Filters (advanced)*
You can extend Botkube functionality by writing additional filters that can check resource specs, validate some checks and add messages to the Event struct. Learn more at https://docs.botkube.io/filters
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
Botkube is now active for "testing" cluster :rocket:<br><br>**Using multiple instances**<br>If you are running multiple Botkube instances in the same channel to interact with testing, make sure to specify the cluster name when typing commands.<br>```
--cluster-name=testing
```<br><br>**Ping your cluster**<br>Check the status of connected Kubernetes cluster(s).<br> - `@Botkube ping`<br><br>**Manage incoming notifications**<br>```
@Botkube notifier [start|stop|status]
```<br> - `@Botkube notifier start`<br> - `@Botkube notifier stop`<br> - `@Botkube notifier status`<br><br>**Notification settings for this channel**<br>By default, Botkube will notify only about cluster errors and recommendations.<br> - `@Botkube edit SourceBindings`<br><br>**Manage automated actions**<br>```
@Botkube [start|stop|status] notifications
```<br> - `@Botkube start notifications`<br> - `@Botkube stop notifications`<br> - `@Botkube status notifications`<br><br>**Notification settings for this channel**<br>By default, Botkube will notify only about cluster errors and recommendations.<br> - `@Botkube edit SourceBindings`<br><br>**Manage automated actions**<br>```
@Botkube [list|enable|disable] action [action name]
```<br> - `@Botkube list actions`<br><br>**Run kubectl commands (if enabled)**<br>:warning: Botkube 0.17 and above will require a prefix (`k`, `kc`, `kubectl`) when running kubectl commands through the bot.
```<br> - `@Botkube list actions`<br><br>**View current Botkube configuration**<br>```
@Botkube config
```<br> - `@Botkube config`<br><br>**Run kubectl commands (if enabled)**<br>:warning: Botkube 0.17 and above will require a prefix (`k`, `kc`, `kubectl`) when running kubectl commands through the bot.

e.g. `@Botkube k get pods` instead of `@Botkube get pods`

You can run kubectl commands directly from Platform!<br> - `@Botkube kubectl get services`<br> - `@Botkube kubectl get pods`<br> - `@Botkube kubectl get deployments`<br><br>To list all supported kubectl commands<br> - `@Botkube commands list`<br><br>**Filters (advanced)**<br>You can extend Botkube functionality by writing additional filters that can check resource specs, validate some checks and add messages to the Event struct. Learn more at https://docs.botkube.io/filters<br><br>**Angry? Amazed?**<br>Give feedback: https://feedback.botkube.io<br><br>Read our docs: https://docs.botkube.io<br>Join our Slack: https://join.botkube.io<br>Follow us on Twitter: https://twitter.com/botkube_io<br>
You can run kubectl commands directly from Platform!<br> - `@Botkube kubectl get services`<br> - `@Botkube kubectl get pods`<br> - `@Botkube kubectl get deployments`<br><br>To list all supported kubectl commands<br> - `@Botkube list commands`<br><br>**Filters (advanced)**<br>You can extend Botkube functionality by writing additional filters that can check resource specs, validate some checks and add messages to the Event struct. Learn more at https://docs.botkube.io/filters<br><br>**Angry? Amazed?**<br>Give feedback: https://feedback.botkube.io<br><br>Read our docs: https://docs.botkube.io<br>Join our Slack: https://join.botkube.io<br>Follow us on Twitter: https://twitter.com/botkube_io<br>
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ Check the status of connected Kubernetes cluster(s).
- @Botkube ping

Manage incoming notifications
@Botkube notifier [start|stop|status]
@Botkube [start|stop|status] notifications

- @Botkube notifier start
- @Botkube notifier stop
- @Botkube notifier status
- @Botkube start notifications
- @Botkube stop notifications
- @Botkube status notifications

Notification settings for this channel
By default, Botkube will notify only about cluster errors and recommendations.
Expand All @@ -24,6 +24,11 @@ Manage automated actions

- @Botkube list actions

View current Botkube configuration
@Botkube config

- @Botkube config

Run kubectl commands (if enabled)
:warning: Botkube 0.17 and above will require a prefix (`k`, `kc`, `kubectl`) when running kubectl commands through the bot.

Expand All @@ -35,7 +40,7 @@ You can run kubectl commands directly from Platform!
- @Botkube kubectl get deployments

To list all supported kubectl commands
- @Botkube commands list
- @Botkube list commands

Filters (advanced)
You can extend Botkube functionality by writing additional filters that can check resource specs, validate some checks and add messages to the Event struct. Learn more at https://docs.botkube.io/filters
Expand Down
Loading