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

Doctor plugin & Kubernetes plugin usage #1106

Merged
merged 12 commits into from
Jul 7, 2023

Conversation

huseyinbabal
Copy link
Contributor

@huseyinbabal huseyinbabal commented Jun 26, 2023

Description

Changes proposed in this pull request:

  • Added doctor plugin that can provide possible actions for errors
  • Kubernetes & doctor plugin integration is added

Testing

  • Checkout and build plugin
PLUGIN_DOWNLOAD_URL_BASE_PATH="http://localhost:8080/plugin-dist" make gen-plugins-index
  • Serve plugins
npx serve --listen 8080
  • Run botkube withfollowing config
rbac:
  create: true
  rules:
    - apiGroups: ["*"]
      resources: ["*"]
      verbs: ["get", "watch", "list"]
  staticGroupName: &static-group-name "botkube-plugins-default"
communications:
  default-group:
    socketSlack:
      enabled: true
      channels:
        default:
          name: botkube-demo
          bindings:
            sources:
              - 'k8s-all-err-events'
              - 'k8s-recommendation-events'
              - 'k8s-all-events'
            executors:
              - ai
              - k8s-default-tools
      appToken: "xapp-1-"
      botToken: "xoxb-"

sources:
  'k8s-recommendation-events':
    displayName: "Kubernetes Recommendations"
    botkube/kubernetes:
      context: &default-plugin-context
        defaultNamespace: "default"
        rbac:
          group:
            type: Static
            prefix: ""
            static:
              values: [*static-group-name]
      enabled: true
      config:
        log:
          level: debug
        namespaces:
          include:
            - ".*"
        recommendations:
          pod:
            noLatestImageTag: true
            labelsSet: true
          ingress:
            backendServiceValid: true
            tlsSecretValid: true
  'k8s-all-events':
    displayName: "Kubernetes Info"
    botkube/kubernetes:
      context: *default-plugin-context
      enabled: true
      config:
        log:
          level: debug
        filters:
          objectAnnotationChecker: true
          nodeEventsChecker: true
        namespaces:
          include:
            - ".*"
        event:
          types:
            - error
          reason:
            include: []
            exclude: []
          message:
            include: []
            exclude: []

        annotations: {}
        labels: {}

        resources:
          - type: v1/pods
            namespaces:
              include:
                - ".*"
              exclude:
                - some_namespace
            event:
              types:
                - error
          - type: v1/services
            namespaces:
              include:
                - ".*"
              exclude:
                - some_namespace
          - type: networking.k8s.io/v1/ingresses
            namespaces:
              include:
                - ".*"
              exclude:
                - some_namespace
          - type: v1/nodes
            namespaces:
              include:
                - ".*"
              exclude:
                - some_namespace
          - type: v1/namespaces
          - type: v1/persistentvolumes
            namespaces:
              include:
                - ".*"
              exclude:
                - some_namespace
          - type: v1/persistentvolumeclaims
            namespaces:
              include:
                - ".*"
              exclude:
                - some_namespace
          - type: v1/configmaps
            event:
              types:
                - create
                - error
                - delete
            namespaces:
              include:
                - ".*"
              exclude:
                - some_namespace
          - type: rbac.authorization.k8s.io/v1/roles
            namespaces:
              include:
                - ".*"
              exclude:
                - some_namespace
          - type: rbac.authorization.k8s.io/v1/rolebindings
            namespaces:
              include:
                - ".*"
              exclude:
                - some_namespace
          - type: rbac.authorization.k8s.io/v1/clusterrolebindings
            namespaces:
              include:
                - ".*"
              exclude:
                - some_namespace
          - type: rbac.authorization.k8s.io/v1/clusterroles
            namespaces:
              include:
                - ".*"
              exclude:
                - some_namespace
          - type: apps/v1/daemonsets
            namespaces:
              include:
                - ".*"
              exclude:
                - some_namespace
            event:
              types:
                - create
                - update
                - delete
                - error
            updateSetting:
              includeDiff: true
              fields:
                - spec.template.spec.containers[*].image
                - status.numberReady
          - type: batch/v1/jobs
            namespaces:
              include:
                - ".*"
              exclude:
                - some_namespace
            event:
              types:
                - error
            updateSetting:
              includeDiff: true
              fields:
                - spec.template.spec.containers[*].image
                - status.conditions[*].type
          - type: apps/v1/deployments
            namespaces:
              include:
                - ".*"
              exclude:
                - some_namespace
            event:
              types:
                - error
            updateSetting:
              includeDiff: true
              fields:
                - spec.template.spec.containers[*].image
                - status.availableReplicas
          - type: apps/v1/statefulsets
            namespaces:
              include:
                - ".*"
              exclude:
                - some_namespace
            event:
              types:
                - create
                - update
                - delete
                - error
            updateSetting:
              includeDiff: true
              fields:
                - spec.template.spec.containers[*].image
                - spec.replicas
  'k8s-all-err-events':
    displayName: "Kubernetes Errors"
    botkube/kubernetes:
      context:
        defaultNamespace: "default"
        rbac:
          group:
            type: Static
            static:
              values: [ "botkube-plugins-default" ]
      enabled: true
      config:
        log:
          level: debug
        extraButtons:
          - enabled: true
            trigger:
              type: ["error"]
            button:
              displayName: "Get Help"
              commandTpl: "doctor --resource={{ .Kind | lower }}/{{ .Name }} --namespace={{ .Namespace }} --error={{ .Reason }} --bk-cmd-header='AI assistance'"
        namespaces:
          include:
            - ".*"
        event:
          types:
            - error
        resources:
          - type: v1/pods
          - type: v1/services
          - type: v1/nodes
          - type: v1/namespaces
          - type: v1/persistentvolumes
          - type: v1/persistentvolumeclaims
          - type: v1/configmaps
          - type: rbac.authorization.k8s.io/v1/roles
          - type: rbac.authorization.k8s.io/v1/rolebindings
          - type: rbac.authorization.k8s.io/v1/clusterrolebindings
          - type: rbac.authorization.k8s.io/v1/clusterroles
          - type: apps/v1/deployments
          - type: apps/v1/statefulsets
          - type: apps/v1/daemonsets
          - type: batch/v1/jobs

