github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/controllers/core/configmap/reconciler.go (about) 1 package configmap 2 3 import ( 4 "context" 5 6 apierrors "k8s.io/apimachinery/pkg/api/errors" 7 ctrl "sigs.k8s.io/controller-runtime" 8 "sigs.k8s.io/controller-runtime/pkg/builder" 9 ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" 10 "sigs.k8s.io/controller-runtime/pkg/reconcile" 11 12 "github.com/tilt-dev/tilt/internal/store" 13 "github.com/tilt-dev/tilt/internal/store/configmaps" 14 "github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1" 15 ) 16 17 type Reconciler struct { 18 client ctrlclient.Client 19 store store.RStore 20 } 21 22 var _ reconcile.Reconciler = &Reconciler{} 23 24 func NewReconciler(client ctrlclient.Client, store store.RStore) *Reconciler { 25 return &Reconciler{ 26 client: client, 27 store: store, 28 } 29 } 30 31 func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { 32 cm := &v1alpha1.ConfigMap{} 33 err := r.client.Get(ctx, req.NamespacedName, cm) 34 if err != nil && !apierrors.IsNotFound(err) { 35 return ctrl.Result{}, err 36 } 37 38 if apierrors.IsNotFound(err) || cm.ObjectMeta.DeletionTimestamp != nil { 39 r.store.Dispatch(configmaps.NewConfigMapDeleteAction(req.Name)) 40 return ctrl.Result{}, nil 41 } 42 43 // The apiserver is the source of truth, and will ensure the engine state is up to date. 44 r.store.Dispatch(configmaps.NewConfigMapUpsertAction(cm)) 45 46 return ctrl.Result{}, nil 47 } 48 49 func (r *Reconciler) CreateBuilder(mgr ctrl.Manager) (*builder.Builder, error) { 50 b := ctrl.NewControllerManagedBy(mgr). 51 For(&v1alpha1.ConfigMap{}) 52 53 return b, nil 54 }