-
Notifications
You must be signed in to change notification settings - Fork 280
/
types.go
372 lines (314 loc) · 8.88 KB
/
types.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Package types holds most of the types used across Keel
//go:generate jsonenums -type=Notification
//go:generate jsonenums -type=Level
//go:generate jsonenums -type=TriggerType
//go:generate jsonenums -type=ProviderType
package types
import (
"bytes"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"time"
)
// KeelDefaultPort - default port for application
const KeelDefaultPort = 9300
// KeelPolicyLabel - keel update policies (version checking)
const KeelPolicyLabel = "keel.sh/policy"
const KeelImagePullSecretAnnotation = "keel.sh/imagePullSecret"
// KeelTriggerLabel - trigger label is used to specify custom trigger types
// for example keel.sh/trigger=poll would signal poll trigger to start watching for repository
// changes
const KeelTriggerLabel = "keel.sh/trigger"
// KeelForceTagMatchLabel - label that checks whether tags match before force updating
const KeelForceTagMatchLegacyLabel = "keel.sh/match-tag"
const KeelForceTagMatchLabel = "keel.sh/matchTag"
// KeelMatchPreReleaseAnnotation - label or annotation to set pre-release matching for SemVer, defaults to true for backward compatibility
const KeelMatchPreReleaseAnnotation = "keel.sh/matchPreRelease"
// KeelPollScheduleAnnotation - optional variable to setup custom schedule for polling, defaults to @every 10m
const KeelPollScheduleAnnotation = "keel.sh/pollSchedule"
// KeelInitContainerAnnotation - label or annotation to track init containers, defaults to false for backward compatibility
const KeelInitContainerAnnotation = "keel.sh/initContainers"
// KeelPollDefaultSchedule - defaul polling schedule
var KeelPollDefaultSchedule = "@every 1m"
// KeelDigestAnnotation - digest annotation
const KeelDigestAnnotation = "keel.sh/digest"
// KeelNotificationChanAnnotation - optional notification to override
// default notification channel(-s) per deployment/chart
const KeelNotificationChanAnnotation = "keel.sh/notify"
// KeelMinimumApprovalsLabel - min approvals
const KeelMinimumApprovalsLabel = "keel.sh/approvals"
// KeelUpdateTimeAnnotation - update time
const KeelUpdateTimeAnnotation = "keel.sh/update-time"
// KeelApprovalDeadlineLabel - approval deadline
const KeelApprovalDeadlineLabel = "keel.sh/approvalDeadline"
// KeelApprovalDeadlineDefault - default deadline in hours
const KeelApprovalDeadlineDefault = 24
// KeelReleasePage - optional release notes URL passed on with notification
const KeelReleaseNotesURL = "keel.sh/releaseNotes"
func init() {
value, found := os.LookupEnv("POLL_DEFAULTSCHEDULE")
if found {
KeelPollDefaultSchedule = value
}
}
// Repository - represents main docker repository fields that
// keel cares about
type Repository struct {
Host string `json:"host"`
Name string `json:"name"`
Tag string `json:"tag"`
Digest string `json:"digest"` // optional digest field
}
// String gives you [host/]team/repo[:tag] identifier
func (r *Repository) String() string {
b := bytes.NewBufferString(r.Host)
if b.Len() != 0 {
b.WriteRune('/')
}
b.WriteString(r.Name)
if r.Tag != "" {
b.WriteRune(':')
b.WriteString(r.Tag)
}
return b.String()
}
// Event - holds information about new event from trigger
type Event struct {
Repository Repository `json:"repository,omitempty"`
CreatedAt time.Time `json:"createdAt,omitempty"`
// optional field to identify trigger
TriggerName string `json:"triggerName,omitempty"`
}
func (e *Event) Value() (driver.Value, error) {
j, err := json.Marshal(e)
return j, err
}
func (e *Event) Scan(src interface{}) error {
source, ok := src.([]byte)
if !ok {
return errors.New("type assertion .([]byte) failed.")
}
var event Event
if err := json.Unmarshal(source, &event); err != nil {
return err
}
*e = event
return nil
}
// Version - version container
type Version struct {
Major int64
Minor int64
Patch int64
PreRelease string
Metadata string
Original string
}
func (v Version) String() string {
if v.Original != "" {
return v.Original
}
var buf bytes.Buffer
fmt.Fprintf(&buf, "%d.%d.%d", v.Major, v.Minor, v.Patch)
if v.PreRelease != "" {
fmt.Fprintf(&buf, "-%s", v.PreRelease)
}
if v.Metadata != "" {
fmt.Fprintf(&buf, "+%s", v.Metadata)
}
return buf.String()
}
// TriggerType - trigger types
type TriggerType int
// Available trigger types
const (
TriggerTypeDefault TriggerType = iota // default policy is to wait for external triggers
TriggerTypePoll // poll policy sets up watchers for the affected repositories
TriggerTypeApproval // fulfilled approval requests trigger events
)
func (t TriggerType) String() string {
switch t {
case TriggerTypeDefault:
return "default"
case TriggerTypePoll:
return "poll"
case TriggerTypeApproval:
return "approval"
default:
return "default"
}
}
// ParseTrigger - parse trigger string into type
func ParseTrigger(trigger string) TriggerType {
switch trigger {
case "poll":
return TriggerTypePoll
}
return TriggerTypeDefault
}
// EventNotification notification used for sending
type EventNotification struct {
Name string `json:"name"`
Message string `json:"message"`
CreatedAt time.Time `json:"createdAt"`
Type Notification `json:"type"`
Level Level `json:"level"`
ResourceKind string `json:"resourceKind"`
Identifier string `json:"identifier"`
// Channels is an optional variable to override
// default channel(-s) when performing an update
Channels []string `json:"-"`
Metadata map[string]string `json:"metadata"`
}
// ParseEventNotificationChannels - parses deployment annotations or chart config
// to get channel overrides
func ParseEventNotificationChannels(annotations map[string]string) []string {
channels := []string{}
if annotations == nil {
return channels
}
chanStr, ok := annotations[KeelNotificationChanAnnotation]
if ok {
chans := strings.Split(chanStr, ",")
for _, c := range chans {
channels = append(channels, strings.TrimSpace(c))
}
}
return channels
}
func ParseReleaseNotesURL(annotations map[string]string) string {
if annotations == nil {
return ""
}
return annotations[KeelReleaseNotesURL]
}
// Notification - notification types used by notifier
type Notification int
// available notification types for hooks
const (
PreProviderSubmitNotification Notification = iota
PostProviderSubmitNotification
// Kubernetes notification types
NotificationPreDeploymentUpdate
NotificationDeploymentUpdate
// Helm notification types
NotificationPreReleaseUpdate
NotificationReleaseUpdate
NotificationSystemEvent
NotificationUpdateApproved
NotificationUpdateRejected
)
func (n Notification) String() string {
switch n {
case PreProviderSubmitNotification:
return "pre provider submit"
case PostProviderSubmitNotification:
return "post provider submit"
case NotificationPreDeploymentUpdate:
return "preparing deployment update"
case NotificationDeploymentUpdate:
return "deployment update"
case NotificationPreReleaseUpdate:
return "preparing release update"
case NotificationReleaseUpdate:
return "release update"
case NotificationSystemEvent:
return "system event"
case NotificationUpdateApproved:
return "update approved"
case NotificationUpdateRejected:
return "update rejected "
default:
return "unknown"
}
}
// Level - event levet
type Level int
// Available event levels
const (
LevelDebug Level = iota
LevelInfo
LevelSuccess
LevelWarn
LevelError
LevelFatal
)
// ParseLevel takes a string level and returns notification level constant.
func ParseLevel(lvl string) (Level, error) {
switch strings.ToLower(lvl) {
case "fatal":
return LevelFatal, nil
case "error":
return LevelError, nil
case "warn", "warning":
return LevelWarn, nil
case "info":
return LevelInfo, nil
case "success":
return LevelSuccess, nil
case "debug":
return LevelDebug, nil
}
var l Level
return l, fmt.Errorf("not a valid notification Level: %q", lvl)
}
func (l Level) String() string {
switch l {
case LevelDebug:
return "debug"
case LevelInfo:
return "info"
case LevelSuccess:
return "success"
case LevelWarn:
return "warn"
case LevelError:
return "error"
case LevelFatal:
return "fatal"
default:
return "unknown"
}
}
// Color - used to assign different colors for events
func (l Level) Color() string {
switch l {
case LevelError:
return "#F44336"
case LevelInfo:
return "#2196F3"
case LevelSuccess:
return "#00C853"
case LevelFatal:
return "#B71C1C"
case LevelWarn:
return "#FF9800"
default:
return "#9E9E9E"
}
}
// ProviderType - provider type used to differentiate different providers
// when used with plugins
type ProviderType int
// Known provider types
const (
ProviderTypeUnknown ProviderType = iota
ProviderTypeKubernetes
ProviderTypeHelm
)
func (t ProviderType) String() string {
switch t {
case ProviderTypeUnknown:
return "unknown"
case ProviderTypeKubernetes:
return "kubernetes"
case ProviderTypeHelm:
return "helm"
default:
return ""
}
}