executors:
  k8s-default-tools:
    botkube/kubectl:
      enabled: true
      context: *default-plugin-context
  ai:
    botkube/doctor:
      enabled: true
      config:
        apiKey: "sk-" # Get it here https://platform.openai.com/account/api-keys

plugins:
  cacheDir: "/tmp/plugins"

  repositories:
    botkube:
      url: http://localhost:8080/plugins-dev-index.yaml

settings:
  kubeconfig: /Users/huseyin/.kube/config
  log:
    level: "debug"
  clusterName: "k3d-botkube"
  upgradeNotifier: false

analytics:
  disable: true

configWatcher:
  enabled: false
  • Run kubectl run nginx --image=nginx2 to see error event in slack
  • Click "Get Help" button to see result.

Related issue(s)

#1070

@huseyinbabal huseyinbabal requested review from a team and PrasadG193 as code owners June 26, 2023 14:46
@huseyinbabal huseyinbabal linked an issue Jun 26, 2023 that may be closed by this pull request
5 tasks
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.

Please fix the reported issuse in this PR and the rest issues in the follow-up PR. Thanks!

Follow-up PR:

  • add support for --bk-cmd-header='AI assistance' in botkube core
  • add support for rendering inline buttons and dropdowns in the same line
    • in general if all buttons in a given section doesn't have description then they are printed in a single line. In this case we should also handle the dropdown and print dropdowns also in the line with buttons.

both things were handled on the hackathon PR but there require some polishing: https://github.com/kubeshop/botkube/pull/1066/files

internal/executor/doctor/executor.go Outdated Show resolved Hide resolved
internal/executor/doctor/executor.go Outdated Show resolved Hide resolved
internal/executor/doctor/executor.go Outdated Show resolved Hide resolved
internal/executor/doctor/executor.go Show resolved Hide resolved
internal/executor/doctor/executor.go Outdated Show resolved Hide resolved
internal/executor/doctor/executor.go Show resolved Hide resolved
internal/executor/doctor/executor.go Outdated Show resolved Hide resolved
internal/executor/doctor/executor.go Outdated Show resolved Hide resolved
internal/source/kubernetes/msg.go Outdated Show resolved Hide resolved
@huseyinbabal
Copy link
Contributor Author

Please fix the reported issuse in this PR and the rest issues in the follow-up PR. Thanks!

Follow-up PR:

  • add support for --bk-cmd-header='AI assistance' in botkube core

  • add support for rendering inline buttons and dropdowns in the same line

    • in general if all buttons in a given section doesn't have description then they are printed in a single line. In this case we should also handle the dropdown and print dropdowns also in the line with buttons.

both things were handled on the hackathon PR but there require some polishing: https://github.com/kubeshop/botkube/pull/1066/files

@mszostok I tried to handle them all in this PR, I already included AI assistance partially, so just wanted to wrap up them all. Let's sync tomorrow to visit it once more.

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.

currently the kubectl builder doesn't work, the renderer logic needs to be adjusted 👍

}
doctorParams, err := normalizeCommand(in.Command)
if err != nil {
return executor.ExecuteOutput{}, fmt.Errorf("while nomralizing command: %w", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return executor.ExecuteOutput{}, fmt.Errorf("while nomralizing command: %w", err)
return executor.ExecuteOutput{}, fmt.Errorf("while normalizing command: %w", err)

@@ -140,3 +149,46 @@ func regexFlag(flag, escapedParamVal string) (*regexp.Regexp, error) {
escapedParamVal,
escapedParamVal))
}

func extractHeaderParam(cmd string) (string, string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

we already have generic extractParam please use it instead of this 👍

return []slack.Block{
// We use actions layout as we have only buttons that we want to display in a single line.
// https://api.slack.com/reference/block-kit/blocks#actions
slack.NewActionBlock(
"",
btns...,
selects.BlockID,
Copy link
Contributor

@mszostok mszostok Jul 5, 2023

Choose a reason for hiding this comment

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

this might be nil and we get a panic, I tried to use kubectl builder and it doesn't work.

also line with extracting the slice of elements causes panic

@huseyinbabal
Copy link
Contributor Author

@mszostok I have applied suggestions and tested accordingly.

Screenshot 2023-07-06 at 16 59 52
Screenshot 2023-07-06 at 17 00 14

Screenshot 2023-07-06 at 16 59 48

Comment on lines 21 to 22
multipleFilters = "incorrect use of --filter flag: found more than one filter flag"
missingCmdFilterValue = `incorrect use of --filter flag: an argument is missing. use --filter="value" or --filter value`
Copy link
Contributor

Choose a reason for hiding this comment

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

please remove :)

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.

awesome work 🚀

@huseyinbabal huseyinbabal merged commit be710e4 into kubeshop:main Jul 7, 2023
13 checks passed
@huseyinbabal huseyinbabal deleted the doctor-plugin branch July 7, 2023 08:38
@pkosiec pkosiec added the enhancement New feature or request label Jul 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ChatGPT Executor Plugin
3 participants