Skip to content

Commit baa7972

Browse files
author
Yuvaraj Kakaraparthi
committed
optimize machine controller by dropping unwanted get calls
1 parent 2c446da commit baa7972

7 files changed

+153
-294
lines changed

internal/controllers/machine/machine_controller.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,19 +278,22 @@ func (r *Reconciler) reconcile(ctx context.Context, cluster *clusterv1.Cluster,
278278
}))
279279
}
280280

281-
phases := []func(context.Context, *clusterv1.Cluster, *clusterv1.Machine) (ctrl.Result, error){
281+
phases := []func(context.Context, *scope) (ctrl.Result, error){
282282
r.reconcileBootstrap,
283283
r.reconcileInfrastructure,
284284
r.reconcileNode,
285-
r.reconcileInterruptibleNodeLabel,
286285
r.reconcileCertificateExpiry,
287286
}
288287

289288
res := ctrl.Result{}
290289
errs := []error{}
290+
s := &scope{
291+
cluster: cluster,
292+
machine: m,
293+
}
291294
for _, phase := range phases {
292295
// Call the inner reconciliation methods.
293-
phaseResult, err := phase(ctx, cluster, m)
296+
phaseResult, err := phase(ctx, s)
294297
if err != nil {
295298
errs = append(errs, err)
296299
}
@@ -302,6 +305,25 @@ func (r *Reconciler) reconcile(ctx context.Context, cluster *clusterv1.Cluster,
302305
return res, kerrors.NewAggregate(errs)
303306
}
304307

308+
// scope holds the different objects that are read and used during the reconcile.
309+
type scope struct {
310+
// cluster is the Cluster object the Machine belongs to.
311+
// It is set at the beginning of the reconcile function.
312+
cluster *clusterv1.Cluster
313+
314+
// machine is the Machine object. It is set at the beginning
315+
// of the reconcile function.
316+
machine *clusterv1.Machine
317+
318+
// infraMachine is the Infrastructure Machine object that is referenced by the
319+
// Machine. It is set after reconcileInfrastructure is called.
320+
infraMachine *unstructured.Unstructured
321+
322+
// bootstrapConfig is the BootstrapConfig object that is referenced by the
323+
// Machine. It is set after reconcileBootstrap is called.
324+
bootstrapConfig *unstructured.Unstructured
325+
}
326+
305327
func (r *Reconciler) reconcileDelete(ctx context.Context, cluster *clusterv1.Cluster, m *clusterv1.Machine) (ctrl.Result, error) { //nolint:gocyclo
306328
log := ctrl.LoggerFrom(ctx)
307329

internal/controllers/machine/machine_controller_node_labels.go

Lines changed: 0 additions & 96 deletions
This file was deleted.

internal/controllers/machine/machine_controller_node_labels_test.go

Lines changed: 0 additions & 142 deletions
This file was deleted.

internal/controllers/machine/machine_controller_noderef.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/pkg/errors"
2525
corev1 "k8s.io/api/core/v1"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2728
"k8s.io/klog/v2"
2829
ctrl "sigs.k8s.io/controller-runtime"
2930
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -41,8 +42,11 @@ var (
4142
ErrNodeNotFound = errors.New("cannot find node with matching ProviderID")
4243
)
4344

44-
func (r *Reconciler) reconcileNode(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine) (ctrl.Result, error) {
45+
func (r *Reconciler) reconcileNode(ctx context.Context, s *scope) (ctrl.Result, error) {
4546
log := ctrl.LoggerFrom(ctx)
47+
cluster := s.cluster
48+
machine := s.machine
49+
infraMachine := s.infraMachine
4650

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

119+
// Get interruptible instance status from the infrastructure provider and set the interruptible label on the node.
120+
interruptible := false
121+
found := false
122+
if infraMachine != nil {
123+
interruptible, found, err = unstructured.NestedBool(infraMachine.Object, "status", "interruptible")
124+
if err != nil {
125+
return ctrl.Result{}, errors.Wrapf(err, "failed to get status interruptible from infra machine %s", klog.KObj(infraMachine))
126+
}
127+
// If interruptible is set and is true add the interruptible label to the node labels.
128+
if found && interruptible {
129+
nodeLabels[clusterv1.InterruptibleLabel] = ""
130+
}
131+
}
132+
133+
_, nodeHadInterruptibleLabel := node.Labels[clusterv1.InterruptibleLabel]
134+
115135
// Reconcile node taints
116136
if err := r.patchNode(ctx, remoteClient, node, nodeLabels, nodeAnnotations); err != nil {
117137
return ctrl.Result{}, errors.Wrapf(err, "failed to reconcile Node %s", klog.KObj(node))
118138
}
139+
if !nodeHadInterruptibleLabel && interruptible {
140+
// If the interruptible label is added to the node then record the event.
141+
// Nb. Only record the event if the node previously did not have the label to avoid recording
142+
// the event during every reconcile.
143+
r.recorder.Event(machine, corev1.EventTypeNormal, "SuccessfulSetInterruptibleNodeLabel", node.Name)
144+
}
119145

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

0 commit comments

Comments
 (0)