github.com/argoproj/argo-events@v1.9.1/pkg/apis/common/status_types.go (about)

     1  package common
     2  
     3  import (
     4  	"reflect"
     5  	"sort"
     6  	"time"
     7  
     8  	corev1 "k8s.io/api/core/v1"
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  )
    11  
    12  // ConditionType is a valid value of Condition.Type
    13  type ConditionType string
    14  
    15  const (
    16  	// ConditionReady indicates the resource is ready.
    17  	ConditionReady ConditionType = "Ready"
    18  )
    19  
    20  // Condition contains details about resource state
    21  type Condition struct {
    22  	// Condition type.
    23  	// +required
    24  	Type ConditionType `json:"type" protobuf:"bytes,1,opt,name=type"`
    25  	// Condition status, True, False or Unknown.
    26  	// +required
    27  	Status corev1.ConditionStatus `json:"status" protobuf:"bytes,2,opt,name=status,casttype=k8s.io/api/core/v1.ConditionStatus"`
    28  	// Last time the condition transitioned from one status to another.
    29  	// +optional
    30  	LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty" protobuf:"bytes,3,opt,name=lastTransitionTime"`
    31  	// Unique, this should be a short, machine understandable string that gives the reason
    32  	// for condition's last transition. For example, "ImageNotFound"
    33  	// +optional
    34  	Reason string `json:"reason,omitempty" protobuf:"bytes,4,opt,name=reason"`
    35  	// Human-readable message indicating details about last transition.
    36  	// +optional
    37  	Message string `json:"message,omitempty" protobuf:"bytes,5,opt,name=message"`
    38  }
    39  
    40  // IsTrue tells if the condition is True
    41  func (c *Condition) IsTrue() bool {
    42  	if c == nil {
    43  		return false
    44  	}
    45  	return c.Status == corev1.ConditionTrue
    46  }
    47  
    48  // IsFalse tells if the condition is False
    49  func (c *Condition) IsFalse() bool {
    50  	if c == nil {
    51  		return false
    52  	}
    53  	return c.Status == corev1.ConditionFalse
    54  }
    55  
    56  // IsUnknown tells if the condition is Unknown
    57  func (c *Condition) IsUnknown() bool {
    58  	if c == nil {
    59  		return true
    60  	}
    61  	return c.Status == corev1.ConditionUnknown
    62  }
    63  
    64  // GetReason returns as Reason
    65  func (c *Condition) GetReason() string {
    66  	if c == nil {
    67  		return ""
    68  	}
    69  	return c.Reason
    70  }
    71  
    72  // GetMessage returns a Message
    73  func (c *Condition) GetMessage() string {
    74  	if c == nil {
    75  		return ""
    76  	}
    77  	return c.Message
    78  }
    79  
    80  // Status is a common structure which can be used for Status field.
    81  type Status struct {
    82  	// Conditions are the latest available observations of a resource's current state.
    83  	// +optional
    84  	// +patchMergeKey=type
    85  	// +patchStrategy=merge
    86  	Conditions []Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,1,rep,name=conditions"`
    87  }
    88  
    89  // InitializeConditions initializes the contions to Unknown
    90  func (s *Status) InitializeConditions(conditionTypes ...ConditionType) {
    91  	for _, t := range conditionTypes {
    92  		c := Condition{
    93  			Type:   t,
    94  			Status: corev1.ConditionUnknown,
    95  		}
    96  		s.SetCondition(c)
    97  	}
    98  }
    99  
   100  // SetCondition sets a condition
   101  func (s *Status) SetCondition(condition Condition) {
   102  	var conditions []Condition
   103  	for _, c := range s.Conditions {
   104  		if c.Type != condition.Type {
   105  			conditions = append(conditions, c)
   106  		} else {
   107  			condition.LastTransitionTime = c.LastTransitionTime
   108  			if reflect.DeepEqual(&condition, &c) {
   109  				return
   110  			}
   111  		}
   112  	}
   113  	condition.LastTransitionTime = metav1.NewTime(time.Now())
   114  	conditions = append(conditions, condition)
   115  	// Sort for easy read
   116  	sort.Slice(conditions, func(i, j int) bool { return conditions[i].Type < conditions[j].Type })
   117  	s.Conditions = conditions
   118  }
   119  
   120  func (s *Status) markTypeStatus(t ConditionType, status corev1.ConditionStatus, reason, message string) {
   121  	s.SetCondition(Condition{
   122  		Type:    t,
   123  		Status:  status,
   124  		Reason:  reason,
   125  		Message: message,
   126  	})
   127  }
   128  
   129  // MarkTrue sets the status of t to true
   130  func (s *Status) MarkTrue(t ConditionType) {
   131  	s.markTypeStatus(t, corev1.ConditionTrue, "", "")
   132  }
   133  
   134  // MarkTrueWithReason sets the status of t to true with reason
   135  func (s *Status) MarkTrueWithReason(t ConditionType, reason, message string) {
   136  	s.markTypeStatus(t, corev1.ConditionTrue, reason, message)
   137  }
   138  
   139  // MarkFalse sets the status of t to fasle
   140  func (s *Status) MarkFalse(t ConditionType, reason, message string) {
   141  	s.markTypeStatus(t, corev1.ConditionFalse, reason, message)
   142  }
   143  
   144  // MarkUnknown sets the status of t to unknown
   145  func (s *Status) MarkUnknown(t ConditionType, reason, message string) {
   146  	s.markTypeStatus(t, corev1.ConditionUnknown, reason, message)
   147  }
   148  
   149  // GetCondition returns the condition of a condtion type
   150  func (s *Status) GetCondition(t ConditionType) *Condition {
   151  	for _, c := range s.Conditions {
   152  		if c.Type == t {
   153  			return &c
   154  		}
   155  	}
   156  	return nil
   157  }
   158  
   159  // IsReady returns true when all the conditions are true
   160  func (s *Status) IsReady() bool {
   161  	if len(s.Conditions) == 0 {
   162  		return false
   163  	}
   164  	for _, c := range s.Conditions {
   165  		if !c.IsTrue() {
   166  			return false
   167  		}
   168  	}
   169  	return true
   170  }