Skip to content

Commit b2c13ed

Browse files
committed
Hold adding to instance group until machine is provisioned
1 parent 5e0eb53 commit b2c13ed

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

cloud/interfaces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ type MachineGetter interface {
8686
Project() string
8787
Role() string
8888
IsControlPlane() bool
89+
IsMachineProvisioned() bool
8990
ControlPlaneGroupName() string
9091
GetInstanceID() *string
9192
GetProviderID() string

cloud/scope/machine.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ func (m *MachineScope) IsControlPlane() bool {
140140
return IsControlPlaneMachine(m.Machine)
141141
}
142142

143+
// IsMachineProvisioned returns true if the machine has been provisioned.
144+
func (m *MachineScope) IsMachineProvisioned() bool {
145+
// return m.Machine.Status.GetTypedPhase() == clusterv1.MachinePhaseProvisioned
146+
for _, condition := range m.Machine.Status.Conditions {
147+
if condition.Type == clusterv1.MachineNodeHealthyCondition && condition.Reason == clusterv1.WaitingForNodeRefReason {
148+
return true
149+
}
150+
}
151+
return false
152+
}
153+
143154
// Role returns the machine role from the labels.
144155
func (m *MachineScope) Role() string {
145156
if IsControlPlaneMachine(m.Machine) {

cloud/services/compute/instances/reconcile.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,13 @@ func (s *Service) Reconcile(ctx context.Context) error {
8989
s.scope.SetInstanceStatus(infrav1.InstanceStatus(instance.Status))
9090

9191
if s.scope.IsControlPlane() {
92-
if err := s.registerControlPlaneInstance(ctx, instance); err != nil {
93-
return err
92+
if s.scope.IsMachineProvisioned() {
93+
log.V(2).Info("[DEBUG] Registering control plane instance in the instancegroup", "name", instance.Name)
94+
if err := s.registerControlPlaneInstance(ctx, instance); err != nil {
95+
return err
96+
}
97+
} else {
98+
log.V(2).Info("[DEBUG] Skipping registering control plane instance in the instancegroup because machine is not yet provisioned", "name", instance.Name)
9499
}
95100
}
96101

controllers/gcpmachine_controller.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)