Skip to content

Commit cc8d0af

Browse files
authored
fix(enterprise): avoid initial license reconfig if feature isn't enabled (#8586)
1 parent 0965a2d commit cc8d0af

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

enterprise/coderd/coderd.go

+17-13
Original file line numberDiff line numberDiff line change
@@ -395,35 +395,40 @@ func (api *API) updateEntitlements(ctx context.Context) error {
395395
return nil
396396
}
397397

398-
featureChanged := func(featureName codersdk.FeatureName) (changed bool, enabled bool) {
398+
featureChanged := func(featureName codersdk.FeatureName) (initial, changed, enabled bool) {
399399
if api.entitlements.Features == nil {
400-
return true, entitlements.Features[featureName].Enabled
400+
return true, false, entitlements.Features[featureName].Enabled
401401
}
402402
oldFeature := api.entitlements.Features[featureName]
403403
newFeature := entitlements.Features[featureName]
404404
if oldFeature.Enabled != newFeature.Enabled {
405-
return true, newFeature.Enabled
405+
return false, true, newFeature.Enabled
406406
}
407-
return false, newFeature.Enabled
407+
return false, false, newFeature.Enabled
408408
}
409409

410-
if changed, enabled := featureChanged(codersdk.FeatureAuditLog); changed {
410+
shouldUpdate := func(initial, changed, enabled bool) bool {
411+
// Avoid an initial tick on startup unless the feature is enabled.
412+
return changed || (initial && enabled)
413+
}
414+
415+
if initial, changed, enabled := featureChanged(codersdk.FeatureAuditLog); shouldUpdate(initial, changed, enabled) {
411416
auditor := agplaudit.NewNop()
412417
if enabled {
413418
auditor = api.AGPL.Options.Auditor
414419
}
415420
api.AGPL.Auditor.Store(&auditor)
416421
}
417422

418-
if changed, enabled := featureChanged(codersdk.FeatureBrowserOnly); changed {
423+
if initial, changed, enabled := featureChanged(codersdk.FeatureBrowserOnly); shouldUpdate(initial, changed, enabled) {
419424
var handler func(rw http.ResponseWriter) bool
420425
if enabled {
421426
handler = api.shouldBlockNonBrowserConnections
422427
}
423428
api.AGPL.WorkspaceClientCoordinateOverride.Store(&handler)
424429
}
425430

426-
if changed, enabled := featureChanged(codersdk.FeatureTemplateRBAC); changed {
431+
if initial, changed, enabled := featureChanged(codersdk.FeatureTemplateRBAC); shouldUpdate(initial, changed, enabled) {
427432
if enabled {
428433
committer := committer{Database: api.Database}
429434
ptr := proto.QuotaCommitter(&committer)
@@ -433,7 +438,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
433438
}
434439
}
435440

436-
if changed, enabled := featureChanged(codersdk.FeatureAdvancedTemplateScheduling); changed {
441+
if initial, changed, enabled := featureChanged(codersdk.FeatureAdvancedTemplateScheduling); shouldUpdate(initial, changed, enabled) {
437442
if enabled {
438443
store := &EnterpriseTemplateScheduleStore{}
439444
ptr := schedule.TemplateScheduleStore(store)
@@ -444,8 +449,8 @@ func (api *API) updateEntitlements(ctx context.Context) error {
444449
}
445450
}
446451

447-
if changed, enabled := featureChanged(codersdk.FeatureHighAvailability); changed {
448-
coordinator := agpltailnet.NewCoordinator(api.Logger)
452+
if initial, changed, enabled := featureChanged(codersdk.FeatureHighAvailability); shouldUpdate(initial, changed, enabled) {
453+
var coordinator agpltailnet.Coordinator
449454
if enabled {
450455
var haCoordinator agpltailnet.Coordinator
451456
if api.AGPL.Experiments.Enabled(codersdk.ExperimentTailnetHACoordinator) {
@@ -457,9 +462,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
457462
api.Logger.Error(ctx, "unable to set up high availability coordinator", slog.Error(err))
458463
// If we try to setup the HA coordinator and it fails, nothing
459464
// is actually changing.
460-
changed = false
461465
} else {
462-
_ = coordinator.Close()
463466
coordinator = haCoordinator
464467
}
465468

@@ -472,6 +475,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
472475
_ = api.updateEntitlements(ctx)
473476
})
474477
} else {
478+
coordinator = agpltailnet.NewCoordinator(api.Logger)
475479
api.derpMesh.SetAddresses([]string{}, false)
476480
api.replicaManager.SetCallback(func() {
477481
// If the amount of replicas change, so should our entitlements.
@@ -481,7 +485,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
481485
}
482486

483487
// Recheck changed in case the HA coordinator failed to set up.
484-
if changed {
488+
if coordinator != nil {
485489
oldCoordinator := *api.AGPL.TailnetCoordinator.Swap(&coordinator)
486490
err := oldCoordinator.Close()
487491
if err != nil {

0 commit comments

Comments
 (0)