@@ -33,11 +33,14 @@ import (
33
33
"sigs.k8s.io/cluster-api/util/predicates"
34
34
"sigs.k8s.io/cluster-api/util/record"
35
35
ctrl "sigs.k8s.io/controller-runtime"
36
+ "sigs.k8s.io/controller-runtime/pkg/builder"
36
37
"sigs.k8s.io/controller-runtime/pkg/client"
37
38
"sigs.k8s.io/controller-runtime/pkg/controller"
38
39
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
40
+ "sigs.k8s.io/controller-runtime/pkg/event"
39
41
"sigs.k8s.io/controller-runtime/pkg/handler"
40
42
"sigs.k8s.io/controller-runtime/pkg/log"
43
+ "sigs.k8s.io/controller-runtime/pkg/predicate"
41
44
"sigs.k8s.io/controller-runtime/pkg/source"
42
45
)
43
46
@@ -63,6 +66,24 @@ func (r *GCPMachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Ma
63
66
Watches (
64
67
& clusterv1.Machine {},
65
68
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
+ }),
66
87
).
67
88
Watches (
68
89
& infrav1.GCPCluster {},
@@ -224,7 +245,8 @@ func (r *GCPMachineReconciler) reconcile(ctx context.Context, machineScope *scop
224
245
return ctrl.Result {}, err
225
246
}
226
247
227
- if err := instances .New (machineScope ).Reconcile (ctx ); err != nil {
248
+ instancesSvc := instances .New (machineScope )
249
+ if err := instancesSvc .Reconcile (ctx ); err != nil {
228
250
log .Error (err , "Error reconciling instance resources" )
229
251
record .Warnf (machineScope .GCPMachine , "GCPMachineReconcile" , "Reconcile error - %v" , err )
230
252
return ctrl.Result {}, err
0 commit comments