-
Notifications
You must be signed in to change notification settings - Fork 1
/
postmortem_lifecycle.go
116 lines (99 loc) · 2.85 KB
/
postmortem_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
/*
* Copyright (c) 2019.
*
* Metaprov.com
*/
package v1alpha1
import (
"fmt"
team "github.com/metaprov/modelaapi/pkg/apis/team"
"github.com/metaprov/modelaapi/pkg/util"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
)
func (pm *PostMortem) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(pm).
Complete()
}
func (pm *PostMortem) HasFinalizer() bool {
return util.HasFin(&pm.ObjectMeta, team.GroupName)
}
func (pm *PostMortem) AddFinalizer() { util.AddFin(&pm.ObjectMeta, team.GroupName) }
func (pm *PostMortem) RemoveFinalizer() {
util.RemoveFin(&pm.ObjectMeta, team.GroupName)
}
//==============================================================================
// Trackable
//==============================================================================
// Return the on disk rep location
func (pm *PostMortem) RepPath(root string) (string, error) {
return fmt.Sprintf("%s/connections/%s.yaml", root, pm.ObjectMeta.Name), nil
}
func (pm *PostMortem) RootURI() string {
return fmt.Sprintf("tenants/%s/postmortems/%s", pm.Namespace, pm.Name)
}
func (pm *PostMortem) ManifestURI() string {
return fmt.Sprintf("%s/%s-postmortem.yaml", pm.RootURI(), pm.Name)
}
// Merge or update condition
func (pm *PostMortem) CreateOrUpdateCond(cond PostMortemCondition) {
i := pm.GetCondIdx(cond.Type)
now := metav1.Now()
if i == -1 { // not found
cond.LastTransitionTime = &now
pm.Status.Conditions = append(pm.Status.Conditions, cond)
return
}
// else we already have the condition, update it
current := pm.Status.Conditions[i]
current.Message = cond.Message
current.Reason = cond.Reason
current.LastTransitionTime = &now
if current.Status != cond.Status {
current.Status = cond.Status
}
pm.Status.Conditions[i] = current
}
func (pm *PostMortem) GetCondIdx(t PostMortemConditionType) int {
for i, v := range pm.Status.Conditions {
if v.Type == t {
return i
}
}
return -1
}
func (pm *PostMortem) GetCond(t PostMortemConditionType) PostMortemCondition {
for _, v := range pm.Status.Conditions {
if v.Type == t {
return v
}
}
// if we did not find the condition, we return an unknown object
return PostMortemCondition{
Type: t,
Status: corev1.ConditionUnknown,
Reason: "",
Message: "",
}
}
func (pm *PostMortem) Key() string {
return fmt.Sprintf("%s/%s/%s", "connections", pm.Namespace, pm.Name)
}
func (pm *PostMortem) MarkReady() {
pm.CreateOrUpdateCond(PostMortemCondition{
Type: PostMortemReady,
Status: corev1.ConditionTrue,
Reason: string(PostMortemReady),
})
}
func (pm *PostMortem) MarkArchived() {
pm.CreateOrUpdateCond(PostMortemCondition{
Type: PostMortemSaved,
Status: corev1.ConditionTrue,
})
}
func (pm *PostMortem) Archived() bool {
return pm.GetCond(PostMortemSaved).Status == corev1.ConditionTrue
}