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

Kubernetes source plugin #985

Merged
merged 50 commits into from
Mar 2, 2023

Conversation

huseyinbabal
Copy link
Contributor

@huseyinbabal huseyinbabal commented Feb 17, 2023

⚠️ Kubernetes plugin extraction was a huge task, but we couldn't figured it out in the beginning. So, you see a failing tests (2 of them only), but I intentionally left them as is since I have a couple of followup tasks for this PR. You can see them all here

Description

Changes proposed in this pull request:

  • Kubernetes plugin is implemented
  • Filters are removed from core configuration and moved to kubernetes plugin
  • Actions are managed in core, not in kubernetes source anymore
  • Recommendations are part of kubernetes plugin, they are removed from core
  • Please see followup tasks here

Testing

Build Kubernetes Plugin

Following command will build kubernetes plugin

PLUGIN_DOWNLOAD_URL_BASE_PATH=http://localhost:8080/plugin-dist make gen-plugins-index

Run Plugin Server

npx serve --listen 8080

Run Botkube With the Following Configurations

communications:
  'default-group':
    # Settings for Slack
     socketSlack:
       enabled: true
       appToken: "xapp-..."
       botToken: "xoxb-..."
      notification:
        type: "long" # TODO: Change to `short` during testing
      channels:
        'default':
          name: botkube-demo
          bindings:
            sources:
              # - plugin-based
              # - k8s-events
              - k8s-annotated-cm-delete
              - k8s-updates
              # - k8s-pod-create-events
                #              - plugin-based
            executors:
              - kubectl-plugin
        'secondary':
          name: botkube-demo-secondary
          bindings:
            sources:
              # - plugin-based
              - k8s-events
              # - k8s-annotated-cm-delete
              # - k8s-pod-create-events
                #              - plugin-based
            executors:
              - kubectl-plugin

settings:
  clusterName: gke-playground
  configWatcher: true
  lifecycleServer:
    deployment:
      name: botkube
      namespace: botkube
    port: "2113"

configWatcher:
  enabled: false
  initialSyncTimeout: 0

sources:
  'k8s-events':
    displayName: "K8s recommendations"
    'botkube/kubernetes':
      enabled: true
      config:
        kubeConfig: /Users/huseyin/.kube/config
        log:
          level: debug
        recommendations:
          pod:
            noLatestImageTag: true
            labelsSet: true
          ingress:
            backendServiceValid: false
            tlsSecretValid: false
        namespaces:
          include:
            - botkube
        event:
          types:
            - create
            - update
        resources:
          - type: v1/configmaps
  'k8s-annotated-cm-delete':
    displayName: "K8s ConfigMap delete events"
    'botkube/kubernetes':
      enabled: true
      config:
        kubeConfig: /Users/huseyin/.kube/config
        log:
          level: debug
        namespaces:
          include:
            - botkube
        # labels:
        #   test.botkube.io: "true"
        event:
          types:
            - delete
        resources:
          - type: v1/configmaps

  'k8s-pod-create-events':
    'botkube/kubernetes':
      enabled: true
      config:
        log:
          level: debug
        kubeConfig: /Users/huseyin/.kube/config
        namespaces:
          include:
            - botkube
        event:
          types:
            - create
        resources:
          - type: v1/pods

  'k8s-updates':
    displayName: "K8s ConfigMaps updates"
    'botkube/kubernetes':
      enabled: true
      config:
        log:
          level: debug
        kubeConfig: /Users/huseyin/.kube/config
        namespaces:
          include:
            - default
        event:
          types:
            - create
            - update
            - delete
        resources:
          - type: v1/configmaps
            namespaces:
              include:
                - botkube
            event: # overrides top level `event` entry
              types:
                - update
            updateSetting:
              includeDiff: true
              fields:
                - data
executors:
  'kubectl-plugin':
    ## Kubectl executor configuration.
    botkube/kubectl:
      enabled: true
      # namespaces:
      #   # -- List of allowed Kubernetes Namespaces for command execution.
      #   # It can also contain a regex expressions:
      #   #  `- ".*"` - to specify all Namespaces.
      #   include:
      #     - ".*"
      #   # -- List of ignored Kubernetes Namespace.
      #   # It can also contain a regex expressions:
      #   #  `- "test-.*"` - to specify all Namespaces.
      #   exclude: []
      # # -- If true, enables `kubectl` commands execution.
      # enabled: true
      # ## List of allowed `kubectl` commands.
      # commands:
      #   # -- Configures which `kubectl` methods are allowed.
      #   verbs: ["api-resources", "api-versions", "cluster-info", "describe", "explain", "get", "logs", "top"]
      #   # -- Configures which K8s resource are allowed.
      #   resources: ["deployments", "pods", "namespaces", "daemonsets", "statefulsets", "storageclasses", "nodes", "configmaps", "services", "ingresses"]
      # # -- Configures the default Namespace for executing Botkube `kubectl` commands. If not set, uses the 'default'.
      # defaultNamespace: default
      # # -- If true, enables commands execution from configured channel only.
      # restrictAccess: false

actions:
  'describe-created-resource':
    # -- If true, enables the action.
    enabled: false
    # -- Action display name posted in the channels bound to the same source bindings.
    displayName: "Describe created resource"
    # -- Command to execute when the action is triggered. You can use Go template (https://pkg.go.dev/text/template) together with all helper functions defined by Slim-Sprig library (https://go-task.github.io/slim-sprig).
    # You can use the `{{ .Event }}` variable, which contains the event object that triggered the action. See all available event properties on https://github.com/kubeshop/botkube/blob/main/pkg/event/event.go.
    # @default -- See the `values.yaml` file for the command in the Go template form.
    command: "kubectl describe {{ .Event.Kind | lower }}{{ if .Event.Namespace }} -n {{ .Event.Namespace }}{{ end }} {{ .Event.Name }}"

    # -- Bindings for a given action.
    bindings:
      # -- Event sources that trigger a given action.
      sources:
        #- plugin-based
      # -- Executors configuration used to execute a configured command.
      executors:
        - kubectl-plugin
