/
phase.go
79 lines (59 loc) · 1.99 KB
/
phase.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
package phase
import (
"github.com/dominodatalab/controller-util/core"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
hephv1 "github.com/dominodatalab/hephaestus/pkg/api/hephaestus/v1"
)
type PhasedObject interface {
client.Object
core.ConditionObject
GetPhase() hephv1.Phase
SetPhase(p hephv1.Phase)
}
type TransitionConditions struct {
Initialize func() (string, string)
Running func() (string, string)
Success func() (string, string)
}
type TransitionHelper struct {
Client client.Client
ConditionMeta TransitionConditions
ReadyCondition string
}
func (h *TransitionHelper) SetInitializing(ctx *core.Context, obj PhasedObject) {
obj.SetPhase(hephv1.PhaseInitializing)
reason, message := h.ConditionMeta.Initialize()
ctx.Conditions.SetUnknown(h.ReadyCondition, reason, message)
h.updateStatus(ctx, obj)
}
func (h *TransitionHelper) SetSucceeded(ctx *core.Context, obj PhasedObject) {
obj.SetPhase(hephv1.PhaseSucceeded)
reason, message := h.ConditionMeta.Success()
ctx.Conditions.SetTrue(h.ReadyCondition, reason, message)
h.updateStatus(ctx, obj)
}
func (h *TransitionHelper) SetRunning(ctx *core.Context, obj PhasedObject) {
obj.SetPhase(hephv1.PhaseRunning)
reason, message := h.ConditionMeta.Success()
ctx.Conditions.SetUnknown(h.ReadyCondition, reason, message)
h.updateStatus(ctx, obj)
}
func (h *TransitionHelper) SetFailed(ctx *core.Context, obj PhasedObject, err error) error {
obj.SetPhase(hephv1.PhaseFailed)
ctx.Conditions.SetFalse(h.ReadyCondition, "ExecutionError", err.Error())
h.updateStatus(ctx, obj)
return err
}
func (h *TransitionHelper) updateStatus(ctx *core.Context, obj PhasedObject) {
ctx.Log.Info("Transitioning status", "phase", obj.GetPhase())
if err := ctx.Client.Status().Update(ctx, obj); err != nil {
ctx.Log.Error(err, "Failed to update status, emitting event")
ctx.Recorder.Eventf(
obj,
corev1.EventTypeWarning,
"StatusUpdate",
"Failed to update phase %s: %w", obj.GetPhase(), err,
)
}
}