-
Notifications
You must be signed in to change notification settings - Fork 70
/
events.go
300 lines (267 loc) · 11.6 KB
/
events.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//
// DISCLAIMER
//
// Copyright 2016-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
package k8sutil
import (
"fmt"
core "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
driver "github.com/arangodb/go-driver"
upgraderules "github.com/arangodb/go-upgrade-rules"
"github.com/arangodb/kube-arangodb/pkg/util/strings"
)
// Event is used to create events using an EventRecorder.
type Event struct {
InvolvedObject runtime.Object
Type string
Reason string
Message string
}
type K8SObject interface {
meta.Object
// AsOwner creates an OwnerReference for the given deployment
AsOwner() meta.OwnerReference
}
// APIObject helps to abstract an object from our custom API.
type APIObject interface {
runtime.Object
K8SObject
OwnerOf(in meta.Object) bool
}
// NewMemberAddEvent creates an event indicating that a member was added.
func NewMemberAddEvent(memberName, role string, apiObject APIObject) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeNormal
event.Reason = fmt.Sprintf("New %s Added", strings.Title(role))
event.Message = fmt.Sprintf("New %s %s added to deployment", role, memberName)
return event
}
// NewMemberRemoveEvent creates an event indicating that an existing member was removed.
func NewMemberRemoveEvent(memberName, role string, apiObject APIObject) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeNormal
event.Reason = fmt.Sprintf("%s Removed", strings.Title(role))
event.Message = fmt.Sprintf("Existing %s %s removed from the deployment", role, memberName)
return event
}
// NewPodCreatedEvent creates an event indicating that a pod has been created
func NewPodCreatedEvent(podName, role string, apiObject APIObject) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeNormal
event.Reason = fmt.Sprintf("Pod Of %s Created", strings.Title(role))
event.Message = fmt.Sprintf("Pod %s of member %s is created", podName, role)
return event
}
// NewPodGoneEvent creates an event indicating that a pod is missing
func NewPodGoneEvent(podName, role string, apiObject APIObject) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeNormal
event.Reason = fmt.Sprintf("Pod Of %s Gone", strings.Title(role))
event.Message = fmt.Sprintf("Pod %s of member %s is gone", podName, role)
return event
}
// NewImmutableFieldEvent creates an event indicating that an attempt was made to change a field
// that is immutable.
func NewImmutableFieldEvent(fieldName string, apiObject APIObject) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeNormal
event.Reason = "Immutable Field Change"
event.Message = fmt.Sprintf("Changing field %s is not possible. It has been reset to its original value.", fieldName)
return event
}
// NewPodsSchedulingFailureEvent creates an event indicating that one of more cannot be scheduled.
func NewPodsSchedulingFailureEvent(unscheduledPodNames []string, apiObject APIObject) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeNormal
event.Reason = "Pods Scheduling Failure"
event.Message = fmt.Sprintf("One or more pods are not scheduled in time. Pods: %v", unscheduledPodNames)
return event
}
// NewPodsSchedulingResolvedEvent creates an event indicating that an earlier problem with
// pod scheduling has been resolved.
func NewPodsSchedulingResolvedEvent(apiObject APIObject) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeNormal
event.Reason = "Pods Scheduling Resolved"
event.Message = "All pods have been scheduled"
return event
}
// NewSecretsChangedEvent creates an event indicating that one of more secrets have changed.
func NewSecretsChangedEvent(changedSecretNames []string, apiObject APIObject) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeNormal
event.Reason = "Secrets changed"
event.Message = fmt.Sprintf("Found %d changed secrets. You must revert them before the operator can continue. Secrets: %v", len(changedSecretNames), changedSecretNames)
return event
}
// NewSecretsRestoredEvent creates an event indicating that all secrets have been restored
// to their original values.
func NewSecretsRestoredEvent(apiObject APIObject) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeNormal
event.Reason = "Secrets restored"
event.Message = "All secrets have been restored to their original value"
return event
}
// NewAccessPackageCreatedEvent creates an event indicating that a secret containing an access package
// has been created.
func NewAccessPackageCreatedEvent(apiObject APIObject, apSecretName string) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeNormal
event.Reason = "Access package created"
event.Message = fmt.Sprintf("An access package named %s has been created", apSecretName)
return event
}
// NewAccessPackageDeletedEvent creates an event indicating that a secret containing an access package
// has been deleted.
func NewAccessPackageDeletedEvent(apiObject APIObject, apSecretName string) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeNormal
event.Reason = "Access package deleted"
event.Message = fmt.Sprintf("An access package named %s has been deleted", apSecretName)
return event
}
// NewPlanAppendEvent creates an event indicating that an item on a reconciliation plan has been added
func NewPlanAppendEvent(apiObject APIObject, itemType, memberID, role, reason string) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeNormal
event.Reason = "Plan Action added"
msg := fmt.Sprintf("A plan item of type %s", itemType)
if role != "" {
msg = fmt.Sprintf("%s for member %s with role %s", msg, memberID, role)
}
msg = fmt.Sprintf("%s has been added", msg)
if reason != "" {
msg = fmt.Sprintf("%s with reason: %s", msg, reason)
}
event.Message = msg
return event
}
// NewPlanTimeoutEvent creates an event indicating that an item on a reconciliation plan did not
// finish before its deadline.
func NewPlanTimeoutEvent(apiObject APIObject, itemType, memberID, role string) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeNormal
event.Reason = "Reconciliation Plan Timeout"
event.Message = fmt.Sprintf("A plan item of type %s or member %s with role %s did not finish in time", itemType, memberID, role)
return event
}
// NewPlanAbortedEvent creates an event indicating that an item on a reconciliation plan wants to abort
// the entire plan.
func NewPlanAbortedEvent(apiObject APIObject, itemType, memberID, role string) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeNormal
event.Reason = "Reconciliation Plan Aborted"
event.Message = fmt.Sprintf("A plan item of type %s or member %s with role %s wants to abort the plan", itemType, memberID, role)
return event
}
// NewCannotChangeStorageClassEvent creates an event indicating that an item would need to use a different StorageClass,
// but this is not possible for the given reason.
func NewCannotChangeStorageClassEvent(apiObject APIObject, memberID, role, subReason string) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeNormal
event.Reason = fmt.Sprintf("%s Member StorageClass Cannot Change", strings.Title(role))
event.Message = fmt.Sprintf("Member %s with role %s should use a different StorageClass, but is cannot because: %s", memberID, role, subReason)
return event
}
// NewDowntimeNotAllowedEvent creates an event indicating that an operation cannot be executed because downtime
// is currently not allowed.
func NewDowntimeNotAllowedEvent(apiObject APIObject, operation string) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeNormal
event.Reason = "Downtime Operation Postponed"
event.Message = fmt.Sprintf("The '%s' operation is postponed because downtime it not allowed. Set `spec.downtimeAllowed` to true to execute this operation", operation)
return event
}
// NewPVCResizedEvent creates an event indicating that a PVC has been resized
func NewPVCResizedEvent(apiObject APIObject, pvcname string) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeNormal
event.Reason = "PVC Resized"
event.Message = fmt.Sprintf("The persistent volume claim %s has been resized", pvcname)
return event
}
// NewCannotShrinkVolumeEvent creates an event indicating that the user tried to shrink a PVC
func NewCannotShrinkVolumeEvent(apiObject APIObject, pvcname string) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeNormal
event.Reason = "PVC Shrinked"
event.Message = fmt.Sprintf("The persistent volume claim %s can not be shrinked", pvcname)
return event
}
// NewUpgradeNotAllowedEvent creates an event indicating that an upgrade (or downgrade) is not allowed.
func NewUpgradeNotAllowedEvent(apiObject APIObject,
fromVersion, toVersion driver.Version,
fromLicense, toLicense upgraderules.License) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeNormal
formatLicense := func(l upgraderules.License) string {
if l == upgraderules.LicenseCommunity {
return "Community Edition"
}
return "Enterprise Edition"
}
var verb string
if fromVersion.CompareTo(toVersion) < 0 {
event.Reason = "Upgrade not allowed"
verb = "Upgrading"
} else {
event.Reason = "Downgrade not allowed"
verb = "Downgrading"
}
if fromLicense == toLicense {
event.Message = fmt.Sprintf("%s ArangoDB %s from version %s to version %s is not allowed",
verb, formatLicense(fromLicense), fromVersion, toVersion)
} else {
event.Message = fmt.Sprintf("%s ArangoDB from %s version %s to %s version %s is not allowed",
verb, formatLicense(fromLicense), fromVersion, formatLicense(toLicense), toVersion)
}
return event
}
// NewErrorEvent creates an even of type error.
func NewErrorEvent(reason string, err error, apiObject APIObject) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeWarning
event.Reason = strings.Title(reason)
event.Message = err.Error()
return event
}
// newDeploymentEvent creates a new event for the given api object & owner.
func newDeploymentEvent(apiObject runtime.Object) *Event {
return &Event{
InvolvedObject: apiObject,
}
}
// NewOperatorEngineOpsAlertEvent creates an even of type OperatorEngineOpsAlert.
func NewOperatorEngineOpsAlertEvent(reason string, apiObject APIObject) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeWarning
event.Reason = "OperatorEngineOpsAlert"
event.Message = fmt.Sprintf("Event OperatorEngineOpsAlert raised, investigation needed: %s", reason)
return event
}
// NewCannotSetArchitectureEvent creates an even of type CannotSetArchitectureEvent.
func NewCannotSetArchitectureEvent(apiObject runtime.Object, arch, memberId string) *Event {
event := newDeploymentEvent(apiObject)
event.Type = core.EventTypeWarning
event.Reason = "Can not set architecture"
event.Message = fmt.Sprintf("Can not apply %s arch for member %s. It is not supported in current ArangoDB version", arch, memberId)
return event
}