Skip to content

[feat] add controller concurrency options with elevated defaults #407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 41 additions & 14 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
capi "sigs.k8s.io/cluster-api/api/v1beta1"
ctrl "sigs.k8s.io/controller-runtime"
crcontroller "sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

infrastructurev1alpha1 "github.com/linode/cluster-api-provider-linode/api/v1alpha1"
infrastructurev1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2"
controller2 "github.com/linode/cluster-api-provider-linode/controller"
"github.com/linode/cluster-api-provider-linode/controller"
"github.com/linode/cluster-api-provider-linode/observability/tracing"
"github.com/linode/cluster-api-provider-linode/observability/wrappers/reconciler"
"github.com/linode/cluster-api-provider-linode/version"
Expand All @@ -58,10 +59,12 @@
)

const (
controllerName = "cluster-api-provider-linode.linode.com"
gracePeriod = 5 * time.Second
envK8sNodeName = "K8S_NODE_NAME"
envK8sPodName = "K8S_POD_NAME"
controllerName = "cluster-api-provider-linode.linode.com"
envK8sNodeName = "K8S_NODE_NAME"
envK8sPodName = "K8S_POD_NAME"
concurrencyDefault = 10
qpsDefault = 20
burstDefault = 30
)

func init() {
Expand All @@ -84,6 +87,13 @@
metricsAddr string
enableLeaderElection bool
probeAddr string

restConfigQPS int
restConfigBurst int
linodeClusterConcurrency int
linodeMachineConcurrency int
linodeObjectStorageBucketConcurrency int
linodeVPCConcurrency int

Check warning on line 96 in cmd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/main.go#L91-L96

Added lines #L91 - L96 were not covered by tests
)
flag.StringVar(&machineWatchFilter, "machine-watch-filter", "", "The machines to watch by label.")
flag.StringVar(&clusterWatchFilter, "cluster-watch-filter", "", "The clusters to watch by label.")
Expand All @@ -93,6 +103,18 @@
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.IntVar(&restConfigQPS, "kube-api-qps", qpsDefault,
"Maximum queries per second from the controller client to the Kubernetes API server. Defaults to 20")
flag.IntVar(&restConfigBurst, "kube-api-burst", burstDefault,
"Maximum number of queries that should be allowed in one burst from the controller client to the Kubernetes API server. Default 30")
flag.IntVar(&linodeClusterConcurrency, "linodecluster-concurrency", concurrencyDefault,
"Number of LinodeClusters to process simultaneously. Default 10")
flag.IntVar(&linodeMachineConcurrency, "linodemachine-concurrency", concurrencyDefault,
"Number of LinodeMachines to process simultaneously. Default 10")
flag.IntVar(&linodeObjectStorageBucketConcurrency, "linodeobjectstoragebucket-concurrency", concurrencyDefault,
"Number of linodeObjectStorageBuckets to process simultaneously. Default 10")
flag.IntVar(&linodeVPCConcurrency, "linodevpc-concurrency", concurrencyDefault,
"Number of LinodeVPCs to process simultaneously. Default 10")

Check warning on line 117 in cmd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/main.go#L106-L117

Added lines #L106 - L117 were not covered by tests
opts := zap.Options{
Development: true,
}
Expand All @@ -110,7 +132,12 @@
linodeDNSToken = linodeToken
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
restConfig := ctrl.GetConfigOrDie()
restConfig.QPS = float32(restConfigQPS)
restConfig.Burst = restConfigBurst
restConfig.UserAgent = fmt.Sprintf("CAPL/%s", version.GetVersion())

Check warning on line 138 in cmd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/main.go#L135-L138

Added lines #L135 - L138 were not covered by tests

