...

Source file src/edge-infra.dev/pkg/edge/controllers/sequel/reconciler.go

Documentation: edge-infra.dev/pkg/edge/controllers/sequel

     1  package sequel
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"k8s.io/client-go/dynamic"
     7  	"sigs.k8s.io/cli-utils/pkg/kstatus/watcher"
     8  	ctrl "sigs.k8s.io/controller-runtime"
     9  	"sigs.k8s.io/controller-runtime/pkg/client"
    10  
    11  	"edge-infra.dev/pkg/k8s/runtime/controller/metrics"
    12  	"edge-infra.dev/pkg/k8s/runtime/controller/reconcile"
    13  	"edge-infra.dev/pkg/k8s/runtime/patch"
    14  	"edge-infra.dev/pkg/k8s/runtime/sap"
    15  )
    16  
    17  const (
    18  	// reconciler name for all reconcilers.
    19  	controllerName = "sequel"
    20  )
    21  
    22  // Reconciler implements the base reconcile functionality needed for controllers.
    23  type Reconciler struct {
    24  	client.Client
    25  
    26  	// Name is the controller's name, used to consistently represent the controller
    27  	// in various cluster interactions, e.g., as field manager
    28  	Name string
    29  
    30  	Namespace string
    31  
    32  	// ResourceManager is a server-side apply client that can poll for the resources
    33  	// we are applying to the server
    34  	ResourceManager *sap.ResourceManager
    35  
    36  	// OwnerGroupLabel is the resource group used to build the full label applied
    37  	// to owned resources applied via sap.
    38  	OwnerGroupLabel string
    39  
    40  	// Conditions is the reconcile conditions configuration that defines the
    41  	// condition management behavior for this reconciler
    42  	Conditions reconcile.Conditions
    43  
    44  	// Metrics records condition, duration, and suspension metrics by default
    45  	Metrics metrics.Metrics
    46  }
    47  
    48  func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
    49  	r.Name = controllerName
    50  	r.Client = mgr.GetClient()
    51  
    52  	if r.OwnerGroupLabel == "" {
    53  		return fmt.Errorf("reconciler OwnerGroupLabel must be set")
    54  	}
    55  	if r.Conditions.IsEmpty() {
    56  		return fmt.Errorf("non-empty target condition and conditions to summarize "+
    57  			"reconcile.Conditions is required. reconcile.Conditions: %v", r.Conditions)
    58  	}
    59  
    60  	d, err := dynamic.NewForConfig(mgr.GetConfig())
    61  	if err != nil {
    62  		return fmt.Errorf("failed to create config for dynamic client: %w", err)
    63  	}
    64  	r.ResourceManager = sap.NewResourceManager(
    65  		r.Client,
    66  		watcher.NewDefaultStatusWatcher(d, mgr.GetRESTMapper()),
    67  		sap.Owner{Field: r.Name, Group: r.OwnerGroupLabel},
    68  	)
    69  
    70  	return nil
    71  }
    72  
    73  func (r *Reconciler) PatchOpts() []patch.Option {
    74  	return []patch.Option{
    75  		patch.WithOwnedConditions{Conditions: r.Conditions.Owned},
    76  		patch.WithFieldOwner(r.Name),
    77  	}
    78  }
    79  

View as plain text