plugins:
  repositories:
    botkube:
      url: http://localhost:8080/plugins-index.yaml

### To See Notifications in Action

```shell
kubectl run s9 --image=nginx:latest -n botkube
kubectl create configmap my-config-def --from-literal=key1=config1 -n botkube

and then update configmap to see diff is shown in slack

Related issue(s)

Relates #840

@huseyinbabal huseyinbabal added breaking Contains breaking change enhancement New feature or request labels Feb 21, 2023
@huseyinbabal huseyinbabal marked this pull request as ready for review February 27, 2023 11:31
@huseyinbabal huseyinbabal requested review from a team and PrasadG193 as code owners February 27, 2023 11:31
@mszostok mszostok self-assigned this Feb 27, 2023
Copy link
Contributor

@mszostok mszostok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still need to check 5 more files:

  • internal/source/dispatcher.go
  • internal/source/kubernetes/registration.go
  • internal/source/kubernetes/router.go
  • internal/source/kubernetes/source.go
  • internal/source/scheduler.go

And run some manual tests.

cmd/botkube/main.go Outdated Show resolved Hide resolved
cmd/botkube/main.go Outdated Show resolved Hide resolved
cmd/executor/gh/main.go Outdated Show resolved Hide resolved
internal/analytics/reporter.go Outdated Show resolved Hide resolved
internal/source/kubernetes/commander/commander.go Outdated Show resolved Hide resolved
internal/source/kubernetes/k8sutil/resource.go Outdated Show resolved Hide resolved
pkg/api/executor/grpc_adapter.go Outdated Show resolved Hide resolved
pkg/api/source/grpc_adapter.go Show resolved Hide resolved
pkg/event/event.go Show resolved Hide resolved
pkg/k8sutil/resource.go Outdated Show resolved Hide resolved
@mszostok
Copy link
Contributor

You need to move filters under k8s source:

# -- 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.
# @default -- See the `values.yaml` file for full object.
filters:
kubernetes:
# -- If true, enables support for `botkube.io/disable` and `botkube.io/channel` resource annotations.
objectAnnotationChecker: true
# -- If true, filters out Node-related events that are not important.
nodeEventsChecker: true

@mszostok
Copy link
Contributor

As a part of this PR, please also document all breaking under: #972. Thanks! 🙇

internal/source/kubernetes/source.go Outdated Show resolved Hide resolved
internal/source/kubernetes/source.go Show resolved Hide resolved
internal/source/kubernetes/source.go Outdated Show resolved Hide resolved
internal/source/kubernetes/source.go Outdated Show resolved Hide resolved
Copy link
Member

@pkosiec pkosiec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Looks like the basic functionality works OK, I tested a few configurations and they worked well. Telemetry also OK 👍

I agree with all @mszostok comments, and here are just a few ones from my side 🙂

cmd/source/kubernetes/main.go Show resolved Hide resolved
cmd/source/kubernetes/main.go Show resolved Hide resolved
cmd/source/kubernetes/main.go Show resolved Hide resolved
internal/source/kubernetes/config/config.go Outdated Show resolved Hide resolved
internal/source/kubernetes/config/config.go Outdated Show resolved Hide resolved
helm/botkube/values.yaml Show resolved Hide resolved
Copy link
Contributor

@mszostok mszostok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall good work 🚀

Once all comments will be resolved I will play with it locally as Paweł did.
Please also track all work that you would like to do in the follow-ups. Thanks!

internal/source/scheduler.go Outdated Show resolved Hide resolved
pkg/api/source/grpc_adapter.go Show resolved Hide resolved
internal/source/kubernetes/source.go Outdated Show resolved Hide resolved
internal/source/kubernetes/source.go Outdated Show resolved Hide resolved
internal/source/kubernetes/source.go Outdated Show resolved Hide resolved
@huseyinbabal
Copy link
Contributor Author

LGTM with minor comments 🚀

Overall good work 🚀

Once all comments will be resolved I will play with it locally as Paweł did. Please also track all work that you would like to do in the follow-ups. Thanks!

I addressed all the feedbacks.

types:
- create
- delete
- error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: after this merging this PR we won't merge configuration anymore. As a result, enabling both k8s-err-events and 'k8s-all-events' results in duplicated error messages. Probably will be good to mention that in the release notes/docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will be stating that in both release docs and main doc

Copy link
Contributor

@mszostok mszostok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 but before merge please address my last comments, and this one too: #985 (comment)

Also please wait for @pkosiec feedback and approve. Thanks!

cmd/botkube/main.go Outdated Show resolved Hide resolved
internal/source/kubernetes/source.go Outdated Show resolved Hide resolved
pkg/execute/factory.go Outdated Show resolved Hide resolved
pkg/filterengine/filterengine.go Show resolved Hide resolved
Copy link
Member

@pkosiec pkosiec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Awesome work!

@huseyinbabal
Copy link
Contributor Author

Addressed all the feedbacks, will check locally and focus on docs changes.

@huseyinbabal huseyinbabal merged commit 3e1d8a9 into kubeshop:main Mar 2, 2023
@huseyinbabal huseyinbabal deleted the kubernetes-source-plugin branch March 2, 2023 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Contains breaking change enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants