Skip to content

Commit

Permalink
Concurrently reconcile CloudStackMachine resources
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdoherty4 committed Jul 14, 2023
1 parent 4ccf853 commit 9f73dae
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
9 changes: 5 additions & 4 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ spec:
- /manager
args:
- --leader-elect
- --cloudstackmachine-concurrency=${CAPC_CLOUDSTACKMACHINE_CONCURRENCY:=10}
image: controller:latest
name: manager
securityContext:
Expand All @@ -48,7 +49,7 @@ spec:
serviceAccountName: controller-manager
terminationGracePeriodSeconds: 10
tolerations:
- effect: NoSchedule
key: node-role.kubernetes.io/master
- effect: NoSchedule
key: node-role.kubernetes.io/control-plane
- effect: NoSchedule
key: node-role.kubernetes.io/master
- effect: NoSchedule
key: node-role.kubernetes.io/control-plane
5 changes: 3 additions & 2 deletions controllers/cloudstackmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"sigs.k8s.io/cluster-api/util"
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/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
Expand Down Expand Up @@ -233,7 +234,6 @@ func (r *CloudStackMachineReconciliationRunner) GetOrCreateVMInstance() (retRes

userData := processCustomMetadata(data, r)
err := r.CSUser.GetOrCreateVMInstance(r.ReconciliationSubject, r.CAPIMachine, r.CSCluster, r.FailureDomain, r.AffinityGroup, userData)

if err != nil {
r.Recorder.Eventf(r.ReconciliationSubject, "Warning", "Creating", CSMachineCreationFailed, err.Error())
}
Expand Down Expand Up @@ -345,8 +345,9 @@ func (r *CloudStackMachineReconciliationRunner) ReconcileDelete() (retRes ctrl.R
}

// SetupWithManager registers the machine reconciler to the CAPI controller manager.
func (reconciler *CloudStackMachineReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (reconciler *CloudStackMachineReconciler) SetupWithManager(mgr ctrl.Manager, opts controller.Options) error {
controller, err := ctrl.NewControllerManagedBy(mgr).
WithOptions(opts).
For(&infrav1.CloudStackMachine{}).
WithEventFilter(
predicate.Funcs{
Expand Down
6 changes: 4 additions & 2 deletions controllers/cloudstackmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package controllers_test

import (
"fmt"
"strings"

"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -31,8 +33,8 @@ import (
"sigs.k8s.io/cluster-api/util/patch"
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/controller/controllerutil"
"strings"
)

var _ = Describe("CloudStackMachineReconciler", func() {
Expand All @@ -43,7 +45,7 @@ var _ = Describe("CloudStackMachineReconciler", func() {
dummies.CSCluster.Spec.FailureDomains[0].Name = dummies.CSFailureDomain1.Spec.Name

SetupTestEnvironment() // Must happen before setting up managers/reconcilers.
Ω(MachineReconciler.SetupWithManager(k8sManager)).Should(Succeed()) // Register the CloudStack MachineReconciler.
Ω(MachineReconciler.SetupWithManager(k8sManager, controller.Options{})).Should(Succeed()) // Register the CloudStack MachineReconciler.

// Point CAPI machine Bootstrap secret ref to dummy bootstrap secret.
dummies.CAPIMachine.Spec.Bootstrap.DataSecretName = &dummies.BootstrapSecret.Name
Expand Down
19 changes: 15 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/manager"

Expand Down Expand Up @@ -73,6 +74,8 @@ type managerOpts struct {
WatchingNamespace string
WatchFilterValue string
CertDir string

CloudStackMachineConcurrency int
}

func setFlags() *managerOpts {
Expand Down Expand Up @@ -117,6 +120,13 @@ func setFlags() *managerOpts {
"webhook-cert-dir",
"/tmp/k8s-webhook-server/serving-certs/",
"Specify the directory where webhooks will get tls certificates.")
flag.IntVar(
&opts.CloudStackMachineConcurrency,
"cloudstackmachine-concurrency",
10,
"Maximum concurrent reconciles for CloudStackMachine resources",
)

return opts
}

Expand Down Expand Up @@ -152,10 +162,11 @@ func main() {
K8sClient: mgr.GetClient(),
BaseLogger: ctrl.Log.WithName("controllers"),
Recorder: mgr.GetEventRecorderFor("capc-controller-manager"),
Scheme: mgr.GetScheme()}
Scheme: mgr.GetScheme(),
}

ctx := ctrl.SetupSignalHandler()
setupReconcilers(ctx, base, mgr)
setupReconcilers(ctx, base, *opts, mgr)
infrav1b3.K8sClient = base.K8sClient

// +kubebuilder:scaffold:builder
Expand Down Expand Up @@ -191,12 +202,12 @@ func main() {
}
}

func setupReconcilers(ctx context.Context, base utils.ReconcilerBase, mgr manager.Manager) {
func setupReconcilers(ctx context.Context, base utils.ReconcilerBase, opts managerOpts, mgr manager.Manager) {
if err := (&controllers.CloudStackClusterReconciler{ReconcilerBase: base}).SetupWithManager(ctx, mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CloudStackCluster")
os.Exit(1)
}
if err := (&controllers.CloudStackMachineReconciler{ReconcilerBase: base}).SetupWithManager(mgr); err != nil {
if err := (&controllers.CloudStackMachineReconciler{ReconcilerBase: base}).SetupWithManager(mgr, controller.Options{MaxConcurrentReconciles: opts.CloudStackMachineConcurrency}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CloudStackMachine")
os.Exit(1)
}
Expand Down
4 changes: 0 additions & 4 deletions pkg/cloud/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ func (c *client) CheckLimits(
fd *infrav1.CloudStackFailureDomain,
offering *cloudstack.ServiceOffering,
) error {

err := c.CheckAccountLimits(fd, offering)
if err != nil {
return err
Expand All @@ -316,7 +315,6 @@ func (c *client) DeployVM(
offering *cloudstack.ServiceOffering,
userData string,
) error {

templateID, err := c.ResolveTemplate(csCluster, csMachine, fd.Spec.Zone.ID)
if err != nil {
return err
Expand Down Expand Up @@ -391,7 +389,6 @@ func (c *client) GetOrCreateVMInstance(
affinity *infrav1.CloudStackAffinityGroup,
userData string,
) error {

// Check if VM instance already exists.
if err := c.ResolveVMInstanceDetails(csMachine); err == nil ||
!strings.Contains(strings.ToLower(err.Error()), "no match") {
Expand Down Expand Up @@ -499,5 +496,4 @@ func (c *client) listVMInstanceDatadiskVolumeIDs(instanceID string) ([]string, e
}

return ret, nil

}

0 comments on commit 9f73dae

Please sign in to comment.