Skip to content

Commit

Permalink
fix: patch the status and use APIReader to get resource
Browse files Browse the repository at this point in the history
This should hopefully avoid getting into split-brain.

Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
(cherry picked from commit 698e669)
  • Loading branch information
smira authored and rsmitty committed Dec 14, 2021
1 parent d606d32 commit f5f5b2d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
26 changes: 13 additions & 13 deletions controllers/taloscontrolplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ type ControlPlane struct {
// TalosControlPlaneReconciler reconciles a TalosControlPlane object
type TalosControlPlaneReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
APIReader client.Reader
Log logr.Logger
Scheme *runtime.Scheme
}

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

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

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

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

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

addresses := []string{}
for _, machine := range machines {
if len(machine.Status.Addresses) == 0 {
continue
}
found := false

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

found = true

break
}
}

if !found {
return fmt.Errorf("machine %q doesn't have an InternalIP address yet", machine.Name)
}
}

if len(addresses) == 0 {
Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ func main() {
}

if err = (&controllers.TalosControlPlaneReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("TalosControlPlane"),
Scheme: mgr.GetScheme(),
Client: mgr.GetClient(),
APIReader: mgr.GetAPIReader(),
Log: ctrl.Log.WithName("controllers").WithName("TalosControlPlane"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr, controller.Options{MaxConcurrentReconciles: 10}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "TalosControlPlane")
os.Exit(1)
Expand Down

0 comments on commit f5f5b2d

Please sign in to comment.