...

Source file src/edge-infra.dev/pkg/k8s/runtime/controller/reconcile/reconcile_request.go

Documentation: edge-infra.dev/pkg/k8s/runtime/controller/reconcile

     1  package reconcile
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
     8  	"k8s.io/apimachinery/pkg/runtime"
     9  
    10  	edgeunstructured "edge-infra.dev/pkg/k8s/unstructured"
    11  )
    12  
    13  const (
    14  	// ReconcileRequestAnnotation is the annotation used for triggering a
    15  	// reconciliation outside of a defined schedule. The value is interpreted as
    16  	// a token, and any change in value SHOULD trigger a reconciliation.
    17  	ReconcileRequestAnnotation string = "reconcile.f8n.ncr.com/requestedAt"
    18  )
    19  
    20  var ErrLastHandledReconcileAtNotFound = errors.New("last handled reconcile at not found")
    21  
    22  // RequestStatus is a struct to embed in a status type, so that all types using the mechanism have the same
    23  // field. Use it like this:
    24  //
    25  //		type FooStatus struct {
    26  //	 		reconcile.RequestStatus `json:",inline"`
    27  //	 		// other status fields...
    28  //		}
    29  type RequestStatus struct {
    30  	// LastHandledReconcileAt holds the value of the most recent
    31  	// reconcile request value, so a change of the annotation value
    32  	// can be detected.
    33  	// +optional
    34  	LastHandledReconcileAt string `json:"lastHandledReconcileAt,omitempty"`
    35  }
    36  
    37  // GetStatusLastHandledReconcileAt returns the status.lastHandledReconcileAt
    38  // value of a given runtime object, if present.
    39  func GetStatusLastHandledReconcileAt(obj runtime.Object) (string, error) {
    40  	u, err := edgeunstructured.FromRuntime(obj)
    41  	if err != nil {
    42  		return "", err
    43  	}
    44  	ra, found, err := unstructured.NestedString(u.Object, "status", "lastHandledReconcileAt")
    45  	if err != nil {
    46  		return "", err
    47  	}
    48  	if !found {
    49  		return "", fmt.Errorf("%w", ErrLastHandledReconcileAtNotFound)
    50  	}
    51  	return ra, nil
    52  }
    53  
    54  // SetStatusLastHandledReconcileAt sets the status.lastHandledReconcileAt value
    55  // of a given runtime object.
    56  func SetStatusLastHandledReconcileAt(obj runtime.Object, val string) error {
    57  	u, err := edgeunstructured.FromRuntime(obj)
    58  	if err != nil {
    59  		return err
    60  	}
    61  	if err := unstructured.SetNestedField(u.Object, val, "status", "lastHandledReconcileAt"); err != nil {
    62  		return err
    63  	}
    64  	return runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, obj)
    65  }
    66  
    67  // GetAnnotation returns a value for the reconciliation request annotation,
    68  // which can be used to detect changes; and, a boolean indicating whether the
    69  // annotation was set.
    70  func GetAnnotation(annotations map[string]string) (string, bool) {
    71  	requestedAt, ok := annotations[ReconcileRequestAnnotation]
    72  	return requestedAt, ok
    73  }
    74  

View as plain text