Skip to content

Commit

Permalink
Rework Config Watcher and speed up configuration reload (#774)
Browse files Browse the repository at this point in the history
- Rework Config Watcher to have it a sidecar that calls BotKube endpoint when config should be reloaded
- Implement `/reload` endpoint that restarts BotKube - it is called by the new Config Watcher
- Improve messages when editing Source Bindings: if Config Watcher is disabled, it asks user to restart the app manually
- Fix rendering emojis for amessages on MS Teams
- Fix `make container-image-single` target
- Fix MS teams formatting issues
  • Loading branch information
pkosiec authored Sep 29, 2022
1 parent 96bcd29 commit 74b5f52
Show file tree
Hide file tree
Showing 32 changed files with 572 additions and 338 deletions.
52 changes: 37 additions & 15 deletions cmd/botkube/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/spf13/pflag"
"golang.org/x/sync/errgroup"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/discovery"
cacheddiscovery "k8s.io/client-go/discovery/cached"
"k8s.io/client-go/dynamic"
Expand All @@ -27,6 +28,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager/signals"

"github.com/kubeshop/botkube/internal/analytics"
"github.com/kubeshop/botkube/internal/lifecycle"
"github.com/kubeshop/botkube/internal/storage"
"github.com/kubeshop/botkube/pkg/bot"
"github.com/kubeshop/botkube/pkg/bot/interactive"
Expand All @@ -36,6 +38,7 @@ import (
"github.com/kubeshop/botkube/pkg/execute/kubectl"
"github.com/kubeshop/botkube/pkg/filterengine"
"github.com/kubeshop/botkube/pkg/httpsrv"
"github.com/kubeshop/botkube/pkg/notifier"
"github.com/kubeshop/botkube/pkg/recommendation"
"github.com/kubeshop/botkube/pkg/sink"
"github.com/kubeshop/botkube/pkg/sources"
Expand Down Expand Up @@ -159,7 +162,7 @@ func run() error {

commCfg := conf.Communications
var (
notifiers []controller.Notifier
notifiers []notifier.Notifier
bots = map[string]bot.Bot{}
)

Expand Down Expand Up @@ -241,6 +244,39 @@ func run() error {
}
}

// Lifecycle server
if conf.Settings.LifecycleServer.Enabled {
lifecycleSrv := lifecycle.NewServer(
logger.WithField(componentLogFieldKey, "Lifecycle server"),
k8sCli,
conf.Settings.LifecycleServer,
conf.Settings.ClusterName,
func(msg string) error {
return notifier.SendPlaintextMessage(ctx, notifiers, msg)
},
)
errGroup.Go(func() error {
defer analytics.ReportPanicIfOccurs(logger, reporter)
return lifecycleSrv.Serve(ctx)
})
}

if conf.ConfigWatcher.Enabled {
err := config.WaitForWatcherSync(
ctx,
logger.WithField(componentLogFieldKey, "Config Watcher Sync"),
conf.ConfigWatcher,
)
if err != nil {
if err != wait.ErrWaitTimeout {
return reportFatalError("while waiting for Config Watcher sync", err)
}

// non-blocking error, move forward
logger.Warn("Config Watcher is still not synchronized. Read the logs of the sidecar container to see the cause. Continuing running BotKube...")
}
}

// Send help message
helpDB := storage.NewForHelp(conf.Settings.SystemConfigMap.Namespace, conf.Settings.SystemConfigMap.Name, k8sCli)
err = sendHelp(ctx, helpDB, conf.Settings.ClusterName, bots)
Expand All @@ -264,20 +300,6 @@ func run() error {
})
}

// Start Config Watcher
if conf.Settings.ConfigWatcher {
cfgWatcher := controller.NewConfigWatcher(
logger.WithField(componentLogFieldKey, "Config Watcher"),
confDetails.CfgFilesToWatch,
conf.Settings.ClusterName,
notifiers,
)
errGroup.Go(func() error {
defer analytics.ReportPanicIfOccurs(logger, reporter)
return cfgWatcher.Do(ctx, cancelCtxFn)
})
}

recommFactory := recommendation.NewFactory(logger.WithField(componentLogFieldKey, "Recommendations"), dynamicCli)

// Create and start controller
Expand Down
11 changes: 10 additions & 1 deletion global_config.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,19 @@ settings:
# Cluster name to differentiate incoming messages
clusterName: not-configured
# Set true to enable config watcher
configWatcher: true
# Server configuration which exposes functionality related to the app lifecycle.
lifecycleServer:
deployment:
name: botkube
namespace: botkube
port: "2113"
# Set false to disable upgrade notification
upgradeNotifier: true

# Parameters for the config watcher container.
configWatcher:
enabled: false # Used only on Kubernetes

# Map of enabled executors. The `executors` property name is an alias for a given configuration.
# It's used as a binding reference.
#
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ require (
github.com/aws/aws-sdk-go v1.44.20
github.com/bwmarrin/discordgo v0.25.0
github.com/dustin/go-humanize v1.0.0
github.com/fsnotify/fsnotify v1.5.4
github.com/go-playground/locales v0.14.0
github.com/go-playground/universal-translator v0.18.0
github.com/go-playground/validator/v10 v10.11.0
Expand Down Expand Up @@ -54,6 +53,7 @@ require (
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/fvbommel/sortorder v1.0.1 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.3 // indirect
github.com/go-errors/errors v1.0.1 // indirect
Expand Down
6 changes: 3 additions & 3 deletions hack/goreleaser.sh
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ build() {
}

build_single() {
export GORELEASER_CURRENT_TAG=v9.99.9-dev
export IMAGE_TAG=v9.99.9-dev
docker run --rm --privileged \
-v "$PWD":/go/src/github.com/kubeshop/botkube \
-v /var/run/docker.sock:/var/run/docker.sock \
-w /go/src/github.com/kubeshop/botkube \
-e GORELEASER_CURRENT_TAG=${GORELEASER_CURRENT_TAG} \
-e IMAGE_TAG=${IMAGE_TAG} \
-e ANALYTICS_API_KEY="${ANALYTICS_API_KEY}" \
goreleaser/goreleaser build --single-target --rm-dist --snapshot --id botkube -o "./botkube"
docker build -f "$PWD/build/Dockerfile" --platform "${IMAGE_PLATFORM}" -t "${IMAGE_REGISTRY}/${IMAGE_REPOSITORY}:${GORELEASER_CURRENT_TAG}" .
docker build -f "$PWD/build/Dockerfile" --platform "${IMAGE_PLATFORM}" -t "${IMAGE_REGISTRY}/${IMAGE_REPOSITORY}:${IMAGE_TAG}" .
rm "$PWD/botkube"
}

Expand Down
Loading

0 comments on commit 74b5f52

Please sign in to comment.