/
alert_lifecycle.go
141 lines (119 loc) · 3.46 KB
/
alert_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
132
133
134
135
136
137
138
139
140
141
/*
* 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 (alert *Alert) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(alert).
Complete()
}
//==============================================================================
// Validation
//==============================================================================
//==============================================================================
// Finalizer
//==============================================================================
func (alert *Alert) HasFinalizer() bool {
return util.HasFin(&alert.ObjectMeta, metav1.GroupName)
}
func (alert *Alert) AddFinalizer() { util.AddFin(&alert.ObjectMeta, metav1.GroupName) }
func (alert *Alert) RemoveFinalizer() { util.RemoveFin(&alert.ObjectMeta, metav1.GroupName) }
// Merge or update condition
func (alert *Alert) CreateOrUpdateCond(cond metav1.Condition) {
i := alert.GetCondIdx(cond.Type)
now := metav1.Now()
if i == -1 { // not found
cond.LastTransitionTime = now
alert.Status.Conditions = append(alert.Status.Conditions, cond)
return
}
// else we already have the condition, update it
current := alert.Status.Conditions[i]
current.Message = cond.Message
current.Reason = cond.Reason
current.LastTransitionTime = now
if current.Status != cond.Status {
current.Status = cond.Status
}
alert.Status.Conditions[i] = current
}
func (alert Alert) GetCondIdx(t string) int {
for i, v := range alert.Status.Conditions {
if v.Type == t {
return i
}
}
return -1
}
func (alert Alert) GetCond(t string) metav1.Condition {
for _, v := range alert.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 (alert Alert) IsReady() bool {
return alert.GetCond(AlertSent).Status == metav1.ConditionTrue
}
func (alert Alert) RootURI() string {
return fmt.Sprintf("tenant/%s/apitokens/%s", alert.Namespace, alert.Name)
}
func (alert Alert) ManifestURI() string {
return fmt.Sprintf("%s/%s-apitoken.yaml", alert.RootURI(), alert.Name)
}
func ParseAlertYaml(content []byte) (*Alert, error) {
requiredObj, err := runtime.Decode(scheme.Codecs.UniversalDecoder(SchemeGroupVersion), content)
if err != nil {
return nil, err
}
r := requiredObj.(*Alert)
return r, nil
}
func (alert *Alert) MarkFailed(reason string, err string) {
alert.CreateOrUpdateCond(metav1.Condition{
Type: AlertSent,
Status: metav1.ConditionFalse,
Message: err,
Reason: reason,
})
}
func (alert *Alert) MarkSent() {
alert.CreateOrUpdateCond(metav1.Condition{
Type: string(AlertSent),
Status: metav1.ConditionTrue,
Reason: string(AlertSent),
})
}
func (alert Alert) GetStatus() proto.Message {
return &alert.Status
}
func (alert Alert) GetObservedGeneration() int64 {
return alert.Status.ObservedGeneration
}
func (alert *Alert) SetObservedGeneration(generation int64) {
alert.Status.ObservedGeneration = generation
}
func (alert *Alert) SetUpdatedAt(time *metav1.Time) {
alert.Status.UpdatedAt = time
}
func (alert *Alert) SetStatus(status interface{}) {
alert.Status = *status.(*AlertStatus)
}