Skip to content

Commit e8eb540

Browse files
authored
Merge pull request #8852 from ykakarap/pr-optimizie-reconcileInterruptibleNodeLabel
🌱 optimize `reconcileInterruptibleNodeLabel` of machine controller
2 parents 266685d + baa7972 commit e8eb540

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
@@ -273,19 +273,22 @@ func (r *Reconciler) reconcile(ctx context.Context, cluster *clusterv1.Cluster,
273273
}))
274274
}
275275

276-
phases := []func(context.Context, *clusterv1.Cluster, *clusterv1.Machine) (ctrl.Result, error){
276+
phases := []func(context.Context, *scope) (ctrl.Result, error){
277277
r.reconcileBootstrap,
278278
r.reconcileInfrastructure,
279279
r.reconcileNode,
280-
r.reconcileInterruptibleNodeLabel,
281280
r.reconcileCertificateExpiry,
282281
}
283282

284283
res := ctrl.Result{}
285284
errs := []error{}
285+
s := &scope{
286+
cluster: cluster,
287+
machine: m,
288+
}
286289
for _, phase := range phases {
287290
// Call the inner reconciliation methods.
288-
phaseResult, err := phase(ctx, cluster, m)
291+
phaseResult, err := phase(ctx, s)
289292
if err != nil {
290293
errs = append(errs, err)
291294
}
@@ -297,6 +300,25 @@ func (r *Reconciler) reconcile(ctx context.Context, cluster *clusterv1.Cluster,
297300
return res, kerrors.NewAggregate(errs)
298301
}
299302

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

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)