/
getter.go
104 lines (88 loc) · 2.9 KB
/
getter.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
/*
Copyright 2022 The KCP 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 conditions
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// Getter interface defines methods that a Cluster API object should implement in order to
// use the conditions package for getting conditions.
type Getter interface {
client.Object
// GetConditions returns the list of conditions for a cluster API object.
GetConditions() []metav1.Condition
}
// Get returns the condition with the given type, if the condition does not exists,
// it returns nil.
func Get(from Getter, t string) *metav1.Condition {
conditions := from.GetConditions()
if conditions == nil {
return nil
}
for _, condition := range conditions {
if condition.Type == t {
return &condition
}
}
return nil
}
// Has returns true if a condition with the given type exists.
func Has(from Getter, t string) bool {
return Get(from, t) != nil
}
// IsTrue is true if the condition with the given type is True, otherwise it return false
// if the condition is not True or if the condition does not exist (is nil).
func IsTrue(from Getter, t string) bool {
if c := Get(from, t); c != nil {
return c.Status == metav1.ConditionTrue
}
return false
}
// IsFalse is true if the condition with the given type is False, otherwise it return false
// if the condition is not False or if the condition does not exist (is nil).
func IsFalse(from Getter, t string) bool {
if c := Get(from, t); c != nil {
return c.Status == metav1.ConditionFalse
}
return false
}
// IsUnknown is true if the condition with the given type is Unknown or if the condition
// does not exist (is nil).
func IsUnknown(from Getter, t string) bool {
if c := Get(from, t); c != nil {
return c.Status == metav1.ConditionUnknown
}
return true
}
// GetReason returns a nil safe string of Reason for the condition with the given type.
func GetReason(from Getter, t string) string {
if c := Get(from, t); c != nil {
return c.Reason
}
return ""
}
// GetMessage returns a nil safe string of Message.
func GetMessage(from Getter, t string) string {
if c := Get(from, t); c != nil {
return c.Message
}
return ""
}
// GetLastTransitionTime returns the condition Severity or nil if the condition
// does not exist (is nil).
func GetLastTransitionTime(from Getter, t string) *metav1.Time {
if c := Get(from, t); c != nil {
return &c.LastTransitionTime
}
return nil
}