...

Source file src/edge-infra.dev/pkg/k8s/object/status.go

Documentation: edge-infra.dev/pkg/k8s/object

     1  package object
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  
     7  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     8  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
     9  	"k8s.io/apimachinery/pkg/runtime"
    10  
    11  	edgeunstructured "edge-infra.dev/pkg/k8s/unstructured"
    12  )
    13  
    14  var (
    15  	ErrObservedGenerationNotFound = errors.New("observed generation not found")
    16  	ErrRequeueIntervalNotFound    = errors.New("requeue interval not found")
    17  )
    18  
    19  // GetStatusObservedGeneration returns the status.observedGeneration of a given
    20  // runtime object.
    21  func GetStatusObservedGeneration(obj runtime.Object) (int64, error) {
    22  	u, err := edgeunstructured.FromRuntime(obj)
    23  	if err != nil {
    24  		return 0, err
    25  	}
    26  	og, found, err := unstructured.NestedInt64(u.Object, "status", "observedGeneration")
    27  	if err != nil {
    28  		return 0, err
    29  	}
    30  	if !found {
    31  		return 0, ErrObservedGenerationNotFound
    32  	}
    33  	return og, nil
    34  }
    35  
    36  // GetRequeueInterval returns the spec.interval of a given runtime object, if
    37  // present.
    38  func GetRequeueInterval(obj runtime.Object) (time.Duration, error) {
    39  	period := time.Second
    40  	u, err := edgeunstructured.FromRuntime(obj)
    41  	if err != nil {
    42  		return period, err
    43  	}
    44  	interval, found, err := unstructured.NestedString(u.Object, "spec", "interval")
    45  	if err != nil {
    46  		return period, err
    47  	}
    48  	if !found {
    49  		return period, ErrRequeueIntervalNotFound
    50  	}
    51  	return time.ParseDuration(interval)
    52  }
    53  
    54  // GetConditions returns the list of conditions from an Unstructured object.
    55  //
    56  // NOTE: Due to the constraints of JSON-unmarshal, this operation is to be considered best effort.
    57  // In more details:
    58  //   - Errors during JSON-unmarshal are ignored and a empty collection list is returned.
    59  //   - It's not possible to detect if the object has an empty condition list or if it does not implement conditions;
    60  //     in both cases the operation returns an empty slice.
    61  //   - If the object doesn't implement status conditions as defined in GitOps Toolkit API,
    62  //     JSON-unmarshal matches incoming object keys to the keys; this can lead to to conditions values partially set.
    63  func GetConditions(obj runtime.Object) []metav1.Condition {
    64  	u, err := edgeunstructured.FromRuntime(obj)
    65  	if err != nil {
    66  		return nil
    67  	}
    68  	conditions := []metav1.Condition{}
    69  	if err := edgeunstructured.UnmarshalField(u, &conditions, "status", "conditions"); err != nil {
    70  		return nil
    71  	}
    72  	return conditions
    73  }
    74  

View as plain text