Skip to content

Commit

Permalink
Set up golang context when initiating controller manager
Browse files Browse the repository at this point in the history
Set up golang context when initiating controller manager and also pass it down
to VSphereCluster controller

Signed-off-by: Gong Zhang <[email protected]>
  • Loading branch information
zhanggbj committed Sep 12, 2023
1 parent 2188e96 commit 78226c3
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 87 deletions.
2 changes: 1 addition & 1 deletion controllers/controllers_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func setup() {
panic(fmt.Sprintf("unable to create ClusterCacheReconciler controller: %v", err))
}

if err := AddClusterControllerToManager(testEnv.GetContext(), testEnv.Manager, &infrav1.VSphereCluster{}, controllerOpts); err != nil {
if err := AddClusterControllerToManager(ctx, testEnv.GetContext(), testEnv.Manager, &infrav1.VSphereCluster{}, controllerOpts); err != nil {
panic(fmt.Sprintf("unable to setup VsphereCluster controller: %v", err))
}
if err := AddMachineControllerToManager(testEnv.GetContext(), testEnv.Manager, &infrav1.VSphereMachine{}, controllerOpts); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions controllers/vmware/test/controllers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,15 @@ func getManager(cfg *rest.Config, networkProvider string) manager.Manager {

controllerOpts := controller.Options{MaxConcurrentReconciles: 10}

opts.AddToManager = func(controllerCtx *capvcontext.ControllerManagerContext, mgr ctrlmgr.Manager) error {
if err := controllers.AddClusterControllerToManager(controllerCtx, mgr, &vmwarev1.VSphereCluster{}, controllerOpts); err != nil {
opts.AddToManager = func(ctx context.Context, controllerCtx *capvcontext.ControllerManagerContext, mgr ctrlmgr.Manager) error {
if err := controllers.AddClusterControllerToManager(ctx, controllerCtx, mgr, &vmwarev1.VSphereCluster{}, controllerOpts); err != nil {
return err
}

return controllers.AddMachineControllerToManager(controllerCtx, mgr, &vmwarev1.VSphereMachine{}, controllerOpts)
}

mgr, err := manager.New(opts)
mgr, err := manager.New(ctx, opts)
Expect(err).NotTo(HaveOccurred())
return mgr
}
Expand Down
8 changes: 4 additions & 4 deletions controllers/vspherecluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import (

// AddClusterControllerToManager adds the cluster controller to the provided
// manager.
func AddClusterControllerToManager(controllerCtx *capvcontext.ControllerManagerContext, mgr manager.Manager, clusterControlledType client.Object, options controller.Options) error {
func AddClusterControllerToManager(ctx context.Context, controllerCtx *capvcontext.ControllerManagerContext, mgr manager.Manager, clusterControlledType client.Object, options controller.Options) error {
supervisorBased, err := util.IsSupervisorType(clusterControlledType)
if err != nil {
return err
Expand Down Expand Up @@ -110,7 +110,7 @@ func AddClusterControllerToManager(controllerCtx *capvcontext.ControllerManagerC
ControllerContext: controllerContext,
clusterModuleReconciler: NewReconciler(controllerContext),
}
clusterToInfraFn := clusterToInfrastructureMapFunc(controllerCtx)
clusterToInfraFn := clusterToInfrastructureMapFunc(ctx, controllerCtx)
c, err := ctrl.NewControllerManagedBy(mgr).
// Watch the controlled, infrastructure resource.
For(clusterControlledType).
Expand Down Expand Up @@ -173,7 +173,7 @@ func AddClusterControllerToManager(controllerCtx *capvcontext.ControllerManagerC
return nil
}

func clusterToInfrastructureMapFunc(controllerCtx *capvcontext.ControllerManagerContext) handler.MapFunc {
func clusterToInfrastructureMapFunc(ctx context.Context, controllerCtx *capvcontext.ControllerManagerContext) handler.MapFunc {
gvk := infrav1.GroupVersion.WithKind(reflect.TypeOf(&infrav1.VSphereCluster{}).Elem().Name())
return clusterutilv1.ClusterToInfrastructureMapFunc(controllerCtx, gvk, controllerCtx.Client, &infrav1.VSphereCluster{})
return clusterutilv1.ClusterToInfrastructureMapFunc(ctx, gvk, controllerCtx.Client, &infrav1.VSphereCluster{})
}
80 changes: 41 additions & 39 deletions controllers/vspherecluster_reconciler.go

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions controllers/vspherecluster_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,14 +700,14 @@ func TestClusterReconciler_ReconcileDeploymentZones(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
controllerCtx := fake.NewControllerContext(fake.NewControllerManagerContext(tt.initObjs...))
ctx := fake.NewClusterContext(controllerCtx)
ctx.VSphereCluster.Spec.Server = server
clusterCtx := fake.NewClusterContext(controllerCtx)
clusterCtx.VSphereCluster.Spec.Server = server

r := clusterReconciler{ControllerContext: controllerCtx}
reconciled, err := r.reconcileDeploymentZones(ctx)
reconciled, err := r.reconcileDeploymentZones(ctx, clusterCtx)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(reconciled).To(Equal(tt.reconciled))
tt.assert(ctx.VSphereCluster)
tt.assert(clusterCtx.VSphereCluster)
})
}
})
Expand Down Expand Up @@ -769,15 +769,15 @@ func TestClusterReconciler_ReconcileDeploymentZones(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
controllerCtx := fake.NewControllerContext(fake.NewControllerManagerContext(tt.initObjs...))
ctx := fake.NewClusterContext(controllerCtx)
ctx.VSphereCluster.Spec.Server = server
ctx.VSphereCluster.Spec.FailureDomainSelector = &metav1.LabelSelector{MatchLabels: map[string]string{}}
clusterCtx := fake.NewClusterContext(controllerCtx)
clusterCtx.VSphereCluster.Spec.Server = server
clusterCtx.VSphereCluster.Spec.FailureDomainSelector = &metav1.LabelSelector{MatchLabels: map[string]string{}}

r := clusterReconciler{ControllerContext: controllerCtx}
reconciled, err := r.reconcileDeploymentZones(ctx)
reconciled, err := r.reconcileDeploymentZones(ctx, clusterCtx)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(reconciled).To(Equal(tt.reconciled))
tt.assert(ctx.VSphereCluster)
tt.assert(clusterCtx.VSphereCluster)
})
}
})
Expand All @@ -802,14 +802,14 @@ func TestClusterReconciler_ReconcileDeploymentZones(t *testing.T) {

assertNumberOfZones := func(selector *metav1.LabelSelector, selectedZones int) {
controllerCtx := fake.NewControllerContext(fake.NewControllerManagerContext(zoneOne, zoneTwo, zoneThree))
ctx := fake.NewClusterContext(controllerCtx)
ctx.VSphereCluster.Spec.Server = server
ctx.VSphereCluster.Spec.FailureDomainSelector = selector
clusterCtx := fake.NewClusterContext(controllerCtx)
clusterCtx.VSphereCluster.Spec.Server = server
clusterCtx.VSphereCluster.Spec.FailureDomainSelector = selector

r := clusterReconciler{ControllerContext: controllerCtx}
_, err := r.reconcileDeploymentZones(ctx)
_, err := r.reconcileDeploymentZones(ctx, clusterCtx)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(ctx.VSphereCluster.Status.FailureDomains).To(HaveLen(selectedZones))
g.Expect(clusterCtx.VSphereCluster.Status.FailureDomains).To(HaveLen(selectedZones))
}

t.Run("with no zones matching labels", func(_ *testing.T) {
Expand Down
25 changes: 14 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"context"
"errors"
"flag"
"fmt"
Expand All @@ -43,7 +44,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
ctrlmgr "sigs.k8s.io/controller-runtime/pkg/manager"
ctrlsig "sigs.k8s.io/controller-runtime/pkg/manager/signals"
"sigs.k8s.io/controller-runtime/pkg/webhook"

infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
Expand Down Expand Up @@ -256,7 +256,7 @@ func main() {
managerOpts.RetryPeriod = &leaderElectionRetryPeriod

// Create a function that adds all the controllers and webhooks to the manager.
addToManager := func(controllerCtx *capvcontext.ControllerManagerContext, mgr ctrlmgr.Manager) error {
addToManager := func(ctx context.Context, controllerCtx *capvcontext.ControllerManagerContext, mgr ctrlmgr.Manager) error {
tracker, err := setupRemoteClusterCacheTracker(controllerCtx, mgr)
if err != nil {
return perrors.Wrapf(err, "unable to create remote cluster cache tracker")
Expand All @@ -269,7 +269,7 @@ func main() {
return err
}
if isLoaded {
if err := setupVAPIControllers(controllerCtx, mgr, tracker); err != nil {
if err := setupVAPIControllers(ctx, controllerCtx, mgr, tracker); err != nil {
return fmt.Errorf("setupVAPIControllers: %w", err)
}
} else {
Expand All @@ -283,7 +283,7 @@ func main() {
return err
}
if isLoaded {
if err := setupSupervisorControllers(controllerCtx, mgr, tracker); err != nil {
if err := setupSupervisorControllers(ctx, controllerCtx, mgr, tracker); err != nil {
return fmt.Errorf("setupSupervisorControllers: %w", err)
}
} else {
Expand All @@ -303,17 +303,20 @@ func main() {

setupLog.Info("creating controller manager", "version", version.Get().String())
managerOpts.AddToManager = addToManager
mgr, err := manager.New(managerOpts)

// Set up the context that's going to be used in controllers and for the manager.
ctx := ctrl.SetupSignalHandler()

mgr, err := manager.New(ctx, managerOpts)
if err != nil {
setupLog.Error(err, "problem creating controller manager")
os.Exit(1)
}

setupChecks(mgr)

sigHandler := ctrlsig.SetupSignalHandler()
setupLog.Info("starting controller manager")
if err := mgr.Start(sigHandler); err != nil {
if err := mgr.Start(ctx); err != nil {
setupLog.Error(err, "problem running controller manager")
os.Exit(1)
}
Expand All @@ -330,7 +333,7 @@ func main() {
defer session.Clear()
}

func setupVAPIControllers(controllerCtx *capvcontext.ControllerManagerContext, mgr ctrlmgr.Manager, tracker *remote.ClusterCacheTracker) error {
func setupVAPIControllers(ctx context.Context, controllerCtx *capvcontext.ControllerManagerContext, mgr ctrlmgr.Manager, tracker *remote.ClusterCacheTracker) error {
if err := (&webhooks.VSphereClusterTemplateWebhook{}).SetupWebhookWithManager(mgr); err != nil {
return err
}
Expand All @@ -355,7 +358,7 @@ func setupVAPIControllers(controllerCtx *capvcontext.ControllerManagerContext, m
return err
}

if err := controllers.AddClusterControllerToManager(controllerCtx, mgr, &infrav1.VSphereCluster{}, concurrency(vSphereClusterConcurrency)); err != nil {
if err := controllers.AddClusterControllerToManager(ctx, controllerCtx, mgr, &infrav1.VSphereCluster{}, concurrency(vSphereClusterConcurrency)); err != nil {
return err
}
if err := controllers.AddMachineControllerToManager(controllerCtx, mgr, &infrav1.VSphereMachine{}, concurrency(vSphereMachineConcurrency)); err != nil {
Expand All @@ -371,8 +374,8 @@ func setupVAPIControllers(controllerCtx *capvcontext.ControllerManagerContext, m
return controllers.AddVSphereDeploymentZoneControllerToManager(controllerCtx, mgr, concurrency(vSphereDeploymentZoneConcurrency))
}

func setupSupervisorControllers(controllerCtx *capvcontext.ControllerManagerContext, mgr ctrlmgr.Manager, tracker *remote.ClusterCacheTracker) error {
if err := controllers.AddClusterControllerToManager(controllerCtx, mgr, &vmwarev1.VSphereCluster{}, concurrency(vSphereClusterConcurrency)); err != nil {
func setupSupervisorControllers(ctx context.Context, controllerCtx *capvcontext.ControllerManagerContext, mgr ctrlmgr.Manager, tracker *remote.ClusterCacheTracker) error {
if err := controllers.AddClusterControllerToManager(ctx, controllerCtx, mgr, &vmwarev1.VSphereCluster{}, concurrency(vSphereClusterConcurrency)); err != nil {
return err
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Manager interface {
}

// New returns a new CAPV controller manager.
func New(opts Options) (Manager, error) {
func New(ctx context.Context, opts Options) (Manager, error) {
// Ensure the default options are set.
opts.defaults()

Expand Down Expand Up @@ -101,7 +101,7 @@ func New(opts Options) (Manager, error) {
}

// Add the requested items to the manager.
if err := opts.AddToManager(controllerManagerContext, mgr); err != nil {
if err := opts.AddToManager(ctx, controllerManagerContext, mgr); err != nil {
return nil, errors.Wrap(err, "failed to add resources to the manager")
}

Expand All @@ -124,7 +124,7 @@ func UpdateCredentials(opts *Options) {
opts.readAndSetCredentials()
}

// InitializeWatch adds a filesystem watcher for the capv credentials file
// InitializeWatch adds a filesystem watcher for the capv credentials file.
// In case of any update to the credentials file, the new credentials are passed to the capv manager context.
func InitializeWatch(controllerCtx *capvcontext.ControllerManagerContext, managerOpts *Options) (watch *fsnotify.Watcher, err error) {
capvCredentialsFile := managerOpts.CredentialsFile
Expand Down
16 changes: 8 additions & 8 deletions pkg/manager/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ const (

// GetNetworkProvider will return a network provider instance based on the environment
// the cfg is used to initialize a client that talks directly to api-server without using the cache.
func GetNetworkProvider(ctx *context.ControllerManagerContext) (services.NetworkProvider, error) {
switch ctx.NetworkProvider {
func GetNetworkProvider(controllerCtx *context.ControllerManagerContext) (services.NetworkProvider, error) {
switch controllerCtx.NetworkProvider {
case NSXNetworkProvider:
// TODO: disableFirewall not configurable
ctx.Logger.Info("Pick NSX-T network provider")
return network.NsxtNetworkProvider(ctx.Client, "false"), nil
controllerCtx.Logger.Info("Pick NSX-T network provider")
return network.NsxtNetworkProvider(controllerCtx.Client, "false"), nil
case VDSNetworkProvider:
ctx.Logger.Info("Pick NetOp (VDS) network provider")
return network.NetOpNetworkProvider(ctx.Client), nil
controllerCtx.Logger.Info("Pick NetOp (VDS) network provider")
return network.NetOpNetworkProvider(controllerCtx.Client), nil
case DummyLBNetworkProvider:
ctx.Logger.Info("Pick Dummy network provider")
controllerCtx.Logger.Info("Pick Dummy network provider")
return network.DummyLBNetworkProvider(), nil
default:
ctx.Logger.Info("NetworkProvider not set. Pick Dummy network provider")
controllerCtx.Logger.Info("NetworkProvider not set. Pick Dummy network provider")
return network.DummyNetworkProvider(), nil
}
}
5 changes: 3 additions & 2 deletions pkg/manager/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package manager

import (
"context"
"os"
"strings"
"time"
Expand All @@ -28,13 +29,13 @@ import (
ctrlmgr "sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/yaml"

"sigs.k8s.io/cluster-api-provider-vsphere/pkg/context"
capvcontext "sigs.k8s.io/cluster-api-provider-vsphere/pkg/context"
)

// AddToManagerFunc is a function that can be optionally specified with
// the manager's Options in order to explicitly decide what controllers and
// webhooks to add to the manager.
type AddToManagerFunc func(*context.ControllerManagerContext, ctrlmgr.Manager) error
type AddToManagerFunc func(context.Context, *capvcontext.ControllerManagerContext, ctrlmgr.Manager) error

// Options describes the options used to create a new CAPV manager.
type Options struct {
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/envtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func NewTestEnvironment() *TestEnvironment {
Username: simr.Username(),
Password: simr.Password(),
}
managerOpts.AddToManager = func(controllerCtx *capvcontext.ControllerManagerContext, mgr ctrlmgr.Manager) error {
managerOpts.AddToManager = func(ctx context.Context, controllerCtx *capvcontext.ControllerManagerContext, mgr ctrlmgr.Manager) error {
if err := (&webhooks.VSphereClusterTemplateWebhook{}).SetupWebhookWithManager(mgr); err != nil {
return err
}
Expand All @@ -165,7 +165,7 @@ func NewTestEnvironment() *TestEnvironment {
return (&webhooks.VSphereFailureDomainWebhook{}).SetupWebhookWithManager(mgr)
}

mgr, err := manager.New(managerOpts)
mgr, err := manager.New(ctrl.SetupSignalHandler(), managerOpts)
if err != nil {
klog.Fatalf("failed to create the CAPV controller manager: %v", err)
}
Expand Down

0 comments on commit 78226c3

Please sign in to comment.