@@ -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,24 @@ 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+ return false
85+ },
86+ }),
6687 ).
6788 Watches (
6889 & infrav1.GCPCluster {},
@@ -224,7 +245,8 @@ func (r *GCPMachineReconciler) reconcile(ctx context.Context, machineScope *scop
224245 return ctrl.Result {}, err
225246 }
226247
227- if err := instances .New (machineScope ).Reconcile (ctx ); err != nil {
248+ instancesSvc := instances .New (machineScope )
249+ if err := instancesSvc .Reconcile (ctx ); err != nil {
228250 log .Error (err , "Error reconciling instance resources" )
229251 record .Warnf (machineScope .GCPMachine , "GCPMachineReconcile" , "Reconcile error - %v" , err )
230252 return ctrl.Result {}, err
0 commit comments