Skip to content

Commit

Permalink
optimize machine controller by dropping unwanted get calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuvaraj Kakaraparthi committed Jun 22, 2023
1 parent 2c446da commit baa7972
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 294 deletions.
28 changes: 25 additions & 3 deletions internal/controllers/machine/machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,19 +278,22 @@ func (r *Reconciler) reconcile(ctx context.Context, cluster *clusterv1.Cluster,
}))
}

phases := []func(context.Context, *clusterv1.Cluster, *clusterv1.Machine) (ctrl.Result, error){
phases := []func(context.Context, *scope) (ctrl.Result, error){
r.reconcileBootstrap,
r.reconcileInfrastructure,
r.reconcileNode,
r.reconcileInterruptibleNodeLabel,
r.reconcileCertificateExpiry,
}

res := ctrl.Result{}
errs := []error{}
s := &scope{
cluster: cluster,
machine: m,
}
for _, phase := range phases {
// Call the inner reconciliation methods.
phaseResult, err := phase(ctx, cluster, m)
phaseResult, err := phase(ctx, s)
if err != nil {
errs = append(errs, err)
}
Expand All @@ -302,6 +305,25 @@ func (r *Reconciler) reconcile(ctx context.Context, cluster *clusterv1.Cluster,
return res, kerrors.NewAggregate(errs)
}

// scope holds the different objects that are read and used during the reconcile.
type scope struct {
// cluster is the Cluster object the Machine belongs to.
// It is set at the beginning of the reconcile function.
cluster *clusterv1.Cluster

// machine is the Machine object. It is set at the beginning
// of the reconcile function.
machine *clusterv1.Machine

// infraMachine is the Infrastructure Machine object that is referenced by the
// Machine. It is set after reconcileInfrastructure is called.
infraMachine *unstructured.Unstructured

// bootstrapConfig is the BootstrapConfig object that is referenced by the
// Machine. It is set after reconcileBootstrap is called.
bootstrapConfig *unstructured.Unstructured
}

func (r *Reconciler) reconcileDelete(ctx context.Context, cluster *clusterv1.Cluster, m *clusterv1.Machine) (ctrl.Result, error) { //nolint:gocyclo
log := ctrl.LoggerFrom(ctx)

Expand Down
96 changes: 0 additions & 96 deletions internal/controllers/machine/machine_controller_node_labels.go

This file was deleted.

142 changes: 0 additions & 142 deletions internal/controllers/machine/machine_controller_node_labels_test.go

This file was deleted.

28 changes: 27 additions & 1 deletion internal/controllers/machine/machine_controller_noderef.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -41,8 +42,11 @@ var (
ErrNodeNotFound = errors.New("cannot find node with matching ProviderID")
)

func (r *Reconciler) reconcileNode(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine) (ctrl.Result, error) {
func (r *Reconciler) reconcileNode(ctx context.Context, s *scope) (ctrl.Result, error) {
log := ctrl.LoggerFrom(ctx)
cluster := s.cluster
machine := s.machine
infraMachine := s.infraMachine

// Create a watch on the nodes in the Cluster.
if err := r.watchClusterNodes(ctx, cluster); err != nil {
Expand Down Expand Up @@ -112,10 +116,32 @@ func (r *Reconciler) reconcileNode(ctx context.Context, cluster *clusterv1.Clust
// NOTE: Once we reconcile node labels for the first time, the NodeUninitializedTaint is removed from the node.
nodeLabels := getManagedLabels(machine.Labels)

// Get interruptible instance status from the infrastructure provider and set the interruptible label on the node.
interruptible := false
found := false
if infraMachine != nil {
interruptible, found, err = unstructured.NestedBool(infraMachine.Object, "status", "interruptible")
if err != nil {
return ctrl.Result{}, errors.Wrapf(err, "failed to get status interruptible from infra machine %s", klog.KObj(infraMachine))
}
// If interruptible is set and is true add the interruptible label to the node labels.
if found && interruptible {
nodeLabels[clusterv1.InterruptibleLabel] = ""
}
}

_, nodeHadInterruptibleLabel := node.Labels[clusterv1.InterruptibleLabel]

// Reconcile node taints
if err := r.patchNode(ctx, remoteClient, node, nodeLabels, nodeAnnotations); err != nil {
return ctrl.Result{}, errors.Wrapf(err, "failed to reconcile Node %s", klog.KObj(node))
}
if !nodeHadInterruptibleLabel && interruptible {
// If the interruptible label is added to the node then record the event.
// Nb. Only record the event if the node previously did not have the label to avoid recording
// the event during every reconcile.
r.recorder.Event(machine, corev1.EventTypeNormal, "SuccessfulSetInterruptibleNodeLabel", node.Name)
}

// Do the remaining node health checks, then set the node health to true if all checks pass.
status, message := summarizeNodeConditions(node)
Expand Down
Loading

0 comments on commit baa7972

Please sign in to comment.