Skip to content

Commit 698e669

Browse files
committed
fix: patch the status and use APIReader to get resource
This should hopefully avoid getting into split-brain. Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
1 parent e0041f6 commit 698e669

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

controllers/taloscontrolplane_controller.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ type ControlPlane struct {
6060
// TalosControlPlaneReconciler reconciles a TalosControlPlane object
6161
type TalosControlPlaneReconciler struct {
6262
client.Client
63-
Log logr.Logger
64-
Scheme *runtime.Scheme
63+
APIReader client.Reader
64+
Log logr.Logger
65+
Scheme *runtime.Scheme
6566
}
6667

6768
func (r *TalosControlPlaneReconciler) SetupWithManager(mgr ctrl.Manager, options controller.Options) error {
@@ -90,7 +91,7 @@ func (r *TalosControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.Re
9091

9192
// Fetch the TalosControlPlane instance.
9293
tcp := &controlplanev1.TalosControlPlane{}
93-
if err := r.Client.Get(ctx, req.NamespacedName, tcp); err != nil {
94+
if err := r.APIReader.Get(ctx, req.NamespacedName, tcp); err != nil {
9495
if apierrors.IsNotFound(err) {
9596
return ctrl.Result{}, nil
9697
}
@@ -140,13 +141,8 @@ func (r *TalosControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.Re
140141

141142
// patch and return right away instead of reusing the main defer,
142143
// because the main defer may take too much time to get cluster status
143-
// Patch ObservedGeneration only if the reconciliation completed successfully
144-
patchOpts := []patch.Option{}
145-
if reterr == nil {
146-
patchOpts = append(patchOpts, patch.WithStatusObservedGeneration{})
147-
}
148144

149-
if err := patchTalosControlPlane(ctx, patchHelper, tcp, patchOpts...); err != nil {
145+
if err := patchTalosControlPlane(ctx, patchHelper, tcp, patch.WithStatusObservedGeneration{}); err != nil {
150146
logger.Error(err, "Failed to add finalizer to TalosControlPlane")
151147
return ctrl.Result{}, err
152148
}
@@ -172,7 +168,7 @@ func (r *TalosControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.Re
172168
}
173169

174170
// Always attempt to Patch the TalosControlPlane object and status after each reconciliation.
175-
if err := r.Client.Status().Update(ctx, tcp); err != nil {
171+
if err := patchTalosControlPlane(ctx, patchHelper, tcp, patch.WithStatusObservedGeneration{}); err != nil {
176172
logger.Error(err, "Failed to patch TalosControlPlane")
177173
reterr = kerrors.NewAggregate([]error{reterr, err})
178174
}
@@ -627,17 +623,21 @@ func (r *TalosControlPlaneReconciler) bootstrapCluster(ctx context.Context, clus
627623

628624
addresses := []string{}
629625
for _, machine := range machines {
630-
if len(machine.Status.Addresses) == 0 {
631-
continue
632-
}
626+
found := false
633627

634628
for _, addr := range machine.Status.Addresses {
635629
if addr.Type == clusterv1.MachineInternalIP {
636630
addresses = append(addresses, addr.Address)
637631

632+
found = true
633+
638634
break
639635
}
640636
}
637+
638+
if !found {
639+
return fmt.Errorf("machine %q doesn't have an InternalIP address yet", machine.Name)
640+
}
641641
}
642642

643643
if len(addresses) == 0 {

main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ func main() {
7777
}
7878

7979
if err = (&controllers.TalosControlPlaneReconciler{
80-
Client: mgr.GetClient(),
81-
Log: ctrl.Log.WithName("controllers").WithName("TalosControlPlane"),
82-
Scheme: mgr.GetScheme(),
80+
Client: mgr.GetClient(),
81+
APIReader: mgr.GetAPIReader(),
82+
Log: ctrl.Log.WithName("controllers").WithName("TalosControlPlane"),
83+
Scheme: mgr.GetScheme(),
8384
}).SetupWithManager(mgr, controller.Options{MaxConcurrentReconciles: 10}); err != nil {
8485
setupLog.Error(err, "unable to create controller", "controller", "TalosControlPlane")
8586
os.Exit(1)

0 commit comments

Comments
 (0)