-
Notifications
You must be signed in to change notification settings - Fork 98
/
status_types.go
126 lines (108 loc) · 3.5 KB
/
status_types.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
/*
Copyright 2022 The Numaproj Authors.
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 (
"reflect"
"sort"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// ConditionType is a valid value of Condition.Type
type ConditionType string
const (
// ConditionReady indicates the resource is ready.
ConditionReady ConditionType = "Ready"
)
// Status is a common structure which can be used for Status field.
type Status struct {
// Conditions are the latest available observations of a resource's current state.
// +optional
// +patchMergeKey=type
// +patchStrategy=merge
Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,1,rep,name=conditions"`
}
// InitializeConditions initializes the conditions to Unknown
func (s *Status) InitializeConditions(conditionTypes ...ConditionType) {
for _, t := range conditionTypes {
c := metav1.Condition{
Type: string(t),
Status: metav1.ConditionUnknown,
Reason: "Unknown",
}
s.setCondition(c)
}
}
// setCondition sets a condition
func (s *Status) setCondition(condition metav1.Condition) {
var conditions []metav1.Condition
for _, c := range s.Conditions {
if c.Type != condition.Type {
conditions = append(conditions, c)
} else {
condition.LastTransitionTime = c.LastTransitionTime
if reflect.DeepEqual(&condition, &c) {
return
}
}
}
condition.LastTransitionTime = metav1.NewTime(time.Now())
conditions = append(conditions, condition)
// Sort for easy read
sort.Slice(conditions, func(i, j int) bool { return conditions[i].Type < conditions[j].Type })
s.Conditions = conditions
}
func (s *Status) markTypeStatus(t ConditionType, status metav1.ConditionStatus, reason, message string) {
s.setCondition(metav1.Condition{
Type: string(t),
Status: status,
Reason: reason,
Message: message,
})
}
// MarkTrue sets the status of t to true
func (s *Status) MarkTrue(t ConditionType) {
s.markTypeStatus(t, metav1.ConditionTrue, "Successful", "Successful")
}
// MarkTrueWithReason sets the status of t to true with reason
func (s *Status) MarkTrueWithReason(t ConditionType, reason, message string) {
s.markTypeStatus(t, metav1.ConditionTrue, reason, message)
}
// MarkFalse sets the status of t to fasle
func (s *Status) MarkFalse(t ConditionType, reason, message string) {
s.markTypeStatus(t, metav1.ConditionFalse, reason, message)
}
// MarkUnknown sets the status of t to unknown
func (s *Status) MarkUnknown(t ConditionType, reason, message string) {
s.markTypeStatus(t, metav1.ConditionUnknown, reason, message)
}
// GetCondition returns the condition of a condition type
func (s *Status) GetCondition(t ConditionType) *metav1.Condition {
for _, c := range s.Conditions {
if c.Type == string(t) {
return &c
}
}
return nil
}
// IsReady returns true when all the conditions are true
func (s *Status) IsReady() bool {
if len(s.Conditions) == 0 {
return false
}
for _, c := range s.Conditions {
if c.Status != metav1.ConditionTrue {
return false
}
}
return true
}