@@ -20,6 +20,8 @@ package controllers
2020import (
2121 "context"
2222 "fmt"
23+ "strings"
24+ "time"
2325
2426 "github.com/google/go-cmp/cmp"
2527 corev1 "k8s.io/api/core/v1"
@@ -226,7 +228,8 @@ func (r *GCPMachinePoolReconciler) reconcile(ctx context.Context, machinePoolSco
226228 // set the InstanceTemplateReadyCondition condition
227229 conditions .MarkTrue (machinePoolScope .GCPMachinePool , expinfrav1 .InstanceTemplateReadyCondition )
228230
229- if err := instancegroupmanagers .New (machinePoolScope ).Reconcile (ctx , instanceTemplateKey ); err != nil {
231+ igm , err := instancegroupmanagers .New (machinePoolScope ).Reconcile (ctx , instanceTemplateKey )
232+ if err != nil {
230233 log .Error (err , "Error reconciling instanceGroupManager" )
231234 // record.Warnf(machineScope.GCPMachine, "GCPMachineReconcile", "Reconcile error - %v", err)
232235 conditions .MarkUnknown (machinePoolScope .GCPMachinePool , expinfrav1 .MIGReadyCondition , expinfrav1 .MIGNotFoundReason , "%s" , err .Error ())
@@ -236,7 +239,44 @@ func (r *GCPMachinePoolReconciler) reconcile(ctx context.Context, machinePoolSco
236239 // set the MIGReadyCondition condition
237240 conditions .MarkTrue (machinePoolScope .GCPMachinePool , expinfrav1 .MIGReadyCondition )
238241
239- return ctrl.Result {}, nil
242+ igmInstances , err := instancegroupmanagers .New (machinePoolScope ).ListInstances (ctx , igm )
243+ if err != nil {
244+ log .Error (err , "Error listing instances in instanceGroupManager" )
245+ return ctrl.Result {}, err
246+ }
247+
248+ providerIDList := make ([]string , len (igmInstances ))
249+
250+ for i , instance := range igmInstances {
251+ providerID := ""
252+
253+ // Convert instance URL to providerID format
254+ u := instance .Instance
255+ u = strings .TrimPrefix (u , "https://www.googleapis.com/compute/v1/" )
256+ tokens := strings .Split (u , "/" )
257+ if len (tokens ) == 6 && tokens [0 ] == "projects" && tokens [2 ] == "zones" && tokens [4 ] == "instances" {
258+ providerID = fmt .Sprintf ("gce://%s/%s/%s" , tokens [1 ], tokens [3 ], tokens [5 ])
259+ } else {
260+ return ctrl.Result {}, fmt .Errorf ("unexpected instance URL format: %s" , instance .Instance )
261+ }
262+
263+ providerIDList [i ] = providerID
264+ }
265+
266+ // FUTURE: do we need to verify that the instances are actually running / set ProviderIDList?
267+ machinePoolScope .GCPMachinePool .Spec .ProviderIDList = providerIDList
268+ machinePoolScope .GCPMachinePool .Status .Replicas = int32 (len (providerIDList ))
269+ machinePoolScope .GCPMachinePool .Status .Ready = true
270+
271+ return ctrl.Result {
272+ // Regularly update `AWSMachine` objects, for example if ASG was scaled or refreshed instances
273+ // TODO: Requeueing interval can be removed or prolonged once reconciliation of ASG EC2 instances
274+ // can be triggered by events (e.g. with feature gate `EventBridgeInstanceState`).
275+ // See https://github.com/kubernetes-sigs/cluster-api-provider-aws/issues/5323.
276+ RequeueAfter : 1 * time .Minute ,
277+ }, nil
278+
279+ // return ctrl.Result{}, nil
240280}
241281
242282func (r * GCPMachinePoolReconciler ) reconcileDelete (ctx context.Context , machinePoolScope * scope.MachinePoolScope ) error {
0 commit comments