generated from broadinstitute/golang-project-template
/
environment.go
468 lines (437 loc) · 20.6 KB
/
environment.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package v2models
import (
"fmt"
"github.com/broadinstitute/sherlock/internal/auth/auth_models"
"github.com/broadinstitute/sherlock/internal/config"
"github.com/broadinstitute/sherlock/internal/models/model_actions"
"github.com/broadinstitute/sherlock/internal/utils"
"github.com/rs/zerolog/log"
"math/bits"
"regexp"
"strconv"
"strings"
"time"
"github.com/broadinstitute/sherlock/internal/errors"
"github.com/broadinstitute/sherlock/internal/models/v2models/environment"
"gorm.io/gorm"
)
type Environment struct {
gorm.Model
CiIdentifier *CiIdentifier `gorm:"polymorphic:Resource; polymorphicValue:environment"`
Base string
Lifecycle string `gorm:"not null; default:null"`
Name string `gorm:"not null; default:null"`
NamePrefix string
TemplateEnvironment *Environment
TemplateEnvironmentID *uint
ValuesName string
AutoPopulateChartReleases *bool
UniqueResourcePrefix string `gorm:"not null; default:null"`
DefaultNamespace string
// Mutable
DefaultCluster *Cluster
DefaultClusterID *uint
DefaultFirecloudDevelopRef *string
Owner *User
OwnerID *uint
LegacyOwner *string
RequiresSuitability *bool
BaseDomain *string
NamePrefixesDomain *bool
HelmfileRef *string `gorm:"not null; default:null"`
PreventDeletion *bool
AutoDelete *environment.AutoDelete `gorm:"column:delete_after; -:migration"`
Description *string
PagerdutyIntegration *PagerdutyIntegration
PagerdutyIntegrationID *uint
Offline *bool
OfflineScheduleBeginEnabled *bool
OfflineScheduleBeginTime *string
OfflineScheduleEndEnabled *bool
OfflineScheduleEndTime *string
OfflineScheduleEndWeekends *bool
}
func (e Environment) TableName() string {
return "v2_environments"
}
func (e Environment) getID() uint {
return e.ID
}
func (e Environment) GetCiIdentifier() *CiIdentifier {
if e.CiIdentifier != nil {
return e.CiIdentifier
} else {
return &CiIdentifier{ResourceType: "environment", ResourceID: e.ID}
}
}
var environmentStore *internalModelStore[Environment]
func init() {
environmentStore = &internalModelStore[Environment]{
selectorToQueryModel: environmentSelectorToQuery,
modelToSelectors: environmentToSelectors,
errorIfForbidden: environmentErrorIfForbidden,
validateModel: validateEnvironment,
preCreate: preCreateEnvironment,
postCreate: postCreateEnvironment,
preDeletePostValidate: preDeletePostValidateEnvironment,
}
}
func environmentSelectorToQuery(_ *gorm.DB, selector string) (Environment, error) {
if len(selector) == 0 {
return Environment{}, fmt.Errorf("(%s) environment selector cannot be empty", errors.BadRequest)
}
var query Environment
if utils.IsNumeric(selector) { // ID
id, err := strconv.Atoi(selector)
if err != nil {
return Environment{}, fmt.Errorf("(%s) string to int conversion error of '%s': %v", errors.BadRequest, selector, err)
}
query.ID = uint(id)
return query, nil
} else if utils.IsAlphaNumericWithHyphens(selector) &&
utils.IsStartingWithLetter(selector) &&
utils.IsEndingWithAlphaNumeric(selector) { // Name
if len(selector) > 32 {
return Environment{}, fmt.Errorf("(%s) %T name is too long, was %d characters and the maximum is 32", errors.BadRequest, Environment{}, len(selector))
}
query.Name = selector
return query, nil
} else if strings.Count(selector, "/") == 1 { // "resource-prefix" + unique resource prefix
parts := strings.Split(selector, "/")
// "resource-prefix"
// The reason we have this at all is so that we can differentiate resource prefix selectors from name selectors.
// In other words, a name can't have the slash that "resource-prefix/<blah>" has, so that's our hack to tell
// incoming selectors apart. I expect this selector will be super rarely used but internally it'll be a
// safeguard against duplicates and if a human ever uses it, it'll be some weird edge case that'll be super
// nice to have a solution for.
selectorLabel := parts[0]
if selectorLabel != "resource-prefix" {
return Environment{}, fmt.Errorf("(%s) invalid environment selector %s, unique resource prefix selector needed to start with 'resource-prefix/' but was '%s/'", errors.BadRequest, selector, selectorLabel)
}
// unique resource prefix
uniqueResourcePrefix := parts[1]
if !(utils.IsLowerAlphaNumeric(uniqueResourcePrefix) &&
utils.IsStartingWithLetter(uniqueResourcePrefix) &&
utils.IsEndingWithAlphaNumeric(uniqueResourcePrefix) &&
len(uniqueResourcePrefix) == 4) {
return Environment{}, fmt.Errorf("(%s) invalid environment selector %s, unique resource prefix sub-selector %s was invalid", errors.BadRequest, selector, uniqueResourcePrefix)
}
query.UniqueResourcePrefix = uniqueResourcePrefix
return query, nil
}
return Environment{}, fmt.Errorf("(%s) invalid environment selector '%s'", errors.BadRequest, selector)
}
func environmentToSelectors(environment *Environment) []string {
var selectors []string
if environment != nil {
if environment.Name != "" {
selectors = append(selectors, environment.Name)
}
if environment.ID != 0 {
selectors = append(selectors, fmt.Sprintf("%d", environment.ID))
}
if environment.UniqueResourcePrefix != "" {
selectors = append(selectors, fmt.Sprintf("resource-prefix/%s", environment.UniqueResourcePrefix))
}
}
return selectors
}
func environmentErrorIfForbidden(_ *gorm.DB, environment *Environment, _ model_actions.ActionType, user *auth_models.User) error {
if environment.RequiresSuitability == nil || *environment.RequiresSuitability {
return user.SuitableOrError()
} else {
return nil
}
}
const environmentNameRegexString = "[a-z0-9]([-a-z0-9]*[a-z0-9])?"
func validateEnvironment(environment *Environment) error {
if environment == nil {
return fmt.Errorf("the model passed was nil")
}
if environment.Name == "" {
return fmt.Errorf("a %T must have a non-empty name", environment)
} else if regex, err := regexp.Compile(environmentNameRegexString); err != nil {
return fmt.Errorf("environment-matching regex failed with %v", err)
} else if !regex.MatchString(environment.Name) {
return fmt.Errorf("environment name %s did not match %s", environment.Name, environmentNameRegexString)
}
if environment.OwnerID == nil && (environment.LegacyOwner == nil || *environment.LegacyOwner == "") {
return fmt.Errorf("a %T must have an owner", environment)
}
switch environment.Lifecycle {
case "template":
if environment.TemplateEnvironmentID != nil {
return fmt.Errorf("a template %T cannot itself have a template", environment)
}
case "dynamic":
if environment.TemplateEnvironmentID == nil {
return fmt.Errorf("a dynamic %T must have a template", environment)
}
fallthrough
case "static":
if environment.Base == "" {
return fmt.Errorf("a non-template %T must have a base", environment)
}
if environment.DefaultClusterID == nil {
return fmt.Errorf("a non-template %T must have a default cluster", environment)
}
if environment.RequiresSuitability == nil {
return fmt.Errorf("a non-template %T must set whether it requires suitability or not", environment)
}
default:
return fmt.Errorf("a %T must have a lifecycle of either 'template', 'static', or 'dynamic'", environment)
}
if environment.DefaultNamespace == "" {
return fmt.Errorf("a %T must have a default namespace", environment)
}
if environment.HelmfileRef == nil || *environment.HelmfileRef == "" {
return fmt.Errorf("a %T must have a non-empty terra-helmfile ref", environment)
}
if environment.DefaultFirecloudDevelopRef == nil || *environment.DefaultFirecloudDevelopRef == "" {
return fmt.Errorf("a %T must have a non-empty default firecloud-develop ref", environment)
}
if environment.UniqueResourcePrefix == "" {
return fmt.Errorf("a %T must have a non-empty unique resource prefix", environment)
}
if environment.PreventDeletion != nil && *environment.PreventDeletion && environment.Lifecycle != "dynamic" {
return fmt.Errorf("preventDeletion is only valid for dynamic environments")
}
if environment.AutoDelete != nil {
if err := environment.AutoDelete.Validate(); err != nil {
return err
}
if environment.Lifecycle != "dynamic" {
return fmt.Errorf("autoDelete is only valid for dynamic environments")
}
if environment.PreventDeletion != nil && *environment.PreventDeletion {
return fmt.Errorf("either preventDeletion or autoDelete may be enabled, but not both")
}
}
if environment.Lifecycle != "dynamic" {
if environment.Offline != nil && *environment.Offline {
return fmt.Errorf("%s %T can't be set to offline, only dynamic ones can", environment.Lifecycle, environment)
}
if (environment.OfflineScheduleBeginEnabled != nil && *environment.OfflineScheduleBeginEnabled) || (environment.OfflineScheduleEndEnabled != nil && *environment.OfflineScheduleEndEnabled) {
return fmt.Errorf("%s %T can't have an offline schedule, only dynamic ones can", environment.Lifecycle, environment)
}
}
if environment.OfflineScheduleBeginEnabled != nil && *environment.OfflineScheduleBeginEnabled && environment.OfflineScheduleBeginTime == nil {
return fmt.Errorf("a %T with a begin-offline schedule enabled must have a time", environment)
}
if environment.OfflineScheduleEndEnabled != nil && *environment.OfflineScheduleEndEnabled && environment.OfflineScheduleEndTime == nil {
return fmt.Errorf("a %T with an end-offline schedule enabled must have a time", environment)
}
return nil
}
func preCreateEnvironment(db *gorm.DB, environment *Environment, _ *auth_models.User) error {
if environment != nil && environment.UniqueResourcePrefix == "" {
var generatedUniqueResourcePrefix bool
// Time to derive a unique resource prefix. /^[a-z][a-z0-9]{3}$/ and unique among
// all non-deleted environments. The tricky part is that environments *can* specify
// a custom prefix, for Thelma state-provider migration or debugging purposes.
//
// That means this algorithm needs to make sure it works before continuing. In
// theory, collisions should be rare.
//
// The fact that we need to verify and retry at all, though, means that the worst-
// case runtime behavior here is... potentially really bad. So we set a timeout,
// add some error messages pointing to the problem, and make an attempt at a
// vaguely memory-allocation-efficient solution to help lower the constant runtime
// factor to buy the algorithm time to run.
//
// The goal is that even if we get into a worst-case of somehow needing to iterate
// through a few hundred environments to find a match, we can do so fast enough
// that no one will care, and eventually the environments that conflict with this
// algorithm will get deleted and the runtime will recover.
var countOfAllEnvironmentsEver int64
var unsignedCountOfAllEnvironmentsEver, iterations uint64
var candidate Environment
// First, we take advantage of the domain size as much as we can. We offset by
// how many environments have ever existed, knowing we'll modulo down. This
// ends up being like a ring buffer, where we can assume that the resulting
// "index" is either empty or very old (and most likely soft-deleted).
// Note that we use Unscoped here to include even soft-deleted environments.
db.Unscoped().Model(&Environment{}).Count(&countOfAllEnvironmentsEver)
unsignedCountOfAllEnvironmentsEver = uint64(countOfAllEnvironmentsEver)
// We use a strings.Builder because it offers a typed way to assemble the
// string while also giving us a performance boost in the form of a zero-copy
// method to get the resulting string.
sb := strings.Builder{}
sb.Grow(4)
// We set a deadline here three seconds into the future. This is purely a
// guess based on what we can probably get away with in proxies and UIs without
// needing to add additional handling.
for end := time.Now().Add(3 * time.Second); ; {
// Every 16th iteration via bitmask (faster than modulo), check if we're past the deadline
if iterations&15 == 0 && time.Now().After(end) {
return fmt.Errorf("(%s) could not derive a unique environment resource prefix, used %d iterations based on an initial lifetime environment count of %d",
errors.InternalServerError, iterations, countOfAllEnvironmentsEver)
}
// Write the letter bytes into the strings.Builder, from our starting count plus
// however many iterations we've already used. We modulo that down inside the
// function to encapsulate the part that cares about the string.
generateUniqueResourcePrefix(&sb, unsignedCountOfAllEnvironmentsEver+iterations)
candidate.UniqueResourcePrefix = sb.String()
// Check the database for this candidate prefix existing. Note that we do not use
// Unscoped here like we did above, because now we don't care if there is a
// conflict in the soft-deleted environments.
var firstMatch Environment
err := db.Where(candidate).First(&firstMatch).Error
// check for unexpected errors from DB
if err != nil && err != gorm.ErrRecordNotFound {
return fmt.Errorf("(%s) could not check other existing environments to verify prefix uniqueness: %v", errors.InternalServerError, err)
}
// no other environments with the same resource prefix exist in DB
if err == gorm.ErrRecordNotFound {
// If the candidate prefix we just generated isn't already in a non-deleted
// Environment, we're good to bail
generatedUniqueResourcePrefix = true
break
} else {
// Otherwise, reset the string builder and let's try incrementing.
// That there was a conflict wasn't a huge issue because eventually the
// environment will get deleted, but all we care about right now is
// finding a valid prefix ASAP so we can complete the user's transaction.
sb.Reset()
iterations++
}
}
if !generatedUniqueResourcePrefix {
return fmt.Errorf("(%s) could not derive a unique environment resource prefix, used %d iterations based on an initial lifetime environment count of %d (loop exited but prefix was still empty)",
errors.InternalServerError, iterations, countOfAllEnvironmentsEver)
} else {
environment.UniqueResourcePrefix = candidate.UniqueResourcePrefix
}
}
return nil
}
// Go strings are UTF-8, and these characters all map to single bytes, so this is like a `const`
// slice of bytes for the possible characters (a normal slice can't be a constant).
const (
characterBytes = "abcdefghijklmnopqrstuvwxyz0123456789"
possibleCombinations = uint64(26 * 36 * 36 * 36)
)
func generateUniqueResourcePrefix(sb *strings.Builder, number uint64) {
// We're assembling a string like `[r3][r2][r1][r0]`. r0 through r2 are in
// base 36, while r3 is in base 26 so the string always starts with a letter.
// r0 is the "lowest" digit, and the string is a bit similar to a hexadecimal
// number with letters taking on numeral values. The result is a string-y
// modulo representation of the input number that achieves full coverage of
// the domain to minimize conflicts.
// Example (remember that input is always modulo possibleCombinations):
// possibleCombinations-2 => z998
// possibleCombinations-1 => z999
// possibleCombinations => aaaa
// possibleCombinations+1 => aaab
// possibleCombinations+2 => aaac
number, r0 := bits.Div64(0, number%possibleCombinations, 36)
number, r1 := bits.Div64(0, number, 36)
number, r2 := bits.Div64(0, number, 36)
_, r3 := bits.Div64(0, number, 26)
sb.WriteByte(characterBytes[r3])
sb.WriteByte(characterBytes[r2])
sb.WriteByte(characterBytes[r1])
sb.WriteByte(characterBytes[r0])
}
func postCreateEnvironment(db *gorm.DB, environment *Environment, user *auth_models.User) error {
if environment.AutoPopulateChartReleases != nil && *environment.AutoPopulateChartReleases {
if environment.Lifecycle == "dynamic" && environment.TemplateEnvironmentID != nil {
// This is a dynamic environment that is getting created right now, let's copy the chart releases from the template too
templateChartReleases, err := chartReleaseStore.listAllMatchingByUpdated(db, 0, ChartRelease{EnvironmentID: environment.TemplateEnvironmentID})
if err != nil {
return fmt.Errorf("wasn't able to list chart releases of template %s (disable autoPopulateChartReleases to skip): %v", environment.TemplateEnvironment.Name, err)
}
for _, templateChartRelease := range templateChartReleases {
chartRelease, _, err := chartReleaseStore.create(db,
ChartRelease{
ChartID: templateChartRelease.ChartID,
ClusterID: environment.DefaultClusterID,
DestinationType: "environment",
EnvironmentID: &environment.ID,
Name: fmt.Sprintf("%s-%s", templateChartRelease.Chart.Name, environment.Name),
Namespace: environment.DefaultNamespace,
ChartReleaseVersion: templateChartRelease.ChartReleaseVersion,
Subdomain: templateChartRelease.Subdomain,
Protocol: templateChartRelease.Protocol,
Port: templateChartRelease.Port,
IncludeInBulkChangesets: templateChartRelease.IncludeInBulkChangesets,
}, user)
if err != nil {
return fmt.Errorf("wasn't able to copy template's release of the %s chart (disable autoPopulateChartReleases to skip): %v", templateChartRelease.Chart.Name, err)
}
templateDatabaseInstance, err := databaseInstanceStore.getIfExists(db, DatabaseInstance{ChartReleaseID: templateChartRelease.ID})
if err != nil {
return fmt.Errorf("wasn't able to get possible database instance of template's %s chart instance (disable autoPopulateChartReleases to skip): %v", templateChartRelease.Chart.Name, err)
}
if templateDatabaseInstance != nil {
_, _, err := databaseInstanceStore.create(db,
DatabaseInstance{
ChartReleaseID: chartRelease.ID,
Platform: templateDatabaseInstance.Platform,
GoogleProject: templateDatabaseInstance.GoogleProject,
InstanceName: templateDatabaseInstance.InstanceName,
DefaultDatabase: templateDatabaseInstance.DefaultDatabase,
}, user)
if err != nil {
return fmt.Errorf("wasn't able to copy database instance of template's %s chart instance (disable autoPopulateChartReleases to skip): %v", templateChartRelease.Chart.Name, err)
}
}
}
} else if environment.Lifecycle == "template" {
autoPopulateCharts := config.Config.Slices("model.environments.templates.autoPopulateCharts")
if autoPopulateCharts != nil {
noneString := "none"
latestString := "latest"
headString := "HEAD"
trueBoolean := true
for index, chartEntry := range autoPopulateCharts {
if chartEntry.String("name") != "" {
chart, err := chartStore.get(db, Chart{Name: chartEntry.String("name")})
if err != nil {
return fmt.Errorf("wasn't able to get the honeycomb chart (disable autoPopulateChartReleases to skip): %v", err)
}
_, _, err = chartReleaseStore.create(db,
ChartRelease{
ChartID: chart.ID,
ClusterID: environment.DefaultClusterID,
DestinationType: "environment",
EnvironmentID: &environment.ID,
Name: fmt.Sprintf("%s-%s", chart.Name, environment.Name),
Namespace: environment.DefaultNamespace,
ChartReleaseVersion: ChartReleaseVersion{
AppVersionResolver: &noneString,
ChartVersionResolver: &latestString,
HelmfileRef: &headString,
},
Subdomain: chart.DefaultSubdomain,
Protocol: chart.DefaultProtocol,
Port: chart.DefaultPort,
IncludeInBulkChangesets: &trueBoolean,
}, user)
if err != nil {
return fmt.Errorf("wasn't able to insert model.environments.templates.autoPopulateCharts entry %d, '%s' (disable autoPopulateChartReleases to skip): %v", index, chart.Name, err)
}
} else {
log.Debug().Msgf("couldn't parse model.environments.templates.autoPopulateCharts entry %d", index)
}
}
}
}
}
return nil
}
func preDeletePostValidateEnvironment(db *gorm.DB, environment *Environment, user *auth_models.User) error {
chartReleases, err := chartReleaseStore.listAllMatchingByUpdated(db, 0, ChartRelease{EnvironmentID: &environment.ID})
if err != nil {
return fmt.Errorf("wasn't able to list chart releases: %v", err)
}
for _, chartRelease := range chartReleases {
_, err = chartReleaseStore.delete(db, ChartRelease{
Model: gorm.Model{ID: chartRelease.ID},
}, user)
if err != nil {
return fmt.Errorf("wasn't able to delete chart release %s: %v", chartRelease.Name, err)
}
}
return nil
}