@@ -33,11 +33,14 @@ import (
3333 "sigs.k8s.io/cluster-api/util/predicates"
3434 "sigs.k8s.io/cluster-api/util/record"
3535 ctrl "sigs.k8s.io/controller-runtime"
36+ "sigs.k8s.io/controller-runtime/pkg/builder"
3637 "sigs.k8s.io/controller-runtime/pkg/client"
3738 "sigs.k8s.io/controller-runtime/pkg/controller"
3839 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
40+ "sigs.k8s.io/controller-runtime/pkg/event"
3941 "sigs.k8s.io/controller-runtime/pkg/handler"
4042 "sigs.k8s.io/controller-runtime/pkg/log"
43+ "sigs.k8s.io/controller-runtime/pkg/predicate"
4144 "sigs.k8s.io/controller-runtime/pkg/source"
4245)
4346
@@ -63,6 +66,33 @@ func (r *GCPMachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Ma
6366 Watches (
6467 & clusterv1.Machine {},
6568 handler .EnqueueRequestsFromMapFunc (util .MachineToInfrastructureMapFunc (infrav1 .GroupVersion .WithKind ("GCPMachine" ))),
69+ builder .WithPredicates (predicate.Funcs {
70+ UpdateFunc : func (e event.UpdateEvent ) bool {
71+ oldMachine := e .ObjectOld .(* clusterv1.Machine )
72+ newMachine := e .ObjectNew .(* clusterv1.Machine )
73+
74+ // Reconcile if the spec changes.
75+ if newMachine .GetGeneration () != oldMachine .GetGeneration () {
76+ return true
77+ }
78+
79+ // Reconcile if the machine just transitioned to the Provisioned phase.
80+ if newMachine .Status .GetTypedPhase () == clusterv1 .MachinePhaseProvisioned && oldMachine .Status .GetTypedPhase () != clusterv1 .MachinePhaseProvisioned {
81+ return true
82+ }
83+
84+ for _ , condition := range newMachine .Status .Conditions {
85+ if condition .Type == clusterv1 .MachineNodeHealthyCondition {
86+ // TODO: Reconcile if the MachineNodeHealthyCondition condition changed.
87+ if condition .Type == clusterv1 .MachineNodeHealthyCondition && condition .Reason == clusterv1 .WaitingForNodeRefReason {
88+ return true
89+ }
90+ }
91+ }
92+
93+ return false
94+ },
95+ }),
6696 ).
6797 Watches (
6898 & infrav1.GCPCluster {},
@@ -224,7 +254,8 @@ func (r *GCPMachineReconciler) reconcile(ctx context.Context, machineScope *scop
224254 return ctrl.Result {}, err
225255 }
226256
227- if err := instances .New (machineScope ).Reconcile (ctx ); err != nil {
257+ instancesSvc := instances .New (machineScope )
258+ if err := instancesSvc .Reconcile (ctx ); err != nil {
228259 log .Error (err , "Error reconciling instance resources" )
229260 record .Warnf (machineScope .GCPMachine , "GCPMachineReconcile" , "Reconcile error - %v" , err )
230261 return ctrl.Result {}, err
0 commit comments