From 3ffb31f62f962ac3fbe6bd99f94b3c11138e99e6 Mon Sep 17 00:00:00 2001 From: Kobi Levi Date: Fri, 21 Jul 2023 08:24:39 +0300 Subject: [PATCH] Fix unit-tests (ci) errors --- controllers/suite_test.go | 1 + internal/dataplane/client/client.go | 22 ++++++++++++---------- main.go | 8 ++++++-- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/controllers/suite_test.go b/controllers/suite_test.go index 2b5f6532..8f13b9d2 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -35,6 +35,7 @@ var _ = BeforeSuite(func() { logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) By("bootstrapping test environment") + testEnv = &envtest.Environment{ CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, ErrorIfCRDPathMissing: false, diff --git a/internal/dataplane/client/client.go b/internal/dataplane/client/client.go index afbc6eed..6a389fa5 100644 --- a/internal/dataplane/client/client.go +++ b/internal/dataplane/client/client.go @@ -13,17 +13,12 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/watch" "k8s.io/client-go/kubernetes" - ctrl "sigs.k8s.io/controller-runtime" + "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/log" "github.com/kong/blixt/pkg/vars" ) -var ( - config = ctrl.GetConfigOrDie() - clientset, _ = kubernetes.NewForConfig(config) -) - // clientInfo encapsulates the gathered information about a BackendsClient // along with the gRPC client connection. type clientInfo struct { @@ -39,7 +34,8 @@ type observer interface { // BackendsClientManager is managing the connections and interactions with // the available BackendsClient servers. type BackendsClientManager struct { - log logr.Logger + log logr.Logger + clientset *kubernetes.Clientset mu sync.RWMutex clients map[string]clientInfo @@ -48,13 +44,19 @@ type BackendsClientManager struct { } // NewBackendsClientManager returns an initialized instance of BackendsClientManager. -func NewBackendsClientManager() *BackendsClientManager { +func NewBackendsClientManager(config *rest.Config) (*BackendsClientManager, error) { + clientset, err := kubernetes.NewForConfig(config) + if err != nil { + return nil, err + } + return &BackendsClientManager{ log: log.FromContext(context.Background()), + clientset: clientset, mu: sync.RWMutex{}, clients: map[string]clientInfo{}, observers: []observer{}, - } + }, nil } // RegisterObservers adds new observer(s) to the observers list of BackendsClientManager. @@ -104,7 +106,7 @@ func (c *BackendsClientManager) ManageDataPlanePods(ctx context.Context) error { vars.DefaultDataPlaneAppLabel, vars.DefaultDataPlaneComponentLabel), } - watcher, err := clientset.CoreV1().Pods(vars.DefaultNamespace).Watch(ctx, listOptions) + watcher, err := c.clientset.CoreV1().Pods(vars.DefaultNamespace).Watch(ctx, listOptions) if err != nil { return err } diff --git a/main.go b/main.go index 4e75612e..84e63f36 100644 --- a/main.go +++ b/main.go @@ -52,7 +52,8 @@ func main() { ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) - mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ + cfg := ctrl.GetConfigOrDie() + mgr, err := ctrl.NewManager(cfg, ctrl.Options{ Scheme: scheme, MetricsBindAddress: metricsAddr, Port: 9443, @@ -76,7 +77,10 @@ func main() { os.Exit(1) } - clientsManager := client.NewBackendsClientManager() + clientsManager, err := client.NewBackendsClientManager(cfg) + if err != nil { + setupLog.Error(err, "unable to create backends client manager") + } if err = (&controllers.GatewayReconciler{ Client: mgr.GetClient(),