/
status.go
137 lines (114 loc) · 4.45 KB
/
status.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
/*
Copyright 2020 Skyscanner Limited.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha1
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
// ConditionType represents a condition type.
// +kubebuilder:validation:Enum=Ready
type ConditionType string
const (
// ConditionReady indicates that a is ready for use.
ConditionReady ConditionType = "Ready"
)
// ConditionStatus represents a condition's status.
// +kubebuilder:validation:Enum=True;False;Unknown
type ConditionStatus string
// These are valid condition statuses. "ConditionTrue" means a resource is in
// the condition; "ConditionFalse" means a resource is not in the condition;
// "ConditionUnknown" means kubernetes can't decide if a resource is in the
// condition or not. In the future, we could add other intermediate
// conditions, e.g. ConditionDegraded.
const (
// ConditionTrue represents the fact that a given condition is true
ConditionTrue ConditionStatus = "True"
// ConditionFalse represents the fact that a given condition is false
ConditionFalse ConditionStatus = "False"
// ConditionUnknown represents the fact that a given condition is unknown
ConditionUnknown ConditionStatus = "Unknown"
)
// Condition contains condition information.
type Condition struct {
// Type of the condition, currently ('Ready').
Type ConditionType `json:"type"`
// Status of the condition, one of ('True', 'False', 'Unknown').
// +kubebuilder:validation:Enum=True;False;Unknown
Status ConditionStatus `json:"status"`
// LastTransitionTime is the timestamp corresponding to the last status
// change of this condition.
// +optional
LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty"`
// Reason is a brief machine readable explanation for the condition's last
// transition.
// +optional
Reason string `json:"reason,omitempty"`
// Message is a human readable description of the details of the last
// transition, complementing reason.
// +optional
Message string `json:"message,omitempty"`
}
// NewCondition creates a new condition
func NewCondition(condType ConditionType, status ConditionStatus, reason, msg string) Condition {
return Condition{
Type: condType,
Status: status,
LastTransitionTime: metav1.Now(),
Reason: reason,
Message: msg,
}
}
// Status defines the observed state of on object
type Status struct {
// +optional
Conditions []Condition `json:"conditions,omitempty"`
}
// GetCondition returns a Condition with the provided type if it exists.
// returns nil otherwise.
func (status *Status) GetCondition(condType ConditionType) *Condition {
for _, c := range status.Conditions {
if c.Type == condType {
return &c
}
}
return nil
}
// SetCondition adds/replaces the given condition in the KMSIssuer status. If the condition that we
// are about to add already exists and has the same status and reason then we are not going to update.
func (status *Status) SetCondition(condition *Condition) {
currentCond := status.GetCondition(condition.Type)
if currentCond != nil && currentCond.Status == condition.Status && currentCond.Reason == condition.Reason {
return
}
status.Conditions = append(status.filterOutCondition(condition.Type), *condition)
}
// RemoveCondition removes the condition with the provided type from the replicaset status.
func (status *Status) RemoveCondition(condType ConditionType) {
status.Conditions = status.filterOutCondition(condType)
}
// filterOutCondition returns a new slice of conditions without conditions with the provided type.
func (status *Status) filterOutCondition(condType ConditionType) []Condition {
var newConditions []Condition
for _, c := range status.Conditions {
if c.Type == condType {
continue
}
newConditions = append(newConditions, c)
}
return newConditions
}
// IsReady returns true if the ReadyConditions is true.
func (status *Status) IsReady() bool {
condition := status.GetCondition(ConditionReady)
if condition == nil {
return false
}
return condition.Status == ConditionTrue
}