Skip to content

Commit

Permalink
Remove built-in kubectl, filters, defaultNamespace, fix list bugs, up…
Browse files Browse the repository at this point in the history
…date contributing (#1026)
  • Loading branch information
mszostok authored Mar 29, 2023
1 parent 5c209b9 commit fb83664
Show file tree
Hide file tree
Showing 56 changed files with 536 additions and 4,881 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tags
/analytics.yaml
/resource_config.yaml
/comm_config.yaml
/local_config.yaml

/bin

Expand Down
39 changes: 28 additions & 11 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,36 @@ For faster development, you can also build and run Botkube outside K8s cluster.
go build ./cmd/botkube/
```

2. Use templates to create configuration files:

```sh
cp global_config.yaml.tpl resource_config.yaml
cp comm_config.yaml.tpl comm_config.yaml
2. Create a local configuration file to override default values. For example, set communication credentials, specify cluster name, and disable analytics:

```yaml
cat <<EOF > local_config.yaml
communications:
default-group:
socketSlack:
enabled: true
channels:
default:
name: random
appToken: "xapp-xxxx"
botToken: "xoxb-xxxx"
configWatcher:
enabled: false
settings:
clusterName: "labs"
analytics:
# -- If true, sending anonymous analytics is disabled. To learn what date we collect,
# see [Privacy Policy](https://botkube.io/privacy#privacy-policy).
disable: true
EOF
```
Edit the newly created `resource_config.yaml` and `comm_config.yaml` files to configure resource and set communication credentials.

3. Export the path to directory of `config.yaml`
To learn more about configuration, visit https://docs.botkube.io/configuration/.
3. Export paths to configuration files. The priority will be given to the last (right-most) file specified.
```sh
export BOTKUBE_CONFIG_PATHS="$(pwd)/resource_config.yaml,$(pwd)/comm_config.yaml"
export BOTKUBE_CONFIG_PATHS="$(pwd)/helm/botkube/values.yaml,$(pwd)/local_config.yaml"
```
4. Export the path to Kubeconfig:
Expand Down Expand Up @@ -145,8 +162,8 @@ For faster development, you can also build and run Botkube outside K8s cluster.
go run test/helpers/plugin_server.go
```
> **Note**
> If Botkube runs inside the k3d cluster, export the `PLUGIN_SERVER_HOST=http://host.k3d.internal` environment variable.
> **Note**
> If Botkube runs inside the k3d cluster, export the `PLUGIN_SERVER_HOST=http://host.k3d.internal` environment variable.
2. Export Botkube plugins cache directory:
Expand Down
7 changes: 0 additions & 7 deletions build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ LABEL org.opencontainers.image.source="[email protected]:kubeshop/botkube.git" \
org.opencontainers.image.licenses="MIT"

COPY botkube /usr/local/bin/botkube
# Download the latest kubectl in the appropriate architecture. Currently handles aarch64 (arm64) and x86_64 (amd64).
RUN MACH=$(uname -m); if [[ ${MACH} == "aarch64" ]]; then ARCH=arm64; \
elif [[ ${MACH} == "x86_64" ]]; then ARCH=amd64; \
elif [[ ${MACH} == "armv7l" ]]; then ARCH=arm; \
else echo "Unsupported arch: ${MACH}"; ARCH=${MACH}; fi; \
wget -O /usr/local/bin/kubectl "https://dl.k8s.io/release/$(wget -qO - https://dl.k8s.io/release/stable.txt)/bin/linux/${ARCH}/kubectl" && \
chmod +x /usr/local/bin/kubectl

# Create Non Privileged user
RUN addgroup --gid 1001 botkube && \
Expand Down
63 changes: 6 additions & 57 deletions cmd/botkube/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
Expand Down Expand Up @@ -45,7 +44,6 @@ import (
"github.com/kubeshop/botkube/pkg/config"
"github.com/kubeshop/botkube/pkg/controller"
"github.com/kubeshop/botkube/pkg/execute"
"github.com/kubeshop/botkube/pkg/execute/kubectl"
"github.com/kubeshop/botkube/pkg/httpsrv"
"github.com/kubeshop/botkube/pkg/notifier"
"github.com/kubeshop/botkube/pkg/sink"
Expand Down Expand Up @@ -172,43 +170,19 @@ func run(ctx context.Context) error {
return metricsSrv.Serve(ctx)
})

// Kubectl config merger
kcMerger := kubectl.NewMerger(conf.Executors)

// Load resource variants name if needed
var resourceNameNormalizerFunc kubectl.ResourceVariantsFunc
if kcMerger.IsAtLeastOneEnabled() {
resourceNameNormalizer, err := kubectl.NewResourceNormalizer(
logger.WithField(componentLogFieldKey, "Resource Name Normalizer"),
discoveryCli,
)
if err != nil {
return reportFatalError("while creating resource name normalizer", err)
}
resourceNameNormalizerFunc = resourceNameNormalizer.Normalize
}

cmdGuard := command.NewCommandGuard(logger.WithField(componentLogFieldKey, "Command Guard"), discoveryCli)

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

// Create executor factory
cfgManager := config.NewManager(remoteCfgEnabled, logger.WithField(componentLogFieldKey, "Config manager"), conf.Settings.PersistentConfig, cfgVersion, k8sCli, gqlClient, deployClient)
executorFactory, err := execute.NewExecutorFactory(
execute.DefaultExecutorFactoryParams{
Log: logger.WithField(componentLogFieldKey, "Executor"),
CmdRunner: runner,
Cfg: *conf,
KcChecker: kubectl.NewChecker(resourceNameNormalizerFunc),
Merger: kcMerger,
CfgManager: cfgManager,
AnalyticsReporter: reporter,
NamespaceLister: k8sCli.CoreV1().Namespaces(),
CommandGuard: cmdGuard,
PluginManager: pluginManager,
BotKubeVersion: botkubeVersion,
Expand Down Expand Up @@ -556,41 +530,16 @@ func sendHelp(ctx context.Context, s *storage.Help, clusterName string, executor
return s.MarkHelpAsSent(ctx, sent)
}

func findK8sVersion(runner *execute.OSCommand) (string, error) {
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)
func findVersions(cli *kubernetes.Clientset) (string, error) {
k8sVer, err := cli.ServerVersion()
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)
return "", fmt.Errorf("while getting server version: %w", err)
}

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)
return fmt.Sprintf("K8s Server Version: %s\nBotkube version: %s", k8sVer.String(), botkubeVersion), nil
}
121 changes: 0 additions & 121 deletions comm_config.yaml.tpl

This file was deleted.

Loading

0 comments on commit fb83664

Please sign in to comment.