Skip to content

Commit ad4f901

Browse files
committed
chore: refactor entitlements to keep it in just the options
Duplicating the reference did not feel valuable, just confusing
1 parent ded612d commit ad4f901

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

enterprise/coderd/coderd.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ func New(ctx context.Context, options *Options) (_ *API, err error) {
7575
// from when an additional replica was started.
7676
options.ReplicaErrorGracePeriod = time.Minute
7777
}
78+
if options.Entitlements == nil {
79+
options.Entitlements = entitlements.New()
80+
}
7881

7982
ctx, cancelFunc := context.WithCancel(ctx)
8083

@@ -105,25 +108,22 @@ func New(ctx context.Context, options *Options) (_ *API, err error) {
105108
return nil, xerrors.Errorf("init database encryption: %w", err)
106109
}
107110

108-
entitlementsSet := entitlements.New()
109111
options.Database = cryptDB
110112
api := &API{
111-
ctx: ctx,
112-
cancel: cancelFunc,
113-
Options: options,
114-
entitlements: entitlementsSet,
113+
ctx: ctx,
114+
cancel: cancelFunc,
115+
Options: options,
115116
provisionerDaemonAuth: &provisionerDaemonAuth{
116117
psk: options.ProvisionerDaemonPSK,
117118
authorizer: options.Authorizer,
118119
db: options.Database,
119120
},
120121
licenseMetricsCollector: &license.MetricsCollector{
121-
Entitlements: entitlementsSet,
122+
Entitlements: options.Entitlements,
122123
},
123124
}
124125
// This must happen before coderd initialization!
125126
options.PostAuthAdditionalHeadersFunc = api.writeEntitlementWarningsHeader
126-
options.Options.Entitlements = api.entitlements
127127
api.AGPL = coderd.New(options.Options)
128128
defer func() {
129129
if err != nil {
@@ -561,8 +561,6 @@ type API struct {
561561
// ProxyHealth checks the reachability of all workspace proxies.
562562
ProxyHealth *proxyhealth.ProxyHealth
563563

564-
entitlements *entitlements.Set
565-
566564
provisionerDaemonAuth *provisionerDaemonAuth
567565

568566
licenseMetricsCollector *license.MetricsCollector
@@ -595,7 +593,7 @@ func (api *API) writeEntitlementWarningsHeader(a rbac.Subject, header http.Heade
595593
return
596594
}
597595

598-
api.entitlements.WriteEntitlementWarningHeaders(header)
596+
api.Entitlements.WriteEntitlementWarningHeaders(header)
599597
}
600598

601599
func (api *API) Close() error {
@@ -658,7 +656,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
658656
//
659657
// We don't simply append to entitlement.Errors since we don't want any
660658
// enterprise features enabled.
661-
api.entitlements.Update(func(entitlements *codersdk.Entitlements) {
659+
api.Entitlements.Update(func(entitlements *codersdk.Entitlements) {
662660
entitlements.Errors = []string{
663661
"License requires telemetry but telemetry is disabled",
664662
}
@@ -669,7 +667,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
669667
}
670668

671669
featureChanged := func(featureName codersdk.FeatureName) (initial, changed, enabled bool) {
672-
return api.entitlements.FeatureChanged(featureName, reloadedEntitlements.Features[featureName])
670+
return api.Entitlements.FeatureChanged(featureName, reloadedEntitlements.Features[featureName])
673671
}
674672

675673
shouldUpdate := func(initial, changed, enabled bool) bool {
@@ -835,7 +833,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
835833
}
836834
reloadedEntitlements.Features[codersdk.FeatureExternalTokenEncryption] = featureExternalTokenEncryption
837835

838-
api.entitlements.Replace(reloadedEntitlements)
836+
api.Entitlements.Replace(reloadedEntitlements)
839837
return nil
840838
}
841839

@@ -1015,7 +1013,7 @@ func derpMapper(logger slog.Logger, proxyHealth *proxyhealth.ProxyHealth) func(*
10151013
// @Router /entitlements [get]
10161014
func (api *API) serveEntitlements(rw http.ResponseWriter, r *http.Request) {
10171015
ctx := r.Context()
1018-
httpapi.Write(ctx, rw, http.StatusOK, api.entitlements.AsJSON())
1016+
httpapi.Write(ctx, rw, http.StatusOK, api.Entitlements.AsJSON())
10191017
}
10201018

10211019
func (api *API) runEntitlementsLoop(ctx context.Context) {

enterprise/coderd/coderd_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package coderd_test
33
import (
44
"bytes"
55
"context"
6+
"fmt"
67
"net/http"
78
"reflect"
89
"strings"
@@ -46,14 +47,17 @@ func TestEntitlements(t *testing.T) {
4647
t.Parallel()
4748
t.Run("NoLicense", func(t *testing.T) {
4849
t.Parallel()
49-
adminClient, adminUser := coderdenttest.New(t, &coderdenttest.Options{
50+
adminClient, _, api, adminUser := coderdenttest.NewWithAPI(t, &coderdenttest.Options{
5051
DontAddLicense: true,
5152
})
5253
anotherClient, _ := coderdtest.CreateAnotherUser(t, adminClient, adminUser.OrganizationID)
5354
res, err := anotherClient.Entitlements(context.Background())
5455
require.NoError(t, err)
5556
require.False(t, res.HasLicense)
5657
require.Empty(t, res.Warnings)
58+
59+
// Ensure the entitlements are the same reference
60+
require.Equal(t, fmt.Sprintf("%p", api.Entitlements), fmt.Sprintf("%p", api.AGPL.Entitlements))
5761
})
5862
t.Run("FullLicense", func(t *testing.T) {
5963
// PGCoordinator requires a real postgres

0 commit comments

Comments
 (0)