/
notifier_lifecycle.go
131 lines (111 loc) · 3.46 KB
/
notifier_lifecycle.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
/*
* Copyright (c) 2019.
*
* Metaprov.com
*/
package v1alpha1
import (
"fmt"
"github.com/gogo/protobuf/proto"
"github.com/metaprov/modelaapi/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
)
func (notifier *Notifier) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(notifier).
Complete()
}
//==============================================================================
// Validation
//==============================================================================
//==============================================================================
// Finalizer
//==============================================================================
func (notifier Notifier) HasFinalizer() bool {
return util.HasFin(¬ifier.ObjectMeta, metav1.GroupName)
}
func (notifier *Notifier) AddFinalizer() { util.AddFin(¬ifier.ObjectMeta, metav1.GroupName) }
func (notifier *Notifier) RemoveFinalizer() { util.RemoveFin(¬ifier.ObjectMeta, metav1.GroupName) }
// Merge or update condition
func (notifier *Notifier) CreateOrUpdateCond(cond metav1.Condition) {
i := notifier.GetCondIdx(cond.Type)
now := metav1.Now()
if i == -1 { // not found
cond.LastTransitionTime = now
notifier.Status.Conditions = append(notifier.Status.Conditions, cond)
return
}
// else we already have the condition, update it
current := notifier.Status.Conditions[i]
current.Message = cond.Message
current.Reason = cond.Reason
current.LastTransitionTime = now
if current.Status != cond.Status {
current.Status = cond.Status
}
notifier.Status.Conditions[i] = current
}
func (notifier Notifier) GetCondIdx(t string) int {
for i, v := range notifier.Status.Conditions {
if v.Type == t {
return i
}
}
return -1
}
func (notifier Notifier) GetCond(t string) metav1.Condition {
for _, v := range notifier.Status.Conditions {
if v.Type == t {
return v
}
}
// if we did not find the condition, we return an unknown object
return metav1.Condition{
Type: t,
Status: metav1.ConditionUnknown,
Reason: "",
Message: "",
}
}
func (notifier Notifier) IsReady() bool {
return notifier.GetCond(string(NotifierReady)).Status == metav1.ConditionTrue
}
func (notifier Notifier) RootURI() string {
return fmt.Sprintf("tenant/%s/notifiers/%s", notifier.Namespace, notifier.Name)
}
func (notifier Notifier) ManifestURI() string {
return fmt.Sprintf("%s/%s-notifier.yaml", notifier.RootURI(), notifier.Name)
}
func ParseNotifierYaml(content []byte) (*Notifier, error) {
requiredObj, err := runtime.Decode(scheme.Codecs.UniversalDecoder(SchemeGroupVersion), content)
if err != nil {
return nil, err
}
r := requiredObj.(*Notifier)
return r, nil
}
func (notifier *Notifier) MarkReady() {
notifier.CreateOrUpdateCond(metav1.Condition{
Type: string(NotifierReady),
Status: metav1.ConditionTrue,
Reason: string(NotifierReady),
})
}
func (notifier Notifier) GetStatus() proto.Message {
return ¬ifier.Status
}
func (notifier Notifier) GetObservedGeneration() int64 {
return notifier.Status.ObservedGeneration
}
func (notifier *Notifier) SetObservedGeneration(generation int64) {
notifier.Status.ObservedGeneration = generation
}
func (notifier *Notifier) SetUpdatedAt(time *metav1.Time) {
notifier.Status.UpdatedAt = time
}
func (notifier *Notifier) SetStatus(status interface{}) {
notifier.Status = *status.(*NotifierStatus)
}