Skip to content

Commit

Permalink
Fix logging early errors in goroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
pkosiec committed Jun 13, 2023
1 parent 12e120f commit 6ca9700
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
12 changes: 9 additions & 3 deletions cmd/botkube/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,14 @@ func run(ctx context.Context) error {
defer analytics.ReportPanicIfOccurs(logger, reporter)

reportFatalError := reportFatalErrFn(logger, reporter, statusReporter)

errGroup, ctx := errgroup.WithContext(ctx)
defer func() {
err := errGroup.Wait()
if err != nil {
wrappedErr := reportFatalError("while waiting for goroutines to finish gracefully", err)
logger.Errorf(wrappedErr.Error())
}
}()

collector := plugin.NewCollector(logger)
enabledPluginExecutors, enabledPluginSources := collector.GetAllEnabledAndUsedPlugins(conf)
Expand Down Expand Up @@ -405,14 +411,14 @@ func run(ctx context.Context) error {

healthChecker.MarkAsReady()
err = ctrl.Start(ctx)

if err != nil {
return reportFatalError("while starting controller", err)
}

err = errGroup.Wait()
if err != nil {
return reportFatalError("while waiting for goroutines to finish gracefully", err)
// error from errGroup reported on defer, no need to do it twice
return err
}

return nil
Expand Down
4 changes: 4 additions & 0 deletions internal/kubex/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func BuildConfigFromFlags(masterUrl, kubeconfigPath, saTokenPath string) (*rest.
if err == nil {
return kubeconfig, nil
}

if kubeconfig == nil {
kubeconfig = &rest.Config{}
}
kubeconfig.AuthProvider = &clientcmdapi.AuthProviderConfig{
Name: "token-file",
Config: map[string]string{
Expand Down
13 changes: 11 additions & 2 deletions pkg/bot/cloudslack.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"strings"
"sync"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/google/uuid"
"github.com/sirupsen/logrus"
"github.com/slack-go/slack"
Expand Down Expand Up @@ -116,7 +119,7 @@ func (b *CloudSlack) Start(ctx context.Context) error {
}
c, err := pb.NewCloudSlackClient(conn).Connect(ctx)
if err != nil {
return fmt.Errorf("while initializing gRPC cloud client. %w", err)
return fmt.Errorf("while initializing gRPC cloud client: %w", err)
}
defer func(c pb.CloudSlack_ConnectClient) {
err := c.CloseSend()
Expand All @@ -133,7 +136,13 @@ func (b *CloudSlack) Start(ctx context.Context) error {
for {
data, err := c.Recv()
if err != nil {
return fmt.Errorf("while receiving cloud slack events. %w", err)
errStatus, ok := status.FromError(err)
if ok && errStatus.Code() == codes.Canceled && errStatus.Message() == context.Canceled.Error() {
b.log.Debugf("Context was cancelled. Skipping returning error...")
return nil
}

return fmt.Errorf("while receiving cloud slack events: %w", err)
}
event, err := slackevents.ParseEvent(data.Event, slackevents.OptionNoVerifyToken())
if err != nil {
Expand Down

0 comments on commit 6ca9700

Please sign in to comment.