Skip to content

Commit

Permalink
fix(manager) lock client creation
Browse files Browse the repository at this point in the history
Add a mutex and lock it when creating Kong admin clients. Attempting to
create multiple workspaced clients simultaneously can result in a race
condition where both clients check for the workspace before either
creates it, both clients will attempt to create the workspace, and the
second client to attempt creation will hit a unique violation.
  • Loading branch information
Travis Raines committed Dec 9, 2021
1 parent 0b6d5af commit 906fc43
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/manager/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"strings"
"sync"
"time"

"github.com/bombsimon/logrusr"
Expand All @@ -24,6 +25,8 @@ import (
"github.com/kong/kubernetes-ingress-controller/v2/internal/util"
)

var clientSetup sync.Mutex

// -----------------------------------------------------------------------------
// Controller Manager - Setup Utility Functions
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -92,7 +95,9 @@ func setupControllerOptions(logger logr.Logger, c *Config, scheme *runtime.Schem
}

func setupKongConfig(ctx context.Context, logger logr.Logger, c *Config) (sendconfig.Kong, error) {
clientSetup.Lock()
kongClient, err := c.GetKongClient(ctx)
clientSetup.Unlock()
if err != nil {
return sendconfig.Kong{}, fmt.Errorf("unable to build kong api client: %w", err)
}
Expand Down Expand Up @@ -166,7 +171,9 @@ func setupAdmissionServer(ctx context.Context, managerConfig *Config, managerCli

logger := log.WithField("component", "admission-server")

clientSetup.Lock()
kongclient, err := managerConfig.GetKongClient(ctx)
clientSetup.Unlock()
if err != nil {
return err
}
Expand Down

0 comments on commit 906fc43

Please sign in to comment.