mgr, err := ctrl.NewManager(restConfig, ctrl.Options{

Check warning on line 140 in cmd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/main.go#L140

Added line #L140 was not covered by tests
Scheme: scheme,
Metrics: metricsserver.Options{BindAddress: metricsAddr},
HealthProbeBindAddress: probeAddr,
Expand All @@ -134,52 +161,52 @@
}

if err = reconciler.NewReconcilerWithTracing(
&controller2.LinodeClusterReconciler{
&controller.LinodeClusterReconciler{

Check warning on line 164 in cmd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/main.go#L164

Added line #L164 was not covered by tests
Client: mgr.GetClient(),
Recorder: mgr.GetEventRecorderFor("LinodeClusterReconciler"),
WatchFilterValue: clusterWatchFilter,
LinodeApiKey: linodeToken,
},
).SetupWithManager(mgr); err != nil {
).SetupWithManager(mgr, crcontroller.Options{MaxConcurrentReconciles: linodeClusterConcurrency}); err != nil {

Check warning on line 170 in cmd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/main.go#L170

Added line #L170 was not covered by tests
setupLog.Error(err, "unable to create controller", "controller", "LinodeCluster")
os.Exit(1)
}

if err = reconciler.NewReconcilerWithTracing(
&controller2.LinodeMachineReconciler{
&controller.LinodeMachineReconciler{

Check warning on line 176 in cmd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/main.go#L176

Added line #L176 was not covered by tests
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("LinodeMachineReconciler"),
WatchFilterValue: machineWatchFilter,
LinodeApiKey: linodeToken,
LinodeDNSAPIKey: linodeDNSToken,
},
).SetupWithManager(mgr); err != nil {
).SetupWithManager(mgr, crcontroller.Options{MaxConcurrentReconciles: linodeMachineConcurrency}); err != nil {

Check warning on line 184 in cmd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/main.go#L184

Added line #L184 was not covered by tests
setupLog.Error(err, "unable to create controller", "controller", "LinodeMachine")
os.Exit(1)
}

if err = reconciler.NewReconcilerWithTracing(
&controller2.LinodeVPCReconciler{
&controller.LinodeVPCReconciler{

Check warning on line 190 in cmd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/main.go#L190

Added line #L190 was not covered by tests
Client: mgr.GetClient(),
Recorder: mgr.GetEventRecorderFor("LinodeVPCReconciler"),
WatchFilterValue: clusterWatchFilter,
LinodeApiKey: linodeToken,
},
).SetupWithManager(mgr); err != nil {
).SetupWithManager(mgr, crcontroller.Options{MaxConcurrentReconciles: linodeVPCConcurrency}); err != nil {

Check warning on line 196 in cmd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/main.go#L196

Added line #L196 was not covered by tests
setupLog.Error(err, "unable to create controller", "controller", "LinodeVPC")
os.Exit(1)
}

if err = reconciler.NewReconcilerWithTracing(
&controller2.LinodeObjectStorageBucketReconciler{
&controller.LinodeObjectStorageBucketReconciler{

Check warning on line 202 in cmd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/main.go#L202

Added line #L202 was not covered by tests
Client: mgr.GetClient(),
Logger: ctrl.Log.WithName("LinodeObjectStorageBucketReconciler"),
Recorder: mgr.GetEventRecorderFor("LinodeObjectStorageBucketReconciler"),
WatchFilterValue: objectStorageBucketWatchFilter,
LinodeApiKey: linodeToken,
},
).SetupWithManager(mgr); err != nil {
).SetupWithManager(mgr, crcontroller.Options{MaxConcurrentReconciles: linodeObjectStorageBucketConcurrency}); err != nil {

Check warning on line 209 in cmd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/main.go#L209

Added line #L209 was not covered by tests
setupLog.Error(err, "unable to create controller", "controller", "LinodeObjectStorageBucket")
os.Exit(1)
}
Expand Down
4 changes: 3 additions & 1 deletion controller/linodecluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
crcontroller "sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"

Expand Down Expand Up @@ -288,9 +289,10 @@
}

// SetupWithManager sets up the controller with the Manager.
func (r *LinodeClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *LinodeClusterReconciler) SetupWithManager(mgr ctrl.Manager, options crcontroller.Options) error {

Check warning on line 292 in controller/linodecluster_controller.go

View check run for this annotation

Codecov / codecov/patch

controller/linodecluster_controller.go#L292

Added line #L292 was not covered by tests
err := ctrl.NewControllerManagedBy(mgr).
For(&infrav1alpha2.LinodeCluster{}).
WithOptions(options).

Check warning on line 295 in controller/linodecluster_controller.go

View check run for this annotation

Codecov / codecov/patch

controller/linodecluster_controller.go#L295

Added line #L295 was not covered by tests
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(mgr.GetLogger(), r.WatchFilterValue)).
Watches(
&clusterv1.Cluster{},
Expand Down
4 changes: 3 additions & 1 deletion controller/linodemachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
crcontroller "sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand Down Expand Up @@ -728,14 +729,15 @@
}

// SetupWithManager sets up the controller with the Manager.
func (r *LinodeMachineReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *LinodeMachineReconciler) SetupWithManager(mgr ctrl.Manager, options crcontroller.Options) error {

Check warning on line 732 in controller/linodemachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controller/linodemachine_controller.go#L732

Added line #L732 was not covered by tests
linodeMachineMapper, err := kutil.ClusterToTypedObjectsMapper(r.Client, &infrav1alpha1.LinodeMachineList{}, mgr.GetScheme())
if err != nil {
return fmt.Errorf("failed to create mapper for LinodeMachines: %w", err)
}

err = ctrl.NewControllerManagedBy(mgr).
For(&infrav1alpha1.LinodeMachine{}).
WithOptions(options).

Check warning on line 740 in controller/linodemachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controller/linodemachine_controller.go#L740

Added line #L740 was not covered by tests
Watches(
&clusterv1.Machine{},
handler.EnqueueRequestsFromMapFunc(kutil.MachineToInfrastructureMapFunc(infrav1alpha1.GroupVersion.WithKind("LinodeMachine"))),
Expand Down
4 changes: 3 additions & 1 deletion controller/linodeobjectstoragebucket_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
crcontroller "sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
Expand Down Expand Up @@ -257,14 +258,15 @@
}

// SetupWithManager sets up the controller with the Manager.
func (r *LinodeObjectStorageBucketReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *LinodeObjectStorageBucketReconciler) SetupWithManager(mgr ctrl.Manager, options crcontroller.Options) error {

Check warning on line 261 in controller/linodeobjectstoragebucket_controller.go

View check run for this annotation

Codecov / codecov/patch

controller/linodeobjectstoragebucket_controller.go#L261

Added line #L261 was not covered by tests
linodeObjectStorageBucketMapper, err := kutil.ClusterToTypedObjectsMapper(r.Client, &infrav1alpha1.LinodeObjectStorageBucketList{}, mgr.GetScheme())
if err != nil {
return fmt.Errorf("failed to create mapper for LinodeObjectStorageBuckets: %w", err)
}

err = ctrl.NewControllerManagedBy(mgr).
For(&infrav1alpha1.LinodeObjectStorageBucket{}).
WithOptions(options).

Check warning on line 269 in controller/linodeobjectstoragebucket_controller.go

View check run for this annotation

Codecov / codecov/patch

controller/linodeobjectstoragebucket_controller.go#L269

Added line #L269 was not covered by tests
Owns(&corev1.Secret{}).
WithEventFilter(predicate.And(
predicates.ResourceHasFilterLabel(mgr.GetLogger(), r.WatchFilterValue),
Expand Down
4 changes: 3 additions & 1 deletion controller/linodevpc_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
crcontroller "sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
Expand Down Expand Up @@ -314,14 +315,15 @@
}

// SetupWithManager sets up the controller with the Manager.
func (r *LinodeVPCReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *LinodeVPCReconciler) SetupWithManager(mgr ctrl.Manager, options crcontroller.Options) error {

Check warning on line 318 in controller/linodevpc_controller.go

View check run for this annotation

Codecov / codecov/patch

controller/linodevpc_controller.go#L318

Added line #L318 was not covered by tests
linodeVPCMapper, err := kutil.ClusterToTypedObjectsMapper(r.Client, &infrav1alpha1.LinodeVPCList{}, mgr.GetScheme())
if err != nil {
return fmt.Errorf("failed to create mapper for LinodeVPCs: %w", err)
}

err = ctrl.NewControllerManagedBy(mgr).
For(&infrav1alpha1.LinodeVPC{}).
WithOptions(options).

Check warning on line 326 in controller/linodevpc_controller.go

View check run for this annotation

Codecov / codecov/patch

controller/linodevpc_controller.go#L326

Added line #L326 was not covered by tests
WithEventFilter(
predicate.And(
// Filter for objects with a specific WatchLabel.
Expand Down
3 changes: 2 additions & 1 deletion observability/wrappers/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/manager"
)

Expand All @@ -31,5 +32,5 @@ type Reconciler interface {
client.SubResourceClientConstructor

Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error)
SetupWithManager(mgr manager.Manager) error
SetupWithManager(mgr manager.Manager, options controller.Options) error
}
Loading