-
Notifications
You must be signed in to change notification settings - Fork 52
/
types_util.go
59 lines (51 loc) · 1.49 KB
/
types_util.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
package v2alpha1
import (
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// SetCondition set condition to status
func (cache *Cache) SetCondition(condition CacheConditionType, status metav1.ConditionStatus, message string) bool {
changed := false
for idx := range cache.Status.Conditions {
c := &cache.Status.Conditions[idx]
if c.Type == condition {
if c.Status != status {
c.Status = status
changed = true
}
if c.Message != message {
c.Message = message
changed = true
}
return changed
}
}
cache.Status.Conditions = append(cache.Status.Conditions, CacheCondition{Type: condition, Status: status, Message: message})
return true
}
// GetCondition return the Status of the given condition or nil if condition is not present
func (cache *Cache) GetCondition(condition CacheConditionType) CacheCondition {
for _, c := range cache.Status.Conditions {
if c.Type.equals(condition) {
return c
}
}
// Absence of condition means `False` value
return CacheCondition{Type: condition, Status: metav1.ConditionFalse}
}
func (cache *Cache) GetCacheName() string {
if cache.Spec.Name != "" {
return cache.Spec.Name
}
return cache.Name
}
func (b *Batch) ConfigMapName() string {
if b.Spec.ConfigMap != nil {
return *b.Spec.ConfigMap
}
return b.Name
}
// equals compares two ConditionType's case insensitive
func (a CacheConditionType) equals(b CacheConditionType) bool {
return strings.EqualFold(strings.ToLower(string(a)), strings.ToLower(string(b)))
}