github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/controllers/core/uiresource/reconciler.go (about)

     1  package uiresource
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	apierrors "k8s.io/apimachinery/pkg/api/errors"
     8  	ctrl "sigs.k8s.io/controller-runtime"
     9  	"sigs.k8s.io/controller-runtime/pkg/builder"
    10  	ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
    11  	"sigs.k8s.io/controller-runtime/pkg/reconcile"
    12  
    13  	"github.com/tilt-dev/tilt/internal/hud/server"
    14  	"github.com/tilt-dev/tilt/internal/store"
    15  	"github.com/tilt-dev/tilt/internal/store/uiresources"
    16  	"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
    17  )
    18  
    19  // The uiresource.Reconciler is not a real reconciler because UIResource is not
    20  // a real API object.
    21  //
    22  // It's a fake status object that reports the Status of the legacy engine. The
    23  // uiresource.Reconciler wathces that status and broadcasts it to the legacy web
    24  // UI.
    25  type Reconciler struct {
    26  	client ctrlclient.Client
    27  	wsList *server.WebsocketList
    28  	store  store.RStore
    29  }
    30  
    31  var _ reconcile.Reconciler = &Reconciler{}
    32  
    33  func NewReconciler(client ctrlclient.Client, wsList *server.WebsocketList, store store.RStore) *Reconciler {
    34  	return &Reconciler{
    35  		client: client,
    36  		wsList: wsList,
    37  		store:  store,
    38  	}
    39  }
    40  
    41  func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
    42  	resource := &v1alpha1.UIResource{}
    43  	err := r.client.Get(ctx, req.NamespacedName, resource)
    44  	if err != nil && !apierrors.IsNotFound(err) {
    45  		return ctrl.Result{}, fmt.Errorf("uiresource reconcile: %v", err)
    46  	}
    47  
    48  	if apierrors.IsNotFound(err) || resource.ObjectMeta.DeletionTimestamp != nil {
    49  		r.wsList.ForEach(func(ws *server.WebsocketSubscriber) {
    50  			ws.SendUIResourceUpdate(ctx, req.NamespacedName, nil)
    51  		})
    52  
    53  		r.store.Dispatch(uiresources.NewUIResourceDeleteAction(req.Name))
    54  		return ctrl.Result{}, nil
    55  	}
    56  
    57  	r.store.Dispatch(uiresources.NewUIResourceUpsertAction(resource))
    58  
    59  	r.wsList.ForEach(func(ws *server.WebsocketSubscriber) {
    60  		ws.SendUIResourceUpdate(ctx, req.NamespacedName, resource)
    61  	})
    62  
    63  	return ctrl.Result{}, nil
    64  }
    65  
    66  func (r *Reconciler) CreateBuilder(mgr ctrl.Manager) (*builder.Builder, error) {
    67  	b := ctrl.NewControllerManagedBy(mgr).
    68  		For(&v1alpha1.UIResource{})
    69  
    70  	return b, nil
    71  }