-
Notifications
You must be signed in to change notification settings - Fork 17
/
lifecycle_recorder.go
176 lines (148 loc) · 5 KB
/
lifecycle_recorder.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package v1alpha1
import (
"context"
"time"
"github.com/go-logr/logr"
"github.com/gocardless/theatre/v3/pkg/workloads/console/events"
)
// +kubebuilder:object:generate=false
type ConsoleIdBuilder interface {
BuildId(*Console) string
}
// +kubebuilder:object:generate=false
type LifecycleEventRecorder interface {
ConsoleRequest(context.Context, *Console, *ConsoleAuthorisationRule) error
ConsoleAuthorise(context.Context, *Console, string) error
ConsoleStart(context.Context, *Console, string) error
ConsoleAttach(context.Context, *Console, string, string) error
ConsoleTerminate(context.Context, *Console, bool) error
}
var _ LifecycleEventRecorder = &lifecycleEventRecorderImpl{}
var _ ConsoleIdBuilder = &consoleIdBuilderImpl{}
// lifecycleEventRecorderImpl implements the interface to record lifecycle events given
//
// +kubebuilder:object:generate=false
type lifecycleEventRecorderImpl struct {
// context name for the kubernetes cluster where this recorder runs
contextName string
idBuilder ConsoleIdBuilder
logger logr.Logger
publisher events.Publisher
}
// consoleIdBuilderImpl implements the interface to build console IDs from a console object
//
// +kubebuilder:object:generate=false
type consoleIdBuilderImpl struct {
// context name for the kubernetes cluster where this recorder runs
contextName string
}
func NewConsoleIdBuilder(contextName string) ConsoleIdBuilder {
return &consoleIdBuilderImpl{
contextName: contextName,
}
}
func NewLifecycleEventRecorder(contextName string, logger logr.Logger, publisher events.Publisher, idBuilder ConsoleIdBuilder) LifecycleEventRecorder {
return &lifecycleEventRecorderImpl{
contextName: contextName,
logger: logger,
publisher: publisher,
idBuilder: idBuilder,
}
}
func (b *consoleIdBuilderImpl) BuildId(csl *Console) string {
return events.NewConsoleEventID(b.contextName, csl.Namespace, csl.Name, csl.CreationTimestamp.Time)
}
func (l *lifecycleEventRecorderImpl) makeConsoleCommonEvent(eventKind events.EventKind, csl *Console) events.CommonEvent {
return events.CommonEvent{
Version: "v1alpha1",
Kind: events.KindConsole,
Event: eventKind,
ObservedAt: time.Now().UTC(),
Id: l.idBuilder.BuildId(csl),
Annotations: map[string]string{},
}
}
func (l *lifecycleEventRecorderImpl) ConsoleRequest(ctx context.Context, csl *Console, authRule *ConsoleAuthorisationRule) error {
authCount := 0
if authRule != nil {
authCount = authRule.AuthorisationsRequired
}
event := &events.ConsoleRequestEvent{
CommonEvent: l.makeConsoleCommonEvent(events.EventRequest, csl),
Spec: events.ConsoleRequestSpec{
Reason: csl.Spec.Reason,
Username: csl.Spec.User,
Context: l.contextName,
Namespace: csl.Namespace,
ConsoleTemplate: csl.Spec.ConsoleTemplateRef.Name,
Console: csl.Name,
RequiredAuthorisations: authCount,
Timestamp: csl.CreationTimestamp.Time,
Labels: csl.Labels,
},
}
id, err := l.publisher.Publish(ctx, event)
if err != nil {
return err
}
l.logger.Info("event recorded", "id", id, "event", events.EventRequest)
return nil
}
func (l *lifecycleEventRecorderImpl) ConsoleAuthorise(ctx context.Context, csl *Console, username string) error {
event := &events.ConsoleAuthoriseEvent{
CommonEvent: l.makeConsoleCommonEvent(events.EventAuthorise, csl),
Spec: events.ConsoleAuthoriseSpec{
Username: username,
},
}
id, err := l.publisher.Publish(ctx, event)
if err != nil {
return err
}
l.logger.Info("event recorded", "id", id, "event", events.EventAuthorise)
return nil
}
func (l *lifecycleEventRecorderImpl) ConsoleStart(ctx context.Context, csl *Console, jobName string) error {
event := &events.ConsoleStartEvent{
CommonEvent: l.makeConsoleCommonEvent(events.EventStart, csl),
Spec: events.ConsoleStartSpec{
Job: jobName,
},
}
id, err := l.publisher.Publish(ctx, event)
if err != nil {
return err
}
l.logger.Info("event recorded", "id", id, "event", events.EventStart)
return nil
}
func (l *lifecycleEventRecorderImpl) ConsoleAttach(ctx context.Context, csl *Console, username string, containerName string) error {
event := &events.ConsoleAttachEvent{
CommonEvent: l.makeConsoleCommonEvent(events.EventAttach, csl),
Spec: events.ConsoleAttachSpec{
Username: username,
Pod: csl.Status.PodName,
Container: containerName,
},
}
id, err := l.publisher.Publish(ctx, event)
if err != nil {
return err
}
l.logger.Info("event recorded", "id", id, "event", events.EventAttach)
return nil
}
func (l *lifecycleEventRecorderImpl) ConsoleTerminate(ctx context.Context, csl *Console, timedOut bool) error {
event := &events.ConsoleTerminatedEvent{
CommonEvent: l.makeConsoleCommonEvent(events.EventTerminated, csl),
Spec: events.ConsoleTerminatedSpec{
TimedOut: timedOut,
},
}
id, err := l.publisher.Publish(ctx, event)
if err != nil {
return err
}
l.logger.Info("event recorded", "id", id, "event", events.EventTerminated)
return nil
}