generated from cloudoperators/repository-template
/
conditions.go
149 lines (127 loc) · 5.53 KB
/
conditions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors
// SPDX-License-Identifier: Apache-2.0
package v1alpha1
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// ConditionType is a valid condition of a resource.
type ConditionType string
// ConditionReason is a valid reason for a condition of a resource.
type ConditionReason string
const (
// ReadyCondition reflects the overall readiness status of a resource.
ReadyCondition ConditionType = "Ready"
// Mirroring corev1.NodeConditionType values
// NodeMemoryPressure means the kubelet is under pressure due to insufficient available memory.
NodeMemoryPressure ConditionType = "MemoryPressure"
// NodeDiskPressure means the kubelet is under pressure due to insufficient available disk.
NodeDiskPressure ConditionType = "DiskPressure"
// NodePIDPressure means the kubelet is under pressure due to insufficient available PID.
NodePIDPressure ConditionType = "PIDPressure"
// NodeNetworkUnavailable means that network for the node is not correctly configured.
NodeNetworkUnavailable ConditionType = "NetworkUnavailable"
)
// Condition contains additional information on the state of a resource.
type Condition struct {
// Type of the condition.
Type ConditionType `json:"type"`
// Status of the condition.
Status metav1.ConditionStatus `json:"status"`
// Reason is a one-word, CamelCase reason for the condition's last transition.
Reason ConditionReason `json:"reason,omitempty"`
// LastTransitionTime is the last time the condition transitioned from one status to another.
LastTransitionTime metav1.Time `json:"lastTransitionTime"`
// Message is an optional human readable message indicating details about the last transition.
Message string `json:"message,omitempty"`
}
// NewCondition returns a Condition with the given type, status, reason and message. LastTransitionTime is set to now.
func NewCondition(condition ConditionType, status metav1.ConditionStatus, reason ConditionReason, message string) Condition {
return Condition{
Type: condition,
Status: status,
Reason: reason,
LastTransitionTime: metav1.Now(),
Message: message,
}
}
// TrueCondition returns a Condition with ConditionTrue and the given type, reason and message. LastTransitionTime is set to now.
func TrueCondition(t ConditionType, reason ConditionReason, message string) Condition {
return NewCondition(t, metav1.ConditionTrue, reason, message)
}
// NewTrue returns a Condition with ConditionFalse and the given type, reason and message. LastTransitionTime is set to now.
func FalseCondition(t ConditionType, reason ConditionReason, message string) Condition {
return NewCondition(t, metav1.ConditionFalse, reason, message)
}
// UnknownCondition returns a Condition with ConditionUnknown and the given type, reason and message. LastTransitionTime is set to now.
func UnknownCondition(t ConditionType, reason ConditionReason, message string) Condition {
return NewCondition(t, metav1.ConditionUnknown, reason, message)
}
// Equal returns true if the condition is identical to the supplied condition,
// ignoring the LastTransitionTime.
func (c Condition) Equal(other Condition) bool {
return c.Type == other.Type &&
c.Status == other.Status &&
c.Reason == other.Reason &&
c.Message == other.Message
}
// IsTrue returns true if the condition is true.
func (c Condition) IsTrue() bool {
return c.Status == metav1.ConditionTrue
}
// IsFalse returns true if the condition is false.
func (c Condition) IsFalse() bool {
return c.Status == metav1.ConditionFalse
}
// A StatusConditions contains a list of conditions.
// Only one condition of a given type may exist in the list.
type StatusConditions struct {
// +listType="map"
// +listMapKey=type
Conditions []Condition `json:"conditions,omitempty"`
}
// SetConditions sets the corresponding Conditions in StatusConditions to newConditions.
// If the LastTransitionTime of a new Condition is not set, it is set to the current time.
// If a Condition of the same ConditionType exists, it is updated. The LastTransitionTime is only updated if the Status changes.
// If a condition of the same ConditionType does not exist, it is appended.
func (sc *StatusConditions) SetConditions(conditionsToSet ...Condition) {
for _, conditionToSet := range conditionsToSet {
if conditionToSet.LastTransitionTime.IsZero() {
conditionToSet.LastTransitionTime = metav1.Now()
}
exists := false
for idx, currentCondition := range sc.Conditions {
// if the condition already exists, update it if necessary
if currentCondition.Type == conditionToSet.Type {
exists = true
if !currentCondition.Equal(conditionToSet) {
// do not update LastTransitionTime if status does not change
if currentCondition.Status == conditionToSet.Status {
conditionToSet.LastTransitionTime = currentCondition.LastTransitionTime
}
(sc.Conditions)[idx] = conditionToSet
}
break
}
}
// if the condition does not exist, append it
if !exists {
sc.Conditions = append(sc.Conditions, conditionToSet)
}
}
}
// GetConditionByType returns the condition of the given type, if it exists.
func (sc *StatusConditions) GetConditionByType(conditionType ConditionType) *Condition {
if len(sc.Conditions) == 0 {
return nil
}
for _, condition := range sc.Conditions {
if condition.Type == conditionType {
return &condition
}
}
return nil
}
// IsReadyTrue returns true if the Ready condition is true.
func (sc *StatusConditions) IsReadyTrue() bool {
return sc.GetConditionByType(ReadyCondition).IsTrue